[Tex/LaTex] Put reference above equal sign and refer to it

math-mode

What is the shortest and convenient way to Put reference above equal sign and refer to it in text, i.e. do something like this:

Sample code

It can be done manually, by using \stackrel{}{} command and manual numbering. My question is: can it be done in a smarter way?

Best Answer

Below, I have defined a macro \labelrel that I believe should do what you want. It takes two arguments of which the first is the (relation) symbol you would like to label and the second is a label name. The labels are incremented automatically and you can refer to them using \eqref as if they were equations.

\documentclass{article}

\usepackage{amsmath}

\newcounter{relctr} %% <- counter for relations
\everydisplay\expandafter{\the\everydisplay\setcounter{relctr}{0}} %% <- reset every eq
\renewcommand*\therelctr{\alph{relctr}} %% <- label format

\newcommand\labelrel[2]{%
  \begingroup
    \refstepcounter{relctr}%
    \stackrel{\textnormal{(\alph{relctr})}}{\mathstrut{#1}}%
    \originallabel{#2}%
  \endgroup
}
\AtBeginDocument{\let\originallabel\label} %% <- store original definition

\begin{document}

  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  eirmod invidunt, and therefore
  \begin{equation}\label{myeq}
      X \labelrel={myeq:equality} Y + Z \labelrel\leq{myeq:inequality} \ldots.
  \end{equation}
  The equality~\eqref{myeq:equality} in equation~\eqref{myeq} was proven in Theorem~123,
  and the inequality~\eqref{myeq:inequality} is self-explanatory.
  Here is another equation,
  \[
      f(A \cap B) \labelrel\subseteq{somelabelname} f(A) \cap f(B)
  \]
  which contains an inclusion~\eqref{somelabelname} unrelated to equation~\eqref{myeq}.

\end{document}

enter image description here

A few remarks:

  • I'm using letters instead of numbers to avoid any clashes with equation numbers. To use numbers, replace (both instances of) \alph by \arabic above.

  • The labels start with (a) in every equation. If you don't want this you should remove the \everydisplay line. If you want the numbering to start over at the beginning of every section you should use \newcounter{relctr}[section].

  • It is also possible to refer to the relations in equation (1) as (1a), (1b), etc without changing their tags. This is accomplished by replacing the \therelctr line by \renewcommand*\therelctr{\theequation\alph{relctr}}. It does mean that every equation that has labelled relations must be numbered.

  • I'm storing the definition of \label in \originallabel because amsmath redefines the \label macro inside equations. I do this \AtBeginDocument in case any packages (like hyperref) loaded after this code change the definition.

  • I'm using \mathstrut (which is equivalent to \phantom() to ensure that every label is placed at the same height, independent of the height of the relation it is decorating. You can remove it if you don't like this.

  • \begingroup and \endgroup limit the scope of the effect of \refstepcounter. Without them it would no longer be possible to reference the equation itself.


Related Question