1

(I'm not sure I have titled this question correctly. Modify if preferred.)

I have a document with a large number of Tikz figures, and a lot of text. The figures are in final state. I am now working on the text. How can I avoid compiling the figures every time I compile the document, whilst still keeping the figures in the document? Is there a method to "use figures from previous compilations"? I am using TexStudio.

I have a tendency/compulsion/obsession to compile every half a minute or so, in order to see how the added text looks like. Having many Tikz figures just makes the process inefficient. I don't want to deal with my tendency/compulsion/obsession.

cfr
  • 198,882
luchonacho
  • 4,161
  • 3
    Sounds like http://www.howtotex.com/tips-tricks/faster-latex-part-ii-external-tikz-library/ is exactly what you're asking for. – Teepeemm Sep 23 '16 at 17:59

1 Answers1

5

There are actually several options. Which works best for you will depend on your work-flow and document structure, as well as your preferences. If, for example, you also have non-TikZ/PGF images to compile, a more generic solution may be preferable. If all of your code is inline and your only concern is TikZ/PGF, then a TikZ/PGF-specific solution may work better.

external TikZ library

This works whether your pictures' code is provided inline or in supplementary files.

It is limited to TikZ/PGF pictures e.g. tikzpicture or environments such as forest which are based on tikzpicture or \tikz.

Add the following to your preamble.

\usetikzlibrary{external}
\tikzexternalenable

Then compile with shell escape enabled. For example, use pdflatex --shell-escape rather than pdflatex. The precise option you need may differ, but the idea is that you need to switch off the security restrictions which usually limit TeX's ability to write to arbitrary files.

The first time you compile, the compilation will actually take longer. However, subsequent compilations will be faster. By default, externalisation will include an image rather than compiling the picture code provided

  1. the code for the tikzpicture has not changed;
  2. a compiled version of the image exists;
  3. it can i.e. shell escape is enabled etc.

Some constructions do not take kindly to externalisation. In this case, you can switch it off for those pictures. There are several ways to do this. One is to use

\tikzexternaldisable

and

\tikzexternalenable

to switch it back on.

If you have pictures which use pgfplots or forest, for example, then note that those packages support externalisation via their own options and you should use the appropriate option or package library to enable this.

Externalisation must be switched off for pictures which require overlay or remember picture. For example, you must switch it off for code which uses the tikzmark library or similar.

standalone

This solution is not limited to TikZ/PGF pictures but can also be used for images created using other packages.

It requires you to put the code for your pictures into external files which are then \input into the main document.

By default, standalone compiles the code for each picture each time. However, you may alter its mode so that it instead compiles external images which can then be included on subsequent compilations.

cfr
  • 198,882
  • I tried the external bit, plus the --shell-escape option, but compilation time is the same. Do I need to add the latter inside txs:///pdflatex, or inside pdflatex.exe -synctex=1 -interaction=nonstopmode %.tex? (I'm using TexStudio) – luchonacho Sep 25 '16 at 15:42
  • @luchonacho If you use TeXStudio to compile, then, yes. Add it to the list of options --shell-escape --synctex=1 .... See the TikZ manual for details if this doesn't work as the option might be called something different, depending on your distribution. – cfr Sep 25 '16 at 18:40
  • If it works, you will see additional files generated after successful compilation: usually 3 per image (a PDF image, an MD5 checksum and a log file). – cfr Sep 25 '16 at 18:46