[Tex/LaTex] pgfplots: Externalize file name – use figure reference (e. g. Figure 2)

pgfplotstikz-externaltikz-pgf

Introduction

I have a document with many pgfplots diagrams embedded in figures. I want to generate a separate file for every diagram by using the externalize feature.

Is it possible to name the generated files according to the figure
number?

In my real life example I have a book class and the figures are named e. g. Figure 4.13 (chapter 4, figure 13).

Similar Question(s)

Example Code

\documentclass{article}
% Here just for the figure placement option "H".
\usepackage{float}

% Plotting diagrams
\usepackage{pgfplots}

% Using the "Externalize" feature
\usepgfplotslibrary{external}

% Configuring the "Externalize" feature
\tikzexternalize[prefix=Output/] % Save all externalized files in the subfolder "Output"
\tikzexternalize[shell escape=-enable-write18]

\begin{document}

\section*{Example Section}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
    \caption{Caption of Figure}
\end{figure}

\end{document}

Example Output

enter image description here

enter image description here

Desired Output

The file named according to the figure, e. g. Figure.1.pdf.

enter image description here

Update

I would also accept if I have to add \tikzsetnextfilename from the pgf package before every figure. Then the question is how do I build the next figure name when using the book class?

Best Answer

I've prepended the figure environment with to set the next file name depending on the figure counter value. Even if this figure is not externalized, it will work for other ones. For a chapter.figure numbering style the output of \thefigure would be better, however.

\documentclass{book}
% Here just for the figure placement option "H".
\usepackage{float}

% Plotting diagrams
\usepackage{pgfplots}

% Using the "Externalize" feature
\usepgfplotslibrary{external}

\usepackage{xpatch}

\makeatletter

\xpretocmd{\figure}{%
  \xdef\tmp@a{Figure.\the\numexpr\value{figure}+1}% Prepare the next filename 
  \tikzsetnextfilename{\tmp@a}
}{}{}%
\makeatother
\tikzexternalize[prefix=Output/,] % Save all externalized files in the subfolder "Output"
\tikzexternalize[shell escape=-enable-write18]

\begin{document}

\section*{Example Section}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
    \caption{Caption of Figure}
    \label{foo}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
    \caption{Caption of Figure}
    \label{foobar}
\end{figure}


\end{document}

More configurable version (and some explanation)

\tikzsetnextfilename needs to know the fully expanded filename. Either this is a string or an macro that expands to the full filename.

\xdef\tmp@a{Figure.\the\numpexpr\value{figure}+1} defines \tmp@a globally and expands it Figure.1 or Figure.2 etc. This is a fixed content which may be given to \tikzsetnextfilename (\xdef is the global variant of \edef, meaning expanded definition.

Now, the direct way without \edef (or \xdef) needs multiple \expandafter statements

\expandafter\tikzsetnextfilename\expandafter{\expandafter\myexternalprefix\the\numexpr\value{figure}+1}%

This is not really easier!

\documentclass{book}
% Here just for the figure placement option "H".
\usepackage{float}

% Plotting diagrams
\usepackage{pgfplots}

% Using the "Externalize" feature
\usepgfplotslibrary{external}

\usepackage{xpatch}


\newcommand{\myexternalprefix}{Figure.}

\xpretocmd{\figure}{%
  \expandafter\tikzsetnextfilename\expandafter{\expandafter\myexternalprefix\the\numexpr\value{figure}+1}%
}{\typeout{figure was patched successfully}}{\typeout{figure patching failed}}%

\tikzexternalize[prefix=Output/,] % Save all externalized files in the subfolder "Output"
\tikzexternalize[shell escape=-enable-write18]

\begin{document}

\section*{Example Section}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
    \caption{Caption of Figure}
    \label{foo}
\end{figure}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
        \end{axis}
    \end{tikzpicture}
    \caption{Caption of Figure}
    \label{foobar}
\end{figure}


\end{document}
Related Question