# Using \tikzexternalize

I want to use tikzexternalize in my document, because the amount of tikz pictures is quite large. In general my tikz pictures are generated using matlab2tikz providing every tikz picture as a standalone. For the example the main document looks like this:

\documentclass{article}
\usepackage{standalone}
\usepackage{pgfplots}
\pgfplotsset{/pgf/number format/use comma}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize % activate!
\begin{document}
%
\begin{figure}
\centering
\includestandalone[width=0.8\linewidth]{./fig_01}
\caption{mycaption}
\label{fig:01}
\end{figure}
%
\end{document}


Within the file fig_01 the following simplified code was generated using matlab2tikz:

\documentclass[tikz]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{grffile}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{patchplots}
\usepackage{amsmath}

\begin{document}
%
\begin{tikzpicture}
\draw (2,2) ellipse (3cm and 1cm);
\end{tikzpicture}%
\end{document}


While excluding lines 7 and 8 from the main the compilation works fine - not so with the externalization activated.

My compilation command for pdflatex within TexStudio is:

"/usr/local/texlive/2015/bin/universal-darwin/pdflatex" -synctex=1 -interaction=nonstopmode %.tex

• You need to ass --shell-escape for externalisation to work. – cfr Mar 29 '16 at 20:51
• I just found the answer to my question here. – Nivek Mar 30 '16 at 22:32

Externalisation relies on 'shell escape'. When this is restricted or disabled, externalisation can't work because it requires the compiler to spawn additional compilation commands in order to create each picture separately.

By default, this feature is disabled for security reasons. A restricted set of commands may be spawned, but this set is not sufficient for the compilation of the pictures.

Hence, you need to override the default setting by explicitly allowing unrestricted shell escape for the compilation:

"/usr/local/texlive/2015/bin/universal-darwin/pdflatex" -synctex=1 -interaction=nonstopmode -shell-escape

• This must be a duplicate, though. – cfr Mar 29 '16 at 20:55
• Adding --shell-escape respectively -shell-escape to the compilation command leads to the following error: sh: pdflatex: command not found and furthermore: Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e No file document.aux. Label(s) may have changed. Rerun to get cross-references right. – Nivek Mar 29 '16 at 21:22
• Presumably PATH is not set correctly on your system. Usually, MacTeX should do this on installation, I thought. What does echo $PATH give you on the command line? – cfr Mar 29 '16 at 22:49 • Within the preferences of TeXstudio (advanced options) I set Build->Commands ($PATH) to /usr/local/texlive/2015/bin/universal-darwin/ – Nivek Mar 30 '16 at 9:30
• I don't think the additional compilations will pick that up. It needs to be set correctly for the system generally and not just the application environment. What does the echo $PATH on the command line give? – cfr Mar 30 '16 at 19:22 I just found the answer to my question using this thread. While pdflatex including the option -shell-escape couldn't be found during former compilations, everything works fine now. Changing the folder for the commands to /Library/TeX/texbin/... and adding the same into the Build->Commands ($PATH) in the settings did what i was looking for.

• They should be found without this is PATH is correctly configured. At least, that's the theory. (Unless Apple have decided to disable this or something.) – cfr Mar 30 '16 at 23:16
• Oh, you're using a system updated from the previous OS to El Captain? That's a box of Apple-specific darkness ;). – cfr Mar 30 '16 at 23:19