1 lineno.sty  v2.05beta 1998/04/26

2

3 A L^{A}TEX style option to attach 4 Line numbers on paragraphs

5 Stephan I. Böttcher

6 stephan@alzt.tau.ac.il

7 Contents

8 1 Introduction

9 This package provides line numbers on paragraphs. After TEX has broken 10 a paragraph into lines there will be line numbers attached to them, with 11 the possibility to make references through the L^{A}TEX \ref, \pageref cross 12 reference mechanism. This includes four issues:

13 • attach a line number on each line,

14 • create references to a line number,

15 • control line numbering mode,

16 • count the lines and print the numbers.

17 The first two points are implemented through patches to the output routine. 18 The third by redefining \par, \@par and \@@par. The counting is easy, as 19 long as you want the line numbers run through the text. If they shall start 20 over at the top of each page, the aux-file as well as TEXs memory have to 21 carry a load for each counted line. 22 I wrote this package for my wife Petra, who needs it for transcriptions 23 of interviews. This allows her to precisely refer to passages in the text. It 24 works well together with \marginpars, but not with displaymath.

25 This style option is written for L^{A}TEX 2ε, later than November 1994, since 26 we need the \protected@write macro.

  1 \NeedsTeXFormat{LaTeX2e}[1994/11/04]
  2 \ProvidesPackage{lineno}
  3 [1998/04/26 line numbers on paragraphs (c) SIB v2.05beta]

27 2 Put the line numbers to the lines

28 The line numbers have to be attached by the output routine. We simply set 29 the \interlinepenalty to -100000. The output routine will be called after 30 each line in the paragraph, except the last, where we trigger by \par. The 31 \linenopenalty is small enough to compensate a bunch of penalties (e.g.,

32 with \samepage).
  4 \newcount\linenopenalty\linenopenalty=-100000
  5 \mathchardef\linenopenaltypar=25000

33 So let’s make a hook to \output, the direct way. The L^{A}TEX macro 34 \@reinserts puts the footnotes back on the page.

  6 \output=\expandafter{%
  7  \expandafter \LineNoTest
  8  \expandafter \if@tempswa
  9  \the\output
 10  \else
 11  \@reinserts
 12  \MakeLineNo
 13  \fi

14 }

35 The float mechanism inserts \interlinepenaltys during \output. So care-36 fully reset it before going on. Else we get doubled line numbers on every 37 float placed in horizontal mode, e.g, from \linelabel. 38 Sorry, neither a \linelabel nor a \marginpar should insert a penalty, 39 else the following linenumber could go to the next page. Nor should any 40 other float. So let us suppress the \interlinepenalty altogether with the

41 \@nobreak switch.

42 Since (ltspace.dtx, v1.2p)[1996/07/26], the \@nobreaktrue does it’s job 43 globally. We need to do it locally here.

 15 \def\LineNoTest{%
 16 \ifnum\interlinepenalty<-\linenopenaltypar
 17  \advance\interlinepenalty-\linenopenalty

 18  \my@nobreaktrue
 19  \fi
 20 \@tempswatrue
 21 \ifnum\outputpenalty>-\linenopenaltypar\else
 22  \ifnum\outputpenalty>-175000\relax
 23  \@tempswafalse
 24  \fi
 25  \fi

26 }

27

 28 \def\my@nobreaktrue{\let\if@nobreak\iftrue}

44 We have to return all the page to the current page, and add a box with the 45 line number, without adding breakpoints, glue or space. The depth of our 46 line number should be equal to the previous depth of the page, in case the 47 page breaks here, and the box has to be moved up by that depth.

48  The \interlinepenalty comes after the \vadjust from a \linelabel,

49 so we increment the line number after printing it. The macro 50 \makeLineNumber produces the text of the line number, see section ??. Fi-51 nally we put in the natural \interlinepenalty, except after the last line.

 29 \def\MakeLineNo{\@tempdima\dp\@cclv \unvbox\@cclv
 30 \sbox\@tempboxa{\hbox to\z@{\makeLineNumber}}%
 31 \stepcounter{linenumber}%
 32 \dp\@tempboxa=\@tempdima\ht\@tempboxa=\z@
 33 \nointerlineskip\kern-\@tempdima\box\@tempboxa
 34 \ifnum\outputpenalty=-\linenopenaltypar\else
 35  \@tempcnta\outputpenalty
 36  \advance\@tempcnta -\linenopenalty
 37  \penalty\@tempcnta
 38 \fi

39 }

