26
lineno.sty v3.03 2000/03/10 1 2 AL A T E X style option to attach 3 Line numbers on paragraphs 4 Stephan I. B¨ottcher 5 [email protected] 6 Contents 7 1 Introduction 2 8 2 Put the line numbers to the lines 3 9 3 Control line numbering 5 10 3.1 Display math .......................... 7 11 4 Line number references 8 12 4.1 The linelabel command ..................... 9 13 5 The appearance of the line numbers 10 14 5.1 Running line numbers ...................... 11 15 5.2 Pagewise line numbers ..................... 12 16 5.3 Numbering modulo 5 ...................... 16 17 6 Package options 17 18 6.1 Package Extensions ....................... 18 19 6.1.1 displaymath ....................... 19 20 6.1.2 Line numbers in internal vertical mode ........ 19 21 6.1.3 Line number references with offset ........... 20 22 6.1.4 Numbered quotation environments .......... 21 23 6.1.5 Frame around a paragraph ............... 21 24 1

lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

lineno.sty v3.03 2000/03/101

2

A LATEX style option to attach3

Line numbers on paragraphs4

Stephan I. Bottcher5

[email protected]

Contents7

1 Introduction 28

2 Put the line numbers to the lines 39

3 Control line numbering 510

3.1 Display math . . . . . . . . . . . . . . . . . . . . . . . . . . 711

4 Line number references 812

4.1 The linelabel command . . . . . . . . . . . . . . . . . . . . . 913

5 The appearance of the line numbers 1014

5.1 Running line numbers . . . . . . . . . . . . . . . . . . . . . . 1115

5.2 Pagewise line numbers . . . . . . . . . . . . . . . . . . . . . 1216

5.3 Numbering modulo 5 . . . . . . . . . . . . . . . . . . . . . . 1617

6 Package options 1718

6.1 Package Extensions . . . . . . . . . . . . . . . . . . . . . . . 1819

6.1.1 displaymath . . . . . . . . . . . . . . . . . . . . . . . 1920

6.1.2 Line numbers in internal vertical mode . . . . . . . . 1921

6.1.3 Line number references with offset . . . . . . . . . . . 2022

6.1.4 Numbered quotation environments . . . . . . . . . . 2123

6.1.5 Frame around a paragraph . . . . . . . . . . . . . . . 2124

1

Page 2: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

7 The final touch 221

8 The user commands 232

8.1 Customization hooks . . . . . . . . . . . . . . . . . . . . . . 253

1 Introduction4

This package provides line numbers on paragraphs. After TEX has broken5

a paragraph into lines there will be line numbers attached to them, with6

the possibility to make references through the LATEX \ref, \pageref cross7

reference mechanism. This includes four issues:8

• attach a line number on each line,9

• create references to a line number,10

• control line numbering mode,11

• count the lines and print the numbers.12

The first two points are implemented through patches to the output routine.13

The third by redefining \par, \@par and \@@par. The counting is easy, as14

long as you want the line numbers run through the text. If they shall start15

over at the top of each page, the aux-file as well as TEXs memory have to16

carry a load for each counted line.17

I wrote this package for my wife Petra, who needs it for transcriptions18

of interviews. This allows her to precisely refer to passages in the text. It19

works well together with \marginpars, but not to well with displaymath.20

\footnotes are a problem, especially when they are split, but we may get21

there. lineno.sty works surprisingly well with other packages, for example,22

wrapfig.sty. So please try if it works with whatever you need, and if it23

does, please tell me, and if it does not, tell me as well, so I can try to fix it.24

This style option is written for LATEX2ε, later than November 1994, since25

we need the \protected@write macro.26

\NeedsTeXFormat{LaTeX2e}[1994/11/04]1

\ProvidesPackage{lineno}2

[2000/03/10 line numbers on paragraphs v3.03]3

2

Page 3: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

2 Put the line numbers to the lines1

The line numbers have to be attached by the output routine. We simply set2

the \interlinepenalty to -100000. The output routine will be called after3

each line in the paragraph, except the last, where we trigger by \par. The4

\linenopenalty is small enough to compensate a bunch of penalties (e.g.,5

with \samepage).6

\newcount\linenopenalty\linenopenalty=-1000004

\mathchardef\linenopenaltypar=250005

So let’s make a hook to \output, the direct way. The LATEX macro7

\@reinserts puts the footnotes back on the page.8

(New v3.01) \@reinserts badly screws up split footnotes. The bottom9

part is still on the recent contributions list, and the top part will be put back10

there after the bottom part. Thus, since lineno.sty does not play well with11

\inserts anyway, we can safely experiment with \holdinginserts, without12

making things much worse.13

Or that’s what I thought, but: Just activating \holdinginserts while14

doing the \par will not do the trick: The \output routine may be called15

for a real page break before all line numbers are done, and how can we get16

control over \holdinginserts at that point?17

Let’s try this: When the \output routine is run with \holdinginserts=318

for a real page break, then we reset \holdinginserts and restart \output.19

Then, again, how do we keep the remaining \inserts while doing further20

line numbers?21

If we find \holdinginserts=-3 we activate it again after doing \output.22

(/New v3.01)23

(New v3.02) To work with multicol.sty, the original output routine is24

now called indirectly, instead of being replaced. When multicol.sty changes25

\output, it is a toks register, not the real thing. (/New v3.02)26

\let\@LN@output\output6

\newtoks\output7

\output=\expandafter{\the\@LN@output}8

\@LN@output={%9

\LineNoTest10

\if@tempswa11

\LineNoHoldInsertsTest12

\if@tempswa13

\the\output14

\ifnum\holdinginserts=-315

\global\holdinginserts 316

\fi17

3

Page 4: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\else18

\global\holdinginserts-319

\unvbox\@cclv20

\ifnum\outputpenalty=10000\else21

\penalty\outputpenalty22

\fi23

\fi24

\else25

\MakeLineNo26

\fi27

}28

The float mechanism inserts \interlinepenaltys during \output. So care-1

fully reset it before going on. Else we get doubled line numbers on every2

float placed in horizontal mode, e.g, from \linelabel.3

Sorry, neither a \linelabel nor a \marginpar should insert a penalty,4

else the following linenumber could go to the next page. Nor should any5

other float. So let us suppress the \interlinepenalty altogether with the6

\@nobreak switch.7

Since (ltspace.dtx, v1.2p)[1996/07/26], the \@nobreaktrue does it’s job8

globally. We need to do it locally here.9

\def\LineNoTest{%29

\let\@@par\@@@par30

\ifnum\interlinepenalty<-\linenopenaltypar31

\advance\interlinepenalty-\linenopenalty32

\my@nobreaktrue33

\fi34

\@tempswatrue35

\ifnum\outputpenalty>-\linenopenaltypar\else36

\ifnum\outputpenalty>-175000\relax37

\@tempswafalse38

\fi39

\fi40

}41

42

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

44

\def\LineNoHoldInsertsTest{%45

\ifnum\holdinginserts=3\relax46

\@tempswafalse47

\fi48

}49

We have to return all the page to the current page, and add a box with the10

line number, without adding breakpoints, glue or space. The depth of our11

4

Page 5: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

line number should be equal to the previous depth of the page, in case the1

page breaks here, and the box has to be moved up by that depth.2

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

so we increment the line number after printing it. The macro4

\makeLineNumber produces the text of the line number, see section 5. Finally5

we put in the natural \interlinepenalty, except after the last line.6

\def\MakeLineNo{\@tempdima\dp\@cclv \unvbox\@cclv50

\sbox\@tempboxa{\hbox to\z@{\makeLineNumber}}%51

\stepcounter{linenumber}%52

\dp\@tempboxa=\@tempdima\ht\@tempboxa=\z@53

\nointerlineskip\kern-\@tempdima\box\@tempboxa54

\ifnum\outputpenalty=-\linenopenaltypar\else55

\@tempcnta\outputpenalty56

\advance\@tempcnta -\linenopenalty57

\penalty\@tempcnta58

\fi59

}60

3 Control line numbering7

The line numbering is controlled via \par. LATEX saved the TEX-primitive8

\par in \@@par. We push it one level further out, and redefine \@@par to9

insert the \interlinepenalty needed to trigger the line numbering. And10

we need to allow pagebreaks after a paragraph.11

New (2.05beta): the prevgraf test. A paragraph the ends with a displayed12

equation, a \noindent\par or wrapfig.sty produce empty paragraphs.13

These should not get a spurious line number via \linenopenaltypar.14

\let\@@@par\@@par61

\newcount\linenoprevgraf62

63

\def\linenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else64

\advance\interlinepenalty \linenopenalty65

\linenoprevgraf\prevgraf66

\global\holdinginserts3%67

\@@@par68

\ifnum\prevgraf>\linenoprevgraf69

\penalty-\linenopenaltypar70

\fi71

\kern\z@72

\global\holdinginserts0%73

\advance\interlinepenalty -\linenopenalty74

\fi\fi75

}76

5

Page 6: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

The basic commands to enable and disable line numbers. \@par and \par1

are only touched, when they are \let to \@@@par/\linenumberpar. The line2

number may be reset to 1 with the star-form, or set by an optional argument3

[〈number〉].4

\def\linenumbers{\let\@@par\linenumberpar77

\ifx\@par\@@@par\let\@par\linenumberpar\fi78

\ifx\par\@@@par\let\par\linenumberpar\fi79

\@ifnextchar[{\resetlinenumber}%]80

{\@ifstar{\resetlinenumber}{}}%81

}82

83

\def\nolinenumbers{\let\@@par\@@@par84

\ifx\@par\linenumberpar\let\@par\@@@par\fi85

\ifx\par\linenumberpar\let\par\@@@par\fi86

}87

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

display math

See the subsection below, for a wrapper enviroment to make it work. But5

that requires to wrap each and every display in your LaTeX source.6

The next two commands are provided to turn on line numbering in7

a specific mode. Please note the difference: for pagewise numbering,8

\linenumbers comes first to inhibit it from seeing optional arguments, since9

re-/presetting the counter is useless.10

\def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}88

\def\runninglinenumbers{\setrunninglinenumbers\linenumbers}89

Finally, it is a LATEX style, so we provide for the use of environments, includ-11

ing the suppression of the following paragraphs indentation.12

\@namedef{linenumbers*}{\par\linenumbers*}90

\@namedef{runninglinenumbers*}{\par\runninglinenumbers*}91

92

\def\endlinenumbers{\par\@endpetrue}93

\let\endrunninglinenumbers\endlinenumbers94

\let\endpagewiselinenumbers\endlinenumbers95

\expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers96

\expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers97

\let\endnolinenumbers\endlinenumbers98

6

Page 7: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

3.1 Display math1

Now we tackle the problem to get display math working. There are different2

options.3

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

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

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

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

display math (1)

The star form {linenomath*} should also number the lines of the display8

itself,9

multi line (2)10

display math (3)11

witharray (4)

12

including multline displays.13

First, here are two macros to turn on linenumbering on paragraphs pre-14

ceeding displays, with numbering the lines of the display itself, or without.15

The \ifx.. tests if line numbering is turned on. It does not harm to add16

these wrappers in sections that are not numbered. Nor does it harm to wrap17

a display twice, e.q, in case you have some {equation}s wrapped explicitely,18

and later you redefine \equation to do it automatically.19

\newcommand\linenomathNonumbers{%99

\ifx\@@par\@@@par\else100

\ifnum\interlinepenalty>-\linenopenaltypar101

\global\holdinginserts3%102

\advance\interlinepenalty \linenopenalty103

\advance\predisplaypenalty \linenopenalty104

\fi105

\fi106

\ignorespaces107

}108

109

\newcommand\linenomathWithnumbers{%110

\ifx\@@par\@@@par\else111

\ifnum\interlinepenalty>-\linenopenaltypar112

\global\holdinginserts3%113

7

Page 8: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\advance\interlinepenalty \linenopenalty114

\advance\predisplaypenalty \linenopenalty115

\advance\postdisplaypenalty \linenopenalty116

\advance\interdisplaylinepenalty \linenopenalty117

\fi118

\fi119

\ignorespaces120

}121

The {linenomath} environment has two forms, with and without a star. The1

following two macros define the environment, where the stared/non-stared2

form does/doesn’t number the lines of the display or vice versa.3

\newcommand\linenumberdisplaymath{%122

\def\linenomath{\linenomathWithnumbers}%123

\@namedef{linenomath*}{\linenomathNonumbers}%124

}125

126

\newcommand\nolinenumberdisplaymath{%127

\def\linenomath{\linenomathNonumbers}%128

\@namedef{linenomath*}{\linenomathWithnumbers}%129

}130

131

\def\endlinenomath{%132

\global\holdinginserts0133

\@ignoretrue134

}135

\expandafter\let\csname endlinenomath*\endcsname\endlinenomath136

The default is not to number the lines of a display. But the package option4

mathlines may be used to switch that behavior.5

\nolinenumberdisplaymath137

4 Line number references6

The only way to get a label to a line number in a paragraph is to ask the7

output routine to mark it.8

We use the marginpar mechanism to hook to \output for a second time.9

Marginpars are floats with number −1, we fake marginpars with No −2.10

Originally, every negative numbered float was considered to be a marginpar.11

The float box number \@currbox is used to transfer the label name in a12

macro called \@LNL@〈box-number〉.13

A \newlabel is written to the aux-file. The reference is to14

\theLineNumber, not \thelinenumber. This allows to hook in, as done15

below for pagewise line numbering.16

8

Page 9: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

(New v3.03) The \@LN@ExtraLabelItems are added for a hook to keep1

packages like {hyperref} happy. (/New v3.03)2

\let\@LN@addmarginpar\@addmarginpar138

