[Tex/LaTex] Forcing latex to handle home-made .ps files in an intelligent way

postscript

I write papers in latex and write figures as PostScript programs. (The latter is not as crazy as it sounds, but that's a topic for elsewhere.)

My question is about the most intelligent way to get LaTeX to incorporate my homemade .ps pictures. There are several options that are short of ideal:

  1. Use latex then dvips then ps2pdf and include graphics by

    \documentclass{amsart}
    \usepackage{graphicx}
    \begin{document}
    \includegraphics{my_ps_file.ps}
    \end{document}  
    

    This works, but the ps2pdf step on a file with lots of figures is very time-consuming. (It has been on the order of a minute on a fast computer for some papers. Small .ps files can have a lot in them because PostScript is a complete programming language.)

  2. Manually do ps2pdf on each .ps file

    ps2pdf -dEPSCrop -dNOSAFER my_ps_file.ps
    

    then use pdflatex

    \documentclass{amsart}
    \usepackage{graphicx}
    \begin{document}
    \includegraphics{my_ps_file.pdf}
    \end{document}  
    

    This works too, but there is the extra command-line step for every .ps file that has been modified since your last run of pdflatex.

  3. Use pdflatex and graphics rules

    \documentclass{amsart}
    \usepackage{graphicx,epstopdf}
    \epstopdfsetup{suffix=}
    \DeclareGraphicsExtensions{.ps}
    \DeclareGraphicsRule{.ps}{pdf}{.pdf}{`ps2pdf -dEPSCrop -dNOSAFER #1 \noexpand\OutputFile}
    \begin{document}
    \includegraphics{my_ps_file}
    \end{document}  
    

    The 5th line tells LaTeX that when it sees \includegraphics{my_ps_file}, it should look for a file my_ps_file.ps.

    The 6th line tells LaTeX how to handle my_ps_file.ps:
    In order of the arguments, when (1) it sees a .ps file, it should eventually expect to have a .pdf file to insert, (3) it should read bounding box information from the .pdf file, and (4) it should run a certain command on the .ps file to get the .pdf file.

    The package epstopdf is used here only so that I can use the 4th line \epstopdfsetup{suffix=}, which tells LaTeX to call the .pdf file my_ps_file.pdf rather than my_ps_file-ps-converted-to.pdf.

    This works, and it automatically runs ps2pdf on my_ps_file.ps file, but only if it needs to: if it sees that my_ps_file.pdf already exists, it uses my_ps_file.pdf.
    (To get this feature, we used \includegraphics{my_ps_file} instead of \includegraphics{my_ps_file.ps}).

    This possibility eliminates the slowness of possibility (1) and eliminates the extra command-line steps of possibility (2). But unfortunately, it adds a new command-line step for every modified .ps file: You have to manually remove my_ps_file.pdf every time you change my_ps_file.ps or else LaTeX will keep using the old my_ps_file.pdf rather than making a new one.

Now for the question: The option I want, but don't know how to implement, would work just like (3), except that if my_ps_file.pdf is present and is newer than my_ps_file.ps LaTeX uses it, and otherwise runs ps2pdf on my_ps_file.ps.

Any ideas?

Best Answer

I would use the full power of epstopdf:

\documentclass{amsart}
\usepackage{graphicx,epstopdf}
\epstopdfsetup{update}
\DeclareGraphicsExtensions{.ps}
\epstopdfDeclareGraphicsRule{.ps}{pdf}{.pdf}{ps2pdf -dEPSCrop -dNOSAFER #1 \OutputFile}
\begin{document}
\includegraphics{my_ps_file}
\end{document}

This will run the conversion program only if the target file (.pdf) is older than the source file. Remember to call pdflatex with the --shell-escape command line option.

Related Question