Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40e5efd

Browse files
committed
Make escaped $ work in non-usetex mode
svn path=/trunk/matplotlib/; revision=6613
1 parent 145878b commit 40e5efd

5 files changed

Lines changed: 64 additions & 28 deletions

File tree

doc/users/mathtext.rst

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
Writing mathematical expressions
44
================================
55

6-
You can use TeX markup in any matplotlib text string. Note that you
7-
do not need to have TeX installed, since matplotlib ships its own TeX
8-
expression parser, layout engine and fonts. The layout engine is a
9-
fairly direct adaptation of the layout algorithms in Donald Knuth's
10-
TeX, so the quality is quite good (matplotlib also provides a
11-
``usetex`` option for those who do want to call out to TeX to generate
12-
their text (see :ref:`usetex-tutorial`).
13-
14-
Any text element can use math text. You need to use raw strings
15-
(preceed the quotes with an ``'r'``), and surround the string text
16-
with dollar signs, as in TeX. Regular text and mathtext can be
6+
You can use a subset TeX markup in any matplotlib text string by
7+
placing it inside a pair of dollar signs ($).
8+
9+
Note that you do not need to have TeX installed, since matplotlib
10+
ships its own TeX expression parser, layout engine and fonts. The
11+
layout engine is a fairly direct adaptation of the layout algorithms
12+
in Donald Knuth's TeX, so the quality is quite good (matplotlib also
13+
provides a ``usetex`` option for those who do want to call out to TeX
14+
to generate their text (see :ref:`usetex-tutorial`).
15+
16+
Any text element can use math text. You should use raw strings
17+
(preceed the quotes with an ``'r'``), and surround the math text with
18+
dollar signs ($), as in TeX. Regular text and mathtext can be
1719
interleaved within the same string. Mathtext can use the Computer
1820
Modern fonts (from (La)TeX), `STIX <http://www.aip.org/stixfonts/>`_
1921
fonts (with are designed to blend well with Times) or a Unicode font
@@ -35,6 +37,26 @@ Whereas this::
3537

3638
produces ":math:`\alpha > \beta`".
3739

40+
.. note::
41+
Mathtext should be placed between a pair of dollar signs ($). To
42+
make it easy to display monetary values, e.g. "$100.00", if a
43+
single dollar sign is present in the entire string, it will be
44+
displayed verbatim as a dollar sign. This is a small change from
45+
regular TeX, where the dollar sign in non-math text would have to
46+
be escaped ('\$').
47+
48+
.. note::
49+
While the syntax inside the pair of dollar signs ($) aims to be
50+
TeX-like, the text outside does not. In particular, characters
51+
such as::
52+
53+
# $ % & ~ _ ^ \ { } \( \) \[ \]
54+
55+
have special meaning outside of math mode in TeX. Therefore, these
56+
characters will behave differently depending on the rcParam
57+
``text.usetex`` flag. See the :ref:`usetex tutorial
58+
<usetex-tutorial>` for more information.
59+
3860
Subscripts and superscripts
3961
---------------------------
4062

doc/users/text_intro.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ You have total control over every text property (font size, font
1818
weight, text location and color, etc) with sensible defaults set in
1919
the rc file. And significantly for those interested in mathematical
2020
or scientific figures, matplotlib implements a large number of TeX
21-
math symbols and commands, to support mathematical expressions
22-
anywhere in your figure.
21+
math symbols and commands, to support :ref:`mathematical expressions
22+
<mathtext-tutorial>` anywhere in your figure.
2323

2424

2525
Basic text commands

doc/users/usetex.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ Note that display math mode (``$$ e=mc^2 $$``) is not supported, but adding the
5959
command ``\displaystyle``, as in `tex_demo.py`, will produce the same
6060
results.
6161

62+
.. note::
63+
Certain characters require special escaping in TeX, such as::
64+
65+
# $ % & ~ _ ^ \ { } \( \) \[ \]
66+
67+
Therefore, these characters will behave differently depending on
68+
the rcParam ``text.usetex`` flag.
69+
6270
.. _usetex-unicode:
6371

6472
usetex with unicode

lib/matplotlib/offsetbox.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def get_window_extent(self, renderer):
548548

549549

550550
def get_extent(self, renderer):
551-
ismath = self._text.is_math_text(self._text._text)
551+
clean_line, ismath = self._text.is_math_text(self._text._text)
552552
_, h_, d_ = renderer.get_text_width_height_descent(
553553
"lp", self._text._fontproperties, ismath=False)
554554

@@ -558,30 +558,30 @@ def get_extent(self, renderer):
558558
line = info[0][0] # first line
559559

560560
_, hh, dd = renderer.get_text_width_height_descent(
561-
line, self._text._fontproperties, ismath=ismath)
561+
clean_line, self._text._fontproperties, ismath=ismath)
562562

