# How to make TikZ faster?

I have a long document containing plenty of TikZ figures and it is painfully slow to compile it.

Is there any specific TikZ tips / good practices you could recommend me so that I can write figures that does not take too much time to be compiled?

I am aware of the TikZ external library but it triggers dependency/recompilation problems that are difficult (IMHO) to solve. Also, the bigger the document is, the easier it is to overlook that one figure did not get recompiled as expected.

Is there, for instance, parts of the TikZ API which are notoriously slow?

-
What kind of dependency problems are you thinking of in conjunction with the external library? – Jake Sep 10 '12 at 18:24
As far as I know, the only way to reduce typesetting of tikz pictures is to use image externalization. Here, you have the choice between the external lib and the standalone package. Note that the external lib now detects changes inside of the tikzpicture environment using MD5 checksums (using the CVS unstable). – Christian Feuersänger Sep 10 '12 at 18:27
Using standalone makes things much faster, because you can include the TikZ pictures as images. There is an auto-recompile feature. But, if you mind external you might mind standalone as well. – Martin Scharrer Sep 10 '12 at 18:28
Putting each diagram in a separate input file using standalone document class will give us many advantages: (1) make the preamble of the main cleaner because macro definitions that are specific to a certain diagram will only be placed in the preamble of the input file of the corresponding diagram, (2) which in turn, etc. – kiss my armpit Sep 10 '12 at 18:40
related: draft-mode-for-pgfplots – cmhughes Sep 10 '12 at 20:31

I share similar feelings with the OP, and find the externalization solution good, but for sure not perfect. For example, in addition to the con the the OP pointed, I would like to add two issues:

1. If one inserts a new figure, somewhere in the middle of the document, then tikz will re-build all the following ones, although they were not changed.
2. Editing the image is somehow annoying. You don't want to edit your tikz image as part of a huge document.

I'm not familiar enough with the standalone package, so I cannot really judge, but if I understand correctly, it won't avoid compiling all the image when the main document is compiled.

Following is an outline of an idea that I tried to implement. So far it is not working, but it might be an idea for others...

# Setting

Assume the you have the following structure:

- project
|- main.tex
|- tikz-images
|- tikz1.tex
|- tikz2.tex

and you build using latexmk.

# Idea

Turn the tikz images into standalone documents, build their corresponding pdf's using make and include the pdf(!) in the document. This way, whenever a new tikz image is created, all you need to do is add a target to the makefile in tikz-images.

Problems will come regarding the compilation using latexmk who will not(!) be aware of changes to tikz1.tex.

# Implementation

First, the files in tikz-images should have the extension .tikz, reserving the .tex extension for a common preamble to be included in all figure source codes. This will help you to keep the same font & Co. settings of the main document. Then, in project create the following makefile:

TIKZDIR = tikz-figures
TIKZ_FILES = \$(wildcard \$(TIKZDIR)/*.tikz)
PDFTIKZ_FILES = \$(patsubst %.tikz,%.pdf,\$(TIKZ_FILES))

\$(TIKZDIR)/%.pdf: \$(TIKZDIR)/%.tikz \$(TIKZDIR)/common-preamble.tex
cd \$(TIKZDIR); \
pdflatex \$\$(basename \$<)

.PHONY : FORCE_MAKE
all : main.pdf
main.pdf : main.tex FORCE_MAKE \${PDFTIKZ_FILES}
latexmk main.tex

This way, calling make main.pdf in project will first build the tikz images (incase they were changed, or they are missing) and then latexmk will take care of the rest.

This approach, once fully implemented, could have two main advantages:

1. Developing the pictures can be quicker and be carried out regardless of the main document. Thus, faster.
2. Building of the document can be quicker as well since it will merely include ready PDFs.

# Problems:

1. As I mentioned, completing the implementation of the makefile in `project.
2. One of the most important benefits of externalization library of tikz is that you get the same setting for the images (fonts for example). To have this also in the proposed approach, something has to be done.
3. Improve the makefile of the tikz images.
-
Since a few months I've been using a similar approach based on Makefiles. I name TikZ source file with a .tikz extension, reserving the .tex extension for a common preamble to be included in all figures (actually I create a format file from it to further speed up compilation). Then I define variables TIKZDIR = tikz-images, TIKZ_FILES = \$(wildcard \$(TIKZDIR)/*.tikz), PDFTIKZ_FILES = \$(patsubst %.tikz,%.pdf,\$(TIKZ_FILES)). Then, PDFTIKZ_FILES is a prerequisite to main.pdf and the rule to build all figures is [continues in the following comment...] – giordano Nov 6 '13 at 8:56
\$(TIKZDIR)/%.pdf: \$(TIKZDIR)/%.tikz \$(TIKZDIR)/preamble.tex^J^Icd \$(TIKZDIR); pdflatex \$\$(basename \$<) – giordano Nov 6 '13 at 8:59
@giordano I turned this answer into a community wiki. Would you like to insert your comments into the answer? Correct me if I'm wrong, you don't have a makefile in tikz-images, right? – Dror Nov 6 '13 at 9:11
it's correct. If I need to generate a format file from the common preamble file then a Makefile inside tikz-images is useful, instead with suggestions I sketched above it isn't strictly needed. – giordano Nov 6 '13 at 9:17
@Dror, as for your first point, the recompilation of all subsequent images will only happen if you do not have specific picture names. Else, I can recommend this method: tex.stackexchange.com/questions/40516/… – zeroth Jun 19 '14 at 9:15

On this page you can find a suitable solution which uses externalization of images as mentioned in one of the comments above and describes the advantages of this approach like faster compilation as well as easier reuse and version control.

-

I just have two documents open. I look if the tikzpicture is ok on an seperate document, once it is ok, I use it in the final document.

-