Wednesday, 27 July 2011

Preserve bounding box for .eps figures in LaTeX

Here's a tricky little problem. Sometimes, you want to preserve blank space in a figure for whatever reason. This should be fairly simple if you are using .pdf figures and pdfLaTeX. However, the same cannot be said for .eps figures. Let's assume we're using Inkscape to create an .eps figure from an .svg and let's assume we're using LaTeX to compile the document. To demonstrate the problem, I will use two figures (drawinga.eps and drawingb.eps) - one with the white space and one without. I will plot them side-by-side. Here's the LaTeX code:
\begin{figure}[!!b]
\centering
\subfigure[]{
   \includegraphics[width =.48\textwidth] {drawinga}
   \label{fig:drawinga}
 }
 \subfigure[]{
   \includegraphics[width =.48\textwidth] {drawingb}
   \label{fig:drawingb}
 }
\caption{Two figures (a) and (b).}
\label{fig:drawing}
\end{figure}
This produces the following output:
Clearly, this is not what I want. I actually want subfigure (b) to start at the same location as subfigure (a). If you create this figure with Inkscape and save it as an .eps, you will see that even though you left the page dimensions the same, the resulting .eps figure is such that the white space is eliminated. What is happening is that the bounding box only spans so far as the drawing. We do not want this. So we turn to our faithful friend - the shell. Here, you can save the figure using a special command line option:
$ inkscape --export-ps=drawingb.ps --export-area-page drawingb.svg
This creates a .ps file with the correct bounding box. We now save this file as an .eps using the ps2eps utility:
$ ps2eps drawingb.ps
Input files: drawingb.ps
Processing: drawingb.ps
Rendering with existing %%BoundingBox: 0 0 342 508
Calculating Bounding Box...ready. %%BoundingBox: 0 191 341 508
Creating output file drawingb.eps...ready.
We see that ps2eps created an .eps file with the bounding box with corners defined as 0 0 342 508. We use this information to inform LaTeX to obey the bounding box for the .eps figure:
\begin{figure}[!!b]
\centering
\subfigure[]{
   \includegraphics[width =.48\textwidth] {drawinga}
   \label{fig:drawinga}
 }
 \subfigure[]{
   \includegraphics[width =.48\textwidth, bb=0 0 342 508] {drawingb}
   \label{fig:drawingb}
 }
\caption{Two figures (a) and (b).}
\label{fig:drawing}
\end{figure}
Now, we have the output we desire:

No comments:

Post a Comment