Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 11 '12 at 17:05

This question came from our site for professional and enthusiast programmers.

    
Err...is there no way to get your plot generating tool to spit out PDF in the first place? That's what I've done. Or when you say that you write figures in postscript your real mean that you write them? –  dmckee May 11 '12 at 16:45
1  
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner May 11 '12 at 17:06

3 Answers 3

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.

share|improve this answer

The current release of TeXlive does just this with .eps files:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{tmp1}
\end{document}

I have a file tmp1.eps, but not tmp1.pdf, so the first run of pdflatex says

Package epstopdf Info: Source file: <tmp1.eps>
(epstopdf)                    date: 2012-04-25 19:09:03
(epstopdf)                    size: 204851 bytes
(epstopdf)             Output file: <tmp1-eps-converted-to.pdf>
(epstopdf)             Command: <repstopdf --outfile=tmp1-eps-converted-to.pdf 
tmp1.eps>
(epstopdf)             \includegraphics on input line 4.
runsystem(repstopdf --outfile=tmp1-eps-converted-to.pdf tmp1.eps)...executed sa
fely (allowed).

Package epstopdf Info: Result file: <tmp1-eps-converted-to.pdf>
(epstopdf)                    date: 2012-05-11 13:09:38
(epstopdf)                    size: 64649 bytes.

<tmp1-eps-converted-to.pdf, id=1, 614.295pt x 794.97pt>
File: tmp1-eps-converted-to.pdf Graphic file (type pdf)

<use tmp1-eps-converted-to.pdf>

The second run just uses this file:

[2 <./tmp1-eps-converted-to.pdf>] (./tmp.aux) )

Now I change the postscript file: touch tmp1.eps, and pdf is regenerated:

Package epstopdf Info: Source file: <tmp1.eps>
(epstopdf)                    date: 2012-05-11 13:17:04
(epstopdf)                    size: 204852 bytes
(epstopdf)             Output file: <tmp1-eps-converted-to.pdf>
(epstopdf)                    date: 2012-05-11 13:16:39
(epstopdf)                    size: 64651 bytes
(epstopdf)             Command: <repstopdf --outfile=tmp1-eps-converted-to.pdf 
tmp1.eps>
(epstopdf)             \includegraphics on input line 4.
runsystem(repstopdf --outfile=tmp1-eps-converted-to.pdf tmp1.eps)...executed sa
fely (allowed).

Package epstopdf Info: Result file: <tmp1-eps-converted-to.pdf>
(epstopdf)                    date: 2012-05-11 13:17:08
(epstopdf)                    size: 64651 bytes.

<tmp1-eps-converted-to.pdf, id=1, 614.295pt x 794.97pt>
File: tmp1-eps-converted-to.pdf Graphic file (type pdf)

<use tmp1-eps-converted-to.pdf>

So save your PostScript files as .eps and check the system is intelligent enough to understand what you want.

Of course as the last resort you can use make: it knows how to check whether to regenerate dependencies.

share|improve this answer

I think the answer by egreg is the most pragmatic. For completeness I'd like to point out that for this kind of problem the Make tool was invented. A Makefile contains rules that invoke a tool when a source is more recent than the file that the tool derives from the source.

doc.pdf: psfile1.pdf psfile2.pdf psfile3.pdf
    pdflatex doc.tex

%.pdf: %.ps
    ps2pdf -dEPSCrop -dNOSAFER $<

The above code is just a sketch. It derives a pdf file from a ps file and states that your main pdf file depends on a number of files that are derived from ps files (by the second rule).

Compiling LaTeX documents with Makefiles is a little tricky because there might be other dependencies that are sometimes difficult to capture with Make. Another problem is that pdflatex might need to run several times.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.