~ Linux tricks and other computer wizardry.

Using PSfrag with pdfLaTeX

The Problem:
You want to include graphs with beautifully formatted labels, this is achieved by using PSfrag and EPS files. However, you also want the ease and features from using pdflatex. Unfortunately pdflatex does not natively support postscript, which includes both PSfrag and the EPS format.

If you are unaware of PSfrag and what it does, I suggest looking at its page on wikipedia, CTAN (Comprehensive Tex Archive Network) or the documentation pdf. This is also related to the PSTricks package which can be used in the same way.

The Solution I Use:There is a LaTeX package called auto-pst-pdf, I found it via my question on tex.stackexchange.com, it also has a page on CTAN. Here is a brief tutorial on how to use it.

Firstly you need to make sure it is installed, this is easiest by attempting to compile a pdflatex file with the package included. Most up to date full LaTeX distributions include it, if yours doesn’t, find out how to install it from the documentation of the distribution you use.

At this point you may come up against a compilation error telling you something about being unable to shell-escape. If you are using command line compilation you need to add an option, --shell-escape which tells the compiler it is allowed to escape back to the shell. If you are using an IDE, look in the documentation to find out how to add compilation options. If you are using Windows it is slightly different (it uses write18), but as I don’t use Windows I can’t help, sorry.

Please Note: Enabling shell-escape is a security risk. It allows arbitrary code execution. This is generally not a problem if you only ever compile Latex documents you have written yourself. However, it is possible to completely erase your entire home directory from inside the Latex document. If you have shell-escape enabled please be sure of everything in the document you are compiling. The shell escape commands can even be embedded inside others for example the postscript environment I am about to introduce.

Once shell-escape is enabled you can use it for PSfrag. All PSfrag commands must be placed inside a \begin{postscript} environment. As they are all related to figures I generally find it best to nest them inside your figure environment. The whole postscript environment goes where the \includegraphics{} command would normally go. Inside this postscript environment you put the replacements eg.\psfrag{a}{\alpha}. Finally you put the \includegraphics{} command making sure the graphic file is in EPS format (Encapsulated PostScript).

It will take slightly longer to compile, but this may not even be noticeable. You should now have access to the full power of PSfrag (and PSTricks) whilst using pdflatex.

You will get left with an extra file called “filename-pics.pdf” once you compile the document, see below for brief explanation.

Summary of how it works:
When you compile the document, it looks for any postscript environments. If it finds them, it escapes, and processes them with a different program which can handle postscript commands. It then converts the output of this program to pdf graphics (saved in the “filename-pics.pdf” file), which are then included in the original document as normal figures where the postscript environments originally were.

Hi Prashant,
I don’t know what the problem really is, unless you give me a minimal example or an error message that you’re getting. A few things to check though:
Is the picture you’re trying to include an eps file?
Are you including it inside a postscript environment?
Are you passing the –shell-escape flag to the compiler?
Is the picture in the same folder as the document?
Does filename-pics.pdf include the figure correctly?
If it doesn’t work, can you tell me more information of what you’re using and what is going wrong.
Dom