# Not recompile unchanged pgfplots graph

I'm writing a document with something like 30 graphs with a lot of points in each. The graphs are almost ok and I don't need to change them for now. I'm improving the text, so I need to recompile often when I change something on the file (to see if it is ok). Each time I have to recompile all the graphs and compilation took about 6-8 minutes.

Is there any way not to recompile the graphs but include them on the final pdf however (not commenting them)?

-
 Have you tried the option draft as class option or as package option (package graphicx)? It would help to know how you build and include the graphics ... – Kurt Oct 12 '12 at 14:51 related (duplicate?) tex.stackexchange.com/questions/60434/draft-mode-for-pgfplots – cmhughes Oct 12 '12 at 15:35

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 insert the image and do nothing more (without checking for changing in the code)
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.

A most basic example is to do the following:

\documentclass{article}
\usepackage{tikz}
\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}
\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.

-
Just to clarify: TikZ checks if the figure that it shall process already exists and if yes, it doesn't reprocess it but just includes the already processed one, right? But what if I change the code of some TikZ figure? Do I then have to delete the corresponding, already processed figure to force reprocessing or is TikZ somehow recognizing by itself that it has to rebuild it? – Benedikt Bauer Oct 12 '12 at 15:43
Good question by @BenediktBauer. I had the same question. By the way with \tikzsetexternalprefix{} I will choose the path of the following TikZ pictures, right? Can I use it in the preamble? And \tikzsetnextfilename{} is used to name a file: it creates a "link" whit the following image so if I change order of figures, the filename will help to load the right image? – R. M. Oct 12 '12 at 16:58
@R.M. Yes you can of course use \tikzsetexternalprefix in the preamble. That's what I do. The documentation doesn't say (at least I couldn't find it) if it recompiles if you change something, so I wouldn't count on it. Normally I have to delete the old image (I'm lazy so I just delete the folder). – Juri Robl Oct 12 '12 at 18:06
@BenediktBauer I have updated the answer. In general, if you change the code you need, either to use the makefile solution or delete the images. – zeroth Oct 12 '12 at 18:36
@zeroth I tried your to apply your explanations to my code in TeXShop and I get an error. It is said to check that system calls are enabled... What I have to do? – R. M. Oct 13 '12 at 8:48

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).

-