52 3 Control line numbering

53 The line numbering is controlled via \par. L^{A}TEX saved the TEX-primitive 54 \par in \@@par. We push it one level further out, and redefine \@@par to 55 insert the \interlinepenalty needed to trigger the line numbering. And 56 we need to allow pagebreaks after a paragraph. 57 New (2.05beta): the prevgraf test. A paragraph the ends with a displayed 58 equation, a \noindent\par or wrapfig.sty produce empty paragraphs. 59 These should not get a spurious line number via \linenopenaltypar.

 40 \let\@@@par\@@par

 41 \newcount\linenoprevgraf

42

 43 \def\linenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else
 44  \advance\interlinepenalty \linenopenalty
 45  \linenoprevgraf\prevgraf
 46  \@@@par
 47  \ifnum\prevgraf>\linenoprevgraf
 48  \penalty-\linenopenaltypar
 49  \fi
 50  \kern\z@
 51  \advance\interlinepenalty -\linenopenalty
 52  \fi\fi

53 }

60 The basic commands to enable and disable line numbers. \@par and \par 61 are only touched, when they are \let to \@@@par/\linenumberpar. The line 62 number may be reset to 1 with the star-form, or set by an optional argument 63 [⟨number⟩].

 54 \def\linenumbers{\let\@@par\linenumberpar
 55  \ifx\@par\@@@par\let\@par\linenumberpar\fi
 56  \ifx\par\@@@par\let\par\linenumberpar\fi
 57  \@ifnextchar[{\resetlinenumber}%]
 58  {\@ifstar{\resetlinenumber}{}}%

59 }

60

 61 \def\nolinenumbers{\let\@@par\@@@par
 62 \ifx\@par\linenumberpar\let\@par\@@@par\fi
 63 \ifx\par\linenumberpar\let\par\@@@par\fi

64 }

What happens with a display math? Since \par is not executed, when breaking the lines before a display, they will not get line numbers. Sorry, but I do not dare to change \interlinepenalty globally, nor do I want to redefine the display math environments here.

display math

64 See the subsection below, for a wrapper enviroment to make it work. But 65 that requires to wrap each and every display in your LaTeX source. 66 The next two commands are provided to turn on line numbering in 67 a specific mode. Please note the difference: for pagewise numbering, 68 \linenumbers comes first to inhibit it from seeing optional arguments, since 69 re-/presetting the counter is useless.

 65 \def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}
 66 \def\runninglinenumbers{\setrunninglinenumbers\linenumbers}

70 Finally, it is a L^{A}TEX style, so we provide for the use of environments, includ-71 ing the suppression of the following paragraphs indentation.

 67 \@namedef{linenumbers*}{\par\linenumbers*}
 68 \@namedef{runninglinenumbers*}{\par\runninglinenumbers*}

69

 70 \def\endlinenumbers{\par\@endpetrue}
 71 \let\endrunninglinenumbers\endlinenumbers
 72 \let\endpagewiselinenumbers\endlinenumbers
 73 \expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers
 74 \expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers
 75 \let\endnolinenumbers\endlinenumbers

72 3.1 Display math

73 Now we tackle the problem to get display math working. There are different 74 options.

75 1. Precede every display math with a \par. Not too good.

76 2. Change \interlinepenalty and associates globally. Unstable.

77 3. Wrap each display math with a {linenomath} environment.

78 We’ll go for option 3. See if it works:

display math  (1)

79 The star form {linenomath*} should also number the lines of the display 80 itself,

multi  line  (2) 81
display  math  (3) 82
\begin{matrix}with \\ array \end{matrix}  (4) 83

