# How do define label system with labels given by counter?

I would like to have two commands \makelabel{foo} and \referlabel{foo} to be used as in the example

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$A \overset{\makelabel{foo}}{=} B \overset{\makelabel{bar}}{=} C$
where \referlabel{foo} is given by \ldots and \referlabel{bar} is given by \ldots
$C \overset{\makelabel{foobar}}{=} D$
Equality \referlabel{foobar} \ldots
\end{document}


such that the output is the same as for the code

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$A \overset{(1)}{=} B \overset{(2)}{=} C$
where (1) is given by \ldots and (2) is given by \ldots
$C \overset{(3)}{=} D$
Equality (3) \ldots
\end{document}


That is, the label should be created from a counter. Possibly, \referlabel{foo} should create a hyperlink to \makelabel{foo}. Could you point me to existing solutions or indicate how to implement the commands, please?

\makelabel is already a known command, so I've changed the interface to \setlabel/\reflabel:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcounter{labelcnt}
\renewcommand{\thelabelcnt}{(\arabic{labelcnt})}
\newcommand{\setlabel}[1]{%
\refstepcounter{labelcnt}\ltx@label{lbl:#1}%
{\text{\upshape\thelabelcnt}}%
}
\newcommand{\reflabel}[1]{\text{\upshape\ref{lbl:#1}}}
\makeatother

\begin{document}

$A \overset{\setlabel{foo}}{=} B \overset{\setlabel{bar}}{=} C$
where~\reflabel{foo} is given by \ldots and~\reflabel{bar} is given by \ldots
$C \overset{\setlabel{foobar}}{=} D$
Equality~\reflabel{foobar} \ldots

\end{document}


To avoid a "Multiple \label" error with the loading of amsmath, we use the traditional LaTeX \label macro, stored in \ltx@label.

• Would it suffice to just use \eqref instead of defining a new \reflabel? – Herr K. Jan 30 '15 at 18:48
• @KevinC: Since it would syntactically not refer to an equation, but an equality, I kept the interface, even though it should work. – Werner Jan 30 '15 at 18:49