Making EPS figures

Figure Problems

Even though electronic manuscript preparation has been common practice for nearly twenty years, figure preparation remains an annoying and difficult problem. While LaTeX provides an elegant, full-featured, and mature system for typsetting text and mathematical equations, there is no convenient, free software equivalent for scientific graphics that rivals LaTeX in terms of flexibitlity and quality of output. Fans of grace and gnuplot may disagree. But these programs have some serious limitations and can, on occassion, produce quite amaturish output. A promising new graphics language called asymptote produces excellent output and offers a high degree of control. It's probably the best freeware solution. On the other hand, it has a steep learning curve and requires that you master it's C++-inspired syntax.

Several commercial packages have excellent data analysis and plotting functions: e.g., Maple, Mathematica, Matlab, and Igor. But even these don't always give you full control to lay out your figures as you want. What if you want a to arrange complicated figure insets in order to save space (and get that PRL to fit into four pages)? That isn't always possible with the options that these programs provide.

Example Figure

Gnuplot + Illustrator

The solution I've adopted is to generate rough plots with gnuplot and then touch them up manually with Adobe Illustrator, a powerful vector graphics program from the inventors of postscript. (Adobe makes Illustrator available to academic users at a reduced price.) Gnuplot has quite a good plotting engine. It's fully scriptable and offers built-in nonlinear fitting routines and other goodies. It can also output directly to (a very old) Illustrator format.

gnuplot> set terminal aifm color 9
gnuplot> set output "plot.ai"
gnuplot> plot "run05.dat" using 1:2:3 with errorbars, sin(x), cos(x)

The resulting file plot.ai can then be opened and edited in Illustrator. To save the finished graphic as encapsulated postscript, you have several options:

  1. Select Save as ... Illustrator EPS from the File menu. The appropriate settings are as follows.
    • Compatibilty: Illustrator CSx EPS
    • Preview Format: none
    • Options: Include Document Fonts, CMYK Postscript (for figures with colour)
    • PostScript: Level 2
    If you have imported graphics by link (via Place... from the File menu), you should also select
    • Option: Include Linked Files
    Although this is the most straightforward approach, the next option is much better for reasons I'll explain later.
  2. Save as ... Adobe PDF from the File menu. You'll want to uncheck the box that says
    • Preserve Editing Capabilities
    You can convert the resulting file either with the pdftops -eps or, if you have it installed, Acrobat Professional.

Illustrator Bug

Illustrator eps files that contain embedded raster images (or other binary data) are incompatible with dvips. The “bug” amounts to a disagreement over what is the correct way to specify the byte count for a block of hex data sandwiched between %%BeginData and %%EndData labels. Fortunately, there is a simple workaround. Tomas Rokicki has written a perl script that recomputes the byte count to conform with dvips.

perl fixill.pl < bad.eps > good.eps

Shrinking the File Size

There is an additional complication. Illustrator treats eps as a lossless, intrinsic file format. It does so by storing as comments in the postscript preamble all the preference, history, and layer information needed to reconstruct the editing session. This is an inconvenient feature since it can bloat the file to many megabytes, well beyond the 1mb size limit of the e-Print archive (arXiv).

The simplest solution is to strip those comments out by hand. They reside at the very end of the eps file enclosed in a PrivateData block:

%AI9_PrivateDataBegin
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: Adobe Illustrator(R) 12.0
.
.
.
%AI9_PrivateDataEnd

If, after surgery, the file is still unreasonably large, it may be helpful to convert your figure labels from text to vector objects using Create Outlines from the Type menu. Even if your figure uses only a few letters from a given typeface, a description of the entire font set will be included in your eps file. By using outlines, you're free to disable the Include Document Fonts option when saving.

Sometimes file bloat is a result of graphics objects that contain unnecessarily many points or vertices. Applying Object -> Path -> Simplify... to a curve with Curve Precision = 100% can often reduce the number of points from hundreds or thousands to dozens. It may also help to rasterize extremely complex portions of a figure (e.g., a density plot made up of thousands of filled boxes) using Object -> Rasterize...

If this all sounds like too much trouble, try having GhostScript, which is much less verbose than Adobe's PostScript engine, reinterpret your file.

% gs -r2400 -dBATCH -dNOPAUSE -dSAFER -q -dNOCACHE \ 
     -sDEVICE=epswrite -sOutputFile=trimmed.eps fat.eps

Unfortunately, GhostScript sometimes miscomputes the figure size. You may have to manually copy over the bounding box info from the original file.

%%BoundingBox: 0 0 504 234 
%%HiResBoundingBox: 0 0 504 233.3072 
%%CropBox: 0 0 504 233.3072

Using Computer Modern Fonts

Most graphing programs do not allow you to typeset complicated axis labels. It can be particularly difficult to create labels that include mathematical symbols. It would be nice if you could simply generate the labels in LaTeX and drop them into the Illustrator document.

In fact, you can. The only impediment to doing this is that the Computer Modern fonts don't follow Adobe's PostScript font naming convention and so aren't recognized by Illustrator. Alberto Arabia's Tex-Illustrator fonts package provides Type 1 PostScript versions of Computer Modern with the correct names:

http://www.esm.psu.edu/mac-tex/fonts/TeX-Illustrator.sit

(These and other free font sets are available from http://www.esm.psu.edu/mac-tex. A very good commercial alternative is the OpenType font UniMath from Applied Symbols.) On MacOS X, the fonts should be installed by moving the Tex-Illustrator folder into the /Library/Application Support/Abode/Fonts/Reqrd/ directory for versions up to CS2 and into /Library/Application Support/Abode/Fonts/ for CS3. Starting with CS4, you should install the fonts system-wide using the Font Book application.

Computer Modern fonts in Font Book

When creating a label, it's best to use the minimal latex document class, which suppresses page numbers, headers, footers, and whatnot; e.g.,

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
$1/\sqrt{\bar{N}_{\text{c}}}$
\end{document}

The script above produces a label that looks like

tex label

The file should be processed in two steps, as follows:

% latex label.tex
% dvips -Ppdf -o label.pdf label.dvi

(Do not use pdflatex; Illustrator cannot handle its font encoding.) The resulting pdf file can be opened, edited, and manipulated with Illustrator.

PDF Files

There may be instances when your figures are very large as a result of the inefficiencies of the eps format (e.g., embedded images are stored in uncompressed ascii) and nothing can be done about it. It may then be worth considering switching to pdf.

Conversion of eps files to pdf can be accomplished via Ghostscipt by invoking the ps2pdf command.

ps2pdf figure.eps figure.pdf

Documents with pdf figures must be processed by the pdflatex engine, which can be enforced by setting the pdfoutput variable. In that case, the output of the latex command will be a pdf file. No dvi file will be produced.

\documentclass[aps,prb,twocolumn,superscriptaddress,
               amsmath,amssymb,amsfonts,graphics]{revtex4}
\pdfoutput=1
\usepackage{hyperref}
\hypersetup{pdfauthor={AB Smith and CD Jones},
            pdftitle={Strong correlations in X${}_2$Y${}_{12}$}}

%% preamble %%

\begin{document}            

Figures are then included in the usual way

includegraphics{figure.pdf}

Note that the arXiv accepts pdf figures but as of yet the APS journals do not.