84 including multline displays. 85 First, here are two macros to turn on linenumbering on paragraphs pre-86 ceeding displays, with numbering the lines of the display itself, or without. 87 The \ifx.. tests if line numbering is turned on. It does not harm to add 88 these wrappers in sections that are not numbered. Nor does it harm to wrap 89 a display twice, e.q, in case you have some {equation}s wrapped explicitely, 90 and later you redefine \equation to do it automatically.

76 \newcommand\linenomathNonumbers{%
77 \ifx\@@par\@@@par\else
78  \ifnum\interlinepenalty>-\linenopenaltypar
79  \advance\interlinepenalty \linenopenalty
80  \advance\predisplaypenalty \linenopenalty
81  \fi
82 \fi
83 \ignorespaces

84 } 85

86 \newcommand\linenomathWithnumbers{%
87 \ifx\@@par\@@@par\else
88  \ifnum\interlinepenalty>-\linenopenaltypar
89  \advance\interlinepenalty \linenopenalty
90  \advance\predisplaypenalty \linenopenalty
91  \advance\postdisplaypenalty \linenopenalty
92  \advance\interdisplaylinepenalty \linenopenalty
93  \fi
94 \fi
95 \ignorespaces

96 }

91 The {linenomath} environment has two forms, with and without a star. The 92 following two macros define the environment, where the stared/non-stared 93 form does/doesn’t number the lines of the display or vice versa.

97 \newcommand\linenumberdisplaymath{%
98 \def\linenomath{\linenomathWithnumbers}%
99 \@namedef{linenomath*}{\linenomathNonumbers}%

100 } 101

102 \newcommand\nolinenumberdisplaymath{%
103 \def\linenomath{\linenomathNonumbers}%
104 \@namedef{linenomath*}{\linenomathWithnumbers}%

105 } 106

107 \let\endlinenomath\@ignoretrue
108 \expandafter\let\csname endlinenomath*\endcsname\endlinenomath

94 The default is not to number the lines of a display. But the package option 95 displaymath may be used to switch that behavior.

109 \nolinenumberdisplaymath

96 4 Line number references

97 The only way to get a label to a line number in a paragraph is to ask the 98 output routine to mark it.

99 We use the marginpar mechanism to hook to \output for a second time. 100 Marginpars are floats with number −1, we fake marginpars with No −2. 101 Originally, every negative numbered float was considered to be a marginpar. 102 The float box number \@currbox is used to transfer the label name in a 103 macro called \@LNL@⟨box-number⟩. 104 A \newlabel is written to the aux-file. The reference is to 105 \theLineNumber, not \thelinenumber. This allows to hook in, as done 106 below for pagewise line numbering.

 110 \let\@LN@addmarginpar\@addmarginpar
 111 \def\@addmarginpar{%
 112 \ifnum\count\@currbox>-2\relax
 113  \@LN@addmarginpar
 114 \else
 115  \@cons\@freelist\@currbox
 116  \protected@write\@auxout{}{%
 117  \string\newlabel
 118  {\csname @LNL@\the\@currbox\endcsname}%
 119  {{\theLineNumber}{\thepage}}}%
 120 \fi}

107 4.1 The linelabel command

108 To refer to a place in line \ref{⟨foo⟩} at page \pageref{⟨foo⟩} you place a 109 \linelabel{⟨foo⟩} at that place.

If you use this command outside a \linenumbers paragraph, you will See if it

works: This paragraph starts on page ??, line ??.

110

111 get references to some junky line numbers, sorry. But we don’t disable the 112 command, because only the \par at the end of a paragraph may decides 113 whether to print line numbers on this paragraph or not. A \linelabel may 114 appear earlier than \linenumbers. 115 \linelabel, via a fake float number −2, puts a \penalty into a 116 \vadjust, which triggers the pagebuilder after putting the current line to 117 the main vertical list. A \write is placed on the main vertical list, which 118 prints a reference to the current value of \thelinenumber and \thepage at 119 the time of the \shipout. 120 A \linelabel is allowed only in outer horizontal mode. In outer ver-121 tical mode we start a paragraph, and ignore trailing spaces (by fooling

122 \@esphack).

