[Tex/LaTex] Two table counters and hyperref links

cross-referencinghyperrefpdf

I have a document with two sets of tables. The first set, numbered 1 to 5, are in the main body of the text. The second, numbered 1 to 3, are in an appendix.

When I use \ref to link to one of the tables in the appendix, the hyperlink in the PDF file links to the corresponding table in the main section (so a link to appendix table 1 links to main table 1). This warning message is provided:

pdfTeX warning (ext4): destination with the same identifier (name{table.1}) 
   has been already used, duplicate ignored

Clearly this is a counter problem: when I reset the table counter in the appendix, there are two tables with a counter of 1 and hyperref just points to the first one.

Related problems have been discussed on this forum, and a number of discussions can be found online, but I haven't been able to figure out a solution to this particular problem. (It seems more straightforward to fix the problem for page numbers.) Also, I get the sense that the aliascnt package could work here, but I can't figure out how to make it work.

Best Answer

Indeed, the problem comes from link anchors not having unique names. To provide those unique names, you can appropriately redefine \theHtable to guarantee that it will expand to a unique value (despite the counter resetting); for example:

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\section{test}
\renewcommand*{\theHtable}{\arabic{table}} 
\begin{table}
  \centering Test
  \caption{test1}\label{fig:test1}
\end{table}

\begin{table}
  \centering Test
  \caption{test2}\label{fig:test2}
\end{table}

\begin{table}
  \centering Test
  \caption{test3}\label{fig:test3}
\end{table}

\appendix

\setcounter{table}{0}
\renewcommand*{\theHtable}{\arabic{section}.\arabic{table}} 

\begin{table}
  \centering Test
  \caption{test4}\label{fig:test4}
\end{table}

\begin{table}
  \centering Test
  \caption{test5}\label{fig:test5}
\end{table}

\ref{fig:test1}\ref{fig:test2}\ref{fig:test3}\ref{fig:test4}\ref{fig:test5}

\end{document}
Related Question