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

I've come to a philosophical quandary with respect to my use of LaTeX for scientific writeups. At first I tried to render all figures in LaTeX at compile-time, for example making a graph using PGFPlots instead of importing it from Excel. However as I started bigger projects my compile time got exponentially larger, and I heard that many prefer pre-rendered images in figures rather than figures rendered at compile-time. Which is better practice for large projects with many diagrams?

The project I'm working on now includes a couple of graphs and circuit diagrams in circuitikz, for a certain degree of context. I also am considering taking figures designed in LaTeX, exporting them as images, then reimporting them into the final document. Thanks.

• When a figure is under construction and likely to change as the surrounding discussion changes I generate it on the fly. When that part of my (large) document is stable I comment out the local generation and include the graphic. I would recommend not mixing plots from Excel and PGFplots - use one or the other, for consistency of style. – Ethan Bolker Jul 16 '15 at 20:03
• Welcome to TeX.SX! The TikZ externalization library (section 50 in the manual) might be a good compromise. – erik Jul 16 '15 at 20:11
• Some references in terms of compile-time: How do I speed-up LaTeX compile time?; Speeding up LaTeX compilation; How can I speed up LaTeX compilation? – Werner Jul 16 '15 at 20:18
• Not completely opinion based, but certainly “document depending”. – egreg Jul 16 '15 at 20:29
• TikZ external library, standalone... there are ways to set things up so things are compiled only when necessary (mostly, anyway). But... it all depends. There are surely figures you ought not try to produce using TeX regardless of compilation time ! You have limited accuracy, for example, and extremely limited capacity for 3D rendering. Summary: what @egreg said. – cfr Jul 16 '15 at 22:13

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:

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.

• You don't need to swap them. Isn't that the whole point of standalone? ;) – cfr Jul 17 '15 at 0:21
• @cfr The difference would be putting a pre-compiled graphic in the figure, or compiling the graphic within the main document. I think the OP was searching for a way to decide whether to do one or the other, and so switching would offer some way to compare – repurposer Jul 17 '15 at 0:25
• Yes, but I am pretty sure standalone offers facilities for this. (I've never used that function, but I'm sure I've read it in the docs.) – cfr Jul 17 '15 at 0:30
• @cfr It might, I guess I should get out of the habit of only reading the manual after I have said something embarassing ;) – repurposer Jul 17 '15 at 0:31
• It might not. I should probably get out of that habit, too ;). – cfr Jul 17 '15 at 0:36