# Create nested counters for Supplementary Materials Figures in Revtex

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?

• Welcome to TeX.SX! What do you mean by " Iwill have several objects...having the same identifier"? Under the hood, I agree, they have the same number but in the printed text they will have different numbers so they are easily distinguished. Do you mean with respect to hyper(ref) links or something else? Please provide more details together with a minimal working example that shows the problem that you are really trying to solve. – Andrew May 12 '17 at 1:48
• Yes, the problem arises when trying to use hyperref links. I get a pdfTeX warning destination with the same identifier (name{figure.1}) has been already used, duplicate ignored. And the links in the pdf for Fig S2 will sometimes point to Fig2. – marius May 12 '17 at 2:14

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:

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.