Feeds:
Posts

Create standalone figures which are input-able

If you are using PGF/TikZ (or any drawing language for (La)TeX such as PSTricks), you will soon find yourself either:

• Re-compile an entire long LaTeX document just to see the effect of a small change you have made in a figure; or
• Maintain a simplified master LaTeX file for every figure file (that’s 2 files for each figure) so that you can compile the figure quickly; or
• Keep changing the preamble template for a tool like QtikZ to incorporate your custom definitions and packages for your figures.

Any of the above methods is cumbersome, time-consuming and inefficient. A much better way is to use the standalone package. For example, you are creating a figure in TikZ for your long paper. You put the figure code in a TeX file with a full preamble & the usual document environment, as if you are writing a small document with only one figure in it. But you don’t use the usual document class (e.g. article).  You use the class standalone instead, as follows:

% File myfigure.tex
\documentclass{standalone}
\usepackage{tikz}
\usepackage{mypackage}
\input{mydefs}
\begin{document}
\begin{tikzpicture}
\draw [color=red] (0,0) rectangle (2,1) node [midway] {Cool};
\end{tikzpicture}
\end{document}


If you compile this file, you will get the same result as if you used the preview package. You can make small changes and re-compile only the figure quickly.

In you main LaTeX file, you must use the standalone package at the very beginning of the preamble, and input the figure file as usual. For example:

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\usepackage{mypackage}
\input{mydefs}
\title{Test of the \texttt{standalone} package}
\begin{document}
\maketitle
This is a figure.
\begin{figure}
\centering
\input{subfile}
\caption{A figure.}
\end{figure}
\end{document}


You can compile this main file as usual. When being input, the preamble of the figure file (i.e. everything between \documentclass and \begin{document}) is skipped.

This method is more flexible and more convenient than the above methods. You maintain only one file per figure, and can edit and re-compile graphic code quickly.