[Tex/LaTex] Which is better practice: rendering figures on the fly, or importing them as images

best practicescompilingfloatsgraphics

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.

Best Answer

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.