123 The argument of \linelabel is put into a macro with a name derived 124 from the number of the allocated float box. Much of the rest is dummy float 125 setup.

 121 \def\linelabel#1{%

122 \ifvmode
123  \ifinner \else
124  \leavevmode \@bsphack \@savsk\p@
125  \fi
126 \else
127  \@bsphack
128 \fi
129 \ifhmode
130  \ifinner
131  \@parmoderr
132  \else
133  \@floatpenalty -\@Mii
134  \@next\@currbox\@freelist
135  {\global\count\@currbox-2%
136  \expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%
137  {\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%
138  \begingroup
139  \setbox\@currbox \color@vbox \vbox \bgroup \end@float
140  \endgroup
141  \@ignorefalse \@esphack
142  \fi
143 \else
144  \@parmoderr
145 \fi

146 }

126 5 The appearance of the line numbers

  The line numbers are set as \tiny\sffamily\arabic{linenumber}, 10pt

left of the text. With options to place it right of the text, or … 129 … here are the hooks:

147 \def\makeLineNumberLeft{\hss\linenumberfont\LineNumber\hskip\linenumbersep}

148

149 \def\makeLineNumberRight{\linenumberfont\hskip\linenumbersep\hskip\textwidth
150  \hbox to\linenumberwidth{\hss\LineNumber}\hss}

151

152 \def\linenumberfont{\normalfont\tiny\sffamily}

153

154 \newdimen\linenumbersep
155 \newdimen\linenumberwidth

156

157 \linenumberwidth=10pt
158 \linenumbersep=10pt

Margin switching requires pagewise numbering mode, but choosing the left or right margin for the numbers always works.

159 \def\switchlinenumbers{\@ifstar
160  {\let\makeLineNumberOdd\makeLineNumberRight
161  \let\makeLineNumberEven\makeLineNumberLeft}%
162  {\let\makeLineNumberOdd\makeLineNumberLeft
163  \let\makeLineNumberEven\makeLineNumberRight}%

164 } 165

166 \def\setmakelinenumbers#1{\@ifstar
167 {\let\makeLineNumberRunning#1%
168 \let\makeLineNumberOdd#1%
169 \let\makeLineNumberEven#1}%
170 {\ifx\c@linenumber\c@runninglinenumber
171  \let\makeLineNumberRunning#1%
172 \else
173  \let\makeLineNumberOdd#1%
174  \let\makeLineNumberEven#1%
175 \fi}%

176 } 177

178 \def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}
179 \def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}

180

181 \leftlinenumbers*

132 \LineNumber is a hook which is used for the modulo stuff. It is the command to use for the line number, when you customizes \makeLineNumber. Use \thelinenumber to change the outfit of the digits.

135 We will implement two modes of operation:

• numbers running through (parts of) the text

• pagewise numbers starting over with one on top of each page.

138 Both modes have their own count register, but only one is allocated as a L^{A}TEX counter, with the attached facilities serving both.

182 \newcounter{linenumber}
183 \newcount\c@pagewiselinenumber
184 \let\c@runninglinenumber\c@linenumber

Only the running mode counter may be reset, or preset, for individual para-141 graphs. The pagewise counter must give a unique anonymous number for

each line.

185 \newcommand\resetlinenumber[1][1]{\c@runninglinenumber#1}

5.1 Running line numbers

144 Running mode is easy, \LineNumber and \theLineNumber produce

\thelinenumber, which defaults to \arabic{linenumber}, using the

\c@runninglinenumber counter. This is the default mode of operation.

186 \def\makeRunningLineNumber{\makeLineNumberRunning}

187

188 \def\setrunninglinenumbers{%
189 \def\theLineNumber{\thelinenumber}%
190 \let\c@linenumber\c@runninglinenumber
191 \let\makeLineNumber\makeRunningLineNumber

192 } 193

194 \setrunninglinenumbers\resetlinenumber

147 5.2 Pagewise line numbers

Difficult, if you think about it. The number has to be printed when there is no means to know on which page it will end up, except through the aux-file.

150 My solution is really expensive, but quite robust.

With version v2.00 the hashsize requirements are reduced, because we do not need one controlsequence for each line any more. But this costs some

153 computation time to find out on which page we are.

\makeLineNumber gets a hook to log the line and page number to the aux-file. Another hook tries to find out what the page offset is, and

156 subtracts it from the counter \c@linenumber. Additionally, the switch \ifoddNumberedPage is set true for odd numbered pages, false otherwise.

195 \def\setpagewiselinenumbers{%
196 \let\theLineNumber\thePagewiseLineNumber
197 \let\c@linenumber\c@pagewiselinenumber
198 \let\makeLineNumber\makePagewiseLineNumber

199 } 200

201 \def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber
202 \ifoddNumberedPage
203  \makeLineNumberOdd
204 \else
205  \makeLineNumberEven
206 \fi

207 }