\def\@addmarginpar{%139

\ifnum\count\@currbox>-2\relax140

\expandafter\@LN@addmarginpar141

\else142

\@cons\@freelist\@currbox143

\protected@write\@auxout{}{%144

\string\newlabel145

{\csname @LNL@\the\@currbox\endcsname}%146

{{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%147

\fi}148

149

\let\@LN@ExtraLabelItems\@empty150

4.1 The linelabel command3

To refer to a place in line \ref{〈foo〉} at page \pageref{〈foo〉} you place a4

\linelabel{〈foo〉} at that place.5

If you use this command outside a \linenumbers paragraph, you will See if itworks:Thisparagraphstarts onpage 9,line 6.

6

get references to some bogus line numbers, sorry. But we don’t disable the7

command, because only the \par at the end of a paragraph may decides8

whether to print line numbers on this paragraph or not. A \linelabel may9

legally appear earlier than \linenumbers.10

\linelabel, via a fake float number −2, puts a \penalty into a11

\vadjust, which triggers the pagebuilder after putting the current line to12

the main vertical list. A \write is placed on the main vertical list, which13

prints a reference to the current value of \thelinenumber and \thepage at14

the time of the \shipout.15

A \linelabel is allowed only in outer horizontal mode. In outer ver-16

tical mode we start a paragraph, and ignore trailing spaces (by fooling17

\@esphack).18

The argument of \linelabel is put into a macro with a name derived19

from the number of the allocated float box. Much of the rest is dummy float20

setup.21

\def\linelabel#1{%151

\ifvmode152

\ifinner \else153

\leavevmode \@bsphack \@savsk\p@154

\fi155

\else156

9

Page 10: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\@bsphack157

\fi158

\ifhmode159

\ifinner160

\@parmoderr161

\else162

\@floatpenalty -\@Mii163

\@next\@currbox\@freelist164

{\global\count\@currbox-2%165

\expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%166

{\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%167

\begingroup168

\setbox\@currbox \color@vbox \vbox \bgroup \end@float169

\endgroup170

\@ignorefalse \@esphack171

\fi172

\else173

\@parmoderr174

\fi175

}176

5 The appearance of the line numbers

The line numbers are set as \tiny\sffamily\arabic{linenumber}, 10ptleft of the text. With options to place it right of the text, or . . .3

. . . here are the hooks:

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

178

\def\makeLineNumberRight{\linenumberfont\hskip\linenumbersep\hskip\textwidth179

\hbox to\linenumberwidth{\hss\LineNumber}\hss}180

181

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

183

\newdimen\linenumbersep184

\newdimen\linenumberwidth185

186

\linenumberwidth=10pt187

\linenumbersep=10pt188

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

\def\switchlinenumbers{\@ifstar189

{\let\makeLineNumberOdd\makeLineNumberRight190

\let\makeLineNumberEven\makeLineNumberLeft}%191

{\let\makeLineNumberOdd\makeLineNumberLeft192

10

Page 11: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\let\makeLineNumberEven\makeLineNumberRight}%193

}194

195

\def\setmakelinenumbers#1{\@ifstar196

{\let\makeLineNumberRunning#1%197

\let\makeLineNumberOdd#1%198

\let\makeLineNumberEven#1}%199

{\ifx\c@linenumber\c@runninglinenumber200

\let\makeLineNumberRunning#1%201

\else202

\let\makeLineNumberOdd#1%203

\let\makeLineNumberEven#1%204

\fi}%205

}206

207

\def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}208

\def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}209

210

\leftlinenumbers*211

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

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.6

Both modes have their own count register, but only one is allocated as aLATEX counter, with the attached facilities serving both.

\newcounter{linenumber}212

\newcount\c@pagewiselinenumber213

\let\c@runninglinenumber\c@linenumber214

Only the running mode counter may be reset, or preset, for individual para-9

graphs. The pagewise counter must give a unique anonymous number foreach line.

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

5.1 Running line numbers12

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.15

11

Page 12: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\def\makeRunningLineNumber{\makeLineNumberRunning}216

217

\def\setrunninglinenumbers{%218

\def\theLineNumber{\thelinenumber}%219

\let\c@linenumber\c@runninglinenumber220

\let\makeLineNumber\makeRunningLineNumber221

}222

223

\setrunninglinenumbers\resetlinenumber224

5.2 Pagewise line numbers

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

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 some6

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, and9

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

\def\setpagewiselinenumbers{%225

\let\theLineNumber\thePagewiseLineNumber226

\let\c@linenumber\c@pagewiselinenumber227

\let\makeLineNumber\makePagewiseLineNumber228

}229

230

\def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber231

\ifoddNumberedPage232

\makeLineNumberOdd233

\else234

\makeLineNumberEven235

\fi236

}237

Each numbered line gives a line to the aux file12

\@LN{〈line〉}{〈page〉}

very similar to the \newlabel business, except that we need an arabic rep-resentation of the page number, not what there might else be in \thepage.15

\def\logtheLineNumber{\protected@write\@auxout{}{%238

\string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}239

12

Page 13: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

From the aux-file we get one macro \LN@P〈page〉 for each page with linenumbers 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 on3

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:6

\def\LastNumberedPage{first}240

\def\LN@Pfirst{\nextLN\relax}241

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

\let\lastLN\relax % compare to last line on this page242

\let\firstLN\relax % compare to first line on this page243

\let\pageLN\relax % get the page number, compute the linenumber244

\let\nextLN\relax % move to the next page245

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

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

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 that12

〈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 the15

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 last-

line-number entry.18

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

\def\@LN#1#2{{\expandafter\@@LN\csname LN@P#2\endcsname{#1}{#2}}}247

\def\@@LN#1#2#3{\ifx#1\relax248

\expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1249

\xdef#1{\lastLN{#2}\firstLN{#2}\pageLN{#3}\nextLN\relax}%250

\else251

\def\lastLN##1{\noexpand\lastLN{#2}}%252

\xdef#1{#1}%253

\fi254

\gdef\LastNumberedPage{#3}}255

13

Page 14: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

The previous page macro gets its pointer to the current one, replacing the\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.3

I think it’s difficult to do a reasonable intercept here, because this is runningin an \xdef. Does \PackageError{} work in there?

\def\@@@LN#1#2{{\def\nextLN\relax{\noexpand\nextLN\noexpand#2}%256

\xdef#1{#1}}}257

Now, to print a line number, we need to find the page, where it resides. This6

will most probably be the one where the last one came from or maybe thenext. 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 initialized9

to expand \LN@first, where the poiner to the first numbered page has beenstored in.

\def\NumberedPageCache{\LN@Pfirst}258

To find out on which page the current \c@linenumber is, we define the four12

dynamic macros to do something usefull and execute the current cache macro.\lastLN is run first, testing if the line number in question may be on a laterpage. If so, disable \firstLN, and go on to the next page via \nextLN.15

\def\testLastNumberedPage#1{\ifnum#1<\c@linenumber259

\let\firstLN\@gobble260

\fi}261

Else, if \firstLN finds out that we need an earlier page, we start overfrom the beginning. Else, \nextLN will be disabled, and \pageLN will run\gotNumberedPage with two arguments: the first line number on this page,18

and the page number.

\def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber262

\def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%263

\else264

\let\nextLN\@gobble265

\def\pageLN{\gotNumberedPage{#1}}%266

\fi}267

We start with \pageLN disabled and \nextLN defined to continue the searchwith the next page.21

14

Page 15: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\def\testNumberedPage{%268

\let\lastLN\testLastNumberedPage269

\let\firstLN\testFirstNumberedPage270

\let\pageLN\@gobble271

\let\nextLN\testNextNumberedPage272

\NumberedPageCache273

}274

When we switch to another page, we first have to make sure that it is there.If we are done with the last page, we probably need to run TEX again, butfor the rest of this run, the cache macro will just return two zeros. This saves3

a lot of time, for example if you have half of an aux-file from an aborted run,in the next run the whole page-list would be searched in vain again and againfor the second half of the document.6

If there is another page, we iterate the search.

\def\testNextNumberedPage#1{\ifx#1\relax275

\global\def\NumberedPageCache{\gotNumberedPage00}%276

\PackageWarningNoLine{lineno}%277

{Linenumber reference failed,278

\MessageBreak rerun to get it right}%279

\else280

\global\let\NumberedPageCache#1%281

\fi282

\testNumberedPage283

}284

To separate the official hooks from the internals there is this equivalence, to Let’s see ifit findsthe labelon page 9,line 6, andback hereon page15, line 8.

hook in later for whatever purpose:9

\let\getLineNumber\testNumberedPage285

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

\newif\ifoddNumberedPage286

287

\def\gotNumberedPage#1#2{\oddNumberedPagefalse288

\ifodd#2\relax\oddNumberedPagetrue\fi289

\advance\c@linenumber 1\relax290

\subtractlinenumberoffset{#1}%291

}292

You might want to run the pagewise mode with running line numbers, or12

you might not. It’s your choice:

15

Page 16: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\def\runningpagewiselinenumbers{%293

\let\subtractlinenumberoffset\@gobble294

}295

296

\def\realpagewiselinenumbers{%297

\def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%298

}299

300

\realpagewiselinenumbers301

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

\def\thePagewiseLineNumber{\protect302

\getpagewiselinenumber{\the\c@linenumber}}%303

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

\def\getpagewiselinenumber#1{{%304

\c@linenumber #1\relax\testNumberedPage305

\thelinenumber306

}}307

