4

When using the \labelrel command (with \overset instead of \stackrel as \stackrel is obsolete: Differences between \stackrel and \stackbin) from @Circumscribe Put reference above equal sign and refer to it in an align-environment the alignment is not correct:

\documentclass{article}
\usepackage{mathtools}

\newcounter{relctr} %% <- counter for relations
\renewcommand*\therelctr{\alph{relctr}} %% <- label format

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

\begin{document}

\begin{align*}
    a &= b \\
      &\labelrel{=}{someLabel} c
\end{align*}

\end{document}

Misaligned equal sign

This seems to be a well known problem with the \overset command: \overset and align environment: how to get correct alignment? where the aligned-overset package offers a nice solution.

So my question is how one can achieve similar behavior for the \labelrel command or even more generally specify the behavior with align for new commands.

I don't have any experience with coding new commands myself but I decided to post this as a new questions as this may be an interesting question for other users as well.

Here is what I tried:

  • Search the internet

  • Try using the aligned-overset in the definition of \labelrel but I had no idea on how to pass the & from align to the definition

  • Look at the implementation of aligned-overset to copy the relevant code however I almost didn't understand anything.

New contributor
Grey Eminence is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Welcome to TeX.SE! Commented 2 days ago

2 Answers 2

3

Use

\NewDocumentCommand{\labelrel} { m m }{%
  \mathrel{%
    \refstepcounter{relctr}%
    \overset{\mathclap{\textnormal{\therelctr}}}{\mathstrut{#1}}%
    }%
  \originallabel{#2}%
}

with

  • \mathrel such as the space around the command is set for binary relations;
  • \mathclap to remove the space added by the label;
  • {#1} to remove the space added by = with its surrounding.

Example:

\documentclass{article}
\usepackage{mathtools}

\newcounter{relctr} %% <- counter for relations
\renewcommand*\therelctr{(\alph{relctr})} %% <- label format

\NewDocumentCommand{\labelrel} { m m }{%
  \mathrel{%
    \refstepcounter{relctr}%
    \overset{\mathclap{\textnormal{\therelctr}}}{\mathstrut{#1}}%
    }%
  \originallabel{#2}%
}

\AtBeginDocument{\let\originallabel\label}

\begin{document}

\begin{align*}
    a &= b \\
      &\labelrel{=}{someLabel} c
\end{align*}

\begin{align*}
    a &= b \\
      &\labelrel{=}{someOtherLabel} c
\end{align*}

See \ref{someLabel} and \ref{someOtherLabel}.

\end{document}

Example

11
  • It works great if I only have one \labelrel in an align environment. However when adding a second I get the error: "Package amsmath Error: Multiple \label's: label 'someLabel' will be lost." Commented 2 days ago
  • Furthermore the referencing doesn't work. So I get the warning: "LaTeX Warning: Reference `someLabel' on page 1 undefined" Commented 2 days ago
  • 1
    @GreyEminence It seems to work with \originallabel, as in the answer you linked (I removed it because I didn't see why we need it; note that I also removed \begingroup...\endgroup because I don't understand why it is relevant, but maybe I'm also wrong). Commented 2 days ago
  • 1
    you should probably put the refstepcounter inside the mathrel Commented 2 days ago
  • 1
    @jlab it is. it's only needed with hyperref or similar which causes refstepcounter to make a link anchor which would affect the spacing Commented yesterday
3

There's no need to define \originallabel, because amsmath provides \ltx@label for the same purpose.

However, you need to make \mathstrut into a \mathrel, so \overset can make the object as a relation as well.

\documentclass{article}
\usepackage{mathtools}

\newcounter{relctr} %% <- counter for relations
\renewcommand*\therelctr{\alph{relctr}} %% <- label format

\makeatletter
\newcommand\labelrel[2]{%
  \refstepcounter{relctr}%
  \overset{\makebox[0pt]{\normalfont\scriptsize(\therelctr)}}
    {\mathrel{\mathstrut}#1}%
  \ltx@label{#2}%
}
\makeatother

\begin{document}

\begin{align*}
    a &= b \\
      &\labelrel{=}{someLabel} c
\end{align*}

The ref: \ref{someLabel}

\end{document}

output

(Note: the picture has been made with twocolumn to reduce the size.)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.