Date: August 15th, 2009
Cate: null, software
Tags: , , ,  

Convert LaTeX figures to stand-alone graphics

As I am wont to do, I’ve been frittering away the hours poking and prodding TeX to make beautiful graphics.  This is my way of giving myself the impression of working hard, and actually accomplishing things, though not things that necessarily advance my career.

I’m quite fond of PSTricks, a LaTeX package which allows the author to make use of PostScript’s extensive computational and graphical capabilities from within a TeX document.  My project for this week was to come up with a way to give these figures to my advisor, who works exclusively in MS Word. So what I needed was a way to export TeX figures to graphics files. Here’s what I came up with.

Essentially, I create a file which includes the \pspicture environment and all the graphics commands, which I call somepicture.fig. I then \input{somepicture.fig} into my main document when I want the picture to appear. When I then want to convert the picture into a standalone graphic, I run a script called figuretops.sh(link to file) which performs the conversion by making use of the very neat “file here” notation in bash:

cat >> filename.tex <<EOF
\documentclass{article}
\usepackage{pstricks-add}
\usepackage{calc}

\begin{document}
\thispagestyle{empty}
\color{white}
\fbox{
\color{black}
\input{}
}
\end{document}
EOF

When I run LaTeX on this file, it gets to the blank \input{} directive and asks the user for the filename. I solve this by echoing the filename into the LaTeX invocation in the bash script. I then clean up temporary files and use

convert -density 300 somefigure.eps somefigure.png

to convert the eps image to a 300-DPI image.

Here’s a source figure, the bash script, and an image it produced:

displacement

Here’s another (naively crafted: TeX source) but more interesting figure:

basic-mfa-model

3 Comments

  1. August 15th, 2009
    REPLY))

  2. It seems it would be preferable to keep the drawing in a vector format. I don’t use Windows either, but I think the appropriate format might be “Windows Metafile” or “Enhanced Metafile” (EMF).

    http://en.wikipedia.org/wiki/Enhanced_Metafile

    I found this program that claims to be able to convert postscript files into EMF (and other vector formats):

    http://www.pstoedit.com/

    So, you would just replace the call to ImageMagick’s “convert” to this program instead.

    1F

  3. brandonbrandon  
    August 25th, 2009
    REPLY))

  4. @ Tobin Fricke
    Thanks for the note, Tobin– I was wondering if you would read this!

    It looks like PStoEdit only operates the WMF conversion if it’s running on a Windows system already- so it would be a bit of work to set up my TeX environment on Windows (and it would have to include cygwin, which I’ve so far been able to avoid). But it would be nice to have a vector image.

    Incidentally, the reverse problem (WMF-to-EPS) is solved with a program called WMF2EPS, but it relies on an external postscript driver- and the Adobe standalone driver is not compatible with Vista!

    My advisor is plainly displeased at the results of latex2rtf so it looks like I will have to start composing documents natively in word. or postprocessing the rtf more aggressively. NOBODY TeXes in my new discipline and I’m beginning to find it quite isolating.

    2F

  5. brandonbrandon  
    December 17th, 2009
    REPLY))

  6. UPDATE.. it turns out ‘dvips -E’ is not a good way to generate an eps, because it chooses a resolution and may rasterize fonts. I don’t know exactly what it does, but for text-heavy graphics it doesn’t work too well. My new version uses:

    export GS_OPTIONS=”-dPDFSETTINGS=/prepress -dEmbedAllFonts=true” && ps2eps -f -l

    instead. This also eliminates the need for the \fbox construction, as ps2eps seems to be very smart about determining the bounding box.

    3F

Leave a Reply

 Name

 Mail

 Home

[Name and Mail is required. Mail won't be published.]