A summary of all per line expenses:

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

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 one9

control sequence per line in addition. It blew the hash table after somefive thousand lines.

5.3 Numbering modulo 512

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

are not multiples of five.

\newcount\c@linenumbermodulo308

309

\def\themodulolinenumber{{\@tempcnta\c@linenumber310

\divide\@tempcnta\c@linenumbermodulo311

\multiply\@tempcnta\c@linenumbermodulo312

\ifnum\@tempcnta=\c@linenumber\thelinenumber\fi313

}}314

16

Page 17: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

The user command to set the modulo counter:

\newcommand\modulolinenumbers[1][0]{%315

\let\LineNumber\themodulolinenumber316

\ifnum#1>1\relax317

\c@linenumbermodulo#1\relax318

\else\ifnum#1=1\relax319

\def\LineNumber{\thelinenumber}%320

\fi\fi321

}322

323

\setcounter{linenumbermodulo}{5}324

\modulolinenumbers[1]325

6 Package options2

There is a bunch of package options, all of them executing only user com-3

mands (see below).4

Options left (right) put the line numbers on the left (right) margin.5

This works in all modes. left is the default.6

\DeclareOption{left}{\leftlinenumbers*}326

327

\DeclareOption{right}{\rightlinenumbers*}328

Option switch (switch*) puts the line numbers on the outer (inner) margin7

