[Tex/LaTex] Adding line numbers to latex and respecting equations

equationsline-numbering

It's often convenient to add line numbers to a page automatically and the lineno package generally does the trick. But I've had two problems with it

  1. It doesn't seem to align the line numbers with the actual text lines – linenumbers drift between lines and occasionally skip lines as well as a consequence.

  2. Sequences of equations get skipped. Now I've seen this work properly in other PDFs and so was wondering if there's some magic option I'm missing.

All I do is include the package and then turn line numbering on.

Best Answer

(This is a copy of the comment I posted above. Now an answer due to Will Robertson's suggestion.)

This addresses, partially, problem 2 described in the question. I've never seen problem 1 happen myself; then again, I haven't used lineno that much.

Take a look at ulineno.pdf from http://www.ctan.org/tex-archive/macros/latex/contrib/lineno/ (the user's manual). It explains why lineno has trouble with equations and paragraphs containing equations, and also some work-arounds.

Basically, the line number can only be computed after LaTeX finished typesetting the text. The package does this after every paragraph (this having something to do with LaTeX's internal workflow). However, when a paragraph is punctuated by equations, the portions before each equation environment is processed before the equation is processed. So when the package tries to number the lines for one of such paragraphs, it only knows about the last block of text not interrupted by an equation environment before the paragraph break.

To deal with this the package defines a wrapper to be put around the math environments so that it flushes the numbering before entering the math environment, numbers the equation when leaving the environment, and resumes as usual afterwards. So the work around described in the manual is to enclose the equation environments in this wrapper.

Now, to make life slightly simpler, you can also pass an option to lineno to automatically redefine the math environments to include the wrapper. But this will likely not work with document classes that defines their own math environments (such as AMS-LaTeX's align).

Related Question