# How to use lineno with amsmath align?

Is there a way to have lineno show line numbers for a paragraph that uses the align environment?

In the following example, the (first) paragraph that ends with \begin{align} ... \end{align} does not have the desired line numbers, whereas the (second) paragraph that separates the text with a single new line contains the line numbers.

\documentclass{article}

\usepackage{amsmath}
\usepackage[mathlines]{lineno}
\usepackage{lipsum}

\linenumbers
\title{My Document}
\author{Anonymous Anteater}

\begin{document}
\maketitle

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.
\begin{align}
x = 2
\end{align}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis

\begin{align}
x = 2
\end{align}

\end{document}


• – Werner Nov 21 '18 at 20:20
• @Werner apologies for the duplicate, shall I close my question? – jII Nov 21 '18 at 20:39

The lineno package is not compatible with the amsmath environments, but the two can be made to work together.

Here is (a slightly updated version of) the patch for the amsmath environments that I wrote for it as an answer to this question.

\documentclass{article}

\usepackage{amsmath}
\usepackage[mathlines]{lineno}

\usepackage{etoolbox} %% <- for \pretocmd, \apptocmd and \patchcmd

%% Patch 'normal' math environment: (currently unused, but good to have)
% \newcommand*\linenomathpatch[1]{%
%   \expandafter\pretocmd\csname #1\endcsname {\linenomath}{}{}%
%   \expandafter\pretocmd\csname #1*\endcsname{\linenomath}{}{}%
%   \expandafter\apptocmd\csname end#1\endcsname {\endlinenomath}{}{}%
%   \expandafter\apptocmd\csname end#1*\endcsname{\endlinenomath}{}{}%
% }
%% Patch AMS math environment:
\newcommand*\linenomathpatchAMS[1]{%
\expandafter\pretocmd\csname #1\endcsname {\linenomathAMS}{}{}%
\expandafter\pretocmd\csname #1*\endcsname{\linenomathAMS}{}{}%
\expandafter\apptocmd\csname end#1\endcsname {\endlinenomath}{}{}%
\expandafter\apptocmd\csname end#1*\endcsname{\endlinenomath}{}{}%
}

%% Definition of \linenomathAMS depends on whether the mathlines option is provided
\expandafter\ifx\linenomath\linenomathWithnumbers
\let\linenomathAMS\linenomathWithnumbers
%% The following line gets rid of an extra line numbers at the bottom:
\else
\let\linenomathAMS\linenomathNonumbers
\fi

% \linenomathpatch{equation} %% <- unnecessary, equation is already patched
\linenomathpatchAMS{gather}
\linenomathpatchAMS{multline}
\linenomathpatchAMS{align}
\linenomathpatchAMS{alignat}
\linenomathpatchAMS{flalign}

\linenumbers
\title{My Document}
\author{Anonymous Anteater}

\begin{document}
\maketitle

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.
\begin{align*}
\nabla \cdot E &= \frac{\rho}{\varepsilon_0}
&
\nabla \times E &= - \frac{\partial}{\partial t}
\\
\nabla \cdot B &= 0
&
\nabla \times B &= \mu_0 J + \mu_0 \varepsilon_0 \frac{\partial E}{\partial t}
\end{align*}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
$i \hbar \frac{d}{dt} \lvert \Psi(t) \rangle = \hat H \lvert \Psi(t) \rangle.$

\end{document}


## Some details

The following excerpts are from the lineno documentation:

lineno.sty does not work well with display math. The parts of a paragraph preceeding a display math will not get line numbers. The reason is that the paragraph is not finished, but the part above the display is broken into lines anyway.

[…]

lineno.sty defines a {linenomath} environment, which can be wrapped around a display math to make the line numbering work. This will work with any kind of display math, even if you use explicit  pairs. There is a *-form {linenomath*}which also numbers the lines of the display itself, as good as it can.

To use the align environment together with the lineno package, you thus need to wrap the environment between \begin{linenomath} and \end{linenomath}, like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.
\begin{linenomath}\begin{align}
x = 2
\end{align}\end{linenomath}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
culpa qui officia deserunt mollit anim id est laborum.


Because this is a hassle, the standard math environments are automatically patched to include \linenomath and \endlinenomath. The environments that are patched are displaymath, equation, equation*, eqnarray and eqnarray*, as well as the macros $ and $. If the mathlines option is provided to lineno, then the lines within these equations are also numbered.

The amsmath environments are not patched because (from the documentation):

This will probably not work with AMSmath.

It actually works fine, apart from the fact that each of the amsmath environments receives an additional line number at the end. The macro \linenomathpatchAMS, which I define above, performs a modified version of this patch that gets rid of this extra line number. (For more details, see here.)

The only environment that still misbehaves is multline, which has an extra equation number at the top.

• Awesome! Is there a way to extend this to work with $...$ display math as well? – Peter Grill Nov 21 '18 at 21:02
• @PeterGrill I modified the answer to obey (the presence/absence of) the mathlines option. With this option $…$-equations are also counted as lines. – Circumscribe Nov 21 '18 at 22:34
• Oh I see. I didn't notice that you had removed the mathlines option in your MWE. – Peter Grill Nov 21 '18 at 22:35
• @PeterGrill I thought it was no longer necessary, but I had forgotten about $…$. – Circumscribe Nov 21 '18 at 22:44
• example would be improved by a multi-line align. align isn't meant to be used for single-line formulas. – barbara beeton Nov 22 '18 at 16:04

One way is to replace the align by

\begin{aligned} x = 2 \end{aligned}


or by

\begin{aligned} x = 2 \end{aligned}


if you want numbered version.

• example would be improved by a multi-line aligned. aligned isn't meant to be used for single-line formulas. – barbara beeton Nov 22 '18 at 16:03