[Tex/LaTex] Create nested counters for Supplementary Materials Figures in Revtex

floatshyperrefrevtex

It has become more common to generate papers with Supplementary Materials (SM).
Ideally, I want to write one document containing both the main text and the SM, so that all latex automatic references and hyperlinks are generated.
Many journals like that we specify the figures in SM by Figure S1, S2 etc.
One way I was using to do this was

\renewcommand{\thefigure}{S\arabic{figure}}
\setcounter{figure}{0}

However, this create issues because now I will have several objects : Figure 1 and Figure S1 having the same identifier.

I found another way :

\newcounter{sfigure}
\renewcommand{\thefigure}{S\arabic{sfigure}}

However, now I need to add

 \stepcounter{sfigure} 

after each figure

What would be the syntax to define

\newcounter{sfigure}

so that it gets incremented each time the figure counter is incremented?

Best Answer

Hyperref does cunning things behind the scenes with counters when it adds links, which is why resetting the counter will cause confusion with the corresponding hyperlinks. To get around this you either need to fool hyperref or to fool latex into doing what you want.

I think that fooling latex is probably easier so I suggest redefining the figure counter. Rather than resetting the counter to 0 you can "remember" the number of figures in the main document, say in a macro \presupfigures, and then redefine \thefigure so that it prints an S together with the "adjusted" figure number obtained by subtracting \presupfigures:

\renewcommand\thefigure{S\fpeval{\arabic{figure}-\presupfigures}}

Here \fpeval, from the xfp package, is a convenient way to do the subtraction but of course there are other ways to do this.

The following MWE gives the full details. For completeness, I have also adjusted the section number in the same way.

\documentclass{article}
\usepackage{hyperref}
\usepackage{xfp}
\newcommand\SupplementaryMaterials{%
  \xdef\presupfigures{\arabic{figure}}% save the current figure number
  \xdef\presupsections{\arabic{section}}% save the current section number
  \renewcommand\thefigure{S\fpeval{\arabic{figure}-\presupfigures}}
  \renewcommand\thesection{S\fpeval{\arabic{section}-\presupsections}}
}

\begin{document}
  \section{Main text}
    \begin{figure}\caption{Nice one caption!}\label{Fig:1}
    Here is a nice figure
  \end{figure}

  \SupplementaryMaterials
  \section{Supplementary text}
  \begin{figure}\caption{Nice two caption!}\label{Fig:2}
    Here is a nicer figure
  \end{figure}

  A reference to the nice figure: see \autoref{Fig:1}

  A reference to the nicer figure: see \autoref{Fig:2}
\end{document}

Here is the output:

enter image description here

You can't check that the hyperlinks work from this image but you will find that they work if you compile your own version of the MWE.

BTW, it is better if you provide a minimal working example when asking questions. I am not completely sure that I have solved your problem because the question does not contain enough information to reproduce it.

Related Question