9

Have you ever submitted a LaTeX paper to a journal that wants separate figures? When you want to include a figure composed of a table of images, you must build it separately.

When I run latexpdf, I get an entire 8.5 x 11 page, even when I remove the page number.

Is there a way to "autocrop" my built pdf as I compile it from LaTeX? I suppose I could open it in inkscape and crop and save it, but I'd really like to learn how to do this the right way.

Peter Grill
  • 223,288
user
  • 467

2 Answers2

13

I have been using the standalone package exactly for this purpose. You create a standalone tex file for each figure and use:

\documentclass[preview=true]{standalone}

and the main files which include this standalone file will need to have the complete preamble including \usepackage{standalone}. Martin Scharrer, the package author provides a good example here.


Alternatively you could use the preview package. Add \usepackage[active, graphics]{preview}, and use the \PreviewEnvironment to specify which environments you want to be extracted.

Here is an example that extracts the two tikzpicture environments on separate pages:

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{1pt}%

\begin{document}
\lipsum[1]
\begin{tikzpicture}
    \draw [red, ultra thick] (0,0) rectangle (1,1);
\end{tikzpicture}
\lipsum[2]
\begin{tikzpicture}
    \draw [blue,fill=yellow] (0,0) circle (5pt);
\end{tikzpicture}
\end{document}
Peter Grill
  • 223,288
4

I agree with Peter Grill. You should use standalone for your pictures and other more complicated figures in the future. However, for an existing document using preview directly is easier. However, for complicated figures which are not simply contain a single *picture environment or \includegraphics I would redefine the figure environment instead to use preview directly while removing the \caption:

\documentclass{article}

\usepackage[demo]{graphicx}% remove 'demo' for real documents
\usepackage[active,tightpage]{preview}
\setlength{\PreviewBorder}{0pt}

\renewenvironment{figure}[1][]{%
    \begin{preview}%
        \renewcommand{\caption}[2][]{}%
}{%
    \end{preview}%
}

\begin{document}

text text text text text text 

\begin{figure}[t]
    \centering
    \includegraphics[width=\textwidth]{image}%
    \caption{Some caption}
\end{figure}

text text text text text text 

\begin{figure}[b]
    \centering
    \includegraphics[width=\textwidth]{image}%
    \caption[short]{Some caption}
\end{figure}

text text text text text text 

\end{document}

This will give you one PDF which has every figure as one tight page. You then might separate it using a PDF tool like pdftk with the burst argument.

Martin Scharrer
  • 262,582