33

lineno skips the line numbering in this case:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit: 
\begin{align}
1+1=2
\end{align}
\end{document}

but is fine with

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{lineno}
\linenumbers
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit: 

\begin{align}
1+1=2
\end{align}
\end{document}

Note that though this sounds a lot like this question. I don't believe the answer is related, since this doesn't use $$. Is the latter somehow more correct?

2
  • 5
    try enclosing your math environment by \begin{linenomath} and \end{linenomath} (see documentaion for lineno). Commented Feb 7, 2012 at 4:18
  • @prettygully I think this is the correct answer. Consider coverting this comment to an answer. I did not see this comment, and had to check the documentation (I found it there luckily). It may be worth mentioning that linenomath* attempts to give line numbers to the lines in the equation as well. Commented Feb 2, 2022 at 15:34

3 Answers 3

7

In essence, this is the same, since align issues $$ before assembling the environment contents. Here is the definition of the align environment in amsmath:

\newenvironment{align}{%
  \start@align\@ne\st@rredfalse\m@ne
}{%
  % <removed \end{align} part for brevity>
}

So, at \begin{align} is equivalent to

\start@align{\@ne}{\st@rredfalse}{\m@ne}

where \start@align is defined to take three parameters and condition appropriately. In the definition of \start@align, I've marked the conditionals, as well as the final $$ for entering display math mode:

\def\start@align#1#2#3{%
    \let\xatlevel@#1% always \z@, \@ne, or \tw@
    \maxfields@#3\relax
    \ifnum\maxfields@>\m@ne % <------------------- FALSE
        \checkat@true
        \ifnum\xatlevel@=\tw@
            \xxat@true
        \fi
        \multiply\maxfields@\tw@
    \else % <------------------------------------- TRUE
        \checkat@false
    \fi
    \ifingather@ % <------------------------------ FALSE
        \iffalse{\fi\ifnum0=`}\fi
        \DN@{\vcenter\bgroup\savealignstate@\align@#2}%
    \else % <------------------------------------- TRUE
        \ifmmode % <------------------------------ FALSE
          \if@display
             \DN@{\align@recover}%
          \else
            \nomath@env
            \DN@{\@namedef{end\@currenvir}{}\@gobble}%
          \fi
        \else % <--------------------------------- TRUE
            $$% <--------------------------------- enter display math mode
            \let\split\insplit@
            \DN@{\align@#2}%
        \fi
    \fi
    \collect@body\next@
}

The conclusion? Use an empty line (or paragraph break). The output with or without is the same, except in this case with lineno.

3
  • 13
    I think the solution(s) by Werner and tohecz are very nice but the output in not exactly the same with and without the paragraph break. With it, more blank space is left before the math environment. On the contrary, the solution given at the link by mbudusic seems to reproduce the same behaviour as the equation without space before, but with compatibility with lineno Commented Jul 10, 2012 at 12:07
  • 1
    @FedericoRotolo: As your answer is more like a comment, I converted it.
    – Stefan Kottwitz
    Commented Jul 10, 2012 at 15:44
  • 4
    not only is the space different, a blank line or paragraph break will also permit a page break, which is undesirable. Commented Sep 11, 2015 at 15:32
50

I've found this answer too. It gives a command that you can use to patch math environments as needed. I figure it does the same thing as presented above, but it doesn't hurt to have several solutions.

I cannot see the difference in line spacing when it is included in preamble or not, but the line numbers that aren't shown before, show up after it is added.

http://phaseportrait.blogspot.com/2007/08/lineno-and-amsmath-compatibility.html

Short summary of the link above: Add the following to your preamble:

\newcommand*\patchAmsMathEnvironmentForLineno[1]{%
  \expandafter\let\csname old#1\expandafter\endcsname\csname #1\endcsname
  \expandafter\let\csname oldend#1\expandafter\endcsname\csname end#1\endcsname
  \renewenvironment{#1}%
     {\linenomath\csname old#1\endcsname}%
     {\csname oldend#1\endcsname\endlinenomath}}% 
\newcommand*\patchBothAmsMathEnvironmentsForLineno[1]{%
  \patchAmsMathEnvironmentForLineno{#1}%
  \patchAmsMathEnvironmentForLineno{#1*}}%
\AtBeginDocument{%
\patchBothAmsMathEnvironmentsForLineno{equation}%
\patchBothAmsMathEnvironmentsForLineno{align}%
\patchBothAmsMathEnvironmentsForLineno{flalign}%
\patchBothAmsMathEnvironmentsForLineno{alignat}%
\patchBothAmsMathEnvironmentsForLineno{gather}%
\patchBothAmsMathEnvironmentsForLineno{multline}%
}
5
  • Hello! I had also the same question, and this solution seems to work perfectly to me!!
    – Vicent
    Commented Jul 25, 2014 at 16:13
  • 4
    Because this one doesn't add the extra space above the align environment, this should be first answer. Vote up!
    – kennyB
    Commented Nov 11, 2014 at 23:28
  • 2
    This result numbers every line but it actually over numbers some lines. It gets like clustered. The best thing is that it does not skip lines. Commented Sep 11, 2015 at 19:49
  • 1
    It would be great to wrap this code in \@ifclasswith{revtex4-1}{linenumbers} such that the code is only executed if the linenumbers option is actually used with revtex4-1. Alternatively, one could check if the package lineno is loaded. Otherwise, latex throws an error when this code is run without the package. Commented May 25, 2016 at 20:35
  • This worked for me! Commented Aug 5, 2018 at 20:33
14

Put this in your preamble, at the end (after the math package are loaded):

\makeatletter
% Make a copy of macros responsible for entering display math mode
\let\start@align@nopar\start@align
\let\start@gather@nopar\start@gather
\let\start@multline@nopar\start@multline
% Add the "empty line" command to the macros
\long\def\start@align{\par\start@align@nopar}
\long\def\start@gather{\par\start@gather@nopar}
\long\def\start@multline{\par\start@multline@nopar}
\makeatother

It actually adapts the trick that @Werner speaks about, just without the need of changing every display math in your document.

It is worth noting that this solution is ultimately bad; it destroys proper page breaking and proper vertical spacing.

3
  • 1
    This and the answer by Werner are both excellent, his for avoiding this error in the future and yours to avoid manually fixing existing documents. wish I could accept both, thanks.
    – cboettig
    Commented Feb 9, 2012 at 19:55
  • 1
    I tested this answer more with bigger files. This answer skips some lines. @mbudisic's answer numbers everything but it over numbers some lines, like getting clustered, which is much better situation than leaving some lines unnumbered. Commented Sep 11, 2015 at 19:50
  • most wonderful answer out there Commented Jun 7, 2021 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .