9

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?

1 Answer 1

6

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

enter image description here

\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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .