[Tex/LaTex] Chapter and figure numbers in TiKz externalize prefix

counterstikz-external

I would like my externalised figures to have prefixes reflecting the chapter in which they are from, but when I do:

\usetikzlibrary{external}
\tikzexternalize[prefix=TiKz/\value{chapter}]
\tikzexternalize

or

\usetikzlibrary{external}
\tikzexternalize[prefix=TiKz/\c@chapter]
\tikzexternalize

I get errors and the correct labels are not created.
So the question is:

What is the right way?

What other variables can we stick in there? (ie other than counters, what else can TiKz give us. It obviously has some idea of the filename of that contains the figure…)

How can the filename prefix be suppresed. TiKz does a great job of automagically putting the filename as prefix, but in some circumstances this is not so great, for example when using subfiles, and reusing the externalised figures in the master document.

Please note when I say filename I mean the .tex document that is being built by pdflatex. Not as in the many examples of .tikz include.

8)

Best Answer

The external library comes with two "templates" which make up the file name: the static prefix and a more dynamic figure name.

In order to use chapter/section names, you can make use of figure name:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize[
    prefix=plots/,
    figure name=plot_sec\thesubsection_no,
]

\begin{document}

\section{Section 1}

  \begin{tikzpicture}
  \begin{axis}[
    xlabel=$x$,
    ylabel={$f(x) = x^2 - x +4$}
  ]
  \addplot {x^2 - x +4};
  \end{axis}
  \end{tikzpicture}%

\tikz \draw[-stealth] (0,0) -- (10,0);


\section{Resultsection}

  \begin{tikzpicture}
  \begin{axis}
  \addplot {x^2};
  \end{axis}
  \end{tikzpicture}%

\subsection{Super results}

  \begin{tikzpicture}
  \begin{axis}
  \addplot {e^x};
  \end{axis}
  \end{tikzpicture}%
\end{document}

Compiling this with pdflatex -shell-escape P results in the files

>>ls plots/*.pdf
plots/plot_sec1.0_no0.pdf  plots/plot_sec2.0_no0.pdf
plots/plot_sec1.0_no1.pdf  plots/plot_sec2.1_no0.pdf

Alternatively, you can modify the figure name on your own by adding further suffixes. Personally, I have always used this approach and found it better to read. It looks like

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize[
    prefix=plots/,
    figure name=plot,
]

\begin{document}

\section{Section 1}
{\tikzset{external/figure name/.add={}{_section1}}%

  \begin{tikzpicture}
  \begin{axis}[
    xlabel=$x$,
    ylabel={$f(x) = x^2 - x +4$}
  ]
  \addplot {x^2 - x +4};
  \end{axis}
  \end{tikzpicture}%

\tikz \draw[-stealth] (0,0) -- (10,0);


}%

\section{Resultsection}
{\tikzset{external/figure name/.add={}{_results}}%

  \begin{tikzpicture}
  \begin{axis}
  \addplot {x^2};
  \end{axis}
  \end{tikzpicture}%

\subsection{Super results}
{\tikzset{external/figure name/.add={}{_super}}%

  \begin{tikzpicture}
  \begin{axis}
  \addplot {e^x};
  \end{axis}
  \end{tikzpicture}%
}%
}%
\end{document}

and generates files

>>ls plots/*.pdf
plots/plot_results0.pdf        plots/plot_section10.pdf
plots/plot_results_super0.pdf  plots/plot_section11.pdf

The syntax \pgfkeys{<key name>/.add={<prefix>}{<suffix>}} allows to modify keys which contain a string. It takes the current value and prepends <prefix> and appends <suffix>. Since the values of these assignments are valid until the end of the current group, we can use curly braces to control their start and end of life.

Related Question