[Tex/LaTex] hyperref points wrong figure/table with reset counter

counterselsarticlehyperref

I am trying to refer tables (and figures) in the main text with numbering modified and reset as shown in MWE, the numbers are correct in the main text, but hyperref points to wrong table. How to get correct hyper reference to table and figures with reset counter?

MWE

\documentclass{elsarticle}
\usepackage{lineno,hyperref}

\begin{document}

Table \ref{tb1} in main text. Table \ref{sm_tb1} in Supplementary Material.

\begin{table}
\caption{Main text table}\label{tb1}
\begin{tabular}{ |c|c|c| } 
 \hline
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\ 
 cell7 & cell8 & cell9 \\ 
 \hline
\end{tabular}

\end{table}

%--------------------------------
% Supplementary Material
%--------------------------------
\pagebreak
\section*{Supplementary Material}
\pagebreak
\pagebreak


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


\begin{table}
\caption{Supplemental table}\label{sm_tb1}
\begin{tabular}{||c c c c||} 
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
\hline\hline
1 & 6 & 87837 & 787 \\ 
\hline
2 & 7 & 78 & 5415 \\
\hline
3 & 545 & 778 & 7507 \\
\hline
4 & 545 & 18744 & 7560 \\
\hline
5 & 88 & 788 & 6344 \\ [1ex] 
\hline
\end{tabular}
\end{table}

\end{document}

Best Answer

Resetting table or whatever counter especially in a unnumbered section must confuse hyperref, since this package creates 'unique' label names from section etc. counter values by applying the command \theHtable, for example. Now, since the table counter has the value 1 twice and no other unique value (like a section counter value) is available, the same anchor would be created.

It's sufficient, to provide a new \theHtable implementation for the supplement, making a new anchor name, say, Supplement.

\renewcommand{\theHtable}{Supplement.\thetable}

and the analogous form for the \theHfigure (see the code below)

\documentclass{elsarticle}
\usepackage{lineno}
\usepackage{hyperref}

\begin{document}

Table \ref{tb1} in main text. Table \ref{sm_tb1} in Supplementary Material.

\begin{table}
\caption{Main text table}\label{tb1}
\begin{tabular}{ |c|c|c| } 
 \hline
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\ 
 cell7 & cell8 & cell9 \\ 
 \hline
\end{tabular}

\end{table}

%--------------------------------
% Supplementary Material
%--------------------------------
\clearpage
\section*{Supplementary Material}
\clearpage

\setcounter{table}{0}
\setcounter{figure}{0}
\renewcommand{\thetable}{S\arabic{table}}
\renewcommand\thefigure{S\arabic{figure}}
\renewcommand{\theHtable}{Supplement.\thetable}
\renewcommand{\theHfigure}{Supplement.\thefigure}


\begin{table}
\caption{Supplemental table}\label{sm_tb1}
\begin{tabular}{||c c c c||} 
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
\hline\hline
1 & 6 & 87837 & 787 \\ 
\hline
2 & 7 & 78 & 5415 \\
\hline
3 & 545 & 778 & 7507 \\
\hline
4 & 545 & 18744 & 7560 \\
\hline
5 & 88 & 788 & 6344 \\ [1ex] 
\hline
\end{tabular}
\end{table}

\end{document}
Related Question