mpl.mathtext

mathtext is a module for parsing a subset of the TeX math syntax and drawing them to a matplotlib backend.

For a tutorial of its usage see mathtext-tutorial. This document is primarily concerned with implementation details.

The module uses pyparsing to parse the TeX expression.

The Bakoma distribution of the TeX Computer Modern fonts, and STIX fonts are supported. There is experimental support for using arbitrary fonts, but results may vary without proper tweaking and metrics for those fonts.

Functions

Error(msg) Helper class to raise parser errors.
ceil(x) Return the ceiling of x as a float.
findfont(prop, **kw)
get_data_path(*args, **kwargs)
get_unicode_index((symbol) -> integer) Return the integer index (from the Unicode table) of symbol.
is_string_like(obj) Return True if obj looks like a string
math_to_image(s, filename_or_obj[, prop, ...]) Given a math expression, renders it in a closely-clipped bounding box to an image file.
oneOf(strs[, caseless, useRegex]) Helper to quickly define a set of alternative Literals, and makes sure to do longest-first testing when there is a conflict, regardless of the input order, but returns a C{L{MatchFirst}} for best performance.
unichr((i) -> Unicode character) Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
unichr_safe(index) Return the Unicode character corresponding to the index, or the replacement character if this is a narrow build of Python and the requested character is outside the BMP.
warn Issue a warning, or maybe ignore it or raise an exception.

Classes

AFM(fh)
Accent(c, state) The font metrics need to be dealt with differently for accents, since they are already offset correctly from the baseline in TrueType fonts.
AutoHeightChar(c, height, depth, state[, ...]) AutoHeightChar will create a character as close to the
AutoWidthChar(c, width, state[, always, ...]) AutoWidthChar will create a character as close to the
BakomaFonts(*args, **kwargs) Use the Bakoma TrueType fonts for rendering.
Box(width, height, depth) Represents any node with a physical location.
Bunch(**kwds) Often we want to just collect a bunch of stuff together, naming each item of the bunch; a dictionary’s OK for that, but a small do- nothing class is even handier, and prettier to use.
Char(c, state) Represents a single character.
Combine(expr[, joinString, adjacent]) Converter to concatenate all matching tokens to a single string.
Empty() An empty token, will always match.
FT2Font Create a new FT2Font object
FT2Image
Fil()
Fill()
Filll()
FollowedBy(expr) Lookahead matching of the given parse expression.
FontProperties([family, style, variant, ...]) A class for storing and manipulating font properties.
Fonts(default_font_prop, mathtext_backend) An abstract base class for a system of fonts to use for mathtext.
Forward([other]) Forward declaration of an expression to be defined later - used for recursive grammars, such as algebraic infix notation.
Glue(glue_type[, copy]) Most of the information in this object is stored in the underlying GlueSpec class, which is shared between multiple glue objects.
GlueSpec([width, stretch, stretch_order, ...]) See Glue.
Group(expr) Converter to return the matched tokens as a list - useful for returning tokens of C{L{ZeroOrMore}} and C{L{OneOrMore}} expressions.
HCentered(elements) A convenience class to create an Hlist whose contents are centered within its enclosing box.
Hbox(width) A box with only width (zero height and depth).
Hlist(elements[, w, m, do_kern]) A horizontal list of boxes.
Hrule(state[, thickness]) Convenience class to create a horizontal rule.
Kern(width) A Kern node has a width field to specify a (normally negative) amount of spacing.
List(elements) A list of nodes (either horizontal or vertical).
Literal(matchString) Token to exactly match a specified string.
MathTextParser(output)
MathtextBackend() The base class for the mathtext backend-specific code.
MathtextBackendAgg() Render glyphs and rectangles to an FTImage buffer, which is later transferred to the Agg image by the Agg backend.
MathtextBackendBitmap()
MathtextBackendCairo() Store information to write a mathtext rendering to the Cairo backend.
MathtextBackendPath() Store information to write a mathtext rendering to the text path machinery.
MathtextBackendPdf() Store information to write a mathtext rendering to the PDF backend.
MathtextBackendPs() Store information to write a mathtext rendering to the PostScript backend.
MathtextBackendSvg() Store information to write a mathtext rendering to the SVG backend.
NegFil()
NegFill()
NegFilll()
Node() A node in the TeX box model
OneOrMore(expr[, stopOn]) Repetition of one or more of the given expression.
Optional(expr[, default]) Optional matching of the given expression.
ParseResults([toklist, name, asList, modal, ...]) Structured parse results, to provide multiple means of access to the parsed data:
Parser() This is the pyparsing-based parser for math expressions.
ParserElement([savelist]) Abstract base level parser element class.
QuotedString(quoteChar[, escChar, escQuote, ...]) Token for matching strings that are delimited by quoting characters.
Regex(pattern[, flags]) Token for matching strings that match a given regular expression.
Rule(width, height, depth, state) A Rule node stands for a solid black rectangle; it has width, depth, and height fields just as in an Hlist.
Ship Once the boxes have been set up, this sends them to output.
SsGlue()
StandardPsFonts(default_font_prop) Use the standard postscript fonts for rendering to backend_ps
StixFonts(*args, **kwargs) A font handling class for the STIX fonts.
StixSansFonts(*args, **kwargs) A font handling class for the STIX fonts (that uses sans-serif characters by default).
StringEnd() Matches if current position is at the end of the parse string
SubSuperCluster() SubSuperCluster is a sort of hack to get around that fact
Suppress(expr[, savelist]) Converter for ignoring the results of a parsed expression.
TruetypeFonts(default_font_prop, ...) A generic base class for all font setups that use Truetype fonts (through FT2Font).
UnicodeFonts(*args, **kwargs) An abstract base class for handling Unicode fonts.
VCentered(elements) A convenience class to create a Vlist whose contents are centered within its enclosing box.
Vbox(height, depth) A box with only height (zero width).
Vlist(elements[, h, m]) A vertical list of boxes.
Vrule(state) Convenience class to create a vertical rule.
ZeroOrMore(expr[, stopOn]) Optional repetition of zero or more of the given expression.
maxdict(maxsize) A dictionary with a maximum size; this doesn’t override all the

Exceptions

MathTextWarning
ParseBaseException(pstr[, loc, msg, elem]) base exception class for all parsing runtime exceptions
ParseException(pstr[, loc, msg, elem]) exception thrown when parse expressions don’t match class;
ParseFatalException(pstr[, loc, msg, elem]) user-throwable exception thrown when inconsistent parse content