Speeding Up LaTeX Compilation with TikZ Externalization
Learn how to speed up LaTeX documents that contain TikZ pictures by externalizing each graphic to a separate PDF, preserving editability while cutting compile time.
24 Aug 2025, 11:17 UTC

Problem: Slow Compiles When TikZ Pictures Grow
When a LaTeX document contains many TikZ pictures—or a few very detailed ones—each recompilation forces LaTeX to re-draw every path, node, and style from scratch. The overhead becomes noticeable, especially during iterative writing when you change text but leave the graphics unchanged.
Thesis: Use the TikZ external library to move each picture to a separate PDF, keeping source editable while reducing compile time.
The external library (\usetikzlibrary{external}) tells TikZ to treat each tikzpicture as an independent figure. On the first run TikZ writes the graphic to a PDF file (named <jobname>-figure<N>.pdf) and replaces the picture with a box that includes that PDF. Subsequent runs reuse the cached PDF unless the TikZ source changes, so the heavy drawing step is skipped.
How Externalization Works
Under the hood TikZ invokes the compiler again (via \write18) to produce the external PDF. This requires the shell-escape feature. The generated PDF is vector-based, inherits the document’s fonts, and can be inspected like any other PDF figure.
Setting Up Externalization
- Add the library to the preamble:
\usetikzlibrary{external} - Activate it:
\tikzexternalize(place after loading the library)
Worked Example
To use this feature, you must run your LaTeX engine with the shell-escape flag enabled. Here is a minimal configuration for a diagram that will be externalized:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
\draw[fill=blue] (0,0) rectangle (2,2);
\node at (1,1) {Externalized};
\end{tikzpicture}
\end{document}Run this command in your terminal: pdflatex --shell-escape file.tex. Note that --shell-escape allows the TeX engine to execute external programs, so only use this with source files you trust.
Trade-offs and Limitations
- Compilation Overhead: The first compile is slower because it generates the individual PDFs. Subsequent runs are much faster.
- Complexity: If you change a global style in your preamble, TikZ might not detect the change unless you manually delete the generated PDF files to force a refresh.
- Debugging: Errors inside a TikZ environment can sometimes produce vague log messages that are harder to trace through the externalization layer.
Verification Steps
- Check the generated PDF with a viewer; confirm that the rectangle is vector-based (zoom does not pixelate).
- Check your directory for a file named
file-1.pdfto ensure the externalization worked. - Run
latexmk -pdf -shell-escape fileto ensure the build process handles multiple passes correctly.
Actionable Closing
If your document is becoming sluggish due to complex diagrams, add \usetikzlibrary{external} and \tikzexternalize to your preamble today. It is the single most effective way to maintain a fast workflow without sacrificing vector quality.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.