Details regarding TeX interpreter for different backends

Michael Droettboom <mdroe@...86...> writes:

The other approach is used when "text.usetex" is True. It calls out
to a real "tex" interpreter and then interprets the DVI it produces to
convert it into a form the matplotlib backends can use. Since I
didn't write this code, I'm not as familiar with the details.

There are actually at least three mechanisms used by text.usetex:

(1) The Postscript backend uses psfrag. The Postscript file has
placeholders for LaTeX text, and the file is processed with LaTeX and
dvips to replace these placeholders with typeset text.

(2) The Agg backend puts each piece of TeX text into a separate file
(which you can find in ~/.matplotlib/tex.cache), processes it with LaTeX
into dvi and with dvipng into png, reads in the png files and overlays
them onto the image. (That's at least what I imagine it does; I have not
looked at the code.)

(3) The pdf backend uses the dviread module (written originally by me).
The various pieces of text are written into different tex files, just as
with the Agg backend, then processed with LaTeX into dvi files, which in
turn are translated by dviread into sequences of (x, y, font, character)
tuples. The dviread module reads the configuration file,
which maps the TeX font names into Postscript fonts, which are embedded
in the output PDF file, and the characters are placed at the coordinates
specified in the dvi file.

The dvipng approach avoids dealing with fonts, but the output is a
bitmap. With psfrag you can only get Postscript output (which is
nontrivial to parse), but it includes scalable fonts. The dviread
approach gets you both scalable fonts and the information of where each
character is, but you will have to deal with the fonts yourself. In PDF
output this is relatively easy, since we can piggyback on the
configuration file, which conveniently points to font files that can be
embedded in PDF.


Jouni K. Sepp�nen