TeX - LaTeX Asked by Milo on June 20, 2021
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:
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}
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 the amsmath
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 advancepostdisplaypenaltylinenopenalty
.
Deleting this line from the definition would remove the spurious line number, but it would also remove the only line number for the equation
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.
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
and eqnarray
) and the latter for the amsmath
environments.
Both the starred and unstarred versions of these environments are patched.
I used patchcmd
from the etoolbox
package to remove the unwanted change of linenopenalty
from linenomath
.
The following code does both of the things described above:
documentclass[12pt,a4paper]{report}
usepackage[mathlines]{lineno} %% <- mathlines turns on line numbering in equations
usepackage{amsmath} %% <- N.B., mathtools also loads this
linenumbers
usepackage{etoolbox} %% <- for cspreto, csappto, patchcmd, pretocmd, apptocmd
%% Patch 'normal' math environments:
newcommand*linenomathpatch[1]{%
cspreto{#1}{linenomath}%
cspreto{#1*}{linenomath}%
csappto{end#1}{endlinenomath}%
csappto{end#1*}{endlinenomath}%
}
%% Patch AMS math environments:
newcommand*linenomathpatchAMS[1]{%
cspreto{#1}{linenomathAMS}%
cspreto{#1*}{linenomathAMS}%
csappto{end#1}{endlinenomath}%
csappto{end#1*}{endlinenomath}%
}
%% Definition of linenomathAMS depends on whether the mathlines option is provided
expandafterifxlinenomathlinenomathWithnumbers
letlinenomathAMSlinenomathWithnumbers
%% The following line gets rid of an extra line numbers at the bottom:
patchcmdlinenomathAMS{advancepostdisplaypenaltylinenopenalty}{}{}{}
else
letlinenomathAMSlinenomathNonumbers
fi
linenomathpatch{equation}
linenomathpatchAMS{gather}
linenomathpatchAMS{multline}
linenomathpatchAMS{align}
linenomathpatchAMS{alignat}
linenomathpatchAMS{flalign}
begin{document}
begin{equation}
F = m a
end{equation}
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}
begin{align}
2x - 5y &= 8
3x + 9y &= -12
end{align}
begin{flalign}
a &= b + c &
&= 1 + 1 &
&= 2 &
end{flalign}
end{document}
Note: I've loaded amsmath
after lineno
, but the other order also works.
If the other order is used, equation
and [
will end up being patched twice (because lineno
also patches them), which not very elegant but entirely harmless.
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, when amsmath
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/why multline
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}
.
% Disable line numbering in measurement phase for multline
makeatletter
patchcmd{mmeasure@}{measuring@true}{
measuring@true
ifnum-linenopenaltypar>interdisplaylinepenalty
advanceinterdisplaylinepenalty-linenopenalty
fi
}{}{}
makeatother
I didn't include this in the demonstration above because I believe multline
is not used by too many people.
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 the amsmath
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.
Correct answer by Circumscribe on June 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP