Sunday, May 02, 2010

Manageable-sized, LaTeX-happy .eps figures

In much of physics, LaTeX is the standard for typesetting scholarly work, including research papers and theses.  Traditionally, when working with figures in LaTeX documents, the preferred format is encapsulated postscript (.eps).  There are any number of ways to produce figures in .eps format, but some people seem to have recurring problems doing this with economical file sizes.  For example, today b/c I am on a committee reviewing doctoral theses for a departmental award, I had to download a thesis that was around 50 MB in size, entirely because the figures were unnecessarily huge.  Over the years I've come up with a few different ways of making small, good (in the sense that they render nicely and LaTeX likes them) .eps files.  Here's a quick how-to.  Mostly this is from the point of view of a windows user, by the way.  (I have both PCs and Macs, fwiw).

On a pc, I cannot recommend gsview and ghostscript strongly enough - they're essential tools.  There are linux versions of these as well, of course.  In general, if a postscript file opens cleanly in gsview, you're going to be fine w/ LaTeX compatibility.  gsview is also perfect for redefining bounding boxes, grabbing individual pages from a multipage postscript file, converting .ps into .eps, and other related tasks.    Another set of tools worth having is ImageMagick.  Very helpful for converting between formats.

Option 1:  Use an application that can natively export nice .eps (that is, vector format rather than automatically using bitmapping) with no attached preview.  For example, Origin can do this, as can Matlab or gnuplot if properly set up.  On the Mac, inkscape is a great vector drawing and editing piece of software.  Adobe (who invented postscript, as far as I know) has programs like Illustrator and Photoshop that can do this.  The former seems better at producing economical output.  The latter, without careful intervention, produces bloated, bitmapped, preview-laden junk.  

Option 2:  Use a generic postscript printer driver that can print to a file in .eps format.  Adobe has one for pc that lives here.  Using this, you can do things like use powerpoint as a cheesy compositing tool to draw something or put together multiple images, and then print a particular slide to an .eps file.  The result will be LaTeX-friendly, but not necessarily economical in file size.

Option 3:  Produce an image in a different, nicely economical format and then use a "wrapper" to produce a .eps file.  Here's one example.  Suppose I have a huge bitmap file.  I can use my favorite software (imagemagick, or even MS Paint or powerpoint) to save the image as .jpg or .png.  Then I can use jpeg2ps (in the former case) or bmeps (in either case) to produce a .eps file that is only slightly larger than the originating image.  

This last option provides a way out of the annoying situation of having a huge (say 10 MB) .eps file produced by some other application (like Matlab).  You can open the offending .eps file in gsview, and try to copy the on-screen image (zoomed as needed) to the buffer (that is, click on the displayed image and hit ctl-c on a windows pc).  Then paste the buffer into either paint or powerpoint, and export it as a .png file (nice format - no compression!).  Once you have the .png file, run bmeps to produce a new .eps, and you're all set.  Your 10 MB old .eps file can end up as a 70 kB new .eps file.  This wrapper strategy is also the one recommended by the arxiv folks.

This is by no means exhaustive, but if it saves anyone the pain of having monster .eps files that warp the final documents, then it was worth posting.  (I suspect that someone will comment about how things like this are one reason that journals are drifting toward MS Word.  Word carries with it many, many other problems for scientific writing, in my opinion, but I'd rather not get into a debate on the subject.)    

9 comments:

Zed said...

You can also include pdf figures in TeX as long as you compile directly to pdf. In my experience it's easier to make economical pdf figures than economical eps figures.

Anonymous said...

nobody i knows uses eps anymore; everyone uses pdf files.

easy as pie.

Joe Renes said...

In a similar spirit to the previous two comments, if you're going the PDFLaTeX route, I can highly recommend pgfplots as a means of creating plots directly in a PDFLaTeX-able file. It's based on TikZ, which is an excellent drawing package (like pstricks, but for pdf).

Steve said...

I've had trouble with various latex macros and compatibility with eps. For recent papers I've been going with PDFLaTeX which is also not ideal, but has not caused my computer to barf as much.

Anonymous said...

Thanks for the interesting post, I've always struggled with Maple produced figures as they tend to be fairly large. I'll definitely be using some of the suggested software. I've used the pdf route too, and not found too many problems compared to the eps route.

Anonymous said...

I agree with the above comments; I thought direct production of pdf via pdflatex had long eclipsed using eps figures (and I presume dvips). I produce pdf figures directly from Matlab using a Matlab function I wrote that first uses the Matlab print command to produce an eps file, then uses eps2pdf (comes with modern TeXs) to produce a pdf. Looking at some figs from my last paper, a scatter plot with 1800 points results in a pdf file that is about 100 kb.

Uncle Al said...

Huzzah! for the billed cost of producing a physical thesis. It were as though the sum of degree-granting universities had never done it before, each time, and used lead type plus copper plate figures.

Departments can boost their bottom lines by requiring two volumes: One in clear, the other a hex dump (no .zip or .gz compression allowed; image .bmp only - and then 1600 pixels/inch).

Joel Kelly said...

Out of curiosity (and as someone who is yet to publish in the much desired Nature journal family), how much tweaking do they do to your figures after acceptance? All of their figures are so very pretty and consistent, and I think it really sets them apart. Do they modify existing figures or request raw data?

Douglas Natelson said...

Hello Joel - In my extremely limited experience, they didn't modify any figures. They do have pretty strict guidelines about what they want in terms of formatting. In the one case of a News&Views piece that I wrote for Nature Nano, I was actually surprised at how little they modified about my quick and dirty sketch illustration.