# How to automatically compile my standalone files before inserting them?

My document contains quite a lot of images generated by tikz, forest, bussproofs, etc., so compiling takes way too much time. It would be much better if I could compile my images to PDFs first and simply insert these in my main document. In the ideal case, the image files are only compiled when the source code has been changed and the corresponding PDF isn't up to date anymore.

Yesterday, I read Outsourcing TikZ code and the standalone package seems to be the perfect way to solve my time consuming compilations. I created a subfolder with separate standalone files for every image and included them in my main file with \includestandalone. Unfortunately, that doesn't seem to speed things up. But then I noticed that Martin wrote in his answer on the aforementioned question:

It is also possible to compile all the TikZ files to single PDFs and include these in the document. This speeds up the compilation process significantly (except the first one, of course).

Right, so I actually have to compile all my image files first. Since I don't want to compile them manually, I started reading the standalone manual for a solution and found out that there are several input modes. I thought that mode=buildnew is exactly what I'm looking for, but when I compile the main file, none of the image files are compiled and I still have to wait ages for my PDF output. Same for the build and buildnew modes.

Am I misunderstanding the function of these modes? And my main problem remains unsolved: how can I automatically compile my standalone files before they are inserted in my main document?

• Maybe latexmk can help, the description sounds promising: An important feature is the "preview continuous mode", where the script watches all of the source files (primary file and included TeX and graphics files), and reruns LaTeX, etc., whenever a source file has changed. – Arash Esbati Apr 29 '16 at 11:56
• What you are looking for is basically the externalisation of tikz pictures with the external library. See Section 50 in the pgf manual or have a look at the corresponding topics – JMP Apr 29 '16 at 11:56
• @JMP: the external library is interesting, but is only applicable to TikZ images. As mentioned in my question, I'm looking for a solution that is also suitable for images gerenated by forest, bussproofs, etc. – Jeroen Apr 29 '16 at 12:15
• forest is based on tikz, so externalization should work with it too. There are at least a few topics around here. But of course, this doesn't help for non-tikz pictures. – JMP Apr 29 '16 at 12:22
• Forest has an option to enable TikZ externalisation specifically for Forest. You should use that if using TikZ's external library. That said, it has never worked correctly for me in a real document. – cfr Apr 29 '16 at 13:45

This will copy all the pages except one, to be replaced. Note: Rename the PDF file so that it isn't automatically destroyed each time. (Save as filename)

\documentclass[multi={tikzpicture,wrapper}]{standalone}
\usepackage{tikz}

\newenvironment{wrapper}{\ignorespaces}{\ignorespaces}

\newcommand{\beforepage}[2]% #1=page, #2=standalone filename
{\loop\ifnum\value{page}<#1\relax
\begin{wrapper}
\includegraphics[page=\thepage]{#2}%
\end{wrapper}
\repeat}

\begin{document}
\beforepage{1}{filename}

\begin{tikzpicture}
\node[draw]{this is a test \thepage};
\end{tikzpicture}

\beforepage{2}{filename}% last page plus one

\end{document}


You can also use this to add one page at a time.