I have a long document with multiple graphs. All graphs are done using pgfplots and I'm using tables stored as separate text documents. For instance, to plot data from a table big_table_with_data.txt, I create a file figure1.tex:

  \begin{axis}[colormap/jet, colorbar]
     \foreach \i in {1, 13, 23, ..., 143}
     \addplot+[scatter, mesh, point meta=\i]
     table[x index=0, y index=\i]

And then in my main document I put


It takes a lot of time for each plot to be generated and whenever I'm compiling the entire document, it takes ages. Is there a way of compiling figures only once?

I don't want a draft version, nor demo version, I don't want to comment the figures. I want my figures as they are, but I don't want them to be generated every time.

What I want is some sort of version control for LaTeX. When LaTeX goes through all figures, it checks if there are any changes since the last compilation, and if there aren't any, it just uses the old figures.

Alternatively, maybe I could set it somewhere in the preamble, that LaTeX should not compile figures, just use the previously compiled images?

Is that possible?

  • 3
    Yes, there is an externalization library for this. Check either TikZ or pgfplots manual (I would say the latter is more up-to-date) – percusse Feb 14 '15 at 11:59
  • Great, exactly what I wanted. 'externalization' was the key word, thanks. Do you want to answer my question, so that I can close it, or should I do that? – Agnieszka Feb 14 '15 at 12:21
  • 3
    As you wish, if you have a working code maybe you can share your working small example for future visitors? – percusse Feb 14 '15 at 14:17

As suggested by percusse, the externalization library from pgfplots indeed does the trick

More information (including an MWE) in Chapter 7.1.1, page 465 in pgfplots manual.

The idea is simple: you only need to add the following two lines to the preamble:


And then all tikzpictures are going to be generated as separate files. Next time you compile the document, LaTeX will use the ready-made pdf's.

Important thing to note: as far as I can see there is no easy way of adding new figures to the document. You can put

\tikzset{external/force remake}

somewhere in your document to remake all figures after this mark. If you have to add a new figure in the begining of your document, my suggestion is that you create a figure separately and then change the numbering of the files manually. This question provides an answer of how to change numbering to files of a format number.extension, which is close enough to this case.

  • 1
    Why would you need to manually re-number? Wouldn't temporarily placing \tikzset{external/force remake} at the beginning of the document (or at least before the added figure) do the job? – Paul Gessler Feb 14 '15 at 21:53
  • 2
    Note that it is really not fair to ask a question and then post an answer based on the solution somebody gave you in a comment! I suspect some misunderstanding here, since you yourself suggested that percusse answer. It was then suggested that you produce a minimal working example (MWE). This could have been added to your question and percusse could have shown how to adapt it to use externalisation in an answer. – cfr Feb 14 '15 at 23:05
  • @PaulGessler Unless you are using beamer. But, even then, deleting the files would work. But I suspect the concern here is to avoid recompilation of all the figures. It would, of course, be simpler to just say \tikzsetnextfilename{specialname} for the one picture and avoid both renaming and recompilation. – cfr Feb 14 '15 at 23:06
  • @Paul Gessler It was indeed the need of recompilation that was my concern; I have 50 figures so far which are all numbered in the order they appear in the document and it takes a long time to externalize them. If I had to add a figure somewhere in the begining, I'd rather rename all figures using the script suggested in my answer than recompile all figures. But then cfr's answer is even better one (thanks for the suggestion). – Agnieszka Feb 15 '15 at 13:28
  • 1
    An MWE in this case just needs to include a TiKZ figure so that an answer can demonstrate how to externalise it. The fact that a very simple, single figure wouldn't slow compilation doesn't matter. It is the essence of the problem - getting TeX to compile the figure just once and then reuse it - which is important. – cfr Feb 15 '15 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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