Site icon texblog

Adding line numbers to documents

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

General lineno package example

 

Options

Some of the options the package provides include:

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}

Paragraph line number example

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}

Customized line number example

 

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.

Exit mobile version