Each numbered line gives a line to the aux file

159 \@LN{⟨line⟩}{⟨page⟩} very similar to the \newlabel business, except that we need an arabic representation of the page number, not what there might else be in \thepage.

208 \def\logtheLineNumber{\protected@write\@auxout{}{%
209 \string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}

162 From the aux-file we get one macro \LN@P⟨page⟩ for each page with line numbers on it. This macro calls four other macros with one argument each. These macros are dynamically defined to do tests and actions, to find out on

165 which page the current line number is located.

We need sort of a pointer to the first page with line numbers, initiallized to point to nothing:

210 \def\LastNumberedPage{first}
211 \def\LN@Pfirst{\nextLN\relax}

168 The four dynamic macros are initiallized to reproduce themselves in an \xdef

212 \let\lastLN\relax  % compare to last line on this page
213 \let\firstLN\relax % compare to first line on this page
214 \let\pageLN\relax  % get the page number, compute the linenumber
215 \let\nextLN\relax  % move to the next page

During the end-document run through the aux-files, we disable \@LN. I may put in a check here later, to give a rerun recommendation.

216 \AtEndDocument{\let\@LN\@gobbletwo}

171 Now, this is the tricky part. First of all, the whole definition of \@LN is grouped, to avoid accumulation on the save stack. Somehow \csname⟨cs⟩\endcsname pushes an entry, which stays after an \xdef to that

174 ⟨cs⟩.

If \LN@P⟨page⟩ is undefined, initialize it with the current page and line number, with the pointer-to-the-next-page pointing to nothing. And the

177 macro for the previous page will be redefined to point to the current one.

If the macro for the current page already exists, just redefine the lastline-number entry.

180 Finally, save the current page number, to get the pointer to the following page later.

217 \def\@LN#1#2{{\expandafter\@@LN\csname LN@P#2\endcsname{#1}{#2}}}
218 \def\@@LN#1#2#3{\ifx#1\relax
219  \expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1
220  \xdef#1{\lastLN{#2}\firstLN{#2}\pageLN{#3}\nextLN\relax}%
221 \else

222  \def\lastLN##1{\noexpand\lastLN{#2}}%
223  \xdef#1{#1}%
224 \fi
225 \gdef\LastNumberedPage{#3}}

The previous page macro gets its pointer to the current one, replacing the 183 \relax with the cs-token \LN@P⟨page⟩. In case of page number mismatch,

TEX will trip here, because the argument string for \nextLN is not \relax. I think it’s difficult to do a reasonable intercept here, because this is running

186 in an \xdef. Does \PackageError{} work in there?

226 \def\@@@LN#1#2{{\def\nextLN\relax{\noexpand\nextLN\noexpand#2}%
227  \xdef#1{#1}}}

Now, to print a line number, we need to find the page, where it resides. This will most probably be the one where the last one came from or maybe the

189 next. However, it can be a completely different one. We maintain a cache, which is let to the last accessed pages macro. But for now it is initialized to expand \LN@first, where the poiner to the first numbered page has been

192 stored in.

228 \def\NumberedPageCache{\LN@Pfirst}

To find out on which page the current \c@linenumber is, we define the four dynamic macros to do something usefull and execute the current cache macro.

195 \lastLN is run first, testing if the line number in question may be on a later page. If so, disable \firstLN, and go on to the next page via \nextLN.

229 \def\testLastNumberedPage#1{\ifnum#1<\c@linenumber
230  \let\firstLN\@gobble
231 \fi}

Else, if \firstLN finds out that we need an earlier page, we start over 198 from the beginning. Else, \nextLN will be disabled, and \pageLN will run

\gotNumberedPage with two arguments: the first line number on this page, and the page number.

232 \def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber
233  \def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%
234 \else
235  \let\nextLN\@gobble
236  \def\pageLN{\gotNumberedPage{#1}}%
237 \fi}

201 We start with \pageLN disabled and \nextLN defined to continue the search with the next page.

238 \def\testNumberedPage{%
239 \let\lastLN\testLastNumberedPage
240 \let\firstLN\testFirstNumberedPage
241 \let\pageLN\@gobble
242 \let\nextLN\testNextNumberedPage
243 \NumberedPageCache

244 }

When we switch to another page, we first have to make sure that it is there. 204 If we are done with the last page, we probably need to run TEX again, but

for the rest of this run, the cache macro will just return two zeros. This saves a lot of time, for example if you have half of an aux-file from an aborted run,

207 in the next run the whole page-list would be searched in vain again and again

for the second half of the document.

If there is another page, we iterate the search.

245 \def\testNextNumberedPage#1{\ifx#1\relax
246  \global\def\NumberedPageCache{\gotNumberedPage00}%
247  \PackageWarningNoLine{lineno}%
248  {Linenumber reference failed,
249  \MessageBreak  rerun to get it right}%
250 \else
251  \global\let\NumberedPageCache#1%
252 \fi
253 \testNumberedPage

254 }

To separate the official hooks from the internals there is this equivalence, to Let’s see if

it finds the label on page ??, line ??, and back here on page ??, line ??.

210

hook in later for whatever purpose:

255 \let\getLineNumber\testNumberedPage

So, now we got the page where the number is on. We establish if we are on 213 an odd or even page, and calculate the final line number to be printed.

256 \newif\ifoddNumberedPage

257

258 \def\gotNumberedPage#1#2{\oddNumberedPagefalse
259 \ifodd#2\relax\oddNumberedPagetrue\fi
260 \advance\c@linenumber 1\relax
261 \subtractlinenumberoffset{#1}%

262 }

You might want to run the pagewise mode with running line numbers, or you might not. It’s your choice:

263 \def\runningpagewiselinenumbers{%
264 \let\subtractlinenumberoffset\@gobble

265 } 266

267 \def\realpagewiselinenumbers{%
268 \def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%

269 } 270

271 \realpagewiselinenumbers

216 For line number references, we need a protected call to the whole procedure, with the requested line number stored in the \c@linenumber counter. This is what gets printed to the aux-file to make a label:

272 \def\thePagewiseLineNumber{\protect
273  \getpagewiselinenumber{\the\c@linenumber}}%

219 And here is what happens when the label is refered to:

274 \def\getpagewiselinenumber#1{{%
275 \c@linenumber #1\relax\testNumberedPage
276 \thelinenumber

277 }}

