# Compiling a main document with many TikZ subfiles efficiently

What would be the most efficient way to compile a document of the article class with many TikZ subfiles?

In the main document, I have added \usepackage{standalone} and then \includestandalone for the individual subfiles.

1. Would it be better to add the options subpreambles = true, sort = true, and trim all the TikZ preambles from the main document?

2. Or should I not use those options and keep all the TikZ preambles in both the main .tex file and the subfiles?

Which method would speed up compiling? If neither of these methods are truly efficient, what should I do instead?

As it stands now, my document is around 60 pages but it takes a few minutes to compile. I have over 30 .eps figures and over 30 TikZ subfiles. Besides the TikZ code, everything else is pretty standard LaTeX.

The document class for the subfiles is standalone with the option convert = false.

One of the other problems that slows down compilation is the fact that every standalone picture is compiled when the main document is compiled. However, there may have been nothing change with 95% of the pictures.

Main example doc:

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\usepackage{float}
\begin{document}
text here
\begin{figure}[H]
\centering
\includestandalone{test}
\end{figure}
text here
\end{document}


test example doc

\documentclass[convert = false, tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0, 0) circle[radius = .35cm];
\end{tikzpicture}
\end{document}

-
Take a look into tikz-external (tex.stackexchange.com/questions/tagged/tikz-external) at least for the TiKZ stuff that should be exactly what you are looking for –  sheß Jul 19 at 19:54
@sheß is there a specific thread you have in mind I should look at? –  dustin Jul 19 at 20:05
it's actually pretty streight forwards, except for a few minor problems you might encounter depending on what exactly you're doing. Basically, what you need to do is to load the library via \usepgfplotslibrary{external} and run pdflatex (or whatever you're using) with the option -shell-escape. A short introduction is given here: olezfdtd.wordpress.com/2012/03/28/… –  sheß Jul 19 at 20:19
Although tikz externalization works, I find it incredibly slow. Even if a figure already exists and it is not recompiled, the compilation time or the main document is drastically increased, compared to just including an pdf image created as a standalone tikzpicture. –  Markus Jul 19 at 21:07
@Markus This is very odd since tikz-external does exactly that: rendering the tikz picture into a PDF and including that. –  Christian Jul 20 at 2:36

I use the suggestion in zeroth's discussion. The discussion is for pdflatex.

However, I use xelatex, and I only test the methods by texlive on Linux x64 system.

here is my way to make it works for xelatex compiler:

1. In the document preamble after \usepackage{tikz}:

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[figure list=false,prefix=tikz/]
% make tikz externalize and store in directory "tikz"
\tikzset{external/system call={xelatex \tikzexternalcheckshellescape
-halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
% to let xelatex work


and use the command xelatex -shell-escape xxxxxxx.tex to compile the picture externalized. (Till now the normal xelatex xxxxxxx.tex command would return errors)

2. Once all independent pdf files compiled, normal xelatex xxxxxxx.tex should works as usual.

It's not perfect way to do tikz figure externalized because the options here cannot recognize whether the externalized pdf files are identical to new tikz figure.

-
I will check this out. It may take me a few days to test this though. –  dustin Aug 29 at 7:40
Maybe you could just tell what platform and compiler you use? That may help others to find the answer. –  selwyndd21 Aug 29 at 8:43
Ubuntu 13.04, 64bit and I use Emacs with AUCTeX and Latexmk. –  dustin Aug 29 at 11:53