|
|
OVERVIEW
mathtext is a module for parsing TeX expressions and drawing them
into a matplotlib.ft2font image buffer. You can draw from this
buffer into your backend.
A large set of the TeX symbols are provided (see below).
Subscripting and superscripting are supported, as well as the
over/under style of subscripting with \sum, \int, etc.
The module uses pyparsing to parse the TeX expression, an so can
handle fairly complex TeX expressions Eg, the following renders
correctly
s = r'$\cal{R}\prod_{i=\alpha\cal{B}}^\infty a_i\rm{sin}(2 \pi f x_i)$'
The fonts \cal, \rm, \it, and \tt are allowed.
The spacing elements \ , \/ and \hspace{num} are provided. \/
inserts a small space, and \hspace{num} inserts a fraction of the
current fontsize. Eg, if num=0.5 and the fontsize is 12.0,
hspace{0.5} inserts 6 points of space
If you find TeX expressions that don't parse or render properly,
please email me, but please check KNOWN ISSUES below first.
REQUIREMENTS
mathtext requires matplotlib.ft2font. Set BUILD_FT2FONT=True in
setup.py. See BACKENDS below for a summary of availability by
backend.
LICENSING:
The computer modern fonts this package uses are part of the BaKoMa
fonts, which are (in my understanding) free for noncommercial use.
For commercial use, please consult the licenses in fonts/ttf and the
author Basil K. Malyshev - see also
http://www.mozilla.org/projects/mathml/fonts/encoding/license-bakoma.txt
and the file BaKoMa-CM.Fonts in the matplotlib fonts dir.
Note that all the code in this module is distributed under the
matplotlib license, and a truly free implementation of mathtext for
either freetype or ps would simply require deriving another concrete
implementation from the Fonts class defined in this module which
used free fonts.
USAGE:
See http://matplotlib.sourceforge.net/tutorial.html#mathtext for a
tutorial introduction.
Any text element (xlabel, ylabel, title, text, etc) can use TeX
markup, as in
xlabel(r'$\Delta_i$')
^
use raw strings
The $ symbols must be the first and last symbols in the string. Eg,
you cannot do
r'My label $x_i$'.
but you can change fonts, as in
r'$\rm{My label} x_i$'
to achieve the same effect.
A large set of the TeX symbols are provided. Subscripting and
superscripting are supported, as well as the over/under style of
subscripting with \sum, \int, etc.
Allowed TeX symbols:
\/ \Delta \Downarrow \Gamma \Im \LEFTangle \LEFTbrace \LEFTbracket
\LEFTparen \Lambda \Leftarrow \Leftbrace \Leftbracket \Leftparen
\Leftrightarrow \Omega \P \Phi \Pi \Psi \RIGHTangle \RIGHTbrace
\RIGHTbracket \RIGHTparen \Re \Rightarrow \Rightbrace \Rightbracket
\Rightparen \S \SQRT \Sigma \Sqrt \Theta \Uparrow \Updownarrow
\Upsilon \Vert \Xi \aleph \alpha \approx \angstrom \ast \asymp
\backslash \beta \bigcap \bigcirc \bigcup \bigodot \bigoplus
\bigotimes \bigtriangledown \bigtriangleup \biguplus \bigvee
\bigwedge \bot \bullet \cap \cdot \chi \circ \clubsuit \coprod \cup
\dag \dashv \ddag \delta \diamond \diamondsuit \div \downarrow \ell
\emptyset \epsilon \equiv \eta \exists \flat \forall \frown \gamma
\geq \gg \heartsuit \hspace \imath \in \infty \int \iota \jmath
\kappa \lambda \langle \lbrace \lceil \leftangle \leftarrow
\leftbrace \leftbracket \leftharpoondown \leftharpoonup \leftparen
\leftrightarrow \leq \lfloor \ll \mid \mp \mu \nabla \natural
\nearrow \neg \ni \nu \nwarrow \odot \oint \omega \ominus \oplus
\oslash \otimes \phi \pi \pm \prec \preceq \prime \prod \propto \psi
\rangle \rbrace \rceil \rfloor \rho \rightangle \rightarrow
\rightbrace \rightbracket \rightharpoondown \rightharpoonup
\rightparen \searrow \sharp \sigma \sim \simeq \slash \smile
\spadesuit \sqcap \sqcup \sqrt \sqsubseteq \sqsupseteq \subset
\subseteq \succ \succeq \sum \supset \supseteq \swarrow \tau \theta
\times \top \triangleleft \triangleright \uparrow \updownarrow
\uplus \upsilon \varepsilon \varphi \varphi \varrho \varsigma
\vartheta \vdash \vee \wedge \wp \wr \xi \zeta
BACKENDS
mathtext currently works with GTK, Agg, GTKAgg, TkAgg and WxAgg and
PS, though only horizontal and vertical rotations are supported. If
David incorporates ft2font into paint, it will be easy to add to
Paint. WX can follow the lead of GTK and use pixels API calls to
use mathtext.
mathtext now embeds the TrueType computer modern fonts into the PS
file, so what you see on the screen should be what you get on paper.
Backends which don't support mathtext will just render the TeX
string as a literal. Stay tuned.
KNOWN ISSUES:
- FIXED in 0.54 - some hackish ways I deal with a strange offset in cmex10
- nested subscripts, eg, x_i_j not working; but you can do x_{i_j}
- nesting fonts changes in sub/superscript groups not parsing
- I would also like to add a few more layout commands, like \frac.
Author : John Hunter <jdhunter@ace.bsd.uchicago.edu>
Copyright : John Hunter (2004)
License : matplotlib license (PSF compatible)
Classes |
| |
- DummyFonts
- Element
-
- GroupElement
-
- ExpressionElement
- SpaceElement
- SymbolElement
- Fonts
-
- BakomaPSFonts
- BakomaTrueTypeFonts
- Handler
class BakomaPSFonts(Fonts) |
|
Use the Bakoma postscript fonts for rendering to backend_ps |
|
Methods defined here:
- __init__(self)
- get_metrics(self, font, sym, fontsize, dpi)
- render(self, ox, oy, font, sym, fontsize, dpi)
- set_canvas_size(self, w, h, pswriter)
- Dimension the drawing canvas; may be a noop
Data and other attributes defined here:
- basepath = '/usr/local/share/matplotlib'
- fnames = ('cmmi10', 'cmsy10', 'cmex10', 'cmtt10', 'cmr10')
- fontmap = {None: 'cmmi10', 'cal': 'cmsy10', 'it': 'cmmi10', 'rm': 'cmr10', 'tt': 'cmtt10'}
|
class BakomaTrueTypeFonts(Fonts) |
|
Use the Bakoma true type fonts for rendering |
|
Methods defined here:
- __init__(self)
- get_metrics(self, font, sym, fontsize, dpi)
- render(self, ox, oy, font, sym, fontsize, dpi)
- set_canvas_size(self, w, h)
- Dimension the drawing canvas; may be a noop
Data and other attributes defined here:
- basepath = '/usr/local/share/matplotlib'
- fnames = ('cmmi10', 'cmsy10', 'cmex10', 'cmtt10', 'cmr10')
- fontmap = {None: 'cmmi10', 'cal': 'cmsy10', 'it': 'cmmi10', 'rm': 'cmr10', 'tt': 'cmtt10'}
|
class Element |
| |
Methods defined here:
- __init__(self)
- __repr__(self)
- advance(self)
- get the horiz advance
- centerx(self)
- centery(self)
- height(self)
- get the element height: ymax-ymin
- padx(self)
- pady(self)
- render(self)
- render to the fonts canvas
- set_font(self, font)
- set the font (one of tt, it, rm , cal)
- set_origin(self, ox, oy)
- set_padx(self, pad)
- set the y padding in points
- set_pady(self, pad)
- set the y padding in points
- set_scale(self, scale)
- scale the element by scale
- set_size_info(self, fontsize, dpi)
- width(self)
- get the element width: xmax-xmin
- xmax(self)
- get the xmax of ink rect
- xmin(self)
- get the xmin of ink rect
- ymax(self)
- get the ymax of ink rect
- ymin(self)
- get the ymin of ink rect
Data and other attributes defined here:
- dpi = 72
- font = 'it'
- fontsize = 12
|
class ExpressionElement(GroupElement) |
|
The entire mathtext expression |
|
- Method resolution order:
- ExpressionElement
- GroupElement
- Element
Methods defined here:
- __repr__(self)
Methods inherited from GroupElement:
- __init__(self, elements)
- advance(self)
- get the horiz advance
- height(self)
- get the element height: ymax-ymin
- render(self)
- render to the fonts canvas
- set_font(self, font)
- set the font (one of tt, it, rm , cal)
- set_origin(self, ox, oy)
- set_size_info(self, fontsize, dpi)
- width(self)
- get the element width: xmax-xmin
- xmax(self)
- get the max ink in x
- xmin(self)
- get the minimum ink in x
- ymax(self)
- get the max ink in y
- ymin(self)
- get the minimum ink in y
Methods inherited from Element:
- centerx(self)
- centery(self)
- padx(self)
- pady(self)
- set_padx(self, pad)
- set the y padding in points
- set_pady(self, pad)
- set the y padding in points
- set_scale(self, scale)
- scale the element by scale
Data and other attributes inherited from Element:
- dpi = 72
- font = 'it'
- fontsize = 12
|
class Fonts |
|
An abstract base class for fonts that want to render mathtext
The class must be able to take symbol keys and font file names and
return the character metrics as well as do the drawing |
|
Methods defined here:
- get_metrics(self, font, sym, fontsize, dpi)
- font is one of tt, it, rm, cal or None
sym is a single symbol(alphanum, punct) or a special symbol
like \sigma.
fontsize is in points
Return object has attributes - see
http://www.freetype.org/freetype2/docs/tutorial/step2.html for
a pictoral representation of these attributes
advance
height
width
xmin, xmax, ymin, ymax - the ink rectangle of the glyph
- render(self, ox, oy, font, sym, fontsize, dpi)
- set_canvas_size(self, w, h)
- Dimension the drawing canvas; may be a noop
|
class GroupElement(Element) |
|
A group is a collection of elements |
|
Methods defined here:
- __init__(self, elements)
- __repr__(self)
- advance(self)
- get the horiz advance
- height(self)
- get the element height: ymax-ymin
- render(self)
- render to the fonts canvas
- set_font(self, font)
- set the font (one of tt, it, rm , cal)
- set_origin(self, ox, oy)
- set_size_info(self, fontsize, dpi)
- width(self)
- get the element width: xmax-xmin
- xmax(self)
- get the max ink in x
- xmin(self)
- get the minimum ink in x
- ymax(self)
- get the max ink in y
- ymin(self)
- get the minimum ink in y
Methods inherited from Element:
- centerx(self)
- centery(self)
- padx(self)
- pady(self)
- set_padx(self, pad)
- set the y padding in points
- set_pady(self, pad)
- set the y padding in points
- set_scale(self, scale)
- scale the element by scale
Data and other attributes inherited from Element:
- dpi = 72
- font = 'it'
- fontsize = 12
|
class Handler |
| |
Methods defined here:
- clear(self)
- composite(self, s, loc, toks)
- expression(self, s, loc, toks)
- font(self, s, loc, toks)
- group(self, s, loc, toks)
- is_overunder(self, prev)
- space(self, s, loc, toks)
- subscript(self, s, loc, toks)
- subsuperscript(self, s, loc, toks)
- superscript(self, s, loc, toks)
- symbol(self, s, loc, toks)
Data and other attributes defined here:
- symbols = []
|
class SymbolElement(Element) |
| |
Methods defined here:
- __init__(self, sym)
- __repr__(self)
- advance(self)
- get the horiz advance
- height(self)
- get the element height: ymax-ymin
- render(self)
- render to the fonts canvas
- set_font(self, font)
- set the font (one of tt, it, rm , cal)
- set_origin(self, ox, oy)
- set_size_info(self, fontsize, dpi)
- width(self)
- get the element width: xmax-xmin
- xmax(self)
- get the max ink in x
- xmin(self)
- get the minimum ink in x
- ymax(self)
- get the max ink in y
- ymin(self)
- get the minimum ink in y
Methods inherited from Element:
- centerx(self)
- centery(self)
- padx(self)
- pady(self)
- set_padx(self, pad)
- set the y padding in points
- set_pady(self, pad)
- set the y padding in points
- set_scale(self, scale)
- scale the element by scale
Data and other attributes inherited from Element:
- dpi = 72
- font = 'it'
- fontsize = 12
| |
Functions |
| |
- FT2Font(...)
- FT2Font
- StringIO(...)
- StringIO([s]) -- Return a StringIO-like stream for reading or writing
- math_parse_s_ft2font(s, dpi, fontsize, angle=0)
- Parse the math expression s, return the (bbox, fonts) tuple needed
to render it.
fontsize must be in points
return is width, height, fonts
- math_parse_s_ps(s, dpi, fontsize)
- Parse the math expression s, return the (bbox, fonts) tuple needed
to render it.
fontsize must be in points
return is width, height, fonts
|
Data |
| |
False = False
True = True
absolute = <ufunc 'absolute'>
alphanums = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
alphas = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ampersand = "&"
at = "@"
bakoma_fonts = []
binop = {{{"+" | "-"} | "*"} | "/"}
bslash = "\"
cal = "cal"
char = W:(abcd...)
charOverChars = {r'\angstrom': (('rm', 'A', 1.0), (None, r'\circ', 0.5), 0.0)}
colon = ":"
comma = ","
composite = composite
div = "/"
division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
equals = "="
exclamation = "!"
expression = expression
font = font
fontname = {{{"rm" | "cal"} | "it"} | "tt"}
group = group
grouping = {{{"[" | "]"} | "("} | ")"}
handler = <matplotlib.mathtext.Handler instance>
italics = "it"
langle = "<"
latex_to_bakoma = {'(': ('cmr10', 119), ')': ('cmr10', 68), '*': ('cmsy10', 9), '+': ('cmr10', 76), ',': ('cmmi10', 44), '-': ('cmsy10', 17), '.': ('cmmi10', 51), '/': ('cmmi10', 98), '0': ('cmr10', 40), '1': ('cmr10', 100), ...}
lbrace = Suppress:("{")
lbrack = "["
lparen = "("
minus = "-"
misc = {{{"!" | "@"} | "%"} | "&"}
number = Combine:({{W:(0123...) ["."]} [W:(0123...)]})
nums = '0123456789'
over = "over"
overUnder = {"over" | "under"}
overunder = {r'\coprod': 1, r'\int': 1, r'\prod': 1, r'\sum': 1}
percent = "%"
period = "."
plus = "+"
punctuation = {{{":" | ","} | "."} | ";"}
rangle = ">"
rbrace = Suppress:("}")
rbrack = "]"
relation = {{"<" | ">"} | "="}
roman = "rm"
rparen = ")"
semicolon = ";"
space = space
subscript = subscript
subsuperscript = subsuperscript
superscript = superscript
symbol = {{{{{{Combine:({{"\" W:(abcd...)} ~{Suppress:("{...@"} | "%"} | "&"}} | {{{"[" | "]"} | "("} | ")"}}
symgroup = {{{{{{{Combine:({{"\" W:(abcd...)} ~{Suppress:("...} | "&"}} | {{{"[" | "]"} | "("} | ")"}} | group}
texsym = Combine:({{"\" W:(abcd...)} ~{Suppress:("{")}})
times = "*"
typewriter = "tt"
under = "under" |
|