A summary of all per line expenses:

CPU: The \output routine is called for each line, and the page-search is 222 done.

DISK: One line of output to the aux-file for each numbered line

MEM: One macro per page. Great improvement over v1.02, which had one 225 control sequence per line in addition. It blew the hash table after some

five thousand lines.

5.3 Numbering modulo 5

228 Most users want to have only one in five lines numbered. \LineNumber is supposed to produce the outfit of the line number attached to the line, while \thelinenumber is used also for references, which should appear even if they

231 are not multiples of five.

278 \newcount\c@linenumbermodulo

279

280 \def\themodulolinenumber{{\@tempcnta\c@linenumber
281 \divide\@tempcnta\c@linenumbermodulo
282 \multiply\@tempcnta\c@linenumbermodulo
283 \ifnum\@tempcnta=\c@linenumber\thelinenumber\fi

284 }}

The user command to set the modulo counter:

285 \newcommand\modulolinenumbers[1][0]{%
286 \let\LineNumber\themodulolinenumber
287 \ifnum#1>1\relax
288 \c@linenumbermodulo#1\relax
289 \else\ifnum#1=1\relax
290 \def\LineNumber{\thelinenumber}%
291 \fi\fi

292 } 293

294 \setcounter{linenumbermodulo}{5}
295 \modulolinenumbers[1]

