Line numbers in LaTeX (lineno)

Load the lineno package and write \linenumbers. From there on, every line of text gets a number in the left margin.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{lineno}

Number all lines

In the preamble, \linenumbers applies to the whole document. The numbers run through the document; the package option pagewise restarts them on every page.

This goes into the preamble.

\usepackage{lineno}
\linenumbers

Open in the editor

Number only a part

The linenumbers environment numbers the paragraphs inside it. As commands, \linenumbers switches the numbers on and \nolinenumbers off; leave an empty line before each switch, because the setting counts per paragraph.

A paragraph without numbers.

\begin{linenumbers}
A paragraph with numbers. It is long enough to fill
more than one line of the page, so you see two numbers.
\end{linenumbers}

Another paragraph without numbers.
Result
The compiled example

Open in the editor

Print only every fifth number

\modulolinenumbers[5] prints the numbers 5, 10, 15 and so on. The package option right moves the numbers to the right margin.

This goes into the preamble.

\linenumbers
\modulolinenumbers[5]

Open in the editor

Refer to a line

\linelabel{key} marks the line it stands in, and \ref{key} prints the number of that line. Like every cross-reference it needs two runs.

\linenumbers
The measurement was repeated\linelabel{ln:repeat} three times.

The repetition is described in line~\ref{ln:repeat}.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages