[Tex/LaTex] Using externalize with standalones for tikz figures? – (output-directory and tikzexternalize)

externalizeshell-escapestandalonetexstudiotikz-external

I am using TexStudio on Mac with texlive 2016. For my tikz figures I use ktikz and matlab2tikz. Some matlab plots have quite a few data points, therefore I decided to use standalones for all my tikz figures and wanted to use externalize (which is not working).

To keep my main folder clean I use a build folder and set the following pdflatex command (Preferences -> Commands):

/Library/TeX/texbin/pdflatex -shell-escape -synctex=1 -interaction=nonstopmode -output-directory=./build %.tex

Also the BibTex command had to be adjusted:

/Library/TeX/texbin/bibtex build/%.aux

Then I had to use additional search paths (Preferences -> Build -> Show Advances Options -> Log File and Pdf File):

Log File: ./build
Pdf File: ./build
Commands ($PATH): /usr/local/texlive/2016/bin/x86_64-darwin

Every time I use \usetikzlibrary{external} and \tikzexternalize I get the following error although I have -shell-escape option added to the pdflatex command.

Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "./tikz_figures/main-figure0" "\def\tikzex
ternalrealjob{main}\input{main}"' did NOT result in a usable output file './tik
z_figures/main-figure0' (expected one of .pdf:.jpg:.jpeg:.png:). Please verify 
that you have enabled system calls. For pdflatex, this is 'pdflatex -shell-esca
pe'. Sometimes it is also named 'write 18' or something like that. Or maybe the
 command simply failed? Error messages can be found in './tikz_figures/main-fig
ure0.log'. If you continue now, I'll try to typeset the picture.

Is there something worng with my path setup or the prefix option to \tikzexternalize?

Commenting the two lines \usetikzlibrary{external} and \tikzexternalize it works.



Edit

MWE

main.tex (folder: ./)

\documentclass{report}

% graphics
\usepackage{graphicx, color}
\graphicspath{
    {./figures/tikz/}{./figures/non_tikz/}
}
\usepackage{tikz}
\usetikzlibrary{external}
%\tikzexternalize[shell escape=-enable-write1 -shell-escape, prefix=./build/]
\tikzexternalize[prefix=./build/tikz_figures/]
%\tikzexternalize
\usepackage{standalone}
%\input{./figures/tikz/tikz_definitions}


\begin{document}

\begin{figure}
\centering
\includestandalone[width=\textwidth]{example}
\caption{example caption.}
\label{fig:example}
\end{figure}

\end{document}

example.tikz (folder: ./figures/tikz/)

\begin{tikzpicture}

% draw help lines
\draw[help lines] (0,0) grid (1\textwidth,5);

\end{tikzpicture}

example.tex (folder: ./figures/tikz/)

\documentclass{standalone}

\input{tikz_definitions}

\begin{document}
\input{example.tikz}
\end{document}

No matter which prefix or options I use for \tikzexternalize I get the same error message.


Additional information:

Here is the folder structuer that I use:

main folder
    main.tex (main tex file)
    references.bib
    build (folder)
        epstopdf (folder)
        tikz_figures (folder
        *.aux, *.log, *.pdf, *.toc, (output files)
    chapters (folder)
        *.tex (tex files)
    figures (folder)
        non_tikz (folder)
            *.png, *.jpg, (all non tikz figures)
        tikz (folder)
            blocks (folder containing figures for tikz figures)
            *.tex (multiple standalone tex files)
            *.tikz (multiple tikz files editable with ktiz)
            tikz_definitions.tex (one tikz definition file)

———-

even more information

non MWE

The preamble of my main.tex file looks like this:

\documentclass[a4paper,twoside, openright,12pt]{report}

\graphicspath{
    {./figures/tikz/}{./figures/non_tikz/}
}
\usepackage{graphicx, color}
\usepackage{tikz}
\usetikzlibrary{external}
%\tikzexternalize[shell escape=-enable-write1 -shell-escape, prefix=./build/]
\tikzexternalize[prefix=./tikz_figures/]
%\tikzexternalize
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%% the following commands are needed for some matlab2tikz features
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}

\usetikzlibrary{arrows.meta}
\usepackage{standalone}
\input{./figures/tikz/tikz_definitions}


\begin{document}