of the text. This requires running the pagewise mode, but we turn off the8

page offset subtraction, getting sort of running numbers again. The pagewise9

option may restore true pagewise mode later.10

\DeclareOption{switch}{\setpagewiselinenumbers329

\switchlinenumbers330

\runningpagewiselinenumbers}331

332

\DeclareOption{switch*}{\setpagewiselinenumbers333

\switchlinenumbers*%334

\runningpagewiselinenumbers}335

The options pagewise and running select the major linenumber mechanism.11

running line numbers refer to a real counter value, which can be reset for12

any paragraph, even getting multiple paragraphs on one page starting with13

line number one. pagewise line numbers get a unique hidden number within14

the document, but with the opportunity to establish the page on which they15

finally come to rest. This allows the subtraction of the page offset, getting16

17

Page 18: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

the numbers starting with 1 on top of each page, and margin switching in 1

twoside formats becomes possible. The default mode is running. 2

The order of declaration of the options is important here pagewise must 3

come after switch, to overide running pagewise mode. running comes last, 4

to reset the running line number mode, e.g, after selecting margin switch 5

mode for pagewise running. Once more, if you specify all three of the options 6

[switch,pagewise,running], the result is almost nothing, but if you later 7

say \pagewiselinenumbers, you get margin switching, with real pagewise 8

line numbers. 9

\DeclareOption{pagewise}{\setpagewiselinenumbers336

\realpagewiselinenumbers}337

338

\DeclareOption{running}{\setrunninglinenumbers}339

The option modulo causes only those linenumbers to be printed which are 10

multiples of five. 11

\DeclareOption{modulo}{\modulolinenumbers\relax}340

The package option mathlines switches the behavior of the {linenomath} 12

environment with its star-form. Without this option, the {linenomath} 13

environment does not number the lines of the display, while the star-form 14

does. With this option, its just the opposite. 15

\DeclareOption{mathlines}{\linenumberdisplaymath}341

displaymath now calls for wrappers of the standard LaTeX display math 16

environment. This was previously done by mlineno.sty. 17

\let\do@mlineno\relax342

\DeclareOption{displaymath}{\let\do@mlineno\@empty}343

The hyperref package, via nameref, requires three more groups in the second 18

argment of a \newlabel. Well, why shouldn’t it get them? TODO: This 19

should be automagically detected \AtBeginDocument. 20

\DeclareOption{hyperref}{\def\@LN@ExtraLabelItems{{}{}{}}}344

345

\ProcessOptions346

6.1 Package Extensions 21

The extensions in this section were previously supplied in seperate .sty files. 22

18

Page 19: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

6.1.1 displaymath1

The standard LATEX display math environments are wrapped in a2

{linenomath} environment.3

\ifx\do@mlineno\@empty347

348

\renewenvironment{displaymath}349

{\linenomath\[}350

{\]\endlinenomath}351

352

\renewenvironment{equation}353

{\linenomath$$\refstepcounter{equation}}354

{\eqno\hbox{\@eqnnum}$$\endlinenomath}355

356

\let\LN@eqnarray\eqnarray357

\let\LN@endeqnarray\endeqnarray358

359

\renewenvironment{eqnarray}360

{\linenomath\LN@eqnarray}361

{\LN@endeqnarray\endlinenomath}362

363

\fi364

6.1.2 Line numbers in internal vertical mode4

The command \internallinenumbers adds line numbers in internal vertical5

mode, but with limitations: we assume fixed baseline skip.6

\def\internallinenumbers{\setrunninglinenumbers365

\let\@@par\internallinenumberpar366

\ifx\@par\@@@par\let\@par\internallinenumberpar\fi367

\ifx\par\@@@par\let\par\internallinenumberpar\fi368

\ifx\@par\linenumberpar\let\@par\internallinenumberpar\fi369

\ifx\par\linenumberpar\let\par\internallinenumberpar\fi370

\@ifnextchar[{\resetlinenumber}%]371

{\@ifstar{\let\c@linenumber\c@internallinenumber372

\c@linenumber\@ne}{}}%373

}374

375

\let\endinternallinenumbers\endlinenumbers376

\@namedef{internallinenumbers*}{\internallinenumbers*}377

\expandafter\let\csname endinternallinenumbers*\endcsname\endlinenumbers378

379

\newcount\c@internallinenumber380

\newcount\c@internallinenumbers381

382

\def\internallinenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else\@@@par383

19

Page 20: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\begingroup384

\c@internallinenumbers\prevgraf385

\setbox\@tempboxa\hbox{\vbox{\makeinternalLinenumbers}}%386

\dp\@tempboxa\prevdepth387

