# Using \label with amsmath nested environments

First of all, I'm not sure these environments are supposed to be nested, but the fact is the alignment comes out exactly how I wanted, and no errors are issued.

\documentclass[a5paper]{article}
\usepackage{amsmath}

\begin{document}

\begin{gather}
\begin{align}
a &= b + c \notag \\
%\label{important}
b - a &= -c \\
c - a &= -b
\end{align} \\
a - (b-a) - (c-a) = 2a
\end{gather}

\end{document}


The problem appears when I uncomment the \label, then compilation is interrupted with:

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


The result is still OK, and I wouldn't mind too much if I didn't have to press enter every time I compile...

I could bypass amsmath's redefinition of \label as shown here, but is there another possible fix? Or am I doing something fundamentally wrong?

-

clearly this is a bug, and will be investigated as such.

but here's another horrible hack that merely gives the nasty warning that

LaTeX Warning: Label important' multiply defined.


at least you wouldn't have to press enter every time ...

\documentclass[a5paper]{article}
\usepackage{amsmath}
\begin{document}
\begin{gather}
\begin{align}
a &= b + c \tag*{} \\
b - a &= -c
\label{important} \\
c - a &= -b
\end{align}\\
a - (b-a) - (c-a) = 2a
\end{gather}
some text with an xref: \eqref{important}
\end{document}

-

Using align inside gather is officially supported (see p. 23 of amsldoc.pdf, the end of section 10.2). However, using \label inside the inner align is buggy: if \notag is used, then the error is raised, if \notag is not used, a label in the second line is duplicated.

A horrible hack is to disable the error generation:

\makeatletter
\def\label@in@display{\gdef\df@label}
\makeatother


This of course will suppress the error also when it's legitimately raised, so use with care.

-

The problem is with your \notag command, which instructs amsmath not to put a tag (in this case, a number) by the equation- hence, labelling it doesn't make any sense which is why you get the error.

If you want to \label that first equation, you'll need to remove the \notag from the first line. If you don't want to \label it, then the \notag is fine.

-
\notag is in the first line; the \label is in the second line. – Ian Thompson Jun 20 '12 at 16:18
@IanThompson the \label wants to apply itself to the most recent <thing> which in this case is the first line. the \label works on the most recent thing, not on the next thing – cmhughes Jun 20 '12 at 16:27
Try it for yourself with a simple example. – Ian Thompson Jun 20 '12 at 16:31
@IanThompson already have. perhaps I have misunderstood the issue – cmhughes Jun 20 '12 at 16:57

As @egreg points out, you can run into trouble if you nest the align environment in this way (despite the fact that it is supported). There is a variant called aligned that works inside other display math environments but this doesn't number the individual equations.

In your case I would be inclined to simplify matters by putting the last equation in its own environment, reducing the spacing between this and the align environment, and not using gather at all.

\documentclass[a5paper]{article}
\usepackage{amsmath}
\begin{document}
\bgroup %Grouping restores \belowdisplayskip after use
\setlength\belowdisplayskip\jot
\begin{align}
a &= b + c \notag \\
\label{important}
b - a &= -c \\
c - a &= -b
\end{align}
$$a - (b-a) - (c-a) = 2a$$
\egroup
\end{document}

-
But aligned does not give a number for each equation, it treats the whole block as a single equation instead (which is useful in other cases). – Jellby Jun 20 '12 at 15:53
@Jellby --- I have edited my answer. – Ian Thompson Jun 20 '12 at 16:18
At the end of section 10.2 of the documentation it's explicitly mentioned that align can be used inside gather. – egreg Jun 20 '12 at 16:28
@egreg --- Right as always! I'll edit my answer later. – Ian Thompson Jun 20 '12 at 16:34

Although it does not seem to be your problem here, this bug can also occur because of two \notag commands in the same line. Putting the \notag` twice in the same line is useless and may seem innocuous, but it generates the aforementioned bug.

-