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

Skip to content

Commit 7426f7f

Browse files
committed
Elaborate on mathtext documentation.
svn path=/trunk/matplotlib/; revision=5328
1 parent b832558 commit 7426f7f

3 files changed

Lines changed: 227 additions & 75 deletions

File tree

doc/users/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ and Macintosh OS X.
9393
.. toctree::
9494

9595
pyplot_tutorial.rst
96+
mathtext.rst
9697
navigation_toolbar.rst
9798
customizing.rst
9899
artists.rst

doc/users/mathtext.rst

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
Writing mathematical expressions
2+
================================
3+
4+
You can use TeX markup in any matplotlib text string; see the
5+
:mod:`matplotlib.mathtext` module documentation for details. Note
6+
that you do not need to have TeX installed, since matplotlib ships its
7+
own TeX expression parser, layout engine and fonts. The layout engine
8+
is a fairly direct adaptation of the layout algorithms in Donald
9+
Knuth's TeX, so the quality is quite good (matplotlib also provides a
10+
``usetex`` option for those who do want to call out to TeX to generate
11+
their text).
12+
13+
Any text element can use math text. You need to use raw strings
14+
(preceed the quotes with an ``'r'``), and surround the string text
15+
with dollar signs, as in TeX. Regular text and mathtext can be
16+
interleaved within the same string. Mathtext can use the Computer
17+
Modern fonts (from (La)TeX), `STIX <http://www.aip.org/stixfonts/>`_
18+
fonts (with are designed to blend well with Times) or a Unicode font
19+
that you provide. The mathtext font can be selected with the
20+
customization variable ``mathtext.fontset``.
21+
22+
Here is a simple example::
23+
24+
# plain text
25+
plt.title('alpha > beta')
26+
27+
produces "alpha > beta".
28+
29+
Whereas this::
30+
31+
# math text
32+
plt.title(r'$\alpha > \beta$')
33+
34+
produces ":math:`\alpha > \beta`".
35+
36+
.. TODO: Include a complete list here
37+
38+
Subscripts and superscripts
39+
---------------------------
40+
41+
To make subscripts and superscripts, use the ``'_'`` and ``'^'`` symbols::
42+
43+
r'$\alpha_i > \beta_i$'
44+
45+
.. math::
46+
47+
\alpha_i > \beta_i
48+
49+
Some symbols automatically put their sub/superscripts under and over
50+
the operator. For example, to write the sum of :math:`x_i` from :math:`0` to
51+
:math:`\infty`, you could do::
52+
53+
r'$\sum_{i=0}^\infty x_i$'
54+
55+
.. math::
56+
57+
\sum_{i=0}^\infty x_i
58+
59+
Fractions
60+
---------
61+
62+
Fractions can be created with the ``\frac{}{}`` command::
63+
64+
r'$\frac{3}{4}$'
65+
66+
produces
67+
68+
.. math::
69+
70+
\frac{3}{4}
71+
72+
Fractions can be arbitrarily nested::
73+
74+
r'$\frac{5 - \frac{1}{x}}{4}$'
75+
76+
produces
77+
78+
.. math::
79+
80+
\frac{5 - \frac{1}{x}}{4}
81+
82+
Note that special care needs to be taken to place parentheses and brackets around
83+
fractions. Doing things the obvious way produces brackets that are
84+
too small::
85+
86+
r'$(\frac{5 - \frac{1}{x}}{4})$'
87+
88+
.. math ::
89+
90+
(\frac{5 - \frac{1}{x}}{4})
91+
92+
The solution is to precede the bracket with ``\left`` and ``\right``
93+
to inform the parser that those brackets encompass the entire object::
94+
95+
r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$'
96+
97+
.. math ::
98+
99+
\left(\frac{5 - \frac{1}{x}}{4}\right)
100+
101+
Radicals
102+
--------
103+
104+
Radicals can be produced with the ``\sqrt[]{}`` command. For example:
105+
106+
r'$\sqrt{2}$'
107+
108+
.. math ::
109+
110+
\sqrt{2}
111+
112+
Any base can (optionally) be provided inside square brackets. Note
113+
that the base must be a simple expression, and can not contain layout
114+
commands such as fractions or sub/superscripts.
115+
116+
r'$\sqrt[3]{x}$'
117+
118+
.. math ::
119+
120+
\sqrt[3]{x}
121+
122+
Fonts
123+
-----
124+
125+
The default font is *italics* for mathematical symbols. To change
126+
fonts, eg, to write "sin" in a Roman font, enclose the text in a font
127+
command::
128+
129+
r'$s(t) = \mathcal{A}\mathrm{sin}(2 \omega t)$'
130+
131+
.. math::
132+
133+
s(t) = \mathcal{A}\mathrm{sin}(2 \omega t)
134+
135+
More conveniently, many commonly used function names that are typeset in a
136+
Roman font have shortcuts. So the expression above could be written
137+
as follows::
138+
139+
r'$s(t) = \mathcal{A}\sin(2 \omega t)$'
140+
141+
.. math::
142+
143+
s(t) = \mathcal{A}\sin(2 \omega t)
144+
145+
Here "s" and "t" are variable in italics font (default), "sin" is in
146+
Roman font, and the amplitude "A" is in calligraphy font.
147+
148+
The choices available with all fonts are:
149+
150+
=============== =================================
151+
Command Result
152+
=============== =================================
153+
``\mathrm`` :math:`\mathrm{Roman}`
154+
``\mathit`` :math:`\mathit{Italic}`
155+
``\mathtt`` :math:`\mathtt{Typewriter}`
156+
``\mathcal`` :math:`\mathcal{CALLIGRAPHY}`
157+
=============== =================================
158+
159+
When using the STIX fonts, you also have the choice of:
160+
161+
================ =================================
162+
Command Result
163+
================ =================================
164+
``\mathbb`` :math:`\mathbb{Blackboard}`
165+
``\mathcircled`` :math:`\mathcircled{Circled}`
166+
``\mathfrak`` :math:`\mathfrak{Fraktur}`
167+
``\mathsf`` :math:`\mathsf{sans-serif}`
168+
================ =================================
169+
170+
Accents
171+
-------
172+
173+
An accent command may precede any symbol to add an accent above it.
174+
There are long and short forms for some of them.
175+
176+
============================== =================================
177+
Command Result
178+
============================== =================================
179+
``\acute a`` or ``\'a`` :math:`\acute a`
180+
``\bar a`` :math:`\bar a`
181+
``\breve a`` :math:`\breve a`
182+
``\ddot a`` or ``\"a`` :math:`\ddot a`
183+
``\dot a`` or ``\.a`` :math:`\dot a`
184+
``\grave a`` or ``\\`a`` :math:`\grave a`
185+
``\hat a`` or ``\^a`` :math:`\hat a`
186+
``\tilde a`` or ``\~a`` :math:`\tilde a`
187+
``\vec a`` :math:`\vec a`
188+
============================== =================================
189+
190+
In addition, there are two special accents that automatically adjust
191+
to the width of the symbols below:
192+
193+
============================== =================================
194+
Command Result
195+
============================== =================================
196+
``\widehat{xyz}`` :math:`\widehat{xyz}`
197+
``\widetilde{xyz}`` :math:`\widetilde{xyz}`
198+
============================== =================================
199+
200+
201+
Symbols
202+
-------
203+
204+
You can also use a large number of the TeX symbols, as in ``\infty``,
205+
``\leftarrow``, ``\sum``, ``\int``; see :class:`matplotlib.mathtext` for a
206+
complete list.
207+
208+
If a particular symbol does not have a name (as is true of many of the
209+
more obscure symbols in the STIX fonts), Unicode characters can
210+
also be used::
211+
212+
ur'Generic symbol: $\u23ce$'
213+
214+
Example
215+
-------
216+
217+
Here is an example illustrating many of these features in context.
218+
219+
.. literalinclude:: figures/pyplot_mathtext.py
220+
221+
.. image:: figures/pyplot_mathtext.png
222+
:scale: 50
223+
224+
225+

doc/users/pyplot_tutorial.rst

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ several ways to set line properties
7676

7777
* Use the setter methods of the ``Line2D`` instance. ``plot`` returns a list
7878
of lines; eg ``line1, line2 = plot(x1,y1,x2,x2)``. Below I have only
79-
one line so it is a list of length 1. I use tuple unpacking in the
79+
one line so it is a list of7 length 1. I use tuple unpacking in the
8080
``line, = plot(x, y, 'o')`` to get the first element of the list::
8181

8282
line, = plt.plot(x, y, 'o')
@@ -266,77 +266,3 @@ examples of how to control the alignment and orientation of text.
266266

267267

268268

269-
Writing mathematical expressions
270-
================================
271-
272-
You may have noticed in the histogram example above that we slipped a
273-
little TeX markup into the expression ``r'$\mu=100,\ \sigma=15$')``
274-
You can use TeX markup in any matplotlib text string; see the
275-
:mod:`matplotlib.mathtext` module documentation for details. Note
276-
that you do not need to have TeX installed, since matplotlib ships
277-
its own TeX expression parser, layout engine and fonts. Michael
278-
Droettboom has implemented the Knuth layout algorithms in python, so
279-
the quality is quite good (matplotlib also provides a ``usetex`` option
280-
for those who do want to call out to TeX to generate their text).
281-
282-
Any text element can use math text. You need to use raw strings
283-
(preceed the quotes with an ``'r'``), and surround the string text
284-
with dollar signs, as in TeX. Regular text and mathtext can be
285-
interleaved within the same string. Mathtext can use the Bakoma
286-
Computer Modern fonts, STIX fonts or a Unicode font that you provide.
287-
The mathtext font can be selected with the customization variable
288-
``mathtext.fontset``::
289-
290-
# plain text
291-
plt.title('alpha > beta')
292-
293-
# math text
294-
plt.title(r'$\alpha > \beta$')
295-
296-
297-
To make subscripts and superscripts use the '_' and '^' symbols::
298-
299-
plt.title(r'$\alpha_i > \beta_i$')
300-
301-
You can also use a large number of the TeX symbols, as in ``\infty,
302-
\leftarrow, \sum, \int``; see :class:`matplotlib.mathtext` for a
303-
complete list. The over/under subscript/superscript style is also
304-
supported. To write the sum of x_i from 0 to infinity, you could do::
305-
306-
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$')
307-
308-
The default font is *italics* for mathematical symbols. To change
309-
fonts, eg, to write "sin" in a Roman font, enclose the text in a font
310-
command::
311-
312-
plt.text(1,2, r's(t) = $\mathcal{A}\mathrm{sin}(2 \omega t)$')
313-
314-
315-
Even better, many commonly used function names that are typeset in a
316-
Roman font have shortcuts. So the expression above could be written
317-
as follows::
318-
319-
plt.text(1,2, r's(t) = $\mathcal{A}\sin(2 \omega t)$')
320-
321-
322-
Here "s" and "t" are variable in italics font (default), "sin" is in
323-
Roman font, and the amplitude "A" is in caligraphy font. The font
324-
choices are Roman ``\mathrm``, italics ``\mathit``, caligraphy
325-
``\mathcal``, and typewriter ``\mathtt``. If using the STIX fonts,
326-
you also have the choice of blackboard (double-struck) ``\mathbb``,
327-
circled ``\mathcircled``, Fraktur ``\mathfrak``, script (cursive)
328-
``\mathscr`` and sans-serif ``\mathsf``.
329-
330-
The following accents are provided: ``\hat``, ``\breve``, ``\grave``,
331-
``\bar``, ``\acute``, ``\tilde``, ``\vec``, ``\dot``, ``\ddot``. All
332-
of them have the same syntax, eg to make an overbar you do ``\bar{o}``
333-
or to make an o umlaut you do ``\ddot{o}``.
334-
335-
.. literalinclude:: figures/pyplot_mathtext.py
336-
337-
.. image:: figures/pyplot_mathtext.png
338-
:scale: 50
339-
340-
341-
342-

0 commit comments

Comments
 (0)