TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a document with many graphs containing a lot of data points. Many graphs are finished and I don't need to change them anymore. Since I am improving and correcting the text, I need to recompile often. Each time I have to recompile, all the graphs are recompiled as well and this process takes a huge amount of time.

Is there a way to compile the graphs only once and then include them in the final pdf in subsequent compilations?

share|improve this question
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
up vote 17 down vote accepted

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)
  2. If image is not found 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.

A most basic example is to do the following:

% Load the library
% Enable the library !!!>>> MUST be in the preamble <<<!!!!


  \draw (0,0) -- (1,1);

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'
  \draw[red] (0,0) -- (-1,1);

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:

% Load the library
% Enable the library


% Default all images in the subfolder `figs/`
  \draw[green] (0,0) -- (1,-1);

% You are allowed to rearrange your images to separate folders
  \draw[red] (0,0) -- (1,-1);

  \draw[blue] (0,0) -- (1,-1);

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.

share|improve this answer
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:


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:


This saves all your computed images to the path tikzext/

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.