1

Many journals require the figures of a submission to be handed in separately, using sensible names. Tikz-external seems like the perfect fit for this job if the majority of figures are based on tikz/pgf, as automatic updating and naming are supported.

However, both the pgfmanual and the pgf documentation only state:

A unique counter will be used for each different {<name>}, and each counter will start at 0.

the the pgf documentation further states

These counters are stored into different macros. In other words: no TEX register will be needed.

The start at 0 is somewhat inconvenient, except if you happen to have a graphical abstract. In that case all is well and the numbers of subsequent PDF files correspond to the figure numbers in the document.

graphical_abstract.tex -> Fig0.pdf
Fig1 (tikzpicture) -> Fig1.pdf
...

Another issue occurs when one or more of the figures does not use tikz, e.g., because it is an already existing PDF or PNG image that is simply included.

Fig2 (not a tikzpicture)
Fig3 (tikzpicture) -> Fig2.pdf

As the counter for file names only considers \begin{tikzpicture} ... \end{tikzpicture} it is unaware of such intermediate Figures and "Figure 3" in the document gets generated as Fig2.pdf.

In a similar question @percusse mentioned that the \tikzexternal@getnextfilename@advancecount macro is used to step up the counter.

This raises two questions:

  1. How to set the counter to a specific value (Answered by @Ulrike)
  2. How to increment the counter when a figure is to be skipped

Here is a basic MWE inspired by @Ulrike's answer

\documentclass{article}
\usepackage{tikz, float}
\usetikzlibrary{external}
\tikzexternalize

\tikzsetfigurename{Fig}

% NOTE: Modified from https://tex.stackexchange.com/a/580401/140433
\newcommand{\setpgfexternalcounter}[1]{
  \makeatletter%
  \pgfkeysgetvalue{/tikz/external/figure name}\myexternalname
  \expandafter\gdef\csname c@tikzext@no@\myexternalname\endcsname{#1}%
  \makeatother
}

\begin{document}

  one
  %
  \setpgfexternalcounter{1}
  %
  \begin{figure}[H]
    \centering
    \begin{tikzpicture}
      \draw(0.5, 0)--(0.5, 1);
    \end{tikzpicture}
    \caption{The number one}
    \label{fig:one}
  \end{figure}

  two
  %
  % TODO: \skippgfexternalcounter
  %
  \begin{figure}[H]
    \centering
    % NOTE: use the following to generate Fig2.pdf
    % \tikzsetnextfilename{figures/Fig2}
    % \begin{tikzpicture}
    %   \draw (0.5, 0) -- (0, 0) -- (0, 0.5) -- (0.5, 0.5) -- (0.5, 1) -- (0, 1);
    % \end{tikzpicture}
    \includegraphics{figures/Fig2}
    \caption{The number two}
    \label{fig:two}
  \end{figure}

  three
  %
  % TODO: Avoid explicitly having to give the number of the next figure
  \setpgfexternalcounter{3}
  %
  \begin{figure}[H]
    \centering
    \begin{tikzpicture}
      \draw (0, 0)  -- (0.5, 0) -- (0.5, 1)  -- (0, 1);
      \draw (0, 0.5) -- (0.5, 0.5);
    \end{tikzpicture}
    \caption{The number three}
    \label{fig:three}
  \end{figure}
\end{document}

Note that a solution to the second part of the question, e.g., through some macro \skippgfexternalcounter, would not require the explicit specification of the next figure's number. It would even alow for further automation by including it in all figures that are not tikzpictures. Of course it is also possible to define a new counter, increment it for all non-tikzpictures and then
add the value to the current value of the external figure counter (e.g., within \setpgfexternalcounter). But for this I need to get the value of that counter which I am not able to do, since I still don't quite understand what exatly it is or where the information about it comes from.

1
  • It would be easier to propose a solution if you provided a small complete document (a.k.a. MWE) that illustrates the problem.
    – gernot
    Jan 25 at 13:26
3

The following creates and include XYZ1.pdf and XYZ6.pdf:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\begin{document}
blub
\makeatletter 
\tikzsetfigurename{XYZ} %not needed only for demo.
\pgfkeysgetvalue{/tikz/external/figure name}\myexternalname
\expandafter\gdef\csname c@tikzext@no@\myexternalname\endcsname{1}%
\makeatother
\begin{tikzpicture}
  \draw(0,0)--(1,-1);
\end{tikzpicture}

\makeatletter
\expandafter\gdef\csname c@tikzext@no@\myexternalname\endcsname{6}%
\makeatother
\begin{tikzpicture}
  \draw[red](0,0)--(-1,2);
\end{tikzpicture}

\end{document}
 <./XYZ1.pdf> <./XYZ6.pdf>

But you need to be careful not to create non-unique names ...

1
  • Thank you very much Ulrike, you answer the first part of my question, however, the second one is still unanswered. I took the liberty to modify your answer into a MWE that clarifies what I am looking for.
    – mrclng
    Jan 25 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.