I am using
\usepackage{lineno}
to print line numbers on my document. My document has lots of equations. I use
\usepackage{mathtools}
and frequently use environments such as align
, multline
and flalign
to name a few (I also use their starred *
equivalents). There exists an option called mathlines
\usepackage[mathlines]{lineno}
which as far as I can see only prints numbers for the equation
environment.
I have read that to get line numbers working for all math environments you need to wrap every environment in
\begin{linenomath*}
\begin{align} % or equation,multline,flalign etc
.
.
\end{align}
\end{linenomath*}
and remove the option mathlines
. See this answer from Why doesn't lineno number a paragraph when it is followed by an indented equation?. This is sort of working for me, but I have two problems:
- It is giving me 'extra' numbers on the last line of some equations;
- I have hundreds of equations in my document. It is impossible for me to individually go through and add
begin{linenomath*}
around every equation.
Are there any solutions to these issues? This is what my output looks like. Notice the bunched up numbers (4 , 5), (7 , 8) and (11 , 12).
Code
\documentclass[12pt,a4paper]{report}
\usepackage{mathtools}
\usepackage{lineno}
\linenumbers
\begin{document}
\begin{linenomath*}
\begin{equation}
F=ma
\end{equation}
\end{linenomath*}
\begin{linenomath*}
\begin{multline}
p(x) = 3x^6 + 14x^5y + 590x^4y^2 + 19x^3y^3\\
- 12x^2y^4 - 12xy^5 + 2y^6 - a^3b^3
\end{multline}
\end{linenomath*}
\begin{linenomath*}
\begin{align}
2x - 5y &= 8 \\
3x + 9y &= -12
\end{align}
\end{linenomath*}
\begin{linenomath*}
\begin{flalign}
a &= b+c &\\
&= 1+1 &\\
&= 2 &
\end{flalign}
\end{linenomath*}
\end{document}
Best Answer
Problem 1:
The
lineno
package prints a line number at the end of every display equation, as well between any two lines within the equation (on the first of these two lines). For theamsmath
environments, it however also prints an "interdisplay" line number after the last line, so you should suppress the "postdisplay" line number.The package works by decreasing penalties by a large amount to force (and then abort) page breaks. The
linenomath*
environment calls\linenomathWithnumbers
, which contains the line\advance\postdisplaypenalty\linenopenalty
. Deleting this line from the definition would remove the spurious line number, but it would also remove the only line number for theequation
environment.You could add
\postdisplaypenalty=0
(which is the default value) after\begin{linenomath*}
and before\begin{<multline/align/flalign>}
in your MWE and this would solve problem 1.Problem 2:
Patching the math environments to include the effect of
linenomath*
is not too difficult. I have defined commands\linenomathpatch
and\linenomathpatchAMS
that do this below. The first is intended for the base math environments (equation
andeqnarray
) and the latter for theamsmath
environments. Both the starred and unstarred versions of these environments are patched. I used\patchcmd
from theetoolbox
package to remove the unwanted change of\linenopenalty
from\linenomath
.Code:
The following code does both of the things described above:
Note: I've loaded
amsmath
afterlineno
, but the other order also works. If the other order is used,equation
and\[
will end up being patched twice (becauselineno
also patches them), which not very elegant but entirely harmless.Addendum:
multline
As you can see in the above screenshot,
multline
is the only ASM environment that is not playing nice, as it has an additional line number right above the equation. This extra number is caused by an additional interdisplay penalty (not a predisplay penalty) and it occurs during the “measuring” phase, whenamsmath
typesets the cells that comprise the equation just to determine how wide each of the environment's columns should be. I don't really know how/whymultline
is different.This can be fixed by patching
amsmath
's the internal\measure@
command as follows. The following needs to added to the preamble anywhere after\usepackage{amsmath}
.I didn't include this in the demonstration above because I believe
multline
is not used by too many people.Explanation: how
lineno
works under the hoodThe
lineno
package is rather complicated and I should note that I do not fully understand how it works. This is what I believe to understand:\linenumbers
decreases the penalty for a page break between two lines in a paragraph by 100000. Since the penalty normally lies somewhere between –10000 and +10000 (one 0 fewer), this is normally pretty much guaranteed to force a page break between any two lines (any value below –10000 forces a page break).The package also hooks into the output routine. Whenever a page is about to be shipped, it tests whether the penalty is below –32000 (which would normally never happen). If it is, it prints a number, increases the penalty by 100000 (undoing the earlier decrease) and essentially tells TeX to reconsider the page break. If it isn't below –32000, the page is shipped as normal.
The
linenomath*
environment decreases the penalties for page breaks above/below an equation and between lines within an equation by 100000. For normal equations, only the "postdisplay" and "predisplay" penalties are relevant, but the "interdisplay" penalty also occurs between any two lines of theamsmath
environments (including after the last line).It is the fake page break caused by the postdisplay penalty that is generating the extra the unwanted numbers, so setting this value back to zero (or never changing it to begin with) solves your problem.