Why does \label not work in math mode when using [fleqn] with amsmath?

Try the following

\documentclass[11pt]{article}
\usepackage[fleqn]{amsmath}

\begin{document}
\newcounter{eqnthing}\setcounter{eqnthing}{0}
$a+b=c\refstepcounter{eqnthing}\label{maththing}$
\par See(\ref{maththing}).
\refstepcounter{eqnthing}\label{linething}
\par See (\ref{linething}).
\end{document}


The first reference will not work. Then comment out \usepackage[fleqn]{amsmath} and it will work. It also works with \usepackage{amsmath} so the fleqn option appears to be the culprit.

Is this a known bug? Is there a workaround?

• I edited out my wrong code because it wasn't my intention to imply that was the particular problem I was trying to solve. I just wanted to point out that there is an issue with \label when using [fleqn] in amsmath. It may work well with \tag, but what if you want to do something else? Not being able to imagine that other things might be feasible or even desirable doesn't make it okay to break existing functionality. – Geoff Pointer Nov 4 '14 at 14:06

Its the way amsmath has redesigned equations, especially labelling inside of them - handled differently when using the fleqn package option. Specifically, amsmath redefines how \label works, as in some cases (most notably within multi-parse items like align and friends) it has to switch it on-and-off in order to avoid multiply defined labels.

You can still access the old/traditional LaTeX \label using \ltx@label, for which I've defined an interface as \ltxlabel below:

\documentclass[11pt]{article}
\usepackage[fleqn]{amsmath}
\makeatletter
\newcommand{\ltxlabel}{\ltx@label}
\makeatother

\newcounter{eqnthing}\setcounter{eqnthing}{0}
\begin{document}

$a+b=c\refstepcounter{eqnthing}\hfill(\arabic{eqnthing})\ltxlabel{maththing}$

See (\ref{maththing}).

$a+b=c$\refstepcounter{eqnthing}\hfill(\arabic{eqnthing})\label{linething}

See (\ref{linething}).
\end{document}

• Werner I'm confused by your claim that this has nothing to do with the fleqn option. First, adding or removing the option and making no other changes leads to different behaviour and, secondly, the amsmath code does not put the label into \endmathdisplay@a when fleqn is active. It is true that amsmath redefines what \label does but as far as I can see it is also true that amsmath does not properly cater for labels when the fleqn option is used. What am I missing? – Andrew Nov 4 '14 at 4:35
• @Andrew: True. The tag/label system is handled differently with fleqn. Consider my usage merely a way to circumvent this shortfall. – Werner Nov 4 '14 at 5:42

I'm not sure why you want to define in a wrong way what's already featured in amsmath, namely \tag.

\documentclass[11pt]{article}
\usepackage[fleqn]{amsmath}

\newcounter{eqnthing}\setcounter{eqnthing}{0}
\newcommand\geofflabel[1]{%
\refstepcounter{eqnthing}\tag{\theeqnthing}\label{#1}%
}

\begin{document}
$a+b=c\geofflabel{maththing}$
See~\eqref{maththing}.
\begin{align*}
a&=1\geofflabel{a}\\
b&=11111\\
c&=0\geofflabel{c}
\end{align*}
See \eqref{a}~and~\eqref{c}.
\end{document}


• When you say wrong, I put that code together in a hurray just to show that there was a problem with \label, because that was easier than untangling what I'm doing from my own package. It's not clear why \tag works and why my wrong code doesn't, followed by a \label call. And what if you don't want the \tag thing and actually have a good reason for doing something else? Shouldn't the \label call work on its own, just referencing the counter? – Geoff Pointer Nov 4 '14 at 10:42
• @GeoffPointer It's “wrong” because it doesn't take into account that amsmath redefines \label to work with its setting. Using \tag allows for better integration, because it's a built-in feature of amsmath. – egreg Nov 4 '14 at 11:20
• I have removed the objectionable code which had nothing to do with my question anyway and was just an off the cuff assemblage to show that the use of \label is restricted. It may work when using \tag and that's fine if you want to have numbers in brackets - I agree that's the best way to do it. But, it's my opinion that if the [fleqn] option is to be considered user friendly it shouldn't interfere with the usual use of \label. Just because I can't think of an application right now doesn't change the fact that the use of \label is restricted in this case. – Geoff Pointer Nov 4 '14 at 13:58

Lookng around on TeX.SX it seems that there are many bugs with the fleqn option such as /equation-spacing-issue-with-fleqn-amsmath-and-frenchb-babel, problem-using-option-fleqn-in-amsmath-package, combined-use-of-fleqn-and-qedhere, ...

I am not sure whether this one is known or not. The problem seems to be that when amsmath closes off a displayed equation it forgets to put the label in when the fleqn option is present. Here is a rough hack that fixes the problem with your MWE...although I haven't tested it extensively so perhaps it creates other problems:

Here is the code:

\documentclass[11pt]{article}
\usepackage[fleqn]{amsmath}
\makeatletter
\def\endmathdisplay@a{%
\if@eqnsw \gdef\df@tag{\tagform@\theequation}\fi
\if@fleqn \ifx\df@label\@empty \else \@xp\ltx@label\@xp{\df@label}\fi\@xp\endmathdisplay@fleqn
\else \ifx\df@tag\@empty \else \veqno \alt@tag \df@tag \fi
\ifx\df@label\@empty \else \@xp\ltx@label\@xp{\df@label}\fi
\fi
\ifnum\dspbrk@lvl>\m@ne
\postdisplaypenalty -\@getpen\dspbrk@lvl
\global\dspbrk@lvl\m@ne
\fi
}
\makeatother

\begin{document}
\newcounter{eqnthing}\setcounter{eqnthing}{0}
$a+b=c\refstepcounter{eqnthing}\hfill(\arabic{eqnthing})\label{maththing}$
\par See (\ref{maththing}).
\refstepcounter{eqnthing}\hfill(\arabic{eqnthing})\label{linething}
\par See (\ref{linething}).
\end{document}


No doubt egreg will come along soon and give a "proper" fix:)

• I believe that the problem is wrongly set from the start. – egreg Nov 4 '14 at 7:18