[Tex/LaTex] How to generate postscript files from LaTeX pictures, with correct dimensions

diagramspostscript

I have a document containing, among other things, some figures created using the picture environment. Now I need to create a postscript file for each figure, but if I simply copy them into otherwise blank documents, each one will take up an entire page.

For example, if I have

...
\setlength{\unitlength}{1in}
\begin{picture}(3.14,2.71)
  %Draw things
\end{picture}
...

then I would like to create a .PS file that is 3.14×2.71 inches and which contains nothing but this drawing. I should be able to change my picture environment above to an \includegraphics command that loads the new file, and have it make zero change to the document that is output.

What's the best way to create a postscript file from a LaTeX-drawn picture with the same dimensions as the original?

Best Answer

You should look into the (extremely useful) preview package. You'll probably want the tightpage option, and if you want the generated eps to have exactly the same dimensions as the original you'll need to \setlength\PreviewBorder{0 pt}.

You don't need to copy the picture environments into otherwise blank documents, you just need have a \PreviewEnvironment{picture} line in the preamble, in order to extract only the pictures.

To expand on your example

\documentclass{article}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{0pt}
\PreviewEnvironment{picture}
\begin{document}
\setlength{\unitlength}{1in}
Text before picture
\begin{picture}(3.14,2.71)
 Contents of picture environment
\end{picture}
\begin{picture}(1.00,1.00)
 Contents of picture environment2
\end{picture}
Text after pictures
\end{document}

Gives, when compiled with pdflatex, a pdf which has pages of exactly 3.14in*2.71in and 1.00in*1.00in, and when compiled by latex and dvips gives a ps with the same dimension pages. Either the pdf or the ps can be turned into eps using external tools, but if you want to, you can replace tightpage with psfixbb and then run dvips -E -i on the dvi to get a series of eps files each with the correct dimensions (named jobname.001 jobname.002 etc, which you have to rename as jobname.001.eps etc).

Removing the active option from the usepackage line means the file will compile as if preview was never loaded.

PS: if you want the \includegraphics boxes to be very accurately the same as the original ones then you should also look into the hiresbb option of the graphicx package. Otherwise the dimensions get rounded to 1 bp (big point) accuracy.

Related Question