Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add kwarg 'markup' for all text that allows the user to choose either

'plain' or 'tex' markup.  A default may also be provided in the
rcParam 'text.markup'.
Minor bugfix to mathtext.py

svn path=/trunk/matplotlib/; revision=3664
  • Loading branch information...
commit a3fcaa3b8342f978bc667ddb900f513c933a3d84 1 parent 09d2e64
@mdboom mdboom authored
View
4 examples/accented_text.py
@@ -13,9 +13,9 @@
plot(range(10))
-title(r'$\ddot{o}\acute{e}\grave{e}\hat{O}\breve{i}\bar{A}\tilde{n}\vec{q}$', fontsize=20)
+title(r'$\ddot{o}\acute{e}\grave{e}\hat{O}\breve{i}\bar{A}\tilde{n}\vec{q}$', fontsize=20, markup="tex")
# shorthand is also supported and curly's are optional
-xlabel(r"""$\"o\ddot o \'e\`e\~n\.x\^y$""", fontsize=20)
+xlabel(r"""$\"o\ddot o \'e\`e\~n\.x\^y$""", fontsize=20, markup="tex")
show()
View
4 examples/arrow_demo.py
@@ -52,7 +52,7 @@ def make_arrow_plot(data, size=4, display='length', shape='right', \
min_text_size = size
label_text_size = size*2.5
text_params={'ha':'center', 'va':'center', 'family':'sans-serif',\
- 'fontweight':'bold'}
+ 'fontweight':'bold', 'markup': 'tex'}
r2 = sqrt(2)
deltas = {\
@@ -211,7 +211,7 @@ def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor):
label = '$%s_{_{\mathrm{%s}}}$' % (orig_label[0], orig_label[1:])
text(x, y, label, size=label_text_size, ha='center', va='center', \
- color=labelcolor or fc)
+ color=labelcolor or fc, markup='tex')
for p in positions.keys():
draw_arrow(p)
View
11 examples/dannys_example.py
@@ -29,14 +29,14 @@
pylab.plot((-delta / 2, -delta / 2 + offset * 2), (height, height + offset), 'k', linewidth = 2)
pylab.plot((delta / 2, delta / 2 - offset * 2), (height, height - offset), 'k', linewidth = 2)
pylab.plot((delta / 2, delta / 2 - offset * 2), (height, height + offset), 'k', linewidth = 2)
-pylab.text(-0.06, height - 0.06, r'$\delta$', {'color' : 'k', 'fontsize' : 24})
+pylab.text(-0.06, height - 0.06, r'$\delta$', {'color' : 'k', 'fontsize' : 24}, markup = 'tex')
## X-axis label
pylab.xticks((-1, 0, 1), ('-1', '0', '1'), color = 'k', size = 20)
## Left Y-axis labels
pylab.ylabel(r'\bf{phase field} $\phi$', {'color' : 'b',
- 'fontsize' : 20 })
+ 'fontsize' : 20 }, markup='tex')
pylab.yticks((0, 0.5, 1), ('0', '.5', '1'), color = 'k', size = 20)
## Right Y-axis labels
@@ -44,16 +44,17 @@
horizontalalignment = 'left',
verticalalignment = 'center',
rotation = 90,
- clip_on = False)
+ clip_on = False,
+ markup = 'tex')
pylab.text(1.01, -0.02, "-1", {'color' : 'k', 'fontsize' : 20})
pylab.text(1.01, 0.98, "1", {'color' : 'k', 'fontsize' : 20})
pylab.text(1.01, 0.48, "0", {'color' : 'k', 'fontsize' : 20})
## level set equations
-pylab.text(0.1, 0.85, r'$|\nabla\phi| = 1,$ \newline $ \frac{\partial \phi}{\partial t} + U|\nabla \phi| = 0$', {'color' : 'g', 'fontsize' : 20})
+pylab.text(0.1, 0.85, r'$|\nabla\phi| = 1,$ \newline $ \frac{\partial \phi}{\partial t} + U|\nabla \phi| = 0$', {'color' : 'g', 'fontsize' : 20}, markup='tex')
## phase field equations
-pylab.text(0.2, 0.15, r'$\mathcal{F} = \int f\left( \phi, c \right) dV,$ \newline $ \frac{ \partial \phi } { \partial t } = -M_{ \phi } \frac{ \delta \mathcal{F} } { \delta \phi }$', {'color' : 'b', 'fontsize' : 20})
+pylab.text(0.2, 0.15, r'$\mathcal{F} = \int f\left( \phi, c \right) dV,$ \newline $ \frac{ \partial \phi } { \partial t } = -M_{ \phi } \frac{ \delta \mathcal{F} } { \delta \phi }$', {'color' : 'b', 'fontsize' : 20}, markup='tex')
pylab.savefig('pfm-lsm.png')
pylab.show()
View
2  examples/histogram_demo.py
@@ -15,7 +15,7 @@
xlabel('Smarts')
ylabel('Probability')
-title(r'$\rm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
+title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$', markup='tex')
axis([40, 160, 0, 0.03])
grid(True)
View
2  examples/histogram_demo_canvasagg.py
@@ -33,7 +33,7 @@
ax.set_xlabel('Smarts')
ax.set_ylabel('Probability')
-ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$')
+ax.set_title(r'$\mathrm{Histogram of IQ: }\mu=100, \sigma=15$', markup='tex')
ax.set_xlim( (40, 160))
ax.set_ylim( (0, 0.03))
View
4 examples/integral_demo.py
@@ -22,8 +22,8 @@ def func(x):
ax.add_patch(poly)
text(0.5 * (a + b), 30,
- r"$\int_a^b f(x)\rm{d}x$", horizontalalignment='center',
- fontsize=20)
+ r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center',
+ fontsize=20, markup='tex')
axis([0,10, 0, 180])
figtext(0.9, 0.05, 'x')
View
6 examples/legend_auto.py
@@ -51,9 +51,9 @@ def fig_6():
def fig_7():
figure(7)
xx = x - (N/2.0)
- plot(xx, (xx*xx)-1225, 'bo', label='$y=x^2$')
- plot(xx, 25*xx, 'go', label='$y=25x$')
- plot(xx, -25*xx, 'mo', label='$y=-25x$')
+ plot(xx, (xx*xx)-1225, 'bo', label='$y=x^2$', markup='tex')
+ plot(xx, 25*xx, 'go', label='$y=25x$', markup='tex')
+ plot(xx, -25*xx, 'mo', label='$y=-25x$', markup='tex')
legend()
def fig_8():
View
2  examples/mathtext_demo.py
@@ -17,7 +17,7 @@
ax.set_ylabel(r'$\Delta_{i+1}^j$', fontsize=20)
tex = r'$\mathcal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\sin(2 \pi f x_i)$'
-ax.text(1, 1.6, tex, fontsize=20, va='bottom')
+ax.text(1, 1.6, tex, fontsize=20, va='bottom', markup="tex")
#title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
fig.savefig('mathtext_demo')
View
2  examples/mathtext_examples.py
@@ -62,7 +62,7 @@
x = arange(0.0, 3.0, 0.1)
grid(True)
- text(1, 1.6, s, fontsize=20)
+ text(1, 1.6, s, fontsize=20, markup="tex")
savefig('mathtext_example%02d' % i)
figure()
View
4 examples/scatter_demo2.py
@@ -11,8 +11,8 @@
close = 0.003*intc.close[:-2]/0.003*intc.open[:-2]
p = scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.75)
-xlabel(r'$\Delta_i$', size='x-large')
-ylabel(r'$\Delta_{i+1}$', size='x-large')
+xlabel(r'$\Delta_i$', size='x-large', markup='tex')
+ylabel(r'$\Delta_{i+1}$', size='x-large', markup='tex')
title(r'Volume and percent change')
grid(True)
#savefig('scatter_demo2')
View
6 examples/tex_demo.py
@@ -23,10 +23,10 @@
s = cos(2*2*pi*t)+2
plot(t, s)
-xlabel(r'\textbf{time (s)}')
-ylabel(r'\textit{voltage (mV)}',fontsize=16)
+xlabel(r'\textbf{time (s)}', markup='tex')
+ylabel(r'\textit{voltage (mV)}',fontsize=16, markup='tex')
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
- fontsize=16, color='r')
+ fontsize=16, color='r', markup='tex')
grid(True)
savefig('tex_demo')
View
6 examples/tex_unicode_demo.py
@@ -17,11 +17,11 @@
s = cos(2*2*pi*t)+2
plot(t, s)
-xlabel(r'\textbf{time (s)}')
+xlabel(r'\textbf{time (s)}', markup='tex')
s = unicode(r'\textit{Velocity (°/sec)}','latin-1')
-ylabel(unicode(r'\textit{Velocity (°/sec)}','latin-1'),fontsize=16)
+ylabel(unicode(r'\textit{Velocity (°/sec)}','latin-1'),fontsize=16, markup='tex')
title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
- fontsize=16, color='r')
+ fontsize=16, color='r', markup='tex')
grid(True)
savefig('tex_demo')
View
2  lib/matplotlib/mathtext.py
@@ -1840,6 +1840,8 @@ def __init__(self):
)
)
+ self.clear()
+
def clear(self):
self._expr = None
self._state_stack = None
View
7 lib/matplotlib/rcsetup.py
@@ -198,6 +198,11 @@ def validate_fontsize(s):
except ValueError:
raise ValueError('not a valid font size')
+validate_markup = ValidateInStrings(
+ 'markup',
+ ['plain', 'tex'],
+ ignorecase=True)
+
validate_verbose = ValidateInStrings('verbose',[
'silent', 'helpful', 'debug', 'debug-annoying',
])
@@ -350,7 +355,7 @@ def __call__(self, s):
'text.fontvariant' : ['normal', str],
'text.fontweight' : ['normal', str],
'text.fontsize' : ['medium', validate_fontsize],
-
+ 'text.markup' : ['plain', validate_markup],
'image.aspect' : ['equal', validate_aspect], # equal, auto, a number
'image.interpolation' : ['bilinear', str],
View
26 lib/matplotlib/text.py
@@ -146,6 +146,7 @@ def __init__(self,
fontproperties=None, # defaults to FontProperties()
rotation=None,
linespacing=None,
+ markup=None,
**kwargs
):
"""
@@ -174,6 +175,7 @@ def __init__(self,
if linespacing is None:
linespacing = 1.2 # Maybe use rcParam later.
self._linespacing = linespacing
+ self.set_markup(markup)
self.update(kwargs)
#self.set_bbox(dict(pad=0))
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
@@ -222,6 +224,7 @@ def update_from(self, other):
self._rotation = other._rotation
self._picker = other._picker
self._linespacing = other._linespacing
+ self._markup = other._markup
def _get_layout(self, renderer):
@@ -752,11 +755,11 @@ def set_text(self, s):
self._substrings = [] # ignore embedded mathtext for now
def is_math_text(self):
- if rcParams['text.usetex']: return 'TeX'
- if not matplotlib._havemath: return False
- if len(self._text)<2: return False
- dollar_signs = self._text.count('$') - self._text.count('\\$')
- return dollar_signs > 0 and dollar_signs % 2 == 0
+ if rcParams['text.usetex']: return 'TeX'
+ if self._markup.lower() == 'tex':
+ if not matplotlib._havemath: return False
+ return True
+ return False
def set_fontproperties(self, fp):
"""
@@ -766,8 +769,19 @@ def set_fontproperties(self, fp):
"""
self._fontproperties = fp
+ def set_markup(self, markup):
+ """
+ Set the type of markup used for this text.
-
+ ACCEPTS: 'plain' for plain text, 'tex' for TeX-like markup
+ None to use the default text.markup value.
+ """
+ if markup is None:
+ self._markup = rcParams['text.markup']
+ elif markup.lower() in ('plain', 'tex'):
+ self._markup = markup.lower()
+ else:
+ raise ValueError("Markup type must be 'plain' or 'tex'")
def _get_layout_super(self, renderer, m):
"""
View
8 matplotlibrc.template
@@ -150,6 +150,14 @@ numerix : %(numerix)s # numpy, Numeric or numarray
#text.dvipnghack : False # some versions of dvipng don't handle
# alpha channel properly. Use True to correct and flush
# ~/.matplotlib/tex.cache before testing
+#text.markup : 'plain' # Affects how text, such as titles and lables, are
+ # interpreted by default.
+ # 'plain': As plain, unformatted text
+ # 'tex': As TeX-like text. Text between $'s will be
+ # formatted as a TeX math expression.
+ # This setting has no effect when text.usetex is True.
+ # In that case, all text will be sent to TeX for
+ # processing.
### AXES
# default face and edge color, default tick sizes,
Please sign in to comment.
Something went wrong with that request. Please try again.