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
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?
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 achapter.figure
numbering style the output of\thefigure
would be better, however.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 itFigure.1
orFigure.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
statementsThis is not really easier!