How to use lineno with amsmath align?












4















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}


missing line numbers with lineno and align










share|improve this question


















  • 1





    Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

    – Werner
    Nov 21 '18 at 20:20











  • @Werner apologies for the duplicate, shall I close my question?

    – jesterII
    Nov 21 '18 at 20:39
















4















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}


missing line numbers with lineno and align










share|improve this question


















  • 1





    Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

    – Werner
    Nov 21 '18 at 20:20











  • @Werner apologies for the duplicate, shall I close my question?

    – jesterII
    Nov 21 '18 at 20:39














4












4








4


1






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}


missing line numbers with lineno and align










share|improve this question














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}


missing line numbers with lineno and align







align amsmath line-numbering lineno






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:45









jesterIIjesterII

1773




1773








  • 1





    Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

    – Werner
    Nov 21 '18 at 20:20











  • @Werner apologies for the duplicate, shall I close my question?

    – jesterII
    Nov 21 '18 at 20:39














  • 1





    Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

    – Werner
    Nov 21 '18 at 20:20











  • @Werner apologies for the duplicate, shall I close my question?

    – jesterII
    Nov 21 '18 at 20:39








1




1





Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

– Werner
Nov 21 '18 at 20:20





Related/duplicate: Why doesn't lineno number a paragraph when it is followed by an align equation environment without a space?

– Werner
Nov 21 '18 at 20:20













@Werner apologies for the duplicate, shall I close my question?

– jesterII
Nov 21 '18 at 20:39





@Werner apologies for the duplicate, shall I close my question?

– jesterII
Nov 21 '18 at 20:39










2 Answers
2






active

oldest

votes


















5














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]{%
% expandafterpretocmdcsname #1endcsname {linenomath}{}{}%
% expandafterpretocmdcsname #1*endcsname{linenomath}{}{}%
% expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
% expandafterapptocmdcsname end#1*endcsname{endlinenomath}{}{}%
% }
%% Patch AMS math environment:
newcommand*linenomathpatchAMS[1]{%
expandafterpretocmdcsname #1endcsname {linenomathAMS}{}{}%
expandafterpretocmdcsname #1*endcsname{linenomathAMS}{}{}%
expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
expandafterapptocmdcsname end#1*endcsname{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} %% <- 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}


output







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.






share|improve this answer


























  • 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






  • 1





    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



















2














One way is to replace the align by



[
begin{aligned}
x = 2
end{aligned}
]


or by



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


if you want numbered version.



enter image description here






share|improve this answer
























  • 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











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461186%2fhow-to-use-lineno-with-amsmath-align%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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]{%
% expandafterpretocmdcsname #1endcsname {linenomath}{}{}%
% expandafterpretocmdcsname #1*endcsname{linenomath}{}{}%
% expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
% expandafterapptocmdcsname end#1*endcsname{endlinenomath}{}{}%
% }
%% Patch AMS math environment:
newcommand*linenomathpatchAMS[1]{%
expandafterpretocmdcsname #1endcsname {linenomathAMS}{}{}%
expandafterpretocmdcsname #1*endcsname{linenomathAMS}{}{}%
expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
expandafterapptocmdcsname end#1*endcsname{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} %% <- 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}


output







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.






share|improve this answer


























  • 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






  • 1





    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
















5














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]{%
% expandafterpretocmdcsname #1endcsname {linenomath}{}{}%
% expandafterpretocmdcsname #1*endcsname{linenomath}{}{}%
% expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
% expandafterapptocmdcsname end#1*endcsname{endlinenomath}{}{}%
% }
%% Patch AMS math environment:
newcommand*linenomathpatchAMS[1]{%
expandafterpretocmdcsname #1endcsname {linenomathAMS}{}{}%
expandafterpretocmdcsname #1*endcsname{linenomathAMS}{}{}%
expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
expandafterapptocmdcsname end#1*endcsname{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} %% <- 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}


output







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.






share|improve this answer


























  • 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






  • 1





    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














5












5








5







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]{%
% expandafterpretocmdcsname #1endcsname {linenomath}{}{}%
% expandafterpretocmdcsname #1*endcsname{linenomath}{}{}%
% expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
% expandafterapptocmdcsname end#1*endcsname{endlinenomath}{}{}%
% }
%% Patch AMS math environment:
newcommand*linenomathpatchAMS[1]{%
expandafterpretocmdcsname #1endcsname {linenomathAMS}{}{}%
expandafterpretocmdcsname #1*endcsname{linenomathAMS}{}{}%
expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
expandafterapptocmdcsname end#1*endcsname{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} %% <- 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}


output







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.






share|improve this answer















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]{%
% expandafterpretocmdcsname #1endcsname {linenomath}{}{}%
% expandafterpretocmdcsname #1*endcsname{linenomath}{}{}%
% expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
% expandafterapptocmdcsname end#1*endcsname{endlinenomath}{}{}%
% }
%% Patch AMS math environment:
newcommand*linenomathpatchAMS[1]{%
expandafterpretocmdcsname #1endcsname {linenomathAMS}{}{}%
expandafterpretocmdcsname #1*endcsname{linenomathAMS}{}{}%
expandafterapptocmdcsname end#1endcsname {endlinenomath}{}{}%
expandafterapptocmdcsname end#1*endcsname{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} %% <- 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}


output







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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 16:22

























answered Nov 21 '18 at 20:24









CircumscribeCircumscribe

5,6861836




5,6861836













  • 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






  • 1





    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



















  • 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






  • 1





    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

















Awesome! Is there a way to extend this to work with [ ... ] display math as well?

– Peter Grill
Nov 21 '18 at 21:02





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





@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





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





@PeterGrill I thought it was no longer necessary, but I had forgotten about […].

– Circumscribe
Nov 21 '18 at 22:44




1




1





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





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











2














One way is to replace the align by



[
begin{aligned}
x = 2
end{aligned}
]


or by



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


if you want numbered version.



enter image description here






share|improve this answer
























  • 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
















2














One way is to replace the align by



[
begin{aligned}
x = 2
end{aligned}
]


or by



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


if you want numbered version.



enter image description here






share|improve this answer
























  • 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














2












2








2







One way is to replace the align by



[
begin{aligned}
x = 2
end{aligned}
]


or by



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


if you want numbered version.



enter image description here






share|improve this answer













One way is to replace the align by



[
begin{aligned}
x = 2
end{aligned}
]


or by



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


if you want numbered version.



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 19:52









SigurSigur

24.5k355138




24.5k355138













  • 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



















  • 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

















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





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


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461186%2fhow-to-use-lineno-with-amsmath-align%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin