6

This is my code:

\documentclass{article}
\usepackage{mathtools}
\newcounter{rulec}
\renewcommand\therulec{R\arabic{rulec}}
\newcommand\trule[1]{\refstepcounter{rulec}\label{r:#1}\therulec: }
\begin{document}
\begin{equation}
\begin{split}
& \trule{foo} Foo \\
& \trule{bar} Bar \\
\end{split}
\end{equation}
\end{document}

I'm getting:

! Package amsmath Error: Multiple \label's: label 'r:foo' will be lost.

See the amsmath package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.12 \end{split}

What's wrong and how to fix?

1
  • 5
    the point of equation/split (or better equation/aligned) is to have a single \label for the entire display and amsmath goes to some effort to make that posible even though \label may be in any column of the alignment. So it would be far more natural to use align here Commented Jan 29, 2023 at 10:39

1 Answer 1

12

You cannot have two distinct \label commands in the same split environment.

The amsmath package tweaks \label to fulfill its needs, so it is redefined inside the alignment environments. But there's still its original version available as \ltx@label.

\documentclass{article}
\usepackage{mathtools}

\newcounter{rulec}
\renewcommand\therulec{R\arabic{rulec}}
\makeatletter
\newcommand\trule[1]{\refstepcounter{rulec}\ltx@label{r:#1}\textup{\therulec: }}
\makeatother

\begin{document}

\ref{r:foo}, \ref{r:bar}

\begin{equation}
\begin{split}
& \trule{foo} Foo \\
& \trule{bar} Bar
\end{split}
\end{equation}

\end{document}

enter image description here

2
  • 1
    Works like a charm, thanks! However, when I add \usepackage{cleveref} to the mix, the \ltx@label stops working. Maybe you know a quick workaround or it's better to submit a new question?
    – yegor256
    Commented Jan 29, 2023 at 11:37
  • 2
    @yegor256 With cleveref you need to use \label@noarg instead of \ltx@label.
    – egreg
    Commented Jan 29, 2023 at 13:07

You must log in to answer this question.

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