6 Package options 233

There is a bunch of package options, all of them executing only user com- 234 mands (see below). 235

Options left (right) put the line numbers on the left (right) margin. 236 This works in all modes. left is the default. 237

296 \DeclareOption{left}{\leftlinenumbers*}

297

298 \DeclareOption{right}{\rightlinenumbers*}

Option switch (switch*) puts the line numbers on the outer (inner) margin 238 of the text. This requires running the pagewise mode, but we turn off the 239 page offset subtraction, getting sort of running numbers again. The pagewise 240 option may restore true pagewise mode later. 241

299 \DeclareOption{switch}{\setpagewiselinenumbers
300  \switchlinenumbers
301  \runningpagewiselinenumbers}

302

303 \DeclareOption{switch*}{\setpagewiselinenumbers
304  \switchlinenumbers*%
305  \runningpagewiselinenumbers}

The options pagewise and running select the major linenumber mechanism. 242 running line numbers refer to a real counter value, which can be reset for 243 any paragraph, even getting multiple paragraphs on one page starting with 244 line number one. pagewise line numbers get a unique hidden number within 245 the document, but with the opportunity to establish the page on which they 246 finally come to rest. This allows the subtraction of the page offset, getting 247 the numbers starting with 1 on top of each page, and margin switching in 248 twoside formats becomes possible. The default mode is running. 249

The order of declaration of the options is important here pagewise must 250 come after switch, to overide running pagewise mode. running comes last, 251 to reset the running line number mode, e.g, after selecting margin switch 252 mode for pagewise running. Once more, if you specify all three of the options 253 [switch,pagewise,running], the result is almost nothing, but if you later 254 say \pagewiselinenumbers, you get margin switching, with real pagewise 255 line numbers. 256

306 \DeclareOption{pagewise}{\setpagewiselinenumbers
307  \realpagewiselinenumbers}

308

309 \DeclareOption{running}{\setrunninglinenumbers}

The option modulo causes only those linenumbers to be printed which are 257 multiples of five. 258

310 \DeclareOption{modulo}{\modulolinenumbers\relax}

The package option displaymath switches the behavior of the {linenomath} 259 environment with its star-form. Without this option, the {linenomath} 260 environment does not number the lines of the display, while the star-form 261 does. With this option, its just the opposite. 262

311 \DeclareOption{displaymath}{\linenumberdisplaymath}

312

313 \ProcessOptions

7 The final touch 263

There is one deadcycle for each line number. 264

314 \advance\maxdeadcycles 100

315

316 \endinput

8 The user commands 265

The user command to turn on and off line numbering are 266

\linenumbers  267

Turn on line numbering in the current mode. 268

\linenumbers*  269

and reset the line number to 1. 270

\linenumbers[⟨number⟩] 271

and start with ⟨number⟩. 272

\nolinenumbers  273

Turn off line numbering. 274

\runninglinenumbers*[⟨number⟩]  275

Turn on running line numbers, with the same optional arguments as 276 \linenumbers. The numbers are running through the text over page- 277 breaks. When you turn numbering off and on again, the numbers will 278 continue, except, of cause, if you ask to reset or preset the counter. 279

\pagewiselinenumbers  280

Turn on pagewise line numbers. The lines on each page are numbered 281 beginning with one at the first pagewise numbered line. 282

\resetlinenumber[⟨number⟩]  283

Reset [Set] the line number to 1 [⟨number⟩]. 284

\setrunninglinenumbers  285

Switch to running line number mode. Do not turn it on or off. 286

\setpagewiselinenumbers  287

Switch to pagewise line number mode. Do not turn it on or off. 288

\switchlinenumbers*  289

Causes margin switching in pagewise modes. With the star, put the 290 line numbers on the inner margin. 291

\leftlinenumbers*  292

