How to fix EPS with incorrect bounding box?

I want to get a standalone EPS graphic from DVI. This process can be done using the batch file as follows

latex %1
dvips -E %1 -o %1.eps


And the test file is as follows

\documentclass{minimal}
\usepackage{pstricks}

\begin{document}

\begin{pspicture}[showgrid=false](3,3)
\pscircle(1.5,1.5){1}
\end{pspicture}

\end{document}


However the resulting EPS has incorrect bounding box. How to fix the bounding box?

Update: You can also find a more detailed solution in How to produce EPS instead of PS using latex.exe followed by dvips.

-

This seems to be because EPS (and PDF) uses PostScript points (=1/72in, called bp (big points) in TeX), not the normal TeX points (1pt=1/72.27in). The EPS contains its size (bounding box) as integers, so rounding the size upwards. You can try and add the hi-resolution bounding box header which contains the size as floating point numbers. However, it depends on the used tool which of the two are used.

You can get these headers calculated by Ghostscript using:

gs -dNOPAUSE -dBATCH -q -sDEVICE=bbox file.eps


which prints the required EPS headers:

%%BoundingBox: 0 0 34 31
%%HiResBoundingBox: 0.000000 0.000000 33.317999 30.005999


You can then simply replace the original headers with the updated one. There might be tools which do this for you (e.g. eps2eps) but these might actually change the font settings etc. I wrote my own Perl script called fixbb once which does nothing else then calling gs like above and replacing the headers. (But I'm just seeing know that it doesn't add the HiResBoundingBox header if it wasn't use before.)

As workaround you could make sure that your diagrams are drawn using bp not pt, at least the outer rectangle.

BTW: I would use \documentclass[border=0pt]{standalone} instead. This saves you some steps (e.g. page style is already set to be empty, ...). However, I still get a white border at the right and top.

-
If EPS file also has%%HiResBoundingBox header, use \usepackage[hiresbb]{graphicx} to avoid top and right padding. –  xport Jul 2 '11 at 0:15
+1 and accepted for informing %%HiResBoundingBox. I use neither fixbb nor eps2eps. Instead, I used epstool. –  xport Jul 2 '11 at 8:25
@xport: Ah yes, epstool! I knew there was a tool which made me stop using my own fixbb, but couldn't recall it. I stopped using EPS years ago. –  Martin Scharrer Jul 2 '11 at 8:53
I knew epstool by reading the source of your fixbb. :-) –  xport Jul 2 '11 at 9:55
@xport: Oh, yeah, it's old code of mine ;-) –  Martin Scharrer Jul 2 '11 at 9:58

Another option is keep the image as it is right now and trim off the excess padding when it's put into your document. If, for example, your picture is called MyPicture.eps then it can be trimmed through \includegraphics[trim= left bottom right top, clip=true]{MyPicture.eps} where you enter how much want to trim the figure on each side. For example, see what happens to your picture with this code:

\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
\pagestyle{empty}
\includegraphics[width=2.50in, trim= 8 1 3 .5, clip=true]{MyPicture.eps}
\end{document}


By adjusting values in trim, you control what the picture looks like. See http://www.andy-roberts.net/misc/latex/latextutorial5.html for more details.

-
True, but the OP seems to want to use it as standalone file and not re-include it in a LaTeX document. Also you would need to adjust the trim values for every picture manually. –  Martin Scharrer Jul 1 '11 at 19:29