\begin{figure}[H]
\centering
\includestandalone[width=\textwidth]{example}
\caption{example caption.}
\label{fig:example}
\end{figure}


\addcontentsline{toc}{chapter}{Bibliography}
\bibliography{references}
\bibliographystyle{alphaurl}

\end{document}

Because ktikz cannot handle standard latex packages, I use a template for all the tikz figures. This results in one tikz_definitions.tex (template) and a *.tikz and a *.tex (standalone) for every drawn ktikz figure.

tikz_definitions.tex

\usepackage{tikz}

\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{oneColor}{RGB}{47,128,0}
\definecolor{secondColor}{RGB}{242,30,0}
\definecolor{thirdColor}{RGB}{106,178,248}

\tikzstyle{line} = [dash pattern=on 15pt off 25pt]

%% define reusable figure 
\newcommand{\reusablefigure}[4]{
    % a figure
    \begin{pgfonlayer}{foreground}
    \node[anchor=center, rotate=#4, opacity=0.6, inner sep=0] 
        (#1) at (#2,#3) {\includegraphics[scale=0.8]{blocks/a_figure.pdf}};
    \end{pgfonlayer}

    % white background rectangle
    \begin{pgfonlayer}{main}
    \begin{scope}[shift=(#1.center), rotate=#4]
        \fill [white, rounded corners=10pt] (-1.35,-0.53) rectangle (1.35,0.53);
    \end{scope}
    \end{pgfonlayer}

    % coordinate frame
    \begin{pgfonlayer}{foreground}
    \begin{scope}[shift=(#1.center), rotate=#4]
        \draw [<->, thick, color=#1Color] 
            (-0.8,1.1) node [left] (y) {$y_{#1}$} -- 
            (-0.8,0) -- 
            (2,0) node [below right] (x) {$x_{#1}$};
    \end{scope}
    \end{pgfonlayer}
}

%% define another reusable figure
\newcommand{\anotherfigure}[2]{
    \begin{pgfonlayer}{foreground}
        \draw [fill=red] (#1,#2) circle (0.07cm);
    \end{pgfonlayer}
}

Here is an example of a figure (consisting of *.tikz and *.tex file)

*example.tikz

\begin{tikzpicture}[
    scale=0.8, 
    axis/.style={very thick, ->, >=stealth'},
]

    % draw lines
    \draw[line, ultra thick] (0.2\textwidth, 4) -- (0.95\textwidth, 4);
    \draw[line, ultra thick] (0.2\textwidth, 2) -- (0.95\textwidth, 2);

    % a figure
    \reusablefigure{name1}{0}{0}{45}

    % the same figure
    \reusablefigure{name2}{10}{3}{0}


    % measurements
    \anotherfigure{1}{1}
    \anotherfigure{2}{2}
    \anotherfigure{4}{3}

\end{tikzpicture}

*example.tex

\documentclass{standalone}

\input{tikz_definitions}

\begin{document}
\input{overview.tikz}
\end{document}

Best Answer

Updated: Here is a minimal working example for a simple folder structure like this below. You have to compile the main.tex with pdflatex -shell-escape -output-directory=build %.tex. With \tikzsetnextfilename{<...>} you can set the output name. You have to create the folder build(first) manually before compiling.

main.tex

InputTikz (folder)

---exampletikz.tex (tikz)

build (folder)

The MWE produces a new folder (OutputTikz) with pdf files(standalone figures) and main.aux,main.auxlock,main.txt,main.pdf are saved in the build directory.

main.tex

InputTikz (folder)

---exampletikz.tex(tikz)

OutputTikz(folder)

---testname.pdf(standalone pdf)

---B_name.pdf(standalone pdf)

build (folder)

---main.aux

---main.auxlock

---main.txt

---main.pdf

compilation:

  1. Create the folder build first!
  2. Please compile with: pdflatex -shell-escape -output-directory=build %.tex

main.tex

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=./OutputTikz/]    
\begin{document}
\tikzsetnextfilename{testname}
\input{InputTikz/exampleTikz}

\tikzsetnextfilename{B_name}
\input{InputTikz/exampleTikz}
\end{document}

exampletikz.tex

\begin{tikzpicture}
\draw (0,0) -- (0,1) -- (1,1) -- (2,1) -- (2,0);
\end{tikzpicture}
Related Question