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}
external/figure name={Figure.}
and\getrefnumber
fromrefcount
could do what you want, perhaps\tikzsetnextfilename{myFigure}
before the tikzpicture and give the figure a distinct name.