You are reading documentation for the unreleased version of Matplotlib. Try searching for the released version of this page instead?
Version 2.1.1.post1094+g835fdc5
Fork me on GitHub

Table Of Contents

Related Topics

This Page



A module for reading dvi files output by TeX. Several limitations make this not (currently) useful as a general-purpose dvi preprocessor, but it is currently used by the pdf backend for processing usetex text.


with Dvi(filename, 72) as dvi:
    # iterate over pages:
    for page in dvi:
        w, h, d = page.width, page.height, page.descent
        for x,y,font,glyph,width in page.text:
            fontname = font.texname
            pointsize = font.size
        for x,y,height,width in page.boxes:
class matplotlib.dviread.Dvi(filename, dpi)

Bases: object

A reader for a dvi (“device-independent”) file, as produced by TeX. The current implementation can only iterate through pages in order, and does not even attempt to verify the postamble.

This class can be used as a context manager to close the underlying file upon exit. Pages can be read via iteration. Here is an overly simple way to extract text without trying to detect whitespace:

>>> with matplotlib.dviread.Dvi('input.dvi', 72) as dvi:
>>>     for page in dvi:
>>>         print ''.join(unichr(t.glyph) for t in page.text)

Read the data from the file named filename and convert TeX’s internal units to units of dpi per inch. dpi only sets the units and does not limit the resolution. Use None to return TeX’s internal units.


Close the underlying file if it is open.

class matplotlib.dviread.DviFont(scale, tfm, texname, vf)

Bases: object

Encapsulation of a font that a DVI file can refer to.

This class holds a font’s texname and size, supports comparison, and knows the widths of glyphs in the same units as the AFM file. There are also internal attributes (for use by that are not used for comparison.

The size is in Adobe points (converted from TeX points).


scale : float

Factor by which the font is scaled from its natural size.

tfm : Tfm

TeX font metrics for this font

texname : bytes

Name of the font as used internally by TeX and friends, as an ASCII bytestring. This is usually very different from any external font names, and dviread.PsfontsMap can be used to find the external name of the font.

vf : Vf

A TeX “virtual font” file, or None if this font is not virtual.


texname (bytes)
size (float) Size of the font in Adobe points, converted from the slightly smaller TeX points.
widths (list) Widths of glyphs in glyph-space units, typically 1/1000ths of the point size.
class matplotlib.dviread.Encoding(filename)

Bases: object

Parses a *.enc file referenced from a style file. The format this class understands is a very limited subset of PostScript.

Usage (subject to change):

for name in Encoding(filename):
Parameters:filename : string or bytestring


encoding (list) List of character names

alias of Font

class matplotlib.dviread.PsfontsMap(filename)

Bases: object

A formatted file, mapping TeX fonts to PS fonts.


>>> map = PsfontsMap(find_tex_file(''))
>>> entry = map[b'ptmbo8r']
>>> entry.texname
>>> entry.psname
>>> entry.encoding
>>> entry.effects
{'slant': 0.16700000000000001}
>>> entry.filename
Parameters:filename : string or bytestring


For historical reasons, TeX knows many Type-1 fonts by different names than the outside world. (For one thing, the names have to fit in eight characters.) Also, TeX’s native fonts are not Type-1 but Metafont, which is nontrivial to convert to PostScript except as a bitmap. While high-quality conversions to Type-1 format exist and are shipped with modern TeX distributions, we need to know which Type-1 fonts are the counterparts of which native fonts. For these reasons a mapping is needed from internal font names to font file names.

A texmf tree typically includes mapping files called e.g.,, or The file is used by dvips, by pdfTeX, and by dvipdfm. might avoid embedding the 35 PostScript fonts (i.e., have no filename for them, as in the Times-Bold example above), while the pdf-related files perhaps only avoid the “Base 14” pdf fonts. But the user may have configured these files differently.

class matplotlib.dviread.Tfm(filename)

Bases: object

A TeX Font Metric file.

This implementation covers only the bare minimum needed by the Dvi class.

Parameters:filename : string or bytestring


checksum (int) Used for verifying against the dvi file.
design_size (int) Design size of the font (unknown units)
width, height, depth (dict) Dimensions of each character, need to be scaled by the factor specified in the dvi file. These are dicts because indexing may not start from 0.
class matplotlib.dviread.Vf(filename)

Bases: matplotlib.dviread.Dvi

A virtual font (*.vf file) containing subroutines for dvi files.


vf = Vf(filename)
glyph = vf[code]
glyph.text, glyph.boxes, glyph.width
Parameters:filename : string or bytestring


The virtual font format is a derivative of dvi: This class reuses some of the machinery of Dvi but replaces the _read loop and dispatch mechanism.

matplotlib.dviread.find_tex_file(filename, format=None)

Find a file in the texmf tree.

Calls kpsewhich which is an interface to the kpathsea library [R22]. Most existing TeX distributions on Unix-like systems use kpathsea. It is also available as part of MikTeX, a popular distribution on Windows.


filename : string or bytestring

format : string or bytestring

Used as the value of the --format option to kpsewhich. Could be e.g. ‘tfm’ or ‘vf’ to limit the search to that type of files.


[R22](1, 2) Kpathsea documentation The library that kpsewhich is part of.