Incompatibility between etoolbox and hyperref

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.


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