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 have two macros I've defined that I use to set the context in which equations are numbered in my document. One that establishes something I call a 'block' in which equations are numbered, by resetting the equation counter to 0 and setting the equation grouping to a string provided as an argument,

\newcommand{\setgroupingsection}[1]{\setcounter{section}{#1}\setgroupingblock{#1}}

and another that sets the section (using \setcounter{section}) and then calls my first, block-setting macro:

\newcommand{\setgroupingblock}[1]{\gdef\equationgrouping{#1:}\phantomsection\setcounter{equation}{0}}

I use these for a variety of reasons, to gain section-independent control over numbering of various elements (not just equations), and they use mathtools to label all of my equations as expected. However hyperref cross-reference links to these equations do not work correctly. Cross-references seem to behave as if they are numbered within sections only, ignoring 'blocks'.

I've stared at my code for a while now, but am stumped and would appreciate help sorting out what I'm doing wrong.


\documentclass{article}

\usepackage{mathtools}
\usepackage{hyperref}

% Manually set the section; set a new 'block'
\newcommand{\setgroupingsection}[1]{\setcounter{section}{#1}\setgroupingblock{#1}}
% Set a 'block' for numbering equations, overriding any section, and resetting the equation counter
\newcommand{\setgroupingblock}[1]{\gdef\equationgrouping{#1:}\phantomsection\setcounter{equation}{0}}

% Equation numbering and referencing
\renewcommand{\theequation}{\equationgrouping\arabic{equation}}
\gdef\equationgrouping{}


\begin{document}

\setgroupingsection{10} Section \textbf{10} begins here. Subsequent equations are numbered within this section, until another section is started, or a block is started.

\begin{equation}
q(x)=f(x)\cdot g(x)\label{eq:X}
\end{equation}
\begin{equation}
a(i,j)=a(i,q)\cdot a(p,j)\label{eq:Y}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingblock{A} Block \textbf{A} begins here. Subsequent equations are numbered within this block, until another block is started, or another section is started.

\begin{equation}
f(x)=x^{2}+3x\label{eq:Example1}
\end{equation}
\begin{equation}
g(x)=\sin(\gamma^{2})\label{eq:Example2}
\end{equation}
\begin{equation}
f(x)=\cos(\delta^{2})\label{eq:Example3}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingsection{20} Section \textbf{20} begins here.

\begin{equation}
a(i,j)=a(i,q)\cdot a(p,j)\label{eq:Z}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

\setgroupingblock{B} Block \textbf{B} begins here.

\begin{equation}
f(x)=x^{2}+3x\label{eq:Example4}
\end{equation}

\pagebreak{}%To clearly demonstrate hyperlinking jumps in PDF

Numbering of equations works as desired, but hyperlinks in cross-references do not, Cross-references seem to behave as if they are numbered within sections only, ignoring blocks. For example the links to the first and second equations in block \textbf{A},\eqref{eq:Example1} and \eqref{eq:Example2} go to the corresponding equations in the most recent section before block \textbf{A}, \eqref{eq:X} and \eqref{eq:Y}; though the reference to \eqref{eq:Example3} works correctly (there is no corresponding equation in section \textbf{10} to "confuse" it with. Similarly \eqref{eq:Example4} links to \eqref{eq:Z}.

\end{document}
share|improve this question
    
If it's relevant, I get the same error described here when I compile; three times for each of the mis-linked equations. –  raxacoricofallapatorius Jul 30 '12 at 0:55

2 Answers 2

up vote 5 down vote accepted

Package hyperref prefers \theH<counter> over \the<counter> to get unique destination names, see my answer for Change nameddest default name.

Since your equations can be numbered within section or grouping blocks, both numbers/strings can be added to \theHequation, e.g.:

\renewcommand{\theHequation}{\theHsection.\equationgrouping\arabic{equation}}
share|improve this answer
    
Just to be sure I'm doing this right (since it seems to work): I should leave everything else unchanged, but add the line above, correct? –  raxacoricofallapatorius Jul 30 '12 at 4:08

This elaborates on Heiko's answer.

hyperref requires a unique anchor (referred to by \Hy@AnchorName within hyperref.sty). Viewing your example without

\renewcommand{\theHequation}{\theHsection.\equationgrouping\arabic{equation}}

reveals the following in your .aux file:

%...
\newlabel{eq:X}{{10:1}{1}{\relax }{equation.10.1}{}}
\newlabel{eq:Y}{{10:2}{1}{\relax }{equation.10.2}{}}
\newlabel{eq:Example1}{{A:1}{2}{\relax }{equation.10.1}{}}
\newlabel{eq:Example2}{{A:2}{2}{\relax }{equation.10.2}{}}
\newlabel{eq:Example3}{{A:3}{2}{\relax }{equation.10.3}{}}
\newlabel{eq:Z}{{20:1}{3}{\relax }{equation.20.1}{}}
\newlabel{eq:Example4}{{B:1}{4}{\relax }{equation.20.1}{}}

Note the fourth argument #4 inside the second argument of \newlabel{..}{{#1}{#2}{#3}{#4}{#5}}. This anchor, or destination, has to be unique, while yours is clearly not, causing the incorrect hyper jump.

Adding the information of both section and group makes for a unique anchor/destination with each label:

\renewcommand{\theHequation}{\theHsection.\equationgrouping\arabic{equation}}

This is the renewed .aux:

%...
\newlabel{eq:X}{{10:1}{1}{\relax }{equation.10.10:1}{}}
\newlabel{eq:Y}{{10:2}{1}{\relax }{equation.10.10:2}{}}
\newlabel{eq:Example1}{{A:1}{2}{\relax }{equation.10.A:1}{}}
\newlabel{eq:Example2}{{A:2}{2}{\relax }{equation.10.A:2}{}}
\newlabel{eq:Example3}{{A:3}{2}{\relax }{equation.10.A:3}{}}
\newlabel{eq:Z}{{20:1}{3}{\relax }{equation.20.20:1}{}}
\newlabel{eq:Example4}{{B:1}{4}{\relax }{equation.20.B:1}{}}

showing a clearly unique anchor/destination.

share|improve this answer
    
LaTeX remembers the reference value in macro \@currentlabel that is then used, when a \label is written to the .aux file (example: 10:1). Package hyperref adds \@currentHref for the anchor name (example: equation.10.10:1). Forget about \Hy@AnchorName, it is only a temporary internal command inside \hyperdef, it could also be named \Hy@temp instead. –  Heiko Oberdiek Jul 30 '12 at 9:54

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.