# pgfplots: Externalize file name - use figure reference (e. g. Figure 2)

## 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).

## 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}
\end{axis}
\end{tikzpicture}
\caption{Caption of Figure}
\end{figure}

\end{document}


## Desired Output

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

## 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?

• I've deleted my answer, because it's not what you wanted, but external/figure name={Figure.} and \getrefnumber from refcount could do what you want, perhaps – user31729 Dec 20 '15 at 9:49
• But when the figure number changes, your whole external figures do not match anymore - do you really want that? I'd rather use \tikzsetnextfilename{myFigure} before the tikzpicture and give the figure a distinct name. – thewaywewalk Dec 20 '15 at 12:34
• @thewaywewalk Thanks for the comment. I really want that. It's for non LaTeX users so that they can use the figures in other documents. With this system they can easily find the right file. So far I did it manually, for example here (German): bedienhaptik.de/dissertation/abbildungen-zum-download – Dr. Manuel Kuehner Dec 20 '15 at 12:37
• @Dr.ManuelKuehner: I've undeleted. I think, the new version is better – user31729 Dec 20 '15 at 13:37

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}
\end{axis}
\end{tikzpicture}
\caption{Caption of Figure}
\label{foo}
\end{figure}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}
\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}
\end{axis}
\end{tikzpicture}
\caption{Caption of Figure}
\label{foo}
\end{figure}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\caption{Caption of Figure}
\label{foobar}
\end{figure}

\end{document}

• Thanks for the effort. I will wait a bit longer until I accept the answer. I also updated the question. – Dr. Manuel Kuehner Dec 20 '15 at 18:29
• @Dr.ManuelKuehner: No, it's a e-tex extension/primitive and provided by the (La)TeX core already. No further packages needed. I used other packages/macro earlier but the more knowledge I have of internals, I come back/down to the primitives ;-) – user31729 Dec 21 '15 at 17:57
• @Dr.ManuelKuehner: I thought that I explained it further in my last edit about three weeks ago. – user31729 Jan 30 '16 at 20:34
• \xpretocmd{figure} adds code before \figure starts, (prepend) -- I've to check the issue with Figure.\the\numexpr... however – user31729 Jan 30 '16 at 20:53
• @Dr.ManuelKuehner: The direct usage has its issues. It should work with a huge bunch of \expandafter statements. I don't recommend this – user31729 Jan 30 '16 at 21:09