Not recompile unchanged pgfplots graph

Yes, fortunately it is! And it is even quite easy.

For this Christian has created a library called external which has made it into the pgf, tikz package and is accessible through that package.

Whatever, is shown below also works for pgfplots as they are typeset within a tikzpicture environment.

What it does is, the following:

Check if an image file corresponding to the currently reached image is found:

  1. If image found, check whether the image code has changed, and if not, the image is inserted.
  2. If image is not found or the image code has been changed, do the following:
    1. Invoke a new shell with your executable and create the image
    2. Insert image in the document

This means that for each image that is externalized you will get a new compiled document with additional log, aux files etc. I.e. many new files. It is important to note that since pgf uses hashes of the image code to check whether a recreation of the image is necessary, it cannot determine whether any code in the pre-ample or elsewhere not in the image changes the resulting image. So sometimes it may be advantageous to delete the images.

A most basic example is to do the following:

\documentclass{article}
\usepackage{tikz}
% Load the library
\usetikzlibrary{external}
% Enable the library !!!>>> MUST be in the preamble <<<!!!!
\tikzexternalize

\begin{document}

\begin{tikzpicture}
  \draw (0,0) -- (1,1);
\end{tikzpicture}
\end{document}

This will create one additional file called <document name>-figure0.pdf. Which is the first occurring tikzpicture environment in the file.

This has the disadvantage of reading the 0 file even if you rearrange your tikzpicture environments.
For this there is a command: \tikzsetnextfilename which can be used to force the following tikzpicture environment to a specific file name.
This ensures the correct loading order of the figures. I would encourage you to do this on each picture.

So for instance this:

% Default the externalized name to 'hello'
\tikzsetnextfilename{hello}
\begin{tikzpicture}
  \draw[red] (0,0) -- (-1,1);
\end{tikzpicture}

Lastly the external library creates the images within the current directory. This means that you will have a very cluttered folder of many files (as it compiles many sub documents only containing the images).

A common way to bypass this is to add a prefixed directory to the output of the files. You do that by the command: \tikzsetexternalprefix which is pretty self-explanatory. So a small example:

\documentclass{article}
\usepackage{tikz}
% Load the library
\usetikzlibrary{external}
% Enable the library
\tikzexternalize

\begin{document}

% Default all images in the subfolder `figs/`
\tikzsetexternalprefix{figs/}
\begin{tikzpicture}
  \draw[green] (0,0) -- (1,-1);
\end{tikzpicture}

% You are allowed to rearrange your images to separate folders
\tikzsetexternalprefix{figs/red/}
\begin{tikzpicture}
  \draw[red] (0,0) -- (1,-1);
\end{tikzpicture}

\tikzsetexternalprefix{figs/blue/}
\begin{tikzpicture}
  \draw[blue] (0,0) -- (1,-1);
\end{tikzpicture}
\end{document}

This can thus also be used to sort your images by chapter, or style, or etc..... :)

Otherwise search for the external tag here, or see the manual.

In general it is always a good custom to delete all your externalised images before doing your final compilation. This ensures that images are updated etc.


You can use the externalize library of TikZ, as the documentation of pdfplots suggests (7.1.1). You have to add this code to your preamble, everything else should work automatically:

\usepgfplotslibrary{external}
\tikzexternalize

That means every picture is computed only once, and the built image is saved and reused the next time you compile.

You can even use a special directory, if you want:

\tikzexternalize[prefix=tikzext/]

This saves all your computed images to the path tikzext/

For externalize to work, you need to enable shell-escape (write18).