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}
Best Answer
I would suggest doing your preparation with TikZ, as Przemysław Scherwentke comments, it's much easier if you want to edit your diagrams - and it would be foolhardy to assume this will never be necessary.
Yet at the same time it's likely - and I think the best way to confirm is to contact the relevant journal itself - that the journal will want a separate file for the graphic. Though this is not necessarily because they can't process TikZ, but so they can include the image separately on an article's webpage for example.
To this end a sensible workflow might be to use in the article
and have a separate file
tikzcode.tex
which contains the TikZ code, alongside a second file to compile just the image (https://tex.stackexchange.com/a/29821/106162)You can then replace the
\input{tikzcode}
with\includegraphics{tikzstandalone}
which links to a PDF copy of the image if necessary. By using\input{tikzcode}
like this you always have the latest version of the image when you just compile the main article, yet it's very quick to switch to using a separate image if/when necessary.The TikZ
external
library should alternatively allow for including the TikZ code in the main file and using it to generate separate files for all the images as well (https://tex.stackexchange.com/a/271475/106162).There are I guess some things which can be done in TikZ but not (easily?) replicated when importing a graphic, like references in the tikzpicture Compiling a tikzpicture including \ref but from my own experience, I've yet to construct anything in tikz that would suffer (other than by way of customisability) from generating a pdf of the image and using
\includegraphics{}
.