53

When editing documents it is convenient to have all the lines on a page numbered (on the left margin) so that the reviewer can refer to them in his report. How can I achieve this with LaTeX?

2

2 Answers 2

81

As Martin Scharrer has pointed out -- and to answer the general question once and for all --: Use the lineno package. (The package allows to number every line, as shown in its documentation.) See this entry of the TeX FAQ for possible pitfalls and alternatives.

The following addition to the tex file will cause line numbers to appear (at least in scrbook class using xelatex)

\usepackage{lineno}
\linenumbers
2
  • 1
    A concise description of the available lineno options can be found here. Jan 23, 2021 at 12:18
  • 2
    I think this approach does not number every line. It always skips many lines.
    – dodo
    Jun 30, 2022 at 7:54
17

A slightly different approach to that of the faq pointed out by Lockstep.

Firsty, we need to build a box 3em wide to hold the numbers of all the lines. This box will be later positioned at the left margin and unboxed.

 \newsavebox{\@linebox}
 \savebox{\@linebox}[3em][t]{\parbox[t]{3em}{%
   \@tempcnta\@ne\relax
   \loop{\underline{\scriptsize\the\@tempcnta}}\\
     \advance\@tempcnta by \@ne\ifnum\@tempcnta<48\repeat}}

We use Knuth's loop to generate the line numbers. We can position the box and typeset the line numbers by using thr \put command from the LaTeX's picture environment (although TikZ afficionados can use TikZ). As we need this box on every page, we use the fancyhdr package to achieve this, rather than messing with the output routines. Other possibilities is the background package etc.

\fancyhead[LO]{%
\begin{picture}(0,0)%
  \put(-18,-25){\usebox{\@linebox}}%
 \end{picture}}

You should place the whole code within a conditional to switch between draft and final mode, depending on your class. The full minimal is shown below.

\documentclass[10pt]{article}
\usepackage{fancyhdr,lipsum}
\makeatletter

 \newsavebox{\@linebox}
 \savebox{\@linebox}[3em][t]{\parbox[t]{3em}{%
   \@tempcnta\@ne\relax
   \loop{\underline{\scriptsize\the\@tempcnta}}\\
     \advance\@tempcnta by \@ne\ifnum\@tempcnta<48\repeat}}

 \pagestyle{fancy}
 \fancyhead{}
 \fancyfoot{}
 \fancyhead[CO]{\scriptsize How to Count Lines}
 \fancyhead[RO,LE]{\footnotesize\thepage}
%% insert this block within a conditional
 \fancyhead[LE]{\footnotesize\thepage\begin{picture}(0,0)%
      \put(-26,-25){\usebox{\@linebox}}%
      \end{picture}}

 \fancyhead[LO]{%
    \begin{picture}(0,0)%
      \put(-18,-25){\usebox{\@linebox}}%
     \end{picture}}
\fancyfoot[C]{\scriptsize Draft copy}
%% end conditional
\makeatother
\begin{document}
\section*{Lorem Ipsum}
\lipsum
\end{document}
3
  • How may I adjust this code so that the number of the first line in a given page is not 1 but the successor of the previous page's last line's number? Or more succinctly, if a page's last line is N how can I ensure that the first line of the next page is going to be N+1? Jul 14, 2015 at 19:42
  • You will need to modify the loop macro. Best to do using expl3. Perhaps try and if you don't succeed post a new question.
    – yannisl
    Jul 15, 2015 at 2:18
  • @YiannisLazarides, How do we modify the code so that the line numbering starts a bit early (i.e. line number 1 in the previous version corresponds to line number 3 in the new version) and the spacing between the line numbers increases a little (but the continuity remains)?
    – HARRY
    Mar 19, 2022 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .