28

I have an article with many TikZ pictures. In order to make the code more clear and to save compilation time, I want to remove the TikZ code from the main document. I read that this is possible with the tikzlibrary "external". (for example see this question).
But I am not sure, where to write which commands?

What do I write in the file/the files which consists only of the code for the tikz pictures?
What and where do I write in the main document in order to import the pictures?

2 Answers 2

50

There are two questions hidden in your post, they have two answers that are basically orthogonal:

Speeding up compilation by using tikzexternalize:

You don't actually have to write them into separate files to save compilation time. You can leave your code almost unchanged, that's the beauty of it. This is the simplest possible setup with externalization

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/] % activate and define figures/ as cache folder
\begin{document}
\begin{tikzpicture}  
    \node {real complex figure};
\end{tikzpicture}
\end{document}

This will only run if your LaTeX is set up to run with shell escape (pdflatex -synctex=1 -interaction=nonstopmode --shell-escape %.tex, see here for example)

Having tidy code, by writing TikZ code into separate files:

Of course you can also store the \begin{tikzpicture}...\end{tikzpicture} code in an external .tex or .tikz file and use \input to include it. But that is a matter of taste and does not affect compilation performance.

You could write

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/]
\begin{document}
\input{tikzfigure1.tikz}
\end{document}

and into tikzfigure1.tikz:

\begin{tikzpicture}  
    \node {real complex figure};
\end{tikzpicture}

I tend to define my own command to include tikz files, instead of using \input in order to take care of one more thing:

\newcommand{\inputtikz}[1]{%
  \tikzsetnextfilename{#1}%
  \input{#1.tikz}%
}

This will make sure that the externalization is based on the file name (instead of the order) so that it doesn't get confused if you change the orders of TikZ pictures in your document.

2
  • 2
    Thanks, this was really helpful. Something I want to add though: In order to make this work I needed to change two things: Remove spaces from the name of my .tex file, i.e. "file a.tex" to "filea.tex" and to change the command for pdf latex to "pdflatex -synctex=1 -interaction=nonstopmode --shell-escape %.tex"
    – klirk
    Commented Apr 1, 2019 at 12:57
  • 2
    Ok, makes sense. I never have spaces in file names anyways, but I see how this could lead to problems there. I edited a remark about --shell-escape into my answer
    – sheß
    Commented Apr 1, 2019 at 13:11
9

I prefer a manual way to externalize TikZ pictures. This gives you full control and does not require any shell escape huzzle.

  1. Put every TikZ image in a file of its own (good for keeping track of your code anyways). Use this template (e.g. ext-img1.tex):

    \documentclass[tikz]{standalone}
    
    \usepackage{tikz}
    
    \begin{document}
    %
    \begin{tikzpicture}
      <your tikz code here>
    \end{tikzpicture}
    %
    \end{document}
    
  2. Include the external image in your main document:

    \begin{figure}
         \includegraphics{ext-img1.pdf}
         \caption{external image}
    \end{figure}
    
  3. Compile the external image

    pdflatex ext-img1.tex
    
  4. Compile the main document

Whenever you want to modify the image, you have to rebuild the file defined in the first step.

5
  • 1
    I think your approach works until you don't have \ref from external images to the main file or \label in external images. \tikzexternalize handles \ref and \label automatically.
    – Rishat
    Commented Mar 9, 2021 at 9:39
  • 1
    It does sadly not work in Overleaf. For a sub-directory /fig \includegraphics{fig\target.pdf} does not work, neither does `\includegraphics{fig\output.pdf}. Any ideas?
    – skvery
    Commented May 10, 2021 at 20:35
  • Pdf figures are also not displayed in native Overleaf.
    – skvery
    Commented May 10, 2021 at 20:41
  • @skvery, it works without issue on overleaf to include compiled pdf figures. Have you called the \usepackage{graphicx} package? Also / should be used: \includegraphics{fig/target.pdf}
    – fry
    Commented Jun 1, 2022 at 9:45
  • 1
    I do prefer this method. But the only downside is that there is no way to provide parameters to the already drawn pdf. E.g. i want to draw a tree of all my chapter automatically, then this solution isn't suitable. +1
    – anis
    Commented Dec 14, 2022 at 9:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .