Add Symbols and Equations to IDL Figures with LaTeX and PSfrag

by saurav on January 4, 2011

While IDL does support math equations and symbols—often made easier by routines like and—the results are not aesthetically pleasing. One particular instance that has annoyed many of us astronomers has been the sunsymbol (⦿).

The available solutions are not ideal: the dot inside the circle is off-center in, the vertical alignment is off in, and only Hershey fonts are supported when using Unicode. In the figure, I have produced the M⦿ symbol using,, and LaTeX’s PSfrag package (left to right); the improvement represented by the third method is clear and expected. as this is produced directly using LaTeX. Also see the Coyote’s post for a more detailed discussion.

One way to resolve this issue is to use LaTeX’s PSfrag package (solution via Manodeep S. via IDL Google Groups), which basically “precisely superimposes any LaTeX construction” (e.g. symbols, equations) into over EPS figures. So you have to create a tag for your LaTeX construction, which then is replaced by the appropriate symbol during TeX compilation. Let me give an example for the M⦿ in IDL; but this can be used for EPS figures made in any program.

In IDL (or any plotting package of your choice) create your EPS (or PS) figure:

SET_PLOT, 'PS',/Encapsulated
DEVICE, FILENAME='figure.eps',/times

where Msun works as the tag that PSfrag will replace with the ⦿. The tag can be whatever you want, as long as it is a series of letters. [Update: As Michael Galloy points out below, PSfrag cannot replace substrings, i.e., the string being fed into PSfrag needs to be the entire XTITLE.] Then, create a basic LaTeX document (say, figure.tex):

\usepackage{geometry, graphicx, psfrag}
 %%% Msun is the tag in the eps file to be replaced by M⦿
\psfrag{Msun}[c][][1.75]{Mass (M$_{\odot}$)}

Then, compile LaTeX and convert the resultant PS file to EPS, if you so desire.

latex figure.tex
dvips -o figure.dvi
ps2epsi fig.epsi
### strip out the preview part from fig.epsi
perl -ne 'print unless /^%%BeginPreview/../^%%EndPreview/' < fig.epsi > fig.eps

Done! The Msun in the original figure.eps is replaced by a properly aligned, TeX-like M⦿ in the new fig.eps!

A couple notes as I experiment more with PSfrag:

1) Note that PSfrag only replaces tags in EPS figures, allowing you to mix your LaTeX with your figures. In other words, you can create all of your figures with tags and add them in your manuscript. All the tags will be replaced when you TeX your document.

2) You can have local a \PSfrag that immediately precedes the \includegraphics in addition to a global PSfrags. The definition in the local environment will have precedence. Really nifty!

{ 7 comments… read them below or add one }

1 Nathan January 4, 2011 at 5:40 pm

Thanks for the tip, this is a really nice way of fixing the ever-pernicious sun symbol problem!

Unfortunately, I think there’s a bug in the code you’ve posted. Specifically, the psfrag command should be:

\psfrag{Mass (Msun)}[c][][1.75]{Mass (M$_{\odot}$)}

I think it would be better to make your xtitle tag something simple like ‘x’ and then replace the whole thing with the latex.


2 Saurav January 4, 2011 at 11:03 pm

Thanks for the catch, Nathan! I found (and fixed) the bug in my TeX file but forgot to change it in the post. I have changed it now.

Using something simple for xtitle is a good idea as long as you don’t have that ‘x’ somewhere else in the figure. I made it elaborate in this post for clarity.

3 Michael Galloy January 4, 2011 at 8:37 pm

I was having problems with your example until I changed the XTITLE value to just Msun. According to the psfrag docs:

“And PSfrag can only replace entire strings, not just parts of one. So if your EPS file contains

(I want to replace the XXX here) show

then the \psfrag command will fail if you supply just the XXX.”

While reading the psfrag docs, I found its \tex{} command and think I will use it that way. I have a wrapper routine to handle all of this from within IDL now.


4 Saurav January 4, 2011 at 11:02 pm

Thanks, Michael! I added the rest of the XTITLE while writing the post rather than when testing PSfrag (bad idea!) and saw no reason to think PSfrag would not be able to handle substrings.

BTW, would you be willing to share the wrapper? I cannot make a self-consistent wrapper handle the LaTeX part.

5 Michael Galloy January 5, 2011 at 12:31 am

I will publish the wrapper soon, I want to use it a bit to make sure it handles the common uses (at least for me).


6 Michael Galloy January 5, 2011 at 1:13 pm

Here is my wrapper for psfrag, VIS_PSFRAG (docs). I also created a stupid, simple wrapper to view/edit PostScript attributes, VIS_PSINFO (docs).


7 Manodeep Sinha January 6, 2011 at 11:40 pm

For some reason, the \tex construct does not work with our gs install. So, I took Michael’s and made it into wrapper for psfrag replacement in eps files. Here is the link to my version:

The calling sequence is:

Assumes that sed, ps2epsi and perl are installed. It’s probably better that all the filenames are fully qualified.

1. Font sizes are not identical between IDL generated text and latex generated replacement text.
2. Does not deal with paper sizes (unlike Michael’s code above).
3. Only tested for some special sed characters.

Leave a Comment

Previous post:

Next post: