5

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{\setgroupingblock}[1]{\gdef\equationgrouping{#1:}\phantomsection\setcounter{equation}{0}}

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

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

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}
the_eraser
  • 155
  • 6
orome
  • 10,459
  • If it's relevant, I get the same error described here when I compile; three times for each of the mis-linked equations. – orome Jul 30 '12 at 00:55

2 Answers2

6

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}}
Heiko Oberdiek
  • 271,626
  • 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? – orome Jul 30 '12 at 04:08
3

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.

Werner
  • 603,163
  • 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 09:54