563563

564564
self._baseline_transform.clear()
565565
if len(info) > 1 and self._multilinebaseline: # multi line
566566
d = h-(hh-dd) # the baseline of the first line
567567
d_new = 0.5 * h - 0.5 * (h_ - d_)
568-
568+
569569
self._baseline_transform.translate(0, d - d_new)
570570
d = d_new
571-
571+
572572
else: # single line
573573

574574
h_d = max(h_ - d_, h-dd)
575575

576576
if self.get_minimumdescent():
577577
## to have a minimum descent, #i.e., "l" and "p" have same
578-
## descents.
578+
## descents.
579579
d = max(dd, d_)
580580
else:
581581
d = dd
582582

583583
h = h_d + d
584-
584+
585585
return w, h, 0., d
586586

587587

lib/matplotlib/text.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ def _get_layout(self, renderer):
248248

249249
baseline = None
250250
for i, line in enumerate(lines):
251+
clean_line, ismath = self.is_math_text(line)
251252
w, h, d = renderer.get_text_width_height_descent(
252-
line, self._fontproperties, ismath=self.is_math_text(line))
253+
clean_line, self._fontproperties, ismath=ismath)
253254
if baseline is None:
254255
baseline = h - d
255256
whs[i] = w, h
@@ -480,8 +481,9 @@ def draw(self, renderer):
480481
y = y + posy
481482
if renderer.flipy():
482483
y = canvash-y
484+
clean_line, ismath = self.is_math_text(line)
483485

484-
renderer.draw_tex(gc, x, y, line,
486+
renderer.draw_tex(gc, x, y, clean_line,
485487
self._fontproperties, angle)
486488
return
487489

@@ -490,10 +492,11 @@ def draw(self, renderer):
490492
y = y + posy
491493
if renderer.flipy():
492494
y = canvash-y
495+
clean_line, ismath = self.is_math_text(line)
493496

494-
renderer.draw_text(gc, x, y, line,
497+
renderer.draw_text(gc, x, y, clean_line,
495498
self._fontproperties, angle,
496-
ismath=self.is_math_text(line))
499+
ismath=ismath)
497500

498501
def get_color(self):
499502
"Return the color of the text"
@@ -875,15 +878,18 @@ def is_math_text(self, s):
875878
"""
876879
Returns True if the given string *s* contains any mathtext.
877880
"""
878-
if rcParams['text.usetex']: return 'TeX'
879-
880881
# Did we find an even number of non-escaped dollar signs?
881882
# If so, treat is as math text.
882883
dollar_count = s.count(r'$') - s.count(r'\$')
883-
if dollar_count > 0 and dollar_count % 2 == 0:
884-
return True
884+
even_dollars = (dollar_count > 0 and dollar_count % 2 == 0)
885+
886+
if rcParams['text.usetex']:
887+
return s, 'TeX'
885888

886-
return False
889+
if even_dollars:
890+
return s, True
891+
else:
892+
return s.replace(r'\$', '$'), False
887893

888894
def set_fontproperties(self, fp):
889895
"""

0 commit comments

Comments
 (0)