|
|
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.
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
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 \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 \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. 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.
PS will require more substantial work, doing the metrics and
layouts with the AFM versions of the computer modern fonts.
Backends which don't support mathtext will just render the TeX
string as a literal. Stay tuned.
KNOWN ISSUES:
- some hackish ways I deal with a strange offset in cmex10
- nested subscripts, eg, x_i_i not working
- 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 |
| |
- Handler
- MathText
-
- Expr
- Font
- Grp
- Single
- SubSup
class Font(MathText) |
| |
Methods defined here:
- __init__(self, name, grp)
- __repr__(self)
- get_children(self)
Data and other attributes defined here:
- symmap = {r'\cal': 'cmsy10.ttf', r'\it': 'cmmi10.ttf', r'\rm': 'cmr10.ttf', r'\tt': 'cmtt10.ttf'}
Methods inherited from MathText:
- draw(self, baseline)
- get_bbox(self)
- load_glyphs(self)
- set_fontfile(self, fontfile)
- set_next(self, nx, ny)
- Set the pen location where drawing should begin for the
following MathText
- set_offsets(self, previous)
- Set the offsets assuming previous is the symbol before
you. return a new previous
- set_origin(self, ox, oy)
- Set the pen location where drawing should begin
- set_size(self, size)
Data and other attributes inherited from MathText:
- dpi = 200
- fontfile = 'cmmi10.ttf'
- nx = 0
- ny = 0
- ox = 0
- oy = 0
- size = 12
|
class Handler |
| |
Methods defined here:
- expr(self, s, loc, toks)
- fontgrp(self, s, loc, toks)
- grp(self, s, loc, toks)
- singlesym(self, s, loc, toks)
- subsupgrp(self, s, loc, toks)
|
class MathText |
| |
Methods defined here:
- draw(self, baseline)
- get_bbox(self)
- get_children(self)
- load_glyphs(self)
- set_fontfile(self, fontfile)
- set_next(self, nx, ny)
- Set the pen location where drawing should begin for the
following MathText
- set_offsets(self, previous)
- Set the offsets assuming previous is the symbol before
you. return a new previous
- set_origin(self, ox, oy)
- Set the pen location where drawing should begin
- set_size(self, size)
Data and other attributes defined here:
- dpi = 200
- fontfile = 'cmmi10.ttf'
- nx = 0
- ny = 0
- ox = 0
- oy = 0
- size = 12
|
class SubSup(MathText) |
| |
Methods defined here:
- __init__(self, type, obj)
- __repr__(self)
- get_children(self)
- set_offsets(self, previous)
- set_size(self, size)
Data and other attributes defined here:
- SUBOFFSET = 0.5
- SUBSCALE = 0.69999999999999996
- SUPOFFSET = 0.75
Methods inherited from MathText:
- draw(self, baseline)
- get_bbox(self)
- load_glyphs(self)
- set_fontfile(self, fontfile)
- set_next(self, nx, ny)
- Set the pen location where drawing should begin for the
following MathText
- set_origin(self, ox, oy)
- Set the pen location where drawing should begin
Data and other attributes inherited from MathText:
- dpi = 200
- fontfile = 'cmmi10.ttf'
- nx = 0
- ny = 0
- ox = 0
- oy = 0
- size = 12
| |
Functions |
| |
- FT2Font(...)
- FT2Font(ttffile)
Create a new FT2Font object
The following global font attributes are defined:
num_faces number of faces in file
face_flags face flags (int type); see the ft2font constants
style_flags style flags (int type); see the ft2font constants
num_glyphs number of glyphs in the face
family_name face family name
style_name face syle name
num_fixed_sizes number of bitmap in the face
scalable face is scalable
The following are available, if scalable is true:
bbox face global bounding box (xmin, ymin, xmax, ymax)
units_per_EM number of font units covered by the EM
ascender ascender in 26.6 units
descender descender in 26.6 units
height height in 26.6 units; used to compute a default
line spacing (baseline-to-baseline distance)
max_advance_width maximum horizontal cursor advance for all glyphs
max_advance_height same for vertical layout
underline_position vertical position of the underline bar
underline_thickness vertical thickness of the underline
- math_parse_s(s, dpi, fontsize)
- Parse the math expression s, return the (bbox, fonts) tuple needed
to render it.
fontsize must be in points
bbox: left, bottom, width, heigt
fonts: a seq of FT2Fonts
|
Data |
| |
False = False
OFFSET_EX10 = 4.0
True = True
absolute = <UFunc: 'abs'>
alphanums = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
alphas = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ampersand = "&"
at = "@"
binop = MatchFirst:([MatchFirst:([MatchFirst:(["+", "-"]), "*"]), "/"])
bslash = "\"
cal = "cal"
char = W:(abcd...)
colon = ":"
comma = ","
div = "/"
division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
equals = "="
exclamation = "!"
expr = OneOrMore:(Or:([Or:([Or:([Or:([Or:([Or:([Or:([Or...Optional:(Forward:(Group:(And:([...]))))])))])]))
fontgrp = Group:(And:([Combine:(And:(["\", MatchFirst:([Ma...:(Group:(And:([...])))]))]), Suppress:("}")]))]))
fontname = MatchFirst:([MatchFirst:([MatchFirst:(["rm", "cal"]), "it"]), "tt"])
fonttable = {'(': ('cmr10.ttf', '28'), ')': ('cmr10.ttf', '29'), '*': ('cmsy10.ttf', 'A4'), '+': ('cmr10.ttf', '2B'), ',': ('cmmi10.ttf', '3B'), '-': ('cmsy10.ttf', 'A1'), '.': ('cmmi10.ttf', '3A'), '/': ('cmmi10.ttf', '3D'), '0': ('cmr10.ttf', '30'), '1': ('cmr10.ttf', '31'), ...}
group = Or:([Or:([Group:(And:([And:([Suppress:("{"), One...), Optional:(Forward:(Group:(And:([...]))))])))])
grouping = MatchFirst:([MatchFirst:([MatchFirst:(["[", "]"]), "("]), ")"])
grp = Group:(And:([And:([Suppress:("{"), OneOrMore:(Or:([...]))]), Suppress:("}")]))
handler = <matplotlib.mathtext.Handler instance>
italics = "it"
langle = "<"
lbrace = Suppress:("{")
lbrack = "["
lparen = "("
minus = "-"
misc = MatchFirst:([MatchFirst:([MatchFirst:(["!", "@"]), "%"]), "&"])
nums = '0123456789'
overunder = {r'\coprod': 1, r'\int': 1, r'\prod': 1, r'\sum': 1}
percent = "%"
period = "."
plus = "+"
punctuation = MatchFirst:([MatchFirst:([MatchFirst:([":", ","]), "."]), ";"])
rangle = ">"
rbrace = Suppress:("}")
rbrack = "]"
relation = MatchFirst:([MatchFirst:(["<", ">"]), "="])
roman = "rm"
rparen = ")"
semicolon = ";"
singlesym = Or:([Or:([Or:([Or:([Or:([Or:([Combine:(And:([And...chFirst:([MatchFirst:(["[", "]"]), "("]), ")"])])
subsupgrp = Forward:(Group:(And:([And:([Or:(["_", "^"]), Or:...)]), Optional:(Forward:(Group:(And:([...]))))])))
superscript = "^"
symbol = Or:([Or:([Or:([Or:([Or:([Or:([Or:([Combine:(And:...), Optional:(Forward:(Group:(And:([...]))))])))])
texsym = Combine:(And:([And:(["\", W:(abcd...)]), NotAny:(Suppress:("{"))]))
times = "*"
typewriter = "tt"
underscore = "_" |
|