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}
\linenumbersNumber 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.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]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}.Pitfalls
linenonumbers the lines of the running text. Lines of a code listing get their numbers from the listing package, see Insert source code in LaTeX.\renewcommand{\linenumberfont}{\normalfont\footnotesize\color{gray}}changes font and colour of the numbers; the colour needsxcolor.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
lineno - How-to Insert source code in LaTeX
- How-to Cross-references in LaTeX