# Wrong hyperref with repeated counter

I'm having an interesting problem with hyperref. Apparently, when a counter is reset by \setcounter{mycounter}{0} then hyperref always links to the first possible instance of the counter and not the one where the respective label sits.

Check this MWE out:

% !TEX TS-program = xelatexmk
\documentclass{article}
\usepackage[paperwidth=6in,paperheight=3in]{geometry}
\usepackage{hyperref}
\newcounter{mycounter}\setcounter{mycounter}{0}
\newcommand\countme{\refstepcounter{mycounter}\themycounter}

\begin{document}
First line \countme{} has some interesting text.

The second line \countme \label{onelabel} has \textbf{more} interesting text.

As we saw at line \ref{onelabel} ...

\newpage
A new page brings new numbers \setcounter{mycounter}{0}

A very different first line \countme{} has other interesting text.

The second line \countme \label{otherlabel} has \textbf{different} interesting text.

As we saw at line \ref{otherlabel} ...
\end{document}


As you can see in the screenshot below, the pop up preview of the \ref on the second page doesn't refer to the second line with \label{otherlabel} but to the one on the first page (also has the tooltip "Go to page 1" -- in TeXShop). Am I not understanding something fundamental about hyperref?

The context for all this is an environment that resets my counter to 0 at begin. And all subsequent \refs to labels in a second or third iteration of the environment point to the very first instance of that environment, not the correct one where the label sits.

• This is because the target of a hyperlink is based on the counter value. If the counter is reset then there is a duplicate target, which is ignored and instead the first target is used. Highly related: tex.stackexchange.com/questions/488299/…. – Marijn May 6 at 19:29
• @Marijn Hmm, I see, but is there a way around that in my scenario? – jan May 6 at 19:38
• An additional counter that is never reset which is increased within the countme command and which is used in the theHmycounter definition can be used here as well. Ulrike's answer provides a good starting point for that. – Marijn May 6 at 19:45

hyperref uses \themycounter to create the anchors for the link. If you have two occurences of the same \themycounter value, you get problems. In this cases you must define \theHmycounter so that if gives an unique destination name:

\documentclass{article}
\usepackage[paperwidth=6in,paperheight=3in]{geometry}
\usepackage{hyperref}
\newcounter{mycounter}\setcounter{mycounter}{0}
\newcommand\countme{\refstepcounter{mycounter}\themycounter}

\begin{document}
First line \countme{} has some interesting text.

The second line \countme \label{onelabel} has \textbf{more} interesting text.

As we saw at line \ref{onelabel} ...

\newpage
A new page brings new numbers \setcounter{mycounter}{0}
\renewcommand\theHmycounter{sec.\themycounter} %unique prefix

A very different first line \countme{} has other interesting text.

The second line \countme \label{otherlabel} has \textbf{different} interesting text.

As we saw at line \ref{otherlabel} ...
\end{document}

• +1, note however that this solution requires to redefine the counter after each reset. – Marijn May 6 at 19:42
• Hmm ... so if my counter is reset at begin of an environment, to automate this I'd have to define a second \newcounter{myenvcounter} and then do \setcounter{mycounter}{0}\stepcounter{myenvcounter}\renewcommand\theHmycounter{\themyenvcounter.\themycounter} at every begin? – jan May 6 at 19:50
• @Marijn it should simply show the principle. From the example it was unclear which prefix can used at best (and automatically). – Ulrike Fischer May 6 at 19:50
• @jan yes something like that. But it depends on your real use case what is best, sometimes section/chapter numbers can be used. – Ulrike Fischer May 6 at 19:52