There is a really nice package, lineno
, which provides line-numbering and various options for customization, some of which I will discuss further below. The most common use is continuous line numbering throughout the document. The following two lines in the preamble will do the trick:
\usepackage{lineno} \linenumbers
Options
Some of the options the package provides include:
- [left]: Line numbers in left margin (default)
- [right]: Line numbers in right margin
- [switch]: Line number in outer margin (left for even, right for odd pages)
- [switch*]: Line numbers in inner margin
- [pagewise]: Restart numbering on every page
- [running]: Continuously number lines (default)
- [modulo]: Print only multiples of five
- [displaymath, mathlines]: Line numbers for math environments (both needed)
Example:
\usepackage[switch, modulo]{lineno} \linenumbers
Line-numbers for a paragraph
The package provides a linenumbers
environment to number a single paragraph.
\documentclass[11pt]{article} \usepackage[english]{babel} \usepackage{lineno, blindtext} \begin{document} \blindtext \begin{linenumbers} \blindtext \end{linenumbers} \blindtext \end{document}
If you do not leave an empty line between the previous and the actual paragraph, the previous may also get numbered.
Line numbers for math environments
We saw how math environments can be automatically wrapped for lines to be numbered. To number only a single set of equations, rather than the whole document, use the following code (from the lineno documentation):
\begin{linenomath*} \begin{eqnarray} b_1 &=& a_{11}x_1 + a_{12}x_2 \\ b_2 &=& a_{21}x_1 + a_{22}x_2 \end{eqnarray} \end{linenomath*}
Numbering every other line
Instead of using the modulo
option, it is possible to number every nth line (e.g. every second):
\usepackage{lineno} \linenumbers \modulolinenumbers[2]
Customizing numbers
The font of the numbers is readily changed by redefining linenumberfont
. The default font style/size used is:
\renewcommand\linenumberfont{\normalfont\tiny\sffamily}
Since linenumber
is just a counter, it is possible to change it to roman (roman/Roman
) numbers or alphabetic characters (alph/Alph
, max 26!):
\documentclass[11pt]{article} \usepackage[english]{babel} \usepackage{lineno, blindtext, color} \linenumbers \modulolinenumbers[2] \renewcommand\thelinenumber{\color{red}\Roman{linenumber}} \begin{document} \blindtext \end{document}
Referencing line numbers
The package also implements labels/references to specific line numbers using linelabel
/lineref
. And by loading the hyperref
package (always load last), clicking the references brings the user directly to the corresponding line.
\documentclass{article} \usepackage{lineno, hyperref} \linenumbers \begin{document} Some text.\linelabel{lne:label1} \clearpage See line \lineref{lne:label1}. \end{document}
Documentation
Package documentation (user manual) for lineno.
almutei
Sounds like a nice package. I’d love to use it. Unfortunately, my paragraphs containing align environments don’t get counted or numbered, that is neither the equations nor the text surrounding it.
Any idea how to fix this?
Best wishes,
Almut
tom
Hi Almut!
Fixing the text surrounding is easy, just add an empty line before the
align
environment or some of the solutions provided here.Adding line numbers to the equations is more tricky. I tried a fix posted by Ted some time ago. When I encapsulate
align
withlinenomath
:I get a funny double number on the last equation. He also mentioned double numbers, but only in his first fix.
Does anyone know how to fix this?
Thanks, Tom.
almutei
Thanks for the reply Tom!
However, the line numbers aren’t necessary and would be just for my convenience while writing my thesis, so any changes in the text are overkill in my case. I’ll survive without them. 🙂
Best wishes,
Almut
tedpavlic
The double-line numbers are an artificate of TeX not setting text on a grid. Occassionally (but not always), you’re going to get an extra number between an equation and the surrounding text, especially if your equation is “tall.” Given the substrate on which TeX documents are constructed, preventing these problems is a significant challenge.
tom
Ted, thanks for the clarification. Best, Tom.
Maïeul
You have ledmac for critical edition of text wich allow to number lines.
Robert Paynter
Are there any other methods? I think what I’m after is a page decoration rather than text markup as {lineno} package does – it puts numbers for each line of text. I would like numbering down the edge of the page so that someone can refer to a place on the page irrespective of whether it is a line of text, an equation or a figure (with labels).
tom
Hi Robert,
Below is some code which I adapted slightly from its source. It prints line numbers irrespective of the content. Obviously, these line numbers might not perfectly coincide with the actual content. A shift happens whenever the height of an object (such as a figure) is not a multiple of the ‘line height’.
I hope this is what you were looking for.
Best, Tom
joaodamasceno
But how do I put the numbering on both sides of the text?
tom
Hi there,
That’s an interesting question. Use the following code to show line numbers on both sides of the text. I took the solution from here.
HTH, Tom
João Damasceno
Tom, really thank you. It helped a lot. Problem solved
joaodamasceno
How do I make the numbering appear from a specific passage of the text, not the text header?
tom
Hi there,
Thanks for your question. In the article, I show how to number a specific paragraph. I assume that’s not what you are looking for. Do you want to add line numbers anywhere in the text starting with 1 or with the actual line number counted from the beginning of the text?
Best, Tom
joaodamasceno
Hi Tom, thank you for having responded. Do you know that feature that puts the lines on the sides of the text? well, I use this feature because I find it very cool, however, I need that numbering to appear from the body of the text and not from the header. I have no idea how to modify this. An example of this numbering is what is in the link: https://www.overleaf.com/latex/templates/cvpr-2018-template/qgmrfntfbqns#.WomvIXUbP_g
tom
Hello,
Thanks for your reply. Perhaps you find the solution described here useful.
Best, Tom
Sandy Maguire
this is great. thanks!
rgrosso
Many Thanks!