Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

?

share|improve this question
    
Could you provide a minimal working example please? It makes things easier if we have some complete code to copy and play with. –  qubyte Feb 18 '12 at 9:41
    
Does that work @MarkS.Everitt? –  thequark Feb 18 '12 at 10:09
    
Thanks. It may seem like a petty thing, but it lowers the mental barrier against taking up a question. :) –  qubyte Feb 18 '12 at 11:06

1 Answer 1

up vote 7 down vote accepted

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}
share|improve this answer
1  
Using the page counter may produce wrong results. See this answer for an example. –  lockstep Feb 18 '12 at 14:39
    
@lockstep Thanks for pointing that out, I removed the corresponding part from my answer. –  diabonas Feb 18 '12 at 14:44
1  
This can be automated using \usepackage{etoolbox}\preto\align{\stepcounter}{myequation}}. –  Werner Feb 19 '12 at 7:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.