latex in ps via psfrag

In honor of Darren trying to get his thesis printed using mpl figures,
and encountering a lot of pain with fonts and mathtext, I added a
psfrag latex backend today. psfrag is a latex package that enables
you to substitute sentinel strings in an eps figure with latex
expressions (eg as in xfig). You create an eps with a bunch of marker
strings like "psmarker0" or "psmarker1" and then write a latex file
that does the substitutions

\psfrag{psmarker0}[bl][bl][2.000000][0.000000]{\TeX\ is Number $e^{-i\pi}$!}
\psfrag{psmarker1}[bl][bl][1.000000][0.000000]{0}

The LaTeX backend generates a *.eps file and a *.tex file. You can
then latex and dvips the tex file to get a true ps, or just embed the
generated latex commands directly into your document. This uses latex
for all text elements, giving a unified font look and feel.

Here is an example

  > python examples/tex_demo.py -dLaTeX
  > latex tex_demo.tex
  > dvips -o tex_demo.ps tex_demo.dvi
  > ggv tex_demo.ps

There are a few problems

  * the page width and figure placement in the latex document are off
    center

  * the text color is not being respected

  * to get the width and height of the string, I tex the individual
    strings separately, run dvips on them, and get the bounding box
    from the generated file. This all happens with caching in
    matplotlib.texmanager. Right now the fontsize is being ignored in
    this process so the layout will be off for nonstandard font sizes
    -- anything other than the default design size of latex which
    defaults to 10pt I think.

  * the text doesn't scale right if you provide a size arg to
    includegraphics, eg [width=4.in]

I'm sure these details can be worked out by someone who has the
patience to read the psfrag manual closely and knows a bit about tex
and dvips -- the backend is only 180 lines of code. I'm kind of busy
right now with other work, so I was hoping some enterprising soul
would like to take this and run with it, polish it, fix it and test
it.... Darren? If not, I'll get to it as I can.

Changes in CVS

JDH

In honor of Darren trying to get his thesis printed using mpl figures,
and encountering a lot of pain with fonts and mathtext, I added a
psfrag latex backend today. psfrag is a latex package that enables
you to substitute sentinel strings in an eps figure with latex
expressions (eg as in xfig). You create an eps with a bunch of marker
strings like "psmarker0" or "psmarker1" and then write a latex file
that does the substitutions

\psfrag{psmarker0}[bl][bl][2.000000][0.000000]{\TeX\ is Number
e^\{\-i\\pi\}!} \psfrag{psmarker1}[bl][bl][1.000000][0.000000]{0}

The LaTeX backend generates a *.eps file and a *.tex file. You can
then latex and dvips the tex file to get a true ps, or just embed the
generated latex commands directly into your document. This uses latex
for all text elements, giving a unified font look and feel.

I don't know what to say, except, Thank you.

Here is an example

  > python examples/tex_demo.py -dLaTeX
  > latex tex_demo.tex
  > dvips -o tex_demo.ps tex_demo.dvi
  > ggv tex_demo.ps

There are a few problems

  * the page width and figure placement in the latex document are off
    center

  * the text color is not being respected

  * to get the width and height of the string, I tex the individual
    strings separately, run dvips on them, and get the bounding box
    from the generated file. This all happens with caching in
    matplotlib.texmanager. Right now the fontsize is being ignored in
    this process so the layout will be off for nonstandard font sizes
    -- anything other than the default design size of latex which
    defaults to 10pt I think.

  * the text doesn't scale right if you provide a size arg to
    includegraphics, eg [width=4.in]

I'm sure these details can be worked out by someone who has the
patience to read the psfrag manual closely and knows a bit about tex
and dvips -- the backend is only 180 lines of code. I'm kind of busy
right now with other work, so I was hoping some enterprising soul
would like to take this and run with it, polish it, fix it and test
it.... Darren? If not, I'll get to it as I can.

Changes in CVS

I'll be on a bus 24 of the next 60 hours, to get to my brother's graduation.
I'll print out what is in cvs and the psfrag manual for the trip, and can't
wait to get my hands on the code when I get back.

Darren

···

On Tuesday 17 May 2005 6:44 pm, John Hunter wrote:

John Hunter wrote:

In honor of Darren trying to get his thesis printed using mpl figures,
and encountering a lot of pain with fonts and mathtext, I added a
psfrag latex backend today.

John, this is incredible. For those of you who haven't tried the demo from CVS yet, it looks to naive me like John's achieved the Holy Grail of putting arbitrary TeX into labels in matplotlib. MATLAB doesn't do this, probably never will. Wow.

OK, enough groveling.

