[Tex/LaTex] way to compile tikz graphics outside a document

diagramstikz-pgf

As my document grows, so does the number of drawings. I am using PGF/TikZ and each drawing is what is enclosed between \begin{tikzpicture} and \end{tikzpicture}. Every time the document is compiled, so is each of the drawings. I have recently noticed in connection with using the Lindenmayersystems library that memory is an issue! There must be a way of compiling drawings outside a document and have the document refer to the result of that compilation, instead of having the drawing's source code reside in the document. This would allow complex drawings to be compiled, with all available resources brought to bear on compiling that one drawing. Also, how could one have a large document with hundreds of drawings and not have a problem with memory?

I am using Windows 7, MiKTeX 2.9, TeXMaker 3.3.1.

Best Answer

Use the TikZ external library for this. In your preamble place:

\usetikzlibrary{external}\tikzexternalize

You need to make sure that LaTeX is allowed to use external commands with -enable-write18 or -shell-escape. This will turn each TikZ figure into its own PDF image, and then TikZ knows to grab that instead (i.e. you won't need to change any of your code). For more information, see section 32 of the PGF/TikZ manual.

The benefit of this approach is that LaTeX typesets each figure individually, so that the memory overhead is broken down into smaller chunks, and you will be less likely to run out. You will also notice that each time you typeset it'll save some time after the figures have been generated.