\rightlinenumbers*  293

Set the line numbers in the left/right margin. With the star this works 294 for both modes of operation, without the star only for the currently 295 selected mode. 296

\runningpagewiselinenumbers  297

When using the pagewise line number mode, do not subtract the page 298 offset. This results in running line numbers again, but with the possibil- 299 ity to switch margins. Be careful when doing line number referencing, 300 this mode status must be the same while setting the paragraph and 301 during references. 302

\realpagewiselinenumbers  303
Reverses the effect of \runningpagewiselinenumbers.  304
\modulolinenumbers[⟨number⟩]  305

Give a number only to lines which are multiples of [⟨number⟩]. 306 If ⟨number⟩ is not specified, the current value in the counter 307 linenumbermodulo is retained. ⟨number⟩=1 turns this off without 308 changing linenumbermodulo. The counter is initialized to 5. 309

\linenumberdisplaymath  310

Number the lines of a display math in a {linenomath} environment, 311 but do not in a {linenomath*} environment. This is used by the 312 package option [displaymath]. 313

\nolinenumberdisplaymath  314

Do not Number the lines of a display math in a {linenomath} envi- 315 ronment, but do in a {linenomath*} environment. This is the default. 316

\linelabel  317

Set a \linelabel{⟨foo⟩} to the line number where this commands is 318 in. Refer to it with the L^{A}TEX referencing commands \ref{⟨foo⟩} and 319

\pageref{⟨foo⟩}.  320

The commands can be used globally, locally within groups or as environ- 321 ments. It is important to know that they take action only when the \par is 322 executed. The \end{⟨mode⟩linenumbers} commands provide a \par. Ex- 323 amples: 324

{\linenumbers ⟨text⟩ \par}  325

326

\begin{linenumbers}  327

⟨text⟩ 328

\end{linenumbers}  329

330

⟨paragraph⟩ {\linenumbers\par}  331

332

\linenumbers  333

⟨text⟩ \par 334

\nolinenumbers  335

336

\linenumbers  337
⟨paragraph⟩ {\nolinenumbers\par}  338

8.1 Customization hooks 339

There are several hooks to customize the appearance of the line numbers, 340 and some low level hooks for special effects. 341

\thelinenumber  342

This macro should give the representation of the line number in the 343 L^{A}TEX-counter linenumber. The default is provided by L^{A}TEX: 344

    \arabic{linenumber}  345
\makeLineNumberLeft  346

This macro is used to attach a line number to the left of the text page. 347 This macro should fill an \hbox to 0pt which will be placed at the 348 left margin of the page, with the reference point aligned to the line to 349 which it should give a number. Please use the macro \LineNumber to 350 refer to the line number. 351

The default definition is 352

    \hss\linenumberfont\LineNumber\hskip\linenumbersep  353
\makeLineNumberRight  354

Like \makeLineNumberLeft, but for line numbers on the right margin. 355

The default definition is 356

    \linenumberfont\hskip\linenumbersep\hskip\textwidth  357
    \hbox to\linenumberwidth{\hss\LineNumber}\hss  358

\linenumberfont  359

This macro is initialized to 360

    \normalfont\tiny\sffamily  361
\linenumbersep  362

This dimension register sets the separation of the linenumber to the 363 text. Default value is 10pt. 364

\linenumberwidth  365

This dimension register sets the width of the line number box on the 366 right margin. The distance of the right edge of the text to the right 367

edge of the line number is \linenumbersep + \linenumberwidth. The 368

default value is 10pt. 369

\theLineNumber (for wizards) 370

This macro is called for printing a \newlabel entry to the aux-file. 371 Its definition depends on the mode. For running line numbers it’s just 372 \thelinenumber, while in pagewise mode, the page offset subtraction 373 is done in here. 374

\makeLineNumber (for wizards)  375

This macro produces the line numbers. The definition depends 376 on the mode. In the running line numbers mode it just expands 377

\makeLineNumberLeft.  378

\LineNumber (for wizards) 379

This macro is called by \makeLineNumber to typeset the line number. 380 This hook is changed by the modulo mechanism. 381