\ht\@tempboxa\z@388

\nobreak\vskip-\prevdepth389

\nointerlineskip\box\@tempboxa390

\endgroup391

\fi\fi392

}393

394

\def\makeinternalLinenumbers{\ifnum\c@internallinenumbers>0\relax395

\hbox to\z@{\makeLineNumber}\global\advance\c@linenumber\@ne396

\advance\c@internallinenumbers\m@ne397

\expandafter\makeinternalLinenumbers\fi398

}399

6.1.3 Line number references with offset 1

This extension to defines macros to refer to line numbers with a offset, e.g., 2

to refer to a line which cannot be labeled directly (display math). This was 3

formerly knows as rlineno.sty. 4

To refer to a pagewise line number with offset: 5

\linerefp[〈OFFSET 〉]{〈LABEL〉} 6

To refer to a running line number with offset: 7

\linerefr[〈OFFSET 〉]{〈LABEL〉} 8

To refer to a line number labeled in the same mode as currently selected: 9

\lineref[〈OFFSET 〉]{〈LABEL〉} 10

\newcommand\lineref{%400

\ifx\c@linenumner\c@runninglinenumner401

\expandafter\linerefr402

\else403

\expandafter\linerefp404

\fi405

}406

407

\newcommand\linerefp[2][\z@]{{%408

\let\@thelinenumber\thelinenumber409

\edef\thelinenumber{\advance\c@linenumber#1\relax\noexpand\@thelinenumber}%410

\ref{#2}%411

}}412

20

Page 21: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

This goes deep into LATEXs internals.1

\newcommand\linerefr[2][\z@]{{%413

\def\@@linerefadd{\advance\c@linenumber#1}%414

\expandafter\@setref\csname r@#2\endcsname415

\@linerefadd{#2}%416

}}417

418

\newcommand\@linerefadd[2]{\c@linenumber=#1\@@linerefadd\relax419

\thelinenumber}420

6.1.4 Numbered quotation environments2

The {numquote} and {numquotation} environments are like {quote} and3

{quotation}, except there will be line numbers.4

An optional argument gives the number to count from. A star * (inside5

or outside the closing }) prevent the reset of the line numbers. Default is to6

count from one.7

\newcommand\quotelinenumbers421

{\@ifstar\linenumbers{\@ifnextchar[\linenumbers{\linenumbers*}}}422

423

\newdimen\quotelinenumbersep424

\quotelinenumbersep=\linenumbersep425

\let\quotelinenumberfont\linenumberfont426

427

\newcommand\numquotelist428

{\leftlinenumbers429

\linenumbersep\quotelinenumbersep430

\let\linenumberfont\quotelinenumberfont431

\addtolength{\linenumbersep}{-\@totalleftmargin}%432

\quotelinenumbers433

}434

435

\newenvironment{numquote} {\quote\numquotelist}{\endquote}436

\newenvironment{numquotation} {\quotation\numquotelist}{\enquotation}437

\newenvironment{numquote*} {\quote\numquotelist*}{\endquote}438

\newenvironment{numquotation*}{\quotation\numquotelist*}{\enquotation}439

6.1.5 Frame around a paragraph8

The {bframe} environment draws a frame around some text, across page9

breaks, if necessary.10

This works only for plain text paragraphs, without special height lines.11

All lines must be \baselineskip apart, no display math.12

21

Page 22: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\newenvironment{bframe}440

{\par441

\@tempdima\textwidth442

\advance\@tempdima 2\bframesep443

\setbox\bframebox\hbox to\textwidth{%444

\hskip-\bframesep445

\vrule\@width\bframerule\@height\baselineskip\@depth\bframesep446

\advance\@tempdima-2\bframerule447

\hskip\@tempdima448

\vrule\@width\bframerule\@height\baselineskip\@depth\bframesep449

\hskip-\bframesep450

}%451

\hbox{\hskip-\bframesep452

\vrule\@width\@tempdima\@height\bframerule\@depth\z@}%453

\nointerlineskip454

\copy\bframebox455

\nobreak456

\kern-\baselineskip457

\runninglinenumbers458

\def\makeLineNumber{\copy\bframebox\hss}%459

}460

{\par461

\kern-\prevdepth462

\kern\bframesep463

\nointerlineskip464

\@tempdima\textwidth465

\advance\@tempdima 2\bframesep466

\hbox{\hskip-\bframesep467

\vrule\@width\@tempdima\@height\bframerule\@depth\z@}%468

}469

470

\newdimen\bframerule471

\bframerule=\fboxrule472

473

\newdimen\bframesep474

\bframesep=\fboxsep475

476

\newbox\bframebox477

7 The final touch 1

There is one deadcycle for each line number. 2

\advance\maxdeadcycles 100478

479

\endinput480

22

Page 23: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

8 The user commands1

The user command to turn on and off line numbering are2

\linenumbers3

Turn on line numbering in the current mode.4

\linenumbers*5

and reset the line number to 1.6

\linenumbers[〈number〉]7

and start with 〈number〉.8

\nolinenumbers9

Turn off line numbering.10

\runninglinenumbers*[〈number〉]11

Turn on running line numbers, with the same optional arguments as12

\linenumbers. The numbers are running through the text over page-13

breaks. When you turn numbering off and on again, the numbers will14

continue, except, of cause, if you ask to reset or preset the counter.15

\pagewiselinenumbers16

Turn on pagewise line numbers. The lines on each page are numbered17

beginning with one at the first pagewise numbered line.18

\resetlinenumber[〈number〉]19

Reset [Set] the line number to 1 [〈number〉].20

\setrunninglinenumbers21

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

\setpagewiselinenumbers23

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

\switchlinenumbers*25

Causes margin switching in pagewise modes. With the star, put the26

line numbers on the inner margin.27

\leftlinenumbers*28

23

Page 24: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\rightlinenumbers* 1

Set the line numbers in the left/right margin. With the star this works 2

for both modes of operation, without the star only for the currently 3

selected mode. 4

\runningpagewiselinenumbers 5

When using the pagewise line number mode, do not subtract the page 6

offset. This results in running line numbers again, but with the possibil- 7

ity to switch margins. Be careful when doing line number referencing, 8

this mode status must be the same while setting the paragraph and 9

during references. 10

\realpagewiselinenumbers 11

Reverses the effect of \runningpagewiselinenumbers. 12

\modulolinenumbers[〈number〉] 13

Give a number only to lines which are multiples of [〈number〉]. 14

If 〈number〉 is not specified, the current value in the counter 15

linenumbermodulo is retained. 〈number〉=1 turns this off without 16

changing linenumbermodulo. The counter is initialized to 5. 17

\linenumberdisplaymath 18

Number the lines of a display math in a {linenomath} environment, 19

but do not in a {linenomath*} environment. This is used by the 20

package option [displaymath]. 21

\nolinenumberdisplaymath 22

Do not Number the lines of a display math in a {linenomath} envi- 23

ronment, but do in a {linenomath*} environment. This is the default. 24

\linelabel 25

Set a \linelabel{〈foo〉} to the line number where this commands is 26

in. Refer to it with the LATEX referencing commands \ref{〈foo〉} and 27

\pageref{〈foo〉}. 28

The commands can be used globally, locally within groups or as environ- 29

ments. It is important to know that they take action only when the \par is 30

executed. The \end{〈mode〉linenumbers} commands provide a \par. Ex- 31

amples: 32

24

Page 25: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

{\linenumbers 〈text〉 \par}1

2

\begin{linenumbers}3

〈text〉4

\end{linenumbers}5

6

〈paragraph〉 {\linenumbers\par}7

8

\linenumbers9

〈text〉 \par10

\nolinenumbers11

12

\linenumbers13

〈paragraph〉 {\nolinenumbers\par}14

8.1 Customization hooks15

There are several hooks to customize the appearance of the line numbers,16

and some low level hooks for special effects.17

\thelinenumber18

This macro should give the representation of the line number in the19

LATEX-counter linenumber. The default is provided by LATEX:20

\arabic{linenumber}21

\makeLineNumberLeft22

This macro is used to attach a line number to the left of the text page.23

This macro should fill an \hbox to 0pt which will be placed at the24

left margin of the page, with the reference point aligned to the line to25

which it should give a number. Please use the macro \LineNumber to26

refer to the line number.27

The default definition is28

\hss\linenumberfont\LineNumber\hskip\linenumbersep29

\makeLineNumberRight30

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

The default definition is32

\linenumberfont\hskip\linenumbersep\hskip\textwidth33

\hbox to\linenumberwidth{\hss\LineNumber}\hss34

25

Page 26: lineno.sty v3.03 2000/03/10 2 A LATEX style option to attach … · 2002. 5. 27. · 22 there. lineno.sty works surprisingly well with other packages, for example, 23 wrapfig.sty

\linenumberfont 1

This macro is initialized to 2

\normalfont\tiny\sffamily 3

\linenumbersep 4

This dimension register sets the separation of the linenumber to the 5

text. Default value is 10pt. 6

\linenumberwidth 7

This dimension register sets the width of the line number box on the 8

right margin. The distance of the right edge of the text to the right 9

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

default value is 10pt. 11

\theLineNumber (for wizards) 12

This macro is called for printing a \newlabel entry to the aux-file. 13

Its definition depends on the mode. For running line numbers it’s just 14

\thelinenumber, while in pagewise mode, the page offset subtraction 15

is done in here. 16

\makeLineNumber (for wizards) 17

This macro produces the line numbers. The definition depends 18

on the mode. In the running line numbers mode it just expands 19

\makeLineNumberLeft. 20

\LineNumber (for wizards) 21

This macro is called by \makeLineNumber to typeset the line number. 22

This hook is changed by the modulo mechanism. 23

26