matplotlib.texmanager

Support for embedded TeX expressions in Matplotlib via dvipng and dvips for the raster and PostScript backends. The tex and dvipng/dvips information is cached in ~/.matplotlib/tex.cache for reuse between sessions.

Requirements:

  • LaTeX
  • *Agg backends: dvipng>=1.6
  • PS backend: psfrag, dvips, and Ghostscript>=9.0

For raster output, you can get RGBA numpy arrays from TeX expressions as follows:

texmanager = TexManager()
s = "\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!"
Z = texmanager.get_rgba(s, fontsize=12, dpi=80, rgb=(1, 0, 0))

To enable TeX rendering of all text in your Matplotlib figure, set rcParams["text.usetex"] (default: False) to True.

class matplotlib.texmanager.TexManager[source]

Bases: object

Convert strings to dvi files using TeX, caching the results to a directory.

Repeated calls to this constructor always return the same instance.

property cachedir
property cursive
font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
font_family = 'serif'
font_info = {'avant garde': ('pag', '\\usepackage{avant}'), 'bookman': ('pbk', '\\renewcommand{\\rmdefault}{pbk}'), 'charter': ('pch', '\\usepackage{charter}'), 'computer modern roman': ('cmr', '\\usepackage{type1ec}'), 'computer modern sans serif': ('cmss', '\\usepackage{type1ec}'), 'computer modern typewriter': ('cmtt', '\\usepackage{type1ec}'), 'courier': ('pcr', '\\usepackage{courier}'), 'cursive': ('pzc', '\\usepackage{chancery}'), 'helvetica': ('phv', '\\usepackage{helvet}'), 'monospace': ('cmtt', '\\usepackage{type1ec}'), 'new century schoolbook': ('pnc', '\\renewcommand{\\rmdefault}{pnc}'), 'palatino': ('ppl', '\\usepackage{mathpazo}'), 'sans-serif': ('cmss', ''), 'serif': ('cmr', ''), 'times': ('ptm', '\\usepackage{mathptmx}'), 'zapf chancery': ('pzc', '\\usepackage{chancery}')}
get_basefile(self, tex, fontsize, dpi=None)[source]

Return a filename based on a hash of the string, fontsize, and dpi.

get_custom_preamble(self)[source]

Return a string containing user additions to the tex preamble.

get_font_config(self)[source]
get_font_preamble(self)[source]

Return a string containing font configuration for the tex preamble.

get_grey(self, tex, fontsize=None, dpi=None)[source]

Return the alpha channel.

get_rgba(self, tex, fontsize=None, dpi=None, rgb=0, 0, 0)[source]

Return latex's rendering of the tex string as an rgba array.

get_text_width_height_descent(self, tex, fontsize, renderer=None)[source]

Return width, height and descent of the text.

grey_arrayd = {}
make_dvi(self, tex, fontsize)[source]

Generate a dvi file containing latex's layout of tex string.

Return the file name.

make_dvi_preview(self, tex, fontsize)[source]

[Deprecated] Generate a dvi file containing latex's layout of tex string.

It calls make_tex_preview() method and store the size information (width, height, descent) in a separate file.

Return the file name.

Notes

Deprecated since version 3.3.

make_png(self, tex, fontsize, dpi)[source]

Generate a png file containing latex's rendering of tex string.

Return the file name.

make_tex(self, tex, fontsize)[source]

Generate a tex file to render the tex string at a specific font size.

Return the file name.

make_tex_preview(self, tex, fontsize)[source]

[Deprecated] Generate a tex file to render the tex string at a specific font size.

It uses the preview.sty to determine the dimension (width, height, descent) of the output.

Return the file name.

Notes

Deprecated since version 3.3.

property monospace
property rgba_arrayd
property sans_serif
property serif
texcache = '/home/elliott/.cache/matplotlib/tex.cache'