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 tried to compile Werner's second answer from Automatically number inequalities with the hyperref package. However, the first run will give me the error below:

Runaway argument?
{\@firstoffive }\fi ), you will notice it is different from (\ref {re\ETC.
! Paragraph ended before \Hy@setref@link was complete.
<to be read again> 
                   \par 

While the second run does not give me any errors, but the inequality referencing doesn't work, i.e. I get ?? instead of i and ii.

If I don't include the hyperref package, then the code compiles fine.

For completeness, here is the MWE

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{hyperref}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

\newcounter{numrel}% Counter for numering relations
\renewcommand{\thenumrel}{\roman{numrel}}% Counter numrel uses lowercase roman numerals
\let\textlabel\label% Use text \label rather than math \label
\newcommand{\numrel}[2]{% Relation numbering
  \begingroup%
  \refstepcounter{numrel}% Increment numrel counter and create correct reference hook
  \textlabel{#2}\endgroup% Label numrel counter
  \ensuremath{\stackrel{(\thenumrel)}{#1}}% Print counter + relation
}
\AfterEndEnvironment{align*}{\setcounter{numrel}{0}}% Resets numrel at the end of align*

\begin{document}
\section{First section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\stackrel{(i)}{\leq} cx^3+dx^2+ex+f \\
       &\stackrel{(ii)}{<} gx^4+hx^3+ix^2+jx+k  
\end{align*}
\section{Second section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel1} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel2} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel1}), you will notice it is different from (\ref{rel2}).
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel3} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel4} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel3}), you will notice it is different from (\ref{rel4}).

\end{document}
share|improve this question

2 Answers 2

up vote 3 down vote accepted

The problem has nothing to do with etoolbox, but rather in the managing of labels by align and the other alignment environments of amsmath.

What you want is to avoid using \label as redefined by amsmath inside align, which is already available as \ltx@label; since you also reset the counter, it's necessary to use a hyperref trick in order to have correct links: defining and stepping a Hnumrel counter is what's needed.

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcounter{numrel}% Counter for numering relations
\newcounter{Hnumrel}% Keep hyperref happy and don't duplicate anchors
\renewcommand{\thenumrel}{\roman{numrel}}% Counter numrel uses lowercase roman numerals

\makeatletter
\newcommand{\numrel}[2]{% Relation numbering
  \refstepcounter{numrel}% Increment numrel counter and create correct reference hook
  \stepcounter{Hnumrel}%
  \ifmeasuring@\else\ltx@label{#2}\fi % Label numrel counter (issue only once)
  \overset{\text{(\thenumrel)}}{#1}% Print counter + relation
}
\makeatother
\AfterEndEnvironment{align*}{\setcounter{numrel}{0}}% Resets numrel at the end of align*

\begin{document}

\section{A section}
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel1} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel2} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel1}), you will notice it is different from (\ref{rel2}).
\begin{align*}
  P(x) &= ax^2+bx+c \\
       &\numrel{\leq}{rel3} cx^3+dx^2+ex+f \\
       &\numrel{<}{rel4} gx^4+hx^3+ix^2+jx+k  
\end{align*}
If you look at (\ref{rel3}), you will notice it is different from (\ref{rel4}).

\end{document}

enter image description here

share|improve this answer
    
This works! I can guess from your solution that hyperref automatically associates the counter numref with the counter Hnumref. Is that correct? Is there something I can read to find out more about how this works? –  I Like to Code Aug 28 '13 at 19:53
    
@ILiketoCode If the control sequence \theHfoo exists when hyperref has to make a link associated to the counter foo, then it uses \theHfoo rather than \thefoo; this can be exploited to get unique internal references. The manual of hyperref explains the trick. –  egreg Aug 28 '13 at 20:10

A lot of hyperref stuff is loaded a the start of the document. Our code compile on my system if I make this change to it

\AtBeginDocument{\let\textlabel\label}%

Then it seem to get the proper textual \label stored.

share|improve this answer
    
Indeed, putting this code just before the \begin{document} solves the problem for me. –  I Like to Code Aug 27 '13 at 14:42
    
Actually you could just have moved \let\textlabel... after \begin{document}, but this code almost does the same thing. –  daleif Aug 27 '13 at 14:45
    
Thank you! @egreg's answer is admirably thorough, but this was a lot simpler. –  Nagel Apr 4 at 15:44
    
I added your solution to the Latex Wikibook, @daleif. Hope you don't mind :) –  Nagel Apr 4 at 16:06

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.