[Tex/LaTex] Trouble with hyperref when resetting the counter for equations (specifically with the align environment)

amsmathcounterscross-referencinghyperref

I am trying to reset a counter to zero; however hyperref tries to reference previous equations. I am using:

\setcounter{equation}{0}

And if I have labels in say align environments:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
\setcounter{equation}{0}
\begin{align}
x=10
\label{eq:c} \\
y=20
\label{eq:d} 
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}

If I now \ref{eq:c}, the hyperref package will point me back to eq:a (in text, they both have the same number).

Is there a way to fix this without manually stepping a new counter you make? I.e. can you fix it without doing something like this:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
%% \setcounter{equation}{0}                                %Commented out line
\newcounter{newequation}                                   %Added line
\renewcommand{\theequation}{\arabic{newequation}}          %Added line
\stepcounter{newequation}                                  %Added line
\begin{align}
x=10
\label{eq:c} \\ 
\stepcounter{newequation}                                  %Added line
y=20
\label{eq:d} 
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}

?

Best Answer

To produce correct references, you have to make the counter values unique to hyperref. To do so, you can introduce a parent counter myequation: Each time this counter is increased, the counter equation is reset. Like this, hyperref can take into account both the value of myequation and equation to produce the correct hyperlink.

In order to do this, add the following code to your document after having loaded hyperref:

\newcounter{myequation}
\makeatletter
\@addtoreset{equation}{myequation}
\makeatother

Resetting the equation counter is done by stepping myequation, so use \stepcounter{myequation} instead of \setcounter{equation}{0}.

Full example code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\newcounter{myequation}
\makeatletter
\@addtoreset{equation}{myequation}
\makeatother
\begin{document}
\begin{align}
x=1
\label{eq:a} \\
y=2
\label{eq:b}
\end{align}
Eq.~\eqref{eq:a}.
Reset counter
\newpage
\stepcounter{myequation}
\begin{align}
x=10
\label{eq:c} \\
y=20
\label{eq:d} 
\end{align}
Now trying to refer to third equation Eq.~\eqref{eq:c}
\end{document}