I played with the new latex backend tonight, and made some incremental
improvements. texmanager now calls latex instead of tex, so we can make use
of some of the more complex layout commands, \frac{}{} for instance. A new
version of tex_demo.py is in cvs, try running:

  > python examples/tex_demo.py -dLaTeX
  > latex tex_demo.tex
  > dvips -o tex_demo.ps tex_demo.dvi
  > ggv tex_demo.ps

On a related subject, tex layout via text.usetex does not appear to be working
at the moment. I tried undoing my changes but was not able to get the old
results back. Did I break this or did I catch it in transition?

There are a few problems

  * the page width and figure placement in the latex document are off
    center

  * the text color is not being respected

  * to get the width and height of the string, I tex the individual
    strings separately, run dvips on them, and get the bounding box
    from the generated file. This all happens with caching in
    matplotlib.texmanager. Right now the fontsize is being ignored in
    this process so the layout will be off for nonstandard font sizes
    -- anything other than the default design size of latex which
    defaults to 10pt I think.

The horizontal placement appears to be fixed in CVS. I haven't gone searching
for trouble in the vertical layout yet.

  * the text doesn't scale right if you provide a size arg to
    includegraphics, eg [width=4.in]

Darren

The LaTeX backend generates a *.eps file and a *.tex file. You can
then latex and dvips the tex file to get a true ps, or just embed the
generated latex commands directly into your document. This uses latex
for all text elements, giving a unified font look and feel.

Here is an example

  > python examples/tex_demo.py -dLaTeX
  > latex tex_demo.tex
  > dvips -o tex_demo.ps tex_demo.dvi
  > ggv tex_demo.ps

There are a few problems

  * the page width and figure placement in the latex document are off
    center

Maybe not. If you use a latex centering environment, I think the figure is
centered. Even in a centering environment, it may still appear to be off
center, since the axes+labels may not have been centered in the figure window
in the first place.

  * the text color is not being respected

Fixed in cvs.

  * to get the width and height of the string, I tex the individual
    strings separately, run dvips on them, and get the bounding box
    from the generated file. This all happens with caching in
    matplotlib.texmanager. Right now the fontsize is being ignored in
    this process so the layout will be off for nonstandard font sizes
    -- anything other than the default design size of latex which
    defaults to 10pt I think.

This really is fixed now, for both horizontal and vertical alignment.

  * the text doesn't scale right if you provide a size arg to
    includegraphics, eg [width=4.in]

This is expected behavior for PSfrag. You should instead wrap \includegraphics
in either a \resizebox or a \scalebox to rescale the text with the figure.

It looks like tex_demo.py, without caching, takes about 30% longer with LaTeX
than it does for Tex (3 seconds vs 2.3 seconds on my computer). So CVS is
back to using TeX. We may want to include a link (or a copy) of this pdf on
the MPL website: http://www.csit.fsu.edu/~mimi/tex/tex-refcard.pdf, the
source declares it to be freely distributed.

Darren

···

On Tuesday 17 May 2005 6:44 pm, John Hunter wrote:

Hi everyone,

Darren Dale wrote:

> Would you send me a copies of the bad eps and the fixed version, and also
> a copy of ps2eps? I don't have that program on my system, and was not
> able to find it on the web.

Here goes. As it turns out, ps2eps is a simple perl script which I got god
knows when. So people won't actually have it on their systems, it's
something that lives in /usr/local/bin on my box, sorry.

But no worries. I can lie with using ps2eps for now, until I can upgrade
to a moer current ghostscript.

I am still unhappy with the bitmapped fonts that result from the conversion to
eps, using ghostscript's epswrite. They look ok in Adobe Reader, but not so
in kpdf or ggv. (I was inspired to look at this today after discovering that
Adobe Reader for linux does not display correctly when I run in 1024x768
resolution on my native 1680x1050 laptop. Kpdf displays correctly, but the
fonts are terrible.)

I looked into ps2eps some more today, to see if it would generate a file that
I could embed in a latex document. The short answer is no.

The long answer is that PSFrag was not designed to do what I am trying to do:
generate an eps file that can later be embedded in a document. It uses a
number of PostScript operators that are illegal in an eps file: setglobal,
statusdict and userdict. Here is the blurb from PostScript Language
Reference, Second Edition, Appendix I: setglobal disrupts page independence
and nesting of included documents. [...] Creation and modification of global
objects are uneffected by save-restore operators.

I think it might be worth looking into the way PyX is dealing with TeX/LaTeX,
as someone recently suggested. PyX's eps output looks just like standard (no
tex) MPL output, where these nesting issues do not exist. They even have the
same issue of dumping entire font definitions into the output.

Darren

···

On Wednesday 08 June 2005 11:42 am, Fernando Perez wrote: