I’ve been thinking over the course of the last 6 months or so about the best way to produce EPS and PDF diagrams for inclusion in mathematical documents typeset using LaTeX or pdfLaTeX.

There are many options and many pieces of software, both free and non-free, for doing this. I want to explain the method I settled on as the “best” after trying many different approaches.

I have some requirements: The method must

1. produce high quality vector based EPS or PDF diagrams (not raster images),
2. produce output suitable for inclusion in LaTeX documents which are typeset using pdfLaTeX (so they should be PDF files or EPS files if you include the right package \usepackage{epstopdf} ?). Also this means that the ‘psfrag’ method is ruled out since this doesn’t work under pdfLaTeX,
3. allow for complex diagrams to be produced using a GUI (as opposed to ‘pstricks’ or ‘xy’),
4. allow LaTeX fonts, symbols, macros to be used for labels in the diagram,
5. be cross platform (running under linux and Mac OS X at least).

Here is a partial list of software that I tried at some point in the last 6 months while engaged in a project that required many diagrams to be produced.

1. xfig / jfig
2. Inkscape
3. pstricks package and the graphical editor for it: latexDraw
4. xy package
6. psfrag
7. fragmaster‘ script for getting psfrag functionality in standalone diagrams (see also ‘pdfrack‘)

To cut to the chase, here is the procedure I settled on. It uses GUI drawing program in combination with the ‘fragmaster’ script listed above to get PSFRAG-like functionality in a standalone diagram which can then be converted to PDF and included in a LaTeX document.

1. Using Inkscape or xfig/jfig, produce the diagram. Insert key-letters or keywords where you want LaTeX typeset labels to appear. For instance you could put an ‘a’ where you want $\alpha^5$ to appear. For example:

2. Save the diagram as an EPS file using the following pattern for the filename:  filename_fm.eps . The ‘_fm.eps’ ending is for the fragmaster script. Make sure not to convert text to paths when saving. This is the default in Inkscape for example when saving as EPS and there is a dialog which asks about this after you hit ‘save’.
3. Next we’re going to use the ‘fragmaster’ script, which you can download here: [link]. You need perl installed to run the script, test by running
 perl -v  in a terminal. This script runs with no arguments. It expects two files:

• filename_fm.eps
• filename_fm

and leaves as output the file ‘filename.eps’. The ‘filename_fm’ file contains a dictionary which translates the key-letter/keywords from step 1 into LaTeX code. For the example in step 1 I have the following content in ‘filename_fm’:
 % Just an ordinary comment % % A special comment: % fmopt: width=10cm % % Another special comment: % head: % \usepackage{amsmath} % \usepackage{amsfonts} % end head \psfrag{beta}[Bc]{$\beta(3)$} \psfrag{cdb}[Bc]{$\mathbb{D}_{10 \times 8}(\widetilde{\beta(3)})$} 
The commented lines following ‘special comment’ are important. They set parameters which LaTeX uses to type set the labels which appear in the ‘\psfrag’ commands. There are three arguments passed to the ‘\psfrag’ command: 1st is the keyword that appears in your EPS diagram that you want to replace with some typeset labels, 2nd is a positioning setting (see the psfrag documentation for more details), and 3rd is the LaTeX code for the label.
All that remains is to run the script: (assuming the two files above and the fragmaster.pl script are in the same directory)

 ./fragmaster.pl

Note: You probably need to make fragmaster.pl executable before hand:  chmod a+x fragmaster.pl  or else run it through perl:  perl fragmaster.pl .

4. Now you’ve produced an EPS file called ‘filename.eps’. There is one more (sometimes necessary, sometimes not) step before we have a finished EPS diagram. If your labels occur on the outskirts of the diagram (for instance at the bottom of the diagram in the example of step 1) it can happen that they are partially cut-off by the bounding box of the EPS file. WHen you save your EPS file in the drawing program, the bounding box is set to be just large enough to contain all visible elements. However, when the keyword labels are replaced with LaTeX typeset labels, they may be larger than the keywords and fall outside the original bounding box. One solution to this is the following: enlarge the bounding box by hand. Here’s what I do:
• edit ‘filename.eps’ using ‘vi’ or whatever text editor you like.
• at the begining of the file you see lines such as the following:

 %!PS-Adobe-2.0 EPSF-2.0 %%Creator: dvips(k) 5.96 Copyright 2005 Radical Eye Software %%Title: fm.dvi %%CreationDate: Thu Jul 12 09:15:33 2007 %%BoundingBox: 0 642 284 792 %%DocumentFonts: CMR12 CMMI12 MSBM10 CMR8 CMSY8 CMEX10 %%EndComments 

The important line is the one starting with  %%BoundingBox: . The 4 numbers which follow this define the bounding box for the contents of the EPS file. The first two numbers are (x,y) coordinates for the lower-left corner of the diagram and the second two numbers are coordinates for the upper-right corner. To enlarge the bounding box slightly lets subtract 10 from each of the 1st two numbers and add 10 to each of the last two numbers:

 %%BoundingBox: -10 632 294 802

Negative values are perfectly fine.

5. Now you should have a beautiful EPS diagram with LaTeX typeset labels:

Now, to produce a PDF file for inclusion in a document which is typeset using pdfLaTeX you can simply run the ‘epstopdf’ command: epstopdf filename.eps

6. Sometimes the labels don’t appear in quite the right position after running fragmaster. One option is to go back to the drawing program and adjust the position of the keywords slightly to compensate, save as EPS to ‘filename_fm.eps’, re-run fragmaster, and re-enlarge the bounding box. Another option is to change the positioning arguments to the ‘\psfrag’ commands in the ‘filename_fm’ control file and re-run fragmaster, etc. It usually takes me a couple of runs to get everything perfectly aligned and looking good.

Update 9-22-09: Changed the link to Fragmaster since Vogel’s old homepage no longer exists. The new link points to ctan.org

From → computers, latex, math

Awesome. I’m using this for my paper NOW. 🙂

wrt to fixing the bounding box error, I actually hacked the fragmaster script. basically, change values of the bounding box within the script.

3. Guangming Lang permalink

Awesome! This is what I’ve been looking for.