Which is better practice: rendering figures on the fly, or importing them as images?

standalone can be set up to recompile included pictures only if required. Otherwise, it will include the previously compiled PDF. So it is not necessary to switch to \includegraphics to benefit from pre-compilation.

Here's a basic example.

The figure:

% mytikz.tex
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{cathod}
\begin{document}
  \begin{tikzpicture}
    \pic {cath gorwedd={blue!50}{blue!25}{green}{yellow}{red!20!brown}};
  \end{tikzpicture}
\end{document}

All that is really necessary here is the tikzpicture environment but the rest is handy while working on the image initially and standalone will ignore it when appropriate, so it might as well stay.

Now the main file:

\documentclass{article}
\usepackage[mode=buildnew]{standalone}
\usepackage{tikz}
\usetikzlibrary{cathod}
\begin{document}
  \begin{figure}
    \includestandalone{mytikz}
  \end{figure}
\end{document}

Provided compilation is done with -shell-escape, the following will happen:

  • on the first run, a separate compilation process will run to create a cropped version of the image, mytikz.pdf which will then be included with \includegraphics{};
  • on subsequent runs, the image will be included using \includegraphics{} unless the source has changed, in which case it will be recompiled first.

By altering the value of mode=, this behaviour can be customised as required.

In all cases, I get a lovely blue cat:

blue cat

However, you can use it with pictures of other cats, too. Even non-cat content works, or so I've been given to understand.

An alternative is to use the external TikZ library to externalise images. Again, this requires -shell-escape and it can be configured. By default, it also recompiles a figure only if it has changed. However, this is obviously limited to PGF/TikZ pictures whereas standalone provides a more generalised approach.


It seems to me that publications will mostly want pre-compiled graphics. You can ease your quandary relatively painlessly by moving your preamble and Tikz/PGF graphics to separate .tex files and use \input to give yourself a choice at compile time. For instance, your main document might look like:

\documentclass{report}
\input{preamble}
\begin{document}
\begin{figure}
  %\includegraphics{pgf_graphic.pdf}
  \input{pgf_graphic.tex}
  \caption{\label{fig:...}...}
\end{figure}
\end{document}

and if you want to pre-compile, you make a wrapper for your graphic like:

\documentclass{standalone}
\input{preamble}
\begin{document}
\input{pgf_graphic.tex}
\end{document}

then, you can swap the comments on the lines with \input{pgf... with the \includegraphics{pgf... in the main document based on need, mood, etc...

I do this, but I am still trying to settle on the best file structure to make the workflow seamless.