1  lineno.sty  v4.1c 2005/01/10

2

3 A L^{A}TEX package to attach 4 line numbers to paragraphs

Stephan I. Böttcher Uwe Lück

5

6 boettcher@physik.uni-kiel.de 7 http://www.contact-ednotes.sty.de.vu

8 Contents

9 1 Introductions 2 10 1.1 Introduction to versions v < 4 . . . . . . . . . . . . . . . . . . 2 11 1.2 Introduction to versions v4.00 and v4.1 (UL) . . . . . . . . . . 3 12 1.3 Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 13 1.4 Introductory code . . . . . . . . . . . . . . . . . . . . . . . . . 5

14 2 Put the line numbers to the lines 5 15 2.1 Basic code of lineno.sty \output . . . . . . . . . . . . . . . . . 6 16 2.2 \LineNoTest . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 17 2.3 \LineNoHoldInsertsTest . . . . . . . . . . . . . . . . . . . . 9 18 2.4 \MakeLineNo: Actually attach line number . . . . . . . . . . . 10

19 3 Control line numbering 13 20 3.1 Display math . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

21 4 Line number references 18 22 4.1 The \linelabel command . . . . . . . . . . . . . . . . . . . . 19

23 5 The appearance of the line numbers 21 24 5.1 Running line numbers . . . . . . . . . . . . . . . . . . . . . . 24 25 5.2 Pagewise line numbers . . . . . . . . . . . . . . . . . . . . . . 24 1 5.3 Twocolumn mode (New v3.06) . . . . . . . . . . . . . . . . . . 30 2 5.4 Numbering modulo m, starting at f . . . . . . . . . . . . . . . 30

3 6 Former package extensions 33 4 6.1 displaymath . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5 6.2 Line numbers in internal vertical mode . . . . . . . . . . . . . 34 6 6.3 Line number references with offset . . . . . . . . . . . . . . . . 36 7 6.4 Numbered quotation environments . . . . . . . . . . . . . . . 37 8 6.5 Frame around a paragraph . . . . . . . . . . . . . . . . . . . . 37

9 7 Move \vadjust items (New v4.00) 38 10 7.1 Redefining \vadjust . . . . . . . . . . . . . . . . . . . . . . . 38 11 7.2 Redefining the L^{A}TEX commands . . . . . . . . . . . . . . . . . 39 12 7.3 Reminder on obsoleteness . . . . . . . . . . . . . . . . . . . . 41

13 8 Package options 42 14 8.1 \linelabel in math mode . . . . . . . . . . . . . . . . . . . . 42 15 8.2 \linelabel in tabular environments . . . . . . . . . . . . . . 42 16 8.3 Switch among settings . . . . . . . . . . . . . . . . . . . . . . 43 17 8.4 A note on calling so many options . . . . . . . . . . . . . . . . 45 18 8.5 Execute options . . . . . . . . . . . . . . . . . . . . . . . . . . 46

19 9 The final touch 46

20 10 The user commands 46 21 10.1 Customization hooks . . . . . . . . . . . . . . . . . . . . . . . 49

22 1 Introductions

23 (New v4.00) Parts of former first section have been rendered separate sub-24 sections for package version v4.00. (/New v4.00)

25 1.1 Introduction to versions v < 4

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

30 • attach a line number on each line,

31 • create references to a line number, 1 • control line numbering mode,

2 • count the lines and print the numbers.

3 The first two points are implemented through patches to the output routine. 4 The third by redefining \par, \@par and \@@par. The counting is easy, as 5 long as you want the line numbers run through the text. If they shall start 6 over at the top of each page, the aux-file as well as TEXs memory have to 7 carry a load for each counted line. 8 I wrote this package for my wife Petra, who needs it for transcriptions 9 of interviews. This allows her to precisely refer to passages in the text. It 10 works well together with \marginpars, but not too well with displaymath. 11 \footnotes are a problem, especially when they are split, but we may get 12 there. (New v4.00 UL) Version v4.00 overcomes the problem, I believe. (/UL 13 /New v4.00) 14 lineno.sty works surprisingly well with other packages, for example, 15 wrapfig.sty. So please try if it works with whatever you need, and if it 16 does, please tell me, and if it does not, tell me as well, so I can try to fix it.

17 1.2 Introduction to versions v4.00 and v4.1 (UL)

18 lineno.sty has been maintained by Stephan until version v3.14. From ver-19 sion v4.00 onwards, maintenance is shifting towards Uwe Lück (UL), who is 20 the author of v4… code and of v4… changes in documentation. This came 21 about as follows. 22 Since late 2002, Christian Tapp and Uwe Lück have employed lineno.sty 23 for their ednotes.sty, a package supporting critical editions—cf.

24  http://www.homepages.ucl.ac.uk/~ucgadkw/edmac/

25 —while you find ednotes.sty and surrounding files in CTAN folder /macros/ 26 latex/contrib/ednotes. 27 Soon, some weaknesses of lineno.sty showed up, mainly since Christian’s 28 critical editions (using ednotes.sty) needed lots of \linelabels and foot-29 notes. (These weaknesses are due to weaknesses of L^{A}TEX’s \marginpar mech-30 anism that Stephan used for \linelabel.) So we changed some lineno.sty 31 definitions in some extra files, which moreover offered new features. We 32 sent these files to Stephan, hoping he would take the changes into lineno.sty. 33 However, he was too short of time. 34 Writing a TUGboat article on Ednotes in 2004, we hoped to reduce the 35 number of files in the Ednotes bundle and so asked Stephan again. Now he 36 generously offered maintenance to me, so I could execute the changes on my 37 own.

1 The improvements are as follows:

2 (i) Footnotes placement approaches intentions better (footnotes formerly 3 liked to pile up at late pages).

4 (ii) The number of \linelabels in one paragraph is no longer limited to 5 18.

6 (iii) \pagebreak, \nopagebreak, \vspace, and the star and optional ver-7 sions of \\ work as one would expect (section 7).

8 (iv) A command is offered which chooses the first line number to be printed 9 in the margin (subsection 5.4).

10 (v) (New v4.1) L^{A}TEX tabular environments (optionally) get line numbers 11 as well, and you can refer to them in the usual automatic way. (It may 12 be considered a shortcoming that, precisely, rows are numbered, not 13 lines. See subsection 8.2.)

14 (vi) We are moving towards referring to math items (subsection 8.1 and the 15 hooks in subsection 4.1). (/New v4.1)

16 (Thanks to Stephan for making this possible!) 17 You may trace the earlier developments of these changes by requesting 18 our files linenox0.sty, linenox1.sty, and lnopatch.sty. Most of our changes 19 have been in linenox0.sty. Our linenox1.sty has extended linenox0.sty for 20 one single purpose in a not very stable way. lnopatch.sty has done the first 21 line number thing referred to in case (iv) up to now. (New v4.1) Case (v) 22 earlier was provided by our edtab02.sty—now called ‘edtable.sty’. (/New 23 v4.1) 24 Ednotes moreover profits from Stephan’s offer with regard to the doc-25 umentation of our code which yielded these improvements formerly. This 26 documentation now becomes printable, being part of the lineno.sty docu-27 mentation. 28 Of course, Stephan’s previous lineno.sty versions were a great and inge-29 nious work and exhibit greatest TEXpertise. I never could have done this. I 30 learnt a lot in studying the code when Christian pointed out strange output 31 results and error messages, and there are still large portions of lineno.sty 32 which I don’t understand (consider only pagewise numbering of lines). For-33 tunately, Stephan has offered future help if needed.—My code for attach-34 ing line numbers to tabular environments (as mentioned above, now still 35 in edtable.sty) developed from macros which Stephan and Christian experi-36 mented with in December 2002. Stephan built the basics. (However, I then 37 became too proud to follow his advice only to use and modify longtable.sty.)

1 There are some issues concerning use of counters on which I don’t agree 2 with Stephan and where I would like to change the code if lineno.sty is “mine” 3 as Stephan offered. However, Stephan is afraid of compatibility problems 4 from which, in particular, his wife could suffer in the near future. So he 5 demanded that I change as little as possible for my first version. Instead of 6 executing changes that I plan I just offer my opinions at the single occasions. 7 I hope to get in touch this way with users who consider subtle features vital 8 which I consider strange. 9 On the other hand, the sections on improvements of the implementation 10 have been blown up very much and may be tiring and litte understandable 11 for mere users. These users may profit from the present presentation just by 12 jumping to sections 8 and 10. There is a user’s guide ulineno.tex which may 13 be even more helpful, but it has not been updated for a while.

14 1.3 Availability

15 In case you have found the present file otherwise than from CTAN: A recent 16 version and documentation of this package should be available from CTAN 17 folder /macros/latex/contrib/lineno. Or mail to one of the addresses at top 18 of file.

19 1.4 Introductory code

20 This style option is written for L^{A}TEX 2ε, November 1994 or later, since we

21 need the \protected@write macro.

22 (New v4.00) And we use \newcommand* for controlling length of user 23 macro arguments, which has been available since December 1994.

  1 \NeedsTeXFormat{LaTeX2e}[1994/12/01]
  2 \ProvidesPackage{lineno}
  3 [\filedate\space line numbers on paragraphs \fileversion]

24 (/New v4.00)

25 2 Put the line numbers to the lines

26 (New v4.00) This section contained the most basic package code previously. 27 For various purposes of version 4…, much of these basics have been to be 28 modified. Much of my (UL’s) reasoning on these modifications has been 29 to be reported. Sorry, the present section has been blown up awfully thus 30 and contains ramifications that may be difficult to trace. We add some 1 \subsection commands in order to cope with the new situation. (/New 2 v4.00)

3 2.1 Basic code of lineno.sty \output

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

 8 with \samepage).

9 (New v3.04) Longtable uses \penalty−30000. The lineno penalty range 10 was shrunk to −188000 …−32000. (/New v3.04) (UL) New values are listed 11 below (11111f.). (/UL)

  4 \newcount\linenopenalty\linenopenalty=-100000

12 (UL) Hm. It is never needed below that this is a counter.

13 \def\linenopenalty{-100000\relax} would do. (I guess this consumes

14 more memory, but it is more important to save counters than to save mem-15 ory.) I was frightened by -\linenopenalty below, but indeed TEX interprets 16 the string --100000 as 100000. Has any user or extension package writer ever 17 called \linenopenalty=xxx, or could I really change this?—The counter is 18 somewhat faster than the macro. Together with the compatibility question 19 this seems to support keeping the counter. (???) Note that Stephan chose 20 \mathchardef below, so his choice above seems to have been deliberate. 21 (/UL)

  5 \mathchardef\linenopenaltypar=32000

22 So let’s make a hook to \output, the direct way. The L^{A}TEX macro 23 \@reinserts puts the footnotes back on the page. 24 (New v3.01) \@reinserts badly screws up split footnotes. The bottom 25 part is still on the recent contributions list, and the top part will be put back 26 there after the bottom part. Thus, since lineno.sty does not play well with 27 \inserts anyway, we can safely experiment with \holdinginserts, without 28 making things much worse. 29 Or that’s what I thought, but: Just activating \holdinginserts while 30 doing the \par will not do the trick: The \output routine may be called 31 for a real page break before all line numbers are done, and how can we get 32 control over \holdinginserts at that point? 33 Let’s try this: When the \output routine is run with \holdinginserts=3 34 for a real page break, then we reset \holdinginserts and restart \output.

1 Then, again, how do we keep the remaining \inserts while doing further 2 line numbers? 3 If we find \holdinginserts=−3 we activate it again after doing \output. 4 (/New v3.01) 5 (New v3.02) To work with multicol.sty, the original output routine is 6 now called indirectly, instead of being replaced. When multicol.sty changes 7 \output, it is a toks register, not the real thing. (/New v3.02) 8 (New v4.00) Two further complications are added.

9 (i) Problems with footnotes formerly resulted from L^{A}TEX’s \@reinserts 10 in \@specialoutput which Stephan’s \linelabel called via the 11 \marginpar mechanism.

12 (ii) L^{A}TEX commands using \vadjust formerly didn’t work as one would 13 have hoped. The problem is as follows: Printing the line num-14 ber results from a box that the output routine inserts at the 15 place of the \interlinepenalty. \vadjust items appear above the 16 \interlinepenalty (TEXbook p. 105). So \pagebreak, e.g., for-17 merly sent the line number to the next page, while the penalty from 18 \nopagebreak could not tie the following line, since it was screened 19 off by the line number box.—Our trick is putting the \vadjust items 20 into a list macro from which the output routine transfers them into the 21 vertical list, below the line number box.

22 In this case (ii), like in case (i), footnotes would suffer if \holdinginserts 23 were non-positive. Indeed, in both cases (i) and (ii) we tackle the foot-24 note problem by extending that part of Stephan’s output routine that 25 is active when \holdinginserts is positive. This extension writes the 26 line number \newlabel to the .aux file (which was formerly done under 27 \holdinginserts = −3) and handles the \vadjust items.—To trigger 28 \output and its \linelabel or, resp., \vadjust part, the list of signal penal-29 ties started immediately before is increased here (first for \linelabel, second 30 for postponed \vadjust items):

  6 \mathchardef\@Mllbcodepen=11111
  7 \mathchardef\@Mppvacodepen=11112

31 (/New v4.00)

  8 \let\@LN@output\output
  9 \newtoks\output
 10 \output=\expandafter{\the\@LN@output}

32 Now we add two cases to Stephan’s output routine. (New v4.00)

11 \@LN@output={%
12  \LineNoTest
13  \if@tempswa

1 (New v4.00) We insert recognition of waiting \linelabel items—

14  \ifnum\outputpenalty=-\@Mllbcodepen
15  \WriteLineNo

2 —and of waiting \vadjust items:

16  \else
17  \ifnum\outputpenalty=-\@Mppvacodepen
18  \PassVadjustList
19  \else

3 Now we give control back to Stephan. (/New v4.00)

20  \LineNoHoldInsertsTest
21  \if@tempswa
22  \if@twocolumn\let\@makecol\@LN@makecol\fi
23  \the\output
24  \ifnum\holdinginserts=-3
25  \global\holdinginserts 3
26  \fi
27  \else
28  \global\holdinginserts-3
29  \unvbox\@cclv
30  \ifnum\outputpenalty=10000\else
31  \penalty\outputpenalty
32  \fi
33  \fi

4 (New v4.00) Two new \fis for the \linelabel and \vadjust tests—

34  \fi
35  \fi

5 —and the remaining is Stephan’s code again: (/New v4.00)

36  \else
37  \MakeLineNo
38  \fi

39 }

6 (New v4.00) Our new macros \WriteLineNo and \PassVadjustList will be 7 dealt with in sections 4 and 7.1. (/New v4.00)

1 2.2 \LineNoTest

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

 8 \@nobreak switch.

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

 40 \def\LineNoTest{%
 41 \let\@@par\@@@par
 42 \ifnum\interlinepenalty<-\linenopenaltypar
 43  \advance\interlinepenalty-\linenopenalty

11 (UL) Following line renders previous line obsolete, doesn’t it? (/UL)

 44  \my@nobreaktrue
 45  \fi
 46 \@tempswatrue
 47 \ifnum\outputpenalty>-\linenopenaltypar\else
 48  \ifnum\outputpenalty>-188000\relax
 49  \@tempswafalse
 50  \fi
 51  \fi

52 }

53

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

12 (UL) I would prefer \@LN@nobreaktrue.—I thought here were another case 13 of the save stack problem explained in TEXbook, p. 301, namely through 14 both local and global changing \if@nobreak. However, \my@nobreak is 15 called during \@LN@output only, while \@nobreaktrue is called by L^{A}TEX’s 16 \@startsection only. The latter never happens during \@LN@output. So 17 there is no local value of \if@nobreak on save stack when \@nobreaktrue 18 acts, since \the\@LN@output (where \@LN@output is a new name for the 19 original \output) is executed within a group (TEXbook p. 21). (/UL)

20 2.3 \LineNoHoldInsertsTest

21 (New v4.00) No change here! Just a separate subsection. (/New v4.00)

 55 \def\LineNoHoldInsertsTest{%
 56 \ifnum\holdinginserts=3\relax
 57  \@tempswafalse
 58 \fi

59 }

1 2.4 \MakeLineNo: Actually attach line number

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

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

7 so we increment the line number after printing it. The macro 8 \makeLineNumber produces the text of the line number, see section 5. 9 (UL) I needed a while to understand the sentence on incrementing. Cor-10 rectly: writing the \newlabel to the .aux file is triggered by the signal 11 penalty that \end@float inserts via \vadjust. However, this could be 12 changed by our new \PostponeVadjust. After \c@linenumber has been in-13 troduced as a L^{A}TEX counter, it might be preferable that it behaved like stan-14 dard L^{A}TEX counters which are incremented shortly before printing. But this 15 may be of little practical relevance in this case, as \c@linenumber is driven in 16 a very non-standard way.—However still, this behaviour of \c@linenumber 17 generates a problem with our edtable.sty. (/UL). 18 Finally we put in the natural \interlinepenalty, except after the last 19 line. 20 (New v3.10) Frank Mittelbach points out that box255 may be less deep 21 than the last box inside, so he proposes to measure the page depth with

22 \boxmaxdepth=\maxdimen. (/New v3.10)

23 (UL, New v4.00) We also resume the matter of \vadjust items that was 24 started in section 2.1. 25 TEX puts only nonzero interline penalties into the vertical list (TEXbook 26 p. 105), while lineno.sty formerly replaced the signal interline penalty by 27 something closing with an explicit penalty of the value that the interline 28 penalty would have without lineno.sty. This is usually 0. Now, explicit 29 vertical penalties can be very nasty with respect to \nopagebreak, e.g., a 30 low (even positive) \widowpenalty may force a widow where you explic-31 itly tried to forbid it by \nopagebreak (see explanation soon below). The 32 \nopagebreak we create here would never work if all those zero penalties were 33 present.—On the other hand, we cannot just omit Stephan’s zero penalties, 34 because TEX puts a penalty of 10000 after what lineno.sty inserts (TEXbook 1 p. 125). This penalty must be overridden to allow page breaks between or-2 dinary lines. To revive \nopagebreak, we therefore replace those zero (or 3 low) penalties by penalties that the user demanded by \nopagebreak.— 4 This mechanism is not perfect and does not exactly restore the original 5 L^{A}TEX working of \pagebreak and \nopagebreak. Viz., if there are sev-6 eral vertical penalties after a line which were produced by closely sitting 7 \[no]pagebreaks, without lineno.sty the lowest penalty would be effective 8 (cf. TEXbook exercise 14.10). Our mechanism, by contrast, chooses the last 9 user-set penalty of the line as the effective one. It would not be very difficult 10 to come more close to the original mechanism, but until someone urges us 11 we will cling to the present simple way. You may consider an advantage of 12 the difference between our mechanism and the original one that the user here 13 can actually override low penalties by \nopagebreak, which may be what a 14 lay L^{A}TEX user would expect.—Zero glue would do instead of zero penalty! 15 This could make things easier. Maybe next time. (/UL, /New v4.00)

 60 \def\MakeLineNo{%
 61 \boxmaxdepth\maxdimen\setbox\z@\vbox{\unvbox\@cclv}%
 62 \@tempdima\dp\z@ \unvbox\z@
 63 \sbox\@tempboxa{\hbox to\z@{\makeLineNumber}}%

16 (New v4.00) Previously,

17 %    \stepcounter{linenumber}%

18 followed. (Of course, there was no comment mark; I put it there to make 19 reading the actual code easy.)

20  (UL) I wondered about this.  Why not just

21 \global\advance\c@linenumber\@ne? section 5. OK, I keep it. (/UL)

22 But then, our edtable.sty and its longtable option should use it as well. 23 So use a shorthand supporting uniformity. You can even use it as a hook for

24 choosing \global\advance\c@linenumber\@ne instead of our choice.
 64 \stepLineNumber

25 (/New v4.00)

 65 \dp\@tempboxa=\@tempdima\ht\@tempboxa=\z@
 66 \nointerlineskip\kern-\@tempdima\box\@tempboxa

26 (New v4.00) The line number has now been placed (it may be invisible de-27 pending on the modulo feature), so we can insert the \vadjust items. We 28 cannot do this much later, because their right place is above the artificial 1 interline penalty which Stephan’s code will soon insert (cf. TEXbook p. 105). 2 The next command is just \relax if no \vadjust items have been accumu-3 lated for the current line. Otherwise it is a list macro inserting the \vadjust 4 items and finally resetting itself. (This is made in section 7.1 below.) If the 5 final item is a penalty, it is stored so it can compete with other things about 6 page breaking.

 67 \@LN@do@vadjusts
 68 \count@\lastpenalty

7 At this place,

 8 %    \ifnum\outputpenalty=-\linenopenaltypar\else

9 originally followed. We need something before the \else:

 69 \ifnum\outputpenalty=-\linenopenaltypar
 70  \ifnum\count@=\z@ \else

10 So final \pagebreak[0] or \nopagebreak[0] has no effect—but this will 11 make a difference after headings only, where nobody should place such a 12 thing anyway.

 71  \xdef\@LN@parpgbrk{\penalty\number\count@\relax
 72  \gdef\noexpand\@LN@parpgbrk{\kern\z@}}%

13 That penalty will replace former \kern\z@ in \linenumberpar, see sec-14 tion 3.—A few days earlier, I tried to send just a penalty value. However, the 15 \kern\z@ in \linenumberpar is crucial, as I then found out. See below.— 16 The final penalty is repeated, but this does no harm. (It would not be very 17 difficult to avoid the repeating, but it may even be less efficient.) It may be 18 repeated due to the previous \xdef, but it may be repeated as well below in 19 the present macro where artificial interline penalty is to be overridden.

 73  \fi
 74 \else

20 (/New v4.00)

 75  \@tempcnta\outputpenalty
 76  \advance\@tempcnta -\linenopenalty

21 (New v4.00)

22 %        \penalty\@tempcnta

1 followed previously. To give \nopagebreak a chance, we do

 77  \penalty \ifnum\count@<\@tempcnta \@tempcnta \else \count@ \fi

2 instead.—In linenox0.sty, the \else thing once was omitted. Sergei 3 Mariev’s complaint (thanks!) showed that it is vital (see comment before 4 \MakeLineNo). The remaining \fi from previous package version closes the

 5 \ifnum\outputpenalty. . . (/New v4.00)
 78 \fi

79 }

6 (New v4.00)

 80 \newcommand\stepLineNumber{\stepcounter{linenumber}}

7 For reason, see use above. (/New v4.00)

8 3 Control line numbering

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

16 \linenopenaltypar.
 81 \let\@@@par\@@par
 82 \newcount\linenoprevgraf

17 (UL) And needs \linenoprevgraf to be a counter? Perhaps there may 18 be a paragraph having thousands of lines, so \mathchardef doesn’t suffice 19 (really??). A macro ending on \relax might suffice, but would be somewhat 20 slow. I think I will use \mathchardef next time. Or has any user used

21 \linenoprevgraf? (/UL)
 83 \def\linenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else
 84  \advance\interlinepenalty \linenopenalty
 85  \linenoprevgraf\prevgraf
 86  \global\holdinginserts3%
 87  \@@@par
 88  \ifnum\prevgraf>\linenoprevgraf
 89  \penalty-\linenopenaltypar
 90  \fi

1 (New v4.00)

 2 %          \kern\z@

3 was here previously. What for? According to TEXbook p. 125, Stephan’s 4 interline penalty is changed into 10000. At the end of a paragraph, the 5 \parskip would follow that penalty of 10000, so there could be a page break 6 neither at the \parskip nor at the \baselineskip (TEXbook p. 110)—so 7 there could never be a page break between two paragraphs. So something 8 must screen off the 10000 penalty. Indeed, the \kern is a place to break. 9 (Stephan once knew this: see ‘allow pagebreaks’ above.) 10 Formerly, I tried to replace \kern\z@ by

11 %         \penalty\@LN@parpgpen\relax

12 —but this allows a page break after heading. So:

 91  \@LN@parpgbrk

13 After heading, \kern\z@ resulting from previous line (see below) is followed 14 by \write or \penalty10000, so causes no page break. 15 These and similar changes were formerly done by linenox1.sty. (/New 16 v4.00)

 92  \global\holdinginserts0%
 93  \advance\interlinepenalty -\linenopenalty
 94  \fi\fi

95 }

17 (New v4.00) Initialize \@LN@parpgbrk:

 96 \gdef\@LN@parpgbrk{\kern\z@}

18 (/New v4.00) 19 The basic commands to enable and disable line numbers. \@par and \par 20 are only touched, when they are \let to \@@@par/\linenumberpar. The line 21 number may be reset to 1 with the star-form, or set by an optional argument 22 [⟨number⟩]. 23 (New v4.00) We add \ifLineNumbers etc. since a number of our new ad-24 justments need to know whether linenumbering is active. This just provides a 25 kind of shorthand for \ifx\@@par\linenumberpar; moreover it is more sta-26 ble: who knows what may happen to \@@par?—A caveat: \ifLineNumbers 27 may be wrong. E.g., it may be \iffalse where it acts, while a \linenumbers 28 a few lines below—in the same paragraph—brings about that the line where 29 the \ifLineNumbers appears gets a marginal number.

97 \newif\ifLineNumbers \LineNumbersfalse

98

99 \def\linenumbers{\LineNumberstrue
100  \let\@@par\linenumberpar
1 % \def\linenumbers{\let\@@par\linenumberpar

2 (/New v4.00)

101  \ifx\@par\@@@par\let\@par\linenumberpar\fi
102  \ifx\par\@@@par\let\par\linenumberpar\fi
103  \@ifnextchar[{\resetlinenumber}%]
104  {\@ifstar{\resetlinenumber}{}}%

105 }

3 (New v4.00)

106 \def\nolinenumbers{\LineNumbersfalse
107 \let\@@par\@@@par
4 % \def\nolinenumbers{\let\@@par\@@@par

5 (/New v4.00)

108 \ifx\@par\linenumberpar\let\@par\@@@par\fi
109 \ifx\par\linenumberpar\let\par\@@@par\fi

110 }

6 (New v4.00) Moreover, it is useful to switch to \nolinenumbers in 7 \@arrayparboxrestore. We postpone this to section 7.2 where we’ll have 8 an appending macro for doing this. (/New v4.00)

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

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

111 \def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}
112 \def\runninglinenumbers{\setrunninglinenumbers\linenumbers}

1 Finally, it is a L^{A}TEX style, so we provide for the use of environments, includ-2 ing the suppression of the following paragraph’s indentation. 3 (UL) I’m drawing the following private thoughts of Stephan’s to publicity 4 so that others may think about them—or to remind myself of them in an 5 efficient way. (/UL)

6 % TO DO: add \par to \linenumbers, if called from an environment.
7 % To DO: add an \@endpe hack if \linenumbers are turned on
8 %        in horizontal mode. {\par\parskip\z@\noindent} or
9 %        something.
113 \@namedef{linenumbers*}{\par\linenumbers*}
114 \@namedef{runninglinenumbers*}{\par\runninglinenumbers*}

115

116 \def\endlinenumbers{\par\@endpetrue}
117 \let\endrunninglinenumbers\endlinenumbers
118 \let\endpagewiselinenumbers\endlinenumbers
119 \expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers
120 \expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers
121 \let\endnolinenumbers\endlinenumbers

10 3.1 Display math

11 Now we tackle the problem to get display math working. There are different 12 options.

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

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

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

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

display math  (1)

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

multi  line  (2) 19
display  math  (3) 20
\begin{matrix}with \\ array \end{matrix}  (4) 21

1 including multline displays. 2 First, here are two macros to turn on linenumbering on paragraphs pre-3 ceeding displays, with numbering the lines of the display itself, or without. 4 The \ifx.. tests if line numbering is turned on. It does not harm to add 5 these wrappers in sections that are not numbered. Nor does it harm to wrap 6 a display twice, e.q, in case you have some {equation}s wrapped explicitely, 7 and later you redefine \equation to do it automatically. 8 (UL) Newly, we could replace first lines by \ifLineNumbers. (/UL)

122 \newcommand\linenomathNonumbers{%
123 \ifx\@@par\@@@par\else
124  \ifnum\interlinepenalty>-\linenopenaltypar
125  \global\holdinginserts3%
126  \advance\interlinepenalty \linenopenalty
127  \advance\predisplaypenalty \linenopenalty
128  \fi
129 \fi
130 \ignorespaces

131 } 132

133 \newcommand\linenomathWithnumbers{%
134 \ifx\@@par\@@@par\else
135  \ifnum\interlinepenalty>-\linenopenaltypar
136  \global\holdinginserts3%
137  \advance\interlinepenalty \linenopenalty
138  \advance\predisplaypenalty \linenopenalty
139  \advance\postdisplaypenalty \linenopenalty
140  \advance\interdisplaylinepenalty \linenopenalty
141  \fi
142 \fi
143 \ignorespaces

144 }

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

145 \newcommand\linenumberdisplaymath{%
146 \def\linenomath{\linenomathWithnumbers}%
147 \@namedef{linenomath*}{\linenomathNonumbers}%

148 } 149

150 \newcommand\nolinenumberdisplaymath{%
151 \def\linenomath{\linenomathNonumbers}%
152 \@namedef{linenomath*}{\linenomathWithnumbers}%

153 } 154

155 \def\endlinenomath{%
156 \global\holdinginserts0
157 \@ignoretrue

158 }

159 \expandafter\let\csname endlinenomath*\endcsname\endlinenomath

1 The default is not to number the lines of a display. But the package option 2 mathlines may be used to switch that behavior.

160 \nolinenumberdisplaymath

3 4 Line number references

4 The only way to get a label to a line number in a paragraph is to ask the 5 output routine to mark it. 6 (New v4.00) The following two paragraphs don’t hold any longer, see 7 below. (/New v4.00)

 8 % We use the marginpar mechanism to hook to ~\output~ for a
 9 % second time.  Marginpars are floats with number $-1$, we
10 % fake marginpars with No $-2$. Originally, every negative
11 % numbered float was considered to be a marginpar.

12 %

13 % The float box number ~\@currbox~ is used to transfer the
14 % label name in a macro called ~\@LNL@~<box-number>.

15 A \newlabel is written to the aux-file. The reference is to \theLineNumber, 16 not \thelinenumber. This allows to hook in, as done below for pagewise 17 line numbering. 18 (New v3.03) The \@LN@ExtraLabelItems are added for a hook to keep 19 packages like {hyperref} happy. (/New v3.03) 20 (New v4.00) We fire the \marginpar mechanism, so we leave L^{A}TEX’s

21 \@addmarginpar untouched.
22 % \let\@LN@addmarginpar\@addmarginpar
23 % \def\@addmarginpar{%
24 %    \ifnum\count\@currbox>-2\relax
25 %      \expandafter\@LN@addmarginpar
26 %    \else
27 %      \@cons\@freelist\@currbox
28 %      \protected@write\@auxout{}{%
29 %          \string\newlabel
30 %             {\csname @LNL@\the\@currbox\endcsname}%
31 %             {{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%
32 %    \fi}

1 OK, we keep Stephan’s \@LN@ExtraLabelItems: (/New v4.00)

161 \let\@LN@ExtraLabelItems\@empty

2 (New v4.00) We imitate the \marginpar mechanism without using the 3 \@freelist boxes. \linelabel will indeed place a signal penalty 4 (\@Mllbcodepen, new), and it will put a label into some list macro 5 \@LN@labellist. A new part of the output routine will take the labels 6 from the list and will write \newlabels to the .aux file. 7 The following is a version of L^{A}TEX’s \@xnext.

162 \def\@LN@xnext#1\@lt#2\@@#3#4{\def#3{#1}\gdef#4{#2}}

8 This takes an item #1 from a list #4 into #3; to be used as

 9 \expandafter\@LN@xnext#4\@@#3#4. Our lists use \@lt after each item

10 for separating. Indeed, there will be another list macro which can appear as 11 argument #4, this will be used for moving \vadjust items (section 7.1). The 12 list for \linelabels is the following:

163 \global\let\@LN@labellist\@empty

13 The next is the new part of the output routine writing the \newlabel to the 14 .aux file. Since it is no real page output, the page is put back to top of the 15 main vertical list.

164 \def\WriteLineNo{%
165 \unvbox\@cclv
166 \expandafter \@LN@xnext \@LN@labellist \@@
167  \@LN@label \@LN@labellist
168 \protected@write\@auxout{}{\string\newlabel{\@LN@label}%
169  {{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%

170 }

16 (/New v4.00)

17 4.1 The \linelabel command

18 To refer to a place in line \ref{⟨foo⟩} at page \pageref{⟨foo⟩} you place a 19 \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 19, line 20.

20

21 get references to some bogus line numbers, sorry. But we don’t disable the 22 command, because only the \par at the end of a paragraph may decide 23 whether to print line numbers on this paragraph or not. A \linelabel may 24 legally appear earlier than \linenumbers.

25  \linelabel

 1 %, via a fake float number $-2$, %% new mechanism v4.00

2 puts a \penalty into a \vadjust, which triggers the pagebuilder after 3 putting the current line to the main vertical list. A \write is placed 4 on the main vertical list, which prints a reference to the current value of

 5 \thelinenumber and \thepage at the time of the \shipout.

6 A \linelabel is allowed only in outer horizontal mode. In outer ver-7 tical mode we start a paragraph, and ignore trailing spaces (by fooling

 8 \@esphack).

9 (New v4.00) We aim at relaxing the previous condition. We insert a hook

10 \@LN@mathhook and a shorthand \@LN@postlabel to support the mathrefs

11 option which allows \linelabel in math mode. 12 The next paragraph is no longer valid.

13 % The argument of ~\linelabel~ is put into a macro with a
14 % name derived from the number of the allocated float box.
15 % Much of the rest is dummy float setup.

16 (/New v4.00)

171 \def\linelabel#1{%
172 \ifvmode
173  \ifinner \else
174  \leavevmode \@bsphack \@savsk\p@
 175  \fi
176 \else
177  \@bsphack
 178 \fi
179 \ifhmode
180  \ifinner
181  \@parmoderr
182  \else

17 (New v4.00)

183  \@LN@postlabel{#1}%
18 %        \@floatpenalty -\@Mii
19 %        \@next\@currbox\@freelist
20 %            {\global\count\@currbox-2%
21 %             \expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%
22 %            {\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%
23 %        \begingroup
24 %            \setbox\@currbox \color@vbox \vbox \bgroup \end@float
25 %        \endgroup
26 %        \@ignorefalse \@esphack

1 (/New v4.00)

184  \@esphack

2 (New v4.00) The \@ignorefalse was appropriate before because the

3 \@Esphack in \end@float set \@ignoretrue. Cf. L^{A}TEX’s \@xympar. (/New

4 v4.00)

185  \fi
186 \else

5 (New v4.00)

187  \@LN@mathhook{#1}%
6 %     \@parmoderr

7 Instead of complaining, you may just do your job. (/New v4.00)

188 \fi

189 }

8 (New v4.00) The shorthand just does what happened with linenox0.sty before 9 ednmath0.sty (New v4.1: now mathrefs option) appeared, and the hook is 10 initialized to serve the same purpose. So errors come just where Stephan had 11 built them in, and this is just the L^{A}TEX \marginpar behaviour.

190 \def\@LN@postlabel#1{\g@addto@macro\@LN@labellist{#1\@lt}%
191  \vadjust{\penalty-\@Mllbcodepen}}
192 \def\@LN@mathhook#1{\@parmoderr}

12 (/New v4.00)

13 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 … 16 … here are the hooks:

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

194

195 \def\makeLineNumberRight{\linenumberfont\hskip\linenumbersep\hskip\columnwidth
196  \hbox to\linenumberwidth{\hss\LineNumber}\hss}

197

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

200 \newdimen\linenumbersep
201 \newdimen\linenumberwidth

202

203 \linenumberwidth=10pt
204 \linenumbersep=10pt

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

205 \def\switchlinenumbers{\@ifstar
206  {\let\makeLineNumberOdd\makeLineNumberRight
207  \let\makeLineNumberEven\makeLineNumberLeft}%
208  {\let\makeLineNumberOdd\makeLineNumberLeft
209  \let\makeLineNumberEven\makeLineNumberRight}%

210 } 211

212 \def\setmakelinenumbers#1{\@ifstar
213 {\let\makeLineNumberRunning#1%
214 \let\makeLineNumberOdd#1%
215 \let\makeLineNumberEven#1}%
216 {\ifx\c@linenumber\c@runninglinenumber
217  \let\makeLineNumberRunning#1%
218 \else
219  \let\makeLineNumberOdd#1%
220  \let\makeLineNumberEven#1%
221 \fi}%

222 } 223

224 \def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}
225 \def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}

226

227 \leftlinenumbers*

\LineNumber is a hook which is used for the modulo stuff. It is the command 4 to use for the line number, when you customize \makeLineNumber. Use

\thelinenumber to change the outfit of the digits.

We will implement two modes of operation:

7 • numbers running through (parts of) the text

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

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

228 \newcounter{linenumber}
229 \newcount\c@pagewiselinenumber
230 \let\c@runninglinenumber\c@linenumber

1 Only the running mode counter may be reset, or preset, for individual paragraphs. The pagewise counter must give a unique anonymous number for each line.

4  (UL) \newcounter{linenumber} was the only \newcounter in the

whole package. What is (or: “was”!) its purpose (i.e., Stephan’s reasoning)? (In fact, Stephan couldn’t remember his thoughts on on

7 similar questions—this is why I reason here as if I were a historian.) Firstly, there is the check whether the name has been introduced earlier—forget about this. Secondly, \thelinenumber is defined—

10 we could do this on our own. Note that \setcounter{linenumber} and

\addtocounter{linenumber} work even after \newcount\c@linenumber,

without \newcounter. So the final (main) difference to \newcount is that 13 \stepcounter{linenumber} resets L^{A}TEX counters ⟨foo⟩ that have been de-

clared by \newcounter{⟨foo⟩}[linenumber]. I wondered what this is needed for. It reminds me of “sublines” which are dealt with in John Lavagnino’s

16 and Dominik Wujastyk’s EDMAC.—This is the main reason why I think it is really better to keep the \stepcounter facility and, so, \newcounter. Finally, I found another reason for keeping it in section 5.4.

19 2004/12/11: The above list of effects of \newcounter{linenumber} is not complete. It must be added that this command causes \include to save the final line number (plus 1) of the \included part. This appears to me to

22 be useful. (/UL)

(New v4.00)

231 \newcommand*\resetlinenumber[1][1]{\c@runninglinenumber#1\relax}

Added \relax, being quite sure that this does no harm and is quite impor-25 tant, as with \setcounter etc. I consider this a bug fix (although perhaps

no user has ever had a problem with this). (/New v4.00)

(UL) I thought of incrementing \c@linenumber (which is the same

28 as \c@linenumber when \resetlinenumber has an effect at all) be-

fore printing (see what precedes \MakeLineNo above) and of adding

\advance\c@runninglinenumber\m@ne here correspondingly. Even if

31 incrementing is kept as it was: Now that we have decided to use \stepcounter{linenumber} for incrementing—in order to support “subordinate” counters \c@foo that have been introduced by

34 \newcounter{foo}[linenumber]—subordinate counters should be reset here as well. This could be done as follows.

% \newcommand\resetlinenumber[1][1]{%
37 %   \ifx\c@linenumber\c@runninglinenumber
%     \c@linenumber#1\relax

1 %     \advance\c@linenumber\m@ne
%     \stepcounter{linenumber}%
%   \else
4 %     \PackageError{lineno}%
%       {You can't reset line number in pagewise mode}%
%       {This should suffice.}%
7 %   \fi
% }

But be careful! Note that \resetlinenumber acts locally only, while 10 \stepcounter acts globally!—Well, this is a problem due to the received

\resetlinenumber! The received situation raises the danger of misusing save stack—TEXbook p. 301. \c@linenumber can hardly be incremented

13 in another way than globally! This is a very serious reason to make \resetlinenumber act globally!—I should have added \global right now, but here I am afraid of a serious compatibility problem. Stephan urged me to

16 avoid such problems this time. Moreover, note that section 10 says that the commands can be used “globally” as well as “locally within groups”. Are we allowed to change this? We might introduce a star form which acts globally

19 indeed. Or we just advise the user to precede the command with a \global. (/UL)

5.1 Running line numbers

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

232 \def\makeRunningLineNumber{\makeLineNumberRunning}

233

234 \def\setrunninglinenumbers{%
235 \def\theLineNumber{\thelinenumber}%
236 \let\c@linenumber\c@runninglinenumber
237 \let\makeLineNumber\makeRunningLineNumber

238 } 239

240 \setrunninglinenumbers\resetlinenumber

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

28 My solution is really expensive, but quite robust.

1 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 computation time to find out on which page we are.

4 \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 subtracts it from the counter \c@linenumber. Additionally, the switch

7 \ifoddNumberedPage is set true for odd numbered pages, false otherwise.

241 \def\setpagewiselinenumbers{%
242 \let\theLineNumber\thePagewiseLineNumber
243 \let\c@linenumber\c@pagewiselinenumber
244 \let\makeLineNumber\makePagewiseLineNumber

245 } 246

247 \def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber
248 \ifoddNumberedPage
249  \makeLineNumberOdd
250 \else
251  \makeLineNumberEven
252 \fi

253 }

Each numbered line gives a line to the aux file

\@LN{⟨line⟩}{⟨page⟩}

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

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

(New v4.00) As Daniel Doherty observed, the earlier line

13 %    \string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}

here may lead into an infinite loop when the user resets the page number (think of \pagenumbering, e.g.). (UL) Stephan and I briefly discussed

16 the matter and decided to introduce a “physical”-page counter to which \logtheLineNumber refers. It was Stephan’s idea to use \cl@page for reliably augmenting the “physical”-page counter. However, this relies on the

19 output routine once doing \stepcounter{page}. Before Stephan’s suggestion, I had thought of appending the stepping to L^{A}TEX’s \@outputpage.—So the macro definition ends as follows.

255 \string\@LN{\the\c@linenumber}{%
256  \noexpand\number\n@LN@truepage}}}

257

258 \newcount\n@LN@truepage
259 \g@addto@macro\cl@page{\global\advance\n@LN@truepage\@ne}

1 I had thought of offering more features of a L^{A}TEX counter. However, the user should better not have access to this counter. \c@page should suffice as a pagewise master counter.—To be sure, along the present lines the user can

4 manipulate \n@LN@truepage by \stepcounter{page}. E.g., she might do

this twice in order to manually insert a photography. Well, the physical-page counter will skip some values then, but this will not disable pagewise line

7 numbering.

The above usage of \g@addto@macro and \cl@page may be not as stable as Stephan intended. His proposal used \xdef directly. But he used

10 \cl@page as well, and who knows … And as to \g@addto@macro, I have

introduced it for list macros anyway. (/UL) (/New v4.00)

From the aux-file we get one macro \LN@P⟨page⟩ for each page with line 13 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 which page the current line number is located.

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

260 \def\LastNumberedPage{first}
261 \def\LN@Pfirst{\nextLN\relax}

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

262 \let\lastLN\relax  % compare to last line on this page
263 \let\firstLN\relax % compare to first line on this page
264 \let\pageLN\relax  % get the page number, compute the linenumber
265 \let\nextLN\relax  % move to the next page

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

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

Now, this is the tricky part. First of all, the whole definition of 22 \@LN is grouped, to avoid accumulation on the save stack. Somehow

\csname⟨cs⟩\endcsname pushes an entry, which stays after an \xdef to that ⟨cs⟩.

1 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 macro for the previous page will be redefined to point to the current one.

4 If the macro for the current page already exists, just redefine the last-

line-number entry.

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

267 \def\@LN#1#2{{\expandafter\@@LN
268  \csname LN@P#2C\@LN@column\expandafter\endcsname
269  \csname LN@PO#2\endcsname
270  {#1}{#2}}}

271

272 \def\@@LN#1#2#3#4{\ifx#1\relax
273  \ifx#2\relax\gdef#2{#3}\fi
274  \expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1
275  \xdef#1{\lastLN{#3}\firstLN{#3}\pageLN{#4}{\@LN@column}{#2}\nextLN\relax}%
276 \else
277  \def\lastLN##1{\noexpand\lastLN{#3}}%
278  \xdef#1{#1}%
279 \fi
280 \xdef\LastNumberedPage{#4C\@LN@column}}

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

281 \def\@@@LN#1#2{{\def\nextLN##1{\noexpand\nextLN\noexpand#2}%
282  \xdef#1{#1}}}

10 Now, to print a line number, we need to find the page, where it resides. This will most probably be the page where the last one came from, or maybe the next page. However, it can be a completely different one. We maintain a

13 cache, which is \let to the last page’s macro. But for now it is initialized to expand \LN@first, where the poiner to the first numbered page has been stored in.

283 \def\NumberedPageCache{\LN@Pfirst}

16 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. \lastLN is run first, testing if the line number in question may be on a later

19 page. If so, disable \firstLN, and go on to the next page via \nextLN.

284 \def\testLastNumberedPage#1{\ifnum#1<\c@linenumber
285  \let\firstLN\@gobble
286 \fi}

1 Else, if \firstLN finds out that we need an earlier page, we start over from the beginning. Else, \nextLN will be disabled, and \pageLN will run \gotNumberedPage with four arguments: the first line number on this col-

4 umn, the page number, the column number, and the first line on the page.

287 \def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber
288  \def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%
289 \else
290  \let\nextLN\@gobble
291  \def\pageLN{\gotNumberedPage{#1}}%
292 \fi}

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

293 \long\def \@gobblethree #1#2#3{}

294

295 \def\testNumberedPage{%
296 \let\lastLN\testLastNumberedPage
297 \let\firstLN\testFirstNumberedPage
298 \let\pageLN\@gobblethree
299 \let\nextLN\testNextNumberedPage
300 \NumberedPageCache

301 }

7 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, but for the rest of this run, the cache macro will just return four zeros. This saves a

10 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 again for the second half of the document.

13 If there is another page, we iterate the search.

302 \def\testNextNumberedPage#1{\ifx#1\relax
303  \global\def\NumberedPageCache{\gotNumberedPage0000}%
304  \PackageWarningNoLine{lineno}%
305  {Linenumber reference failed,
306  \MessageBreak  rerun to get it right}%
307 \else
308  \global\let\NumberedPageCache#1%
309 \fi
310 \testNumberedPage

311 }

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

it finds the label on page 19, line 20, and back here on page 28, line 14.

hook in later for whatever purpose:

28

312 \let\getLineNumber\testNumberedPage

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

313 \newif\ifoddNumberedPage
314 \newif\ifcolumnwiselinenumbers
315 \columnwiselinenumbersfalse

316

317 \def\gotNumberedPage#1#2#3#4{\oddNumberedPagefalse
318 \ifodd \if@twocolumn #3\else #2\fi\relax\oddNumberedPagetrue\fi
319 \advance\c@linenumber 1\relax
320 \ifcolumnwiselinenumbers
321  \subtractlinenumberoffset{#1}%
322 \else
323  \subtractlinenumberoffset{#4}%
324 \fi

325 }

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

326 \def\runningpagewiselinenumbers{%
327 \let\subtractlinenumberoffset\@gobble

328 } 329

330 \def\realpagewiselinenumbers{%
331 \def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%

332 } 333

334 \realpagewiselinenumbers

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

7 is what gets printed to the aux-file to make a label:

335 \def\thePagewiseLineNumber{\protect
336  \getpagewiselinenumber{\the\c@linenumber}}%

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

337 \def\getpagewiselinenumber#1{{%
338 \c@linenumber #1\relax\testNumberedPage
339 \thelinenumber

340 }}

A summary of all per line expenses:

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

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

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

7 5.3 Twocolumn mode (New v3.06)

Twocolumn mode requires another patch to the \output routine, in order to print a column tag to the .aux file.

341 \let\@LN@orig@makecol\@makecol
342 \def\@LN@makecol{%
343 \@LN@orig@makecol
344 \setbox\@outputbox \vbox{%
345  \boxmaxdepth \@maxdepth
346  \protected@write\@auxout{}{%
347  \string\@LN@col{\if@firstcolumn1\else2\fi}%

348 }%

349  \box\@outputbox
350 }% \vbox

351 } 352

353 \def\@LN@col#1{\def\@LN@column{#1}}
354 \@LN@col{1}

10 5.4 Numbering modulo m, starting at f

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

13 \thelinenumber is used also for references, which should appear even if they

are not multiples of five.

(New v4.00) Moreover, some users want to control which linenumber 16 should be printed first. Support of this is now introduced here.—numline.sty

by Michael Jaegermann and James Fortune offers controlling which final line numbers should not be printed. What is it good for? We ignore this here

19 until some user demands it.—Peter Wilson’s ledmac.sty offers much different choices of line numbers to be printed, due to Wayne Sullivan. (/New v4.00)

355 \newcount\c@linenumbermodulo

1 (UL) On my question why, e.g., \chardef would not have sufficed, Stephan couldn’t remember exactly; guessed that he wanted to offer L^{A}TEX counter facilities. However, the typical ones don’t come this way. So I’m quite sure

4 that I will change this next time.

However, I observed at least two times that users gave a very high value to \c@linenumbermodulo in order to suppress printing of the line number. One

7 of these users preferred an own way of handling line numbers, just wanted to use \linelabel and Ednotes features. Should we support this? I rather would like to advise them to \let\makeLineNumber\relax. (/UL)

10 (New v4.00) \themodulolinenumber waits for being declared

\LineNumber by \modulolinenumbers. (This has been so before, no

change.) Here is how it looked before:

13 % \def\themodulolinenumber{{\@tempcnta\c@linenumber
%   \divide\@tempcnta\c@linenumbermodulo
%   \multiply\@tempcnta\c@linenumbermodulo
16 %   \ifnum\@tempcnta=\c@linenumber\thelinenumber\fi
%   }}

(UL) This was somewhat slow. This arithmetic happens at every line. This 19 time I tend to declare an extra line counter (as opposed to my usual recom-

mendations to use counters as rarely as possible) which is stepped every line. It could be incremented in the same way as \n@truepage is incremented via

22 \cl@page! This is another point in favour of {linenumber} being a L^{A}TEX counter! When this new counter equals \c@linenumbermodulo, it is reset, and \thelinenumber is executed.—It gets much slower by my support of

25 controlling the first line number below. I should improve this.—On the other hand, time expense means very little nowadays, while the number of TEX counters still is limited.

28 For the same purpose, moreover, attaching the line number box could be intercepted earlier (in \MakeLineNo), without changing \LineNumber. However, this may be bad for the latter’s announcement as a wizard interface in

31 section 10. (/UL)

Here is the new code. It is very near to my lnopatch.sty code which introduced the first line number feature before.—I add starting with a \relax

34 which is so often recommended—without understanding this really. At least, it will not harm.—Former group braces appear as \begingroup/\endgroup here.

356 \def\themodulolinenumber{\relax
357 \ifnum\c@linenumber<\n@firstlinenumber
358 \else
359  \begingroup

360  \@tempcnta\c@linenumber
361  \advance\@tempcnta-\n@firstlinenumber
362  \divide\@tempcnta\c@linenumbermodulo
363  \multiply\@tempcnta\c@linenumbermodulo
364  \advance\@tempcnta\n@firstlinenumber
365  \ifnum\@tempcnta=\c@linenumber \thelinenumber \fi
366  \endgroup
367 \fi

368 }

1 (/New v4.00)

The user command to set the modulo counter:

369 \newcommand\modulolinenumbers[1][0]{%
370 \let\LineNumber\themodulolinenumber
371 \ifnum#1>1\relax
372  \c@linenumbermodulo#1\relax
373 \else\ifnum#1=1\relax
   %    \def\LineNumber{\thelinenumber}%

4 (New v4.00) I’m putting something here to enable \firstlinenumber with \c@linenumbermodulo = 1. With lnopatch.sty, a trick was offered for this purpose. It is now obsolete.

374  \def\LineNumber{\@LN@ifgreat\thelinenumber}%

7 (/New v4.00)

375 \fi\fi

376 }

(New v4.00) The default of \@LN@ifgreat is

377 \let\@LN@ifgreat\relax

The previous changes as soon as \firstlinenumber is used:

378 \newcommand*\firstlinenumber[1]{%
379 \chardef\n@firstlinenumber#1\relax

10 No counter, little values allowed only—OK?—(UL) The change is local— OK? The good thing is that \global\firstlinenumber{⟨number⟩} works. Moreover, \modulolinenumbers acts locally as well. (/UL)

380 \def\@LN@ifgreat{%
381  \ifnum\c@linenumber<\n@firstlinenumber
382  \expandafter \@gobble
383  \fi

384 }% 385 }

1 The default is 0. This is best for what one would expect from modulo printing.

386 \let\n@firstlinenumber=\z@

Note that the line numbers of the present section demonstrate the two 4 devices. (/New v4.00)

387 \setcounter{linenumbermodulo}{5}
388 \modulolinenumbers[1]

6 Former package extensions

The extensions in this section were previously supplied in separate .sty files.

7 6.1 displaymath

The standard L^{A}TEX display math environments are wrapped in a

{linenomath} environment.

10 (New 3.05) The [fleqn] option of the standard L^{A}TEX classes defines the display math environments such that line numbers appear just fine. Thus, we need not do any tricks when [fleqn] is loaded, as indicated by presents

13 of the \mathindent register. (/New 3.05)

(New 3.05a) for {eqnarray}s we rather keep the old trick. (/New 3.05a) (New 3.08) Wrap \[ and \] into {linenomath}, instead of

16 {displaymath}. Also save the definition of \equation, instead of replicating the current L^{A}TEX definition. (/New 3.08)

389 \ifx\do@mlineno\@empty
390 \@ifundefined{mathindent}{

391

392 \let\LN@displaymath\[
393 \let\LN@enddisplaymath\]
394 \renewcommand\[{\begin{linenomath}\LN@displaymath}
395 \renewcommand\]{\LN@enddisplaymath\end{linenomath}}

396

397 \let\LN@equation\equation

398 \let\LN@endequation\endequation
399 \renewenvironment{equation}
400  {\linenomath\LN@equation}
401  {\LN@endequation\endlinenomath}

402

403 }% \@ifundefined{mathindent}

404

405 \let\LN@eqnarray\eqnarray
406 \let\LN@endeqnarray\endeqnarray
407 \renewenvironment{eqnarray}
408  {\linenomath\LN@eqnarray}
409  {\LN@endeqnarray\endlinenomath}

410

411 \fi

1 (UL) Indeed. The L^{A}TEX macros are saved for unnumbered mode, which is detected by \linenomath. (/UL)

6.2 Line numbers in internal vertical mode

4 The command \internallinenumbers adds line numbers in internal vertical mode, but with limitations: we assume fixed baseline skip.

412 \def\internallinenumbers{\setrunninglinenumbers
413  \let\@@par\internallinenumberpar
414  \ifx\@par\@@@par\let\@par\internallinenumberpar\fi
415  \ifx\par\@@@par\let\par\internallinenumberpar\fi
416  \ifx\@par\linenumberpar\let\@par\internallinenumberpar\fi
417  \ifx\par\linenumberpar\let\par\internallinenumberpar\fi
418  \@ifnextchar[{\resetlinenumber}%]
419  {\@ifstar{\let\c@linenumber\c@internallinenumber
420  \c@linenumber\@ne}{}}%

421 } 422

423 \let\endinternallinenumbers\endlinenumbers
424 \@namedef{internallinenumbers*}{\internallinenumbers*}
425 \expandafter\let\csname endinternallinenumbers*\endcsname\endlinenumbers

426

427 \newcount\c@internallinenumber

(UL) This counter appears in \internallinenumbers only. It seems 7 to have been meant to be a version of \c@linenumber which is

changed only locally—see {internallinenumbers*}, where the initialization is local. However, Stephan incremented it globally then, see

10 below. Now, even this global incrementing would not increment the

  \c@linenumber version outside {internallinenumbers}—another reason to

1 consider \c@internallinenumber an “internal version” of \c@linenumber.

And another reason not to increment it globally, see below.—A drawback of this kind of “internal” seems to be: {internallinenumbers} cannot

4 be used to “continue line counting in internal vertical mode temporarily”, exactly because, e.g., \c@runninglinenumber has the same value after

\end{internallinenumbers} as it had at \begin{internallinenumbers}.

7 (/UL)

428 \newcount\c@internallinenumbers

429

430 \def\internallinenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else\@@@par
431  \begingroup
432  \c@internallinenumbers\prevgraf
433  \setbox\@tempboxa\hbox{\vbox{\makeinternalLinenumbers}}%
434  \dp\@tempboxa\prevdepth
435  \ht\@tempboxa\z@
436  \nobreak\vskip-\prevdepth
437  \nointerlineskip\box\@tempboxa
438  \endgroup
439  \fi\fi

440 } 441

442 \def\makeinternalLinenumbers{\ifnum\c@internallinenumbers>0\relax

(New v4.00)

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

10 followed here previously. Why no \stepcounter? OK, with unit \baselineskip there is no space for “sublines” anyway.—More se-

rious: \c@linenumber is \c@internallinenumber here, which in

13 {internallinenumbers*}has been initialized to be 1—locally! Save stack problem again. We could use \global depending on whether the star version is used or not. However, the “external” line counter is not affected anyway,

16 and the \global is not needed internally. So just drop it. I have no idea how a compatibility problem could arise.

443 \hbox to\z@{\makeLineNumber}\advance\c@linenumber\@ne

(/New v4.00)

444 \advance\c@internallinenumbers\m@ne
445 \expandafter\makeinternalLinenumbers\fi

446 }

1 6.3 Line number references with offset

This extension defines macros to refer to line numbers with an offset, e.g., to refer to a line which cannot be labeled directly (display math). This was

4 formerly knows as rlineno.sty.

To refer to a pagewise line number with offset:

\linerefp[⟨OFFSET ⟩]{⟨LABEL⟩}

7 To refer to a running line number with offset:

\linerefr[⟨OFFSET ⟩]{⟨LABEL⟩}

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

10 \lineref[⟨OFFSET ⟩]{⟨LABEL⟩}

447 \newcommand\lineref{%
448 \ifx\c@linenumber\c@runninglinenumber
449  \expandafter\linerefr
450 \else
451  \expandafter\linerefp
452 \fi

453 } 454

455 \newcommand\linerefp[2][\z@]{{%
456 \let\@thelinenumber\thelinenumber
457 \edef\thelinenumber{\advance\c@linenumber#1\relax\noexpand\@thelinenumber}%
458 \ref{#2}%

459 }}

This goes deep into L^{A}TEX’s internals.

460 \newcommand\linerefr[2][\z@]{{%
461 \def\@@linerefadd{\advance\c@linenumber#1}%
462 \expandafter\@setref\csname r@#2\endcsname
463 \@linerefadd{#2}%

464 }} 465

466 \newcommand\@linerefadd[2]{\c@linenumber=#1\@@linerefadd\relax
467  \thelinenumber}

(UL) Insert ‘LN’ in internal command names. (/UL)

1 6.4 Numbered quotation environments

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

{quotation}, except there will be line numbers. 4 An optional argument gives the number to count from. A star * (inside

or outside the closing }) prevent the reset of the line numbers. Default is to count from one.

468 \newcommand\quotelinenumbers
469 {\@ifstar\linenumbers{\@ifnextchar[\linenumbers{\linenumbers*}}}

470

471 \newdimen\quotelinenumbersep
472 \quotelinenumbersep=\linenumbersep
473 \let\quotelinenumberfont\linenumberfont

474

475 \newcommand\numquotelist
476 {\leftlinenumbers
477  \linenumbersep\quotelinenumbersep
478  \let\linenumberfont\quotelinenumberfont
479  \addtolength{\linenumbersep}{-\@totalleftmargin}%
480  \quotelinenumbers

481 } 482

483 \newenvironment{numquote}     {\quote\numquotelist}{\endquote}
484 \newenvironment{numquotation} {\quotation\numquotelist}{\endquotation}
485 \newenvironment{numquote*}    {\quote\numquotelist*}{\endquote}
486 \newenvironment{numquotation*}{\quotation\numquotelist*}{\endquotation}

7 6.5 Frame around a paragraph

The {bframe} environment draws a frame around some text, across page breaks, if necessary.

10 This works only for plain text paragraphs, without special height lines. All lines must be \baselineskip apart, no display math.

487 \newenvironment{bframe}
488 {\par
489 \@tempdima\textwidth
490 \advance\@tempdima 2\bframesep
491 \setbox\bframebox\hbox to\textwidth{%
492  \hskip-\bframesep
493  \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
494  \advance\@tempdima-2\bframerule
495  \hskip\@tempdima
496  \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
497  \hskip-\bframesep

498 }%

499 \hbox{\hskip-\bframesep
500  \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%
501 \nointerlineskip
502 \copy\bframebox
503 \nobreak
504 \kern-\baselineskip
505 \runninglinenumbers
506 \def\makeLineNumber{\copy\bframebox\hss}%

507 }

508 {\par
509 \kern-\prevdepth
510 \kern\bframesep
511 \nointerlineskip
512 \@tempdima\textwidth
513 \advance\@tempdima 2\bframesep
514 \hbox{\hskip-\bframesep
515  \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%

516 } 517

518 \newdimen\bframerule
519 \bframerule=\fboxrule

520

521 \newdimen\bframesep
522 \bframesep=\fboxsep

523

524 \newbox\bframebox

1 7 Move \vadjust items (New v4.00)

This section completes reviving \pagebreak, \nopagebreak, \vspace, and the star and optional form of \\. This was started in section 2.1 and resumed

4 in sections 2.4 and 3. The problem was explained in section 2.1: \vadjust items come out at a bad position, and the L^{A}TEX commands named before work with \vadjust indeed. Our solution was sketched there as well.

7 According to the caveat in section 3 concerning \ifLineNumbers, the L^{A}TEX commands enumerated may go wrong if you switch line numbering inside or at the end of a paragraph.

10 7.1 Redefining \vadjust

\vadjust will temporarily be changed into the following command.

525 \def\PostponeVadjust#1{%
526 \global\let\vadjust\@LN@@vadjust

1 This undoes a \global\let\vadjust\PostponeVadjust which will start

each of the refined L^{A}TEX commands. The \globals are most probably superfluous. They might be useful should one \vadjust appear in a group

4 starting after the change of \vadjust into \PostponeVadjust. (UL) Even the undoing may be superfluous, cf. discussion in section 7.2 below. (UL)

527 \vadjust{\penalty-\@Mppvacodepen}%
528 \g@addto@macro\@LN@vadjustlist{#1\@lt}%

529 }

530 \let\@LN@@vadjust\vadjust
531 \global\let\@LN@vadjustlist\@empty
532 \global\let\@LN@do@vadjusts\relax

These \globals are just to remind that all the changes of the strings af-7 ter \let should be \global (TEXbook p. 301). \@LN@vadjustlist col-

lects the \vadjust items of a paragraph. \PassVadjustList tears one \vadjust item for the current line out of \@LN@vadjustlist and puts it

10 into \@LN@do@vadjusts. The latter is encountered each line in \MakeLineNo (section 2.4), while those L^{A}TEX \vadjust commands will come rather rarely. So I decided that \@LN@do@vadjust is \relax until a \vadjust item is wait-

13 ing. In the latter case, \@LN@do@vadjusts is turned into a list macro which resets itself to \relax when the other contents have been placed in the vertical list.—\PassVadjustList is invoked by the output routine (section 2.1),

16 so the \box255 must be put back.

533 \def\PassVadjustList{%
534 \unvbox\@cclv
535 \expandafter \@LN@xnext \@LN@vadjustlist \@@
536  \@tempa \@LN@vadjustlist
537 \ifx\@LN@do@vadjusts\relax
538  \gdef\@LN@do@vadjusts{\global\let\@LN@do@vadjusts\relax}%
539 \fi
540 \expandafter \g@addto@macro \expandafter \@LN@do@vadjusts
541  \expandafter {\@tempa}%

542 }

7.2 Redefining the L^{A}TEX commands

Now we change \pagebreak etc. so that they use \PostponeVadjust in 19 place of \vadjust. We try to do this as independently as possible of

the implementation of the L^{A}TEX commands to be redefined. Therefore, we don’t just copy macro definition code from any single implementa-

22 tion (say, latest L^{A}TEX) and insert our changes, but attach a conditional

\global\let\vadjust\PostponeVadjust to their left ends in a way which

1 should work rather independantly of their actual code. However, \vadjust should be the primitive again after execution of the command. So the \global\let... may be used only if it’s guaranteed that a \vadjust is

4 near.—(UL) Sure? In line numbering mode, probably each \vadjust coming from a L^{A}TEX command should be \PostponeVadjust. \marginpars and floats seem to be the only cases which are not explicitly dealt with in

7 the present section. This would be a way to avoid \my@nobreaktrue! Of course, the \vadjusts that the present package uses then must be replaced by \@LN@@vadjust.—Maybe next time. (/UL)

10 The next command and something else will be added to the L^{A}TEX commands we are concerned with here.

543 \DeclareRobustCommand\@LN@changevadjust{%
544 \ifvmode\else\ifinner\else
545  \global\let\vadjust\PostponeVadjust
546 \fi\fi

547 }

(UL) What about math mode? Math display? Warn? (/UL) 13 \@tempa will now become a two place macro which adds first argu-

ment (single token), enclosed by \ifLineNumbers … \fi to the left of second argument. As long as we need it, we can’t use the star form of

16 \DeclareRobustCommand or the like, because AMS-L^{A}TEX uses \@tempa for

\@ifstar.
548 \def\@tempa#1#2{%
549 \expandafter \def \expandafter#2\expandafter{\expandafter
550  \ifLineNumbers\expandafter#1\expandafter\fi#2}

551 }

(UL) This \ifLineNumber can be fooled by \linenumbers ahead etc. It 19 might be better to place a signal penalty in any case and let the output

routine decide what to do. (/UL)

We use the occasion to switch off linenumbers where they don’t work 22 anyway and where we don’t want them, especially in footnotes:

552 \@tempa\nolinenumbers\@arrayparboxrestore

We hope this suffices … let’s check one thing at least:

553 \CheckCommand*\@parboxrestore{\@arrayparboxrestore\let\\\@normalcr}

1 Now for the main theme of the section. The next lines assume that \vspace, \pagebreak, and \nopagebreak use \vadjust whenever they occur outside vertical mode; moreover, that they don’t directly read an argument. Indeed

4 \pagebreak and \nopagebreak first call something which tests for a left

bracket ahead, while \vspace first tests for a star.

554 \@tempa\@LN@changevadjust\vspace
555 \@tempa\@LN@changevadjust\pagebreak
556 \@tempa\@LN@changevadjust\nopagebreak

\\, however, uses \vadjust only in star or optional form. We relax indepen-7 dency of implementation in assuming that \@normalcr is the fragile version

of \\ (and we use \@ifstar!). (Using a copy of \\ would be safer, but an ugly repetition of \protect.)

557 \DeclareRobustCommand\\{%
558 \ifLineNumbers
559  \expandafter \@LN@cr
560 \else
561  \expandafter \@normalcr
562 \fi

563 }

564 \def\@LN@cr{%
565 \@ifstar{\@LN@changevadjust\@normalcr*}%
566  {\@ifnextchar[{\@LN@changevadjust\@normalcr}\@normalcr}%

567 }

10 Moreover we hope that \newline never leads to a \vadjust, although names of some commands invoked by \\ contain newline. At last, this seems to have been OK since 1989 or even earlier.

13 Let’s have a few tests. Testing \pagebreak and \nopagebreak would

14 be too expensive here, but—oops!—we have just experienced a successful

15 \vspace*{.5\baselineskip}. A \\*[.5\baselineskip]

16 may look even more drastical, but this time we are happy about it. Note 17 that the line numbers have moved with the lines. Without our changes, one

line number would have “anticipated” the move of the next line, just as you

18

19 can observe it now. (/New v4.00)

7.3 Reminder on obsoleteness 20

(New v4.1) We have completed inclusion of the earlier extension packages 21 linenox0.sty, linenox1.sty, and lnopatch.sty. If one of them is loaded, though, 22 we produce an error message before something weird happens. We avoid 23 \newif because the switchings occur so rarely. 24

568 \AtBeginDocument{%
569 \let\if@LN@obsolete\iffalse
570 \@ifpackageloaded{linenox0}{\let\if@LN@obsolete\iftrue}\relax
571 \@ifpackageloaded{linenox1}{\let\if@LN@obsolete\iftrue}\relax
572 \@ifpackageloaded{lnopatch}{\let\if@LN@obsolete\iftrue}\relax
573 \if@LN@obsolete
574  \PackageError{lineno}{Obsolete extension package(s)}{%
575  With lineno.sty version 4.00 or later,\MessageBreak
576  linenox0/linenox1/lnopatch.sty must no longer be loaded.}%
 577 \fi

578 }

1 8 Package options

2 (New v4.1) The last heading formerly was the heading of what is now sub-3 section 8.3. The options declared there were said to execute user commands 4 only. This was wrong already concerning displaymath and hyperref. At 5 least, however, these options were no or almost no occasion to skip definitions 6 or allocations. This is different with the options that we now insert.

7 8.1 \linelabel in math mode

8 We have made some first steps towards allowing \linelabel in math mode. 9 Because our code for this is presently experimental, we leave it to the user to 10 decide for the experiment by calling option mathrefs. We are in a hurry now 11 and thus leave the code, explanations, and discussion in the separate package 12 ednmath0.sty. Maybe we later find the time to improve the code and move 13 the relevant content of ednmath0.sty to here. The optimal situation would 14 be to define \linelabel from the start so it works in math mode, omitting 15 the mathrefs option. 16 Actually, this package even provides adjustments for analogously allowing 17 ednotes.sty commands in math mode. Loading the package is postponed to 18 \AtBeginDocument when we know whether these adjustments are needed.

579 \DeclareOption{mathrefs}{\AtBeginDocument
580 {\RequirePackage{ednmath0}[2004/08/20]}}

19 8.2 \linelabel in tabular environments

20 We provide adjustments to make \linelabel work in some L^{A}TEX tabular 21 environments. We do this similarly as with with option mathrefs before. We 22 leave code and explanations in the separate package edtable.sty. This package provides adjustments for ednotes.sty as well. However, in the present case we 1 don’t try to avoid them unless ednotes.sty is loaded. Package option edtable 2 defines—by loading edtable.sty—an environment {edtable} which is able to 3 change some L^{A}TEX tabular environments with the desired effects. 4

This method doesn’t work with longtable.sty, however. To make up for 5 this, {longtable} is adjusted in a different way—and this happens only when 6 another lineno.sty option longtable is called. In this case, option edtable 7 needn’t be called explicitly: option longtable works as if edtable had been 8 called. 9

Now, we are convinced that vertical spacing around {longtable} works 10 wrongly—see L^{A}TEX bugs database tools/3180 and 3485, or see explanations 11 in the package ltabptch.sty (which is to be obtained from CTAN folder 12 /macros/latex/ltabptch). Our conviction is so strong that the longtable 13 option loads—after longtable.sty—the patch package ltabptch.sty. If the user 14 doesn’t want this (maybe preferring her own arrangement with the vertical 15 spacing), she can forbid it by calling nolongtablepatch. 16

The following code just collects some choices, which are then executed 17 in section 8.5. We use an \if... without \newif since \if...true and 18 \if...false would occur at most two times and only within the present 19

package. (\AtEndOfClass{\RequirePackage{edtable}} could be used in-  20

stead, I just overlooked this. Now I don’t change it because it allows to 21 change the version requirement at one place only.) 22

581 \let\if@LN@edtable\iffalse

582

583 \DeclareOption{edtable}{\let\if@LN@edtable\iftrue}

584

585 \DeclareOption{longtable}{\let\if@LN@edtable\iftrue
586 \PassOptionsToPackage{longtable}{edtable}}

587

588 \DeclareOption{nolongtablepatch}{%
589 \PassOptionsToPackage{nolongtablepatch}{edtable}}

(/New v4.1) 23

8.3 Switch among settings 24

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

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

590 \DeclareOption{left}{\leftlinenumbers*}

592 \DeclareOption{right}{\rightlinenumbers*}

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

593 \DeclareOption{switch}{\setpagewiselinenumbers
594  \switchlinenumbers
595  \runningpagewiselinenumbers}

596

597 \DeclareOption{switch*}{\setpagewiselinenumbers
598  \switchlinenumbers*%
599  \runningpagewiselinenumbers}

5 In twocolumn mode, we can switch the line numbers to the outer margin, 6 and/or start with number 1 in each column. Margin switching is covered by 7 the switch options.

600 \DeclareOption{columnwise}{\setpagewiselinenumbers
601  \columnwiselinenumberstrue
602  \realpagewiselinenumbers}

8 The options pagewise and running select the major linenumber mechanism. 9 running line numbers refer to a real counter value, which can be reset for 10 any paragraph, even getting multiple paragraphs on one page starting with 11 line number one. pagewise line numbers get a unique hidden number within 12 the document, but with the opportunity to establish the page on which they 13 finally come to rest. This allows the subtraction of the page offset, getting 14 the numbers starting with 1 on top of each page, and margin switching in 15 twoside formats becomes possible. The default mode is running. 16 The order of declaration of the options is important here pagewise must 17 come after switch, to overide running pagewise mode. running comes last, 18 to reset the running line number mode, e.g, after selecting margin switch 19 mode for pagewise running. Once more, if you specify all three of the options 20 [switch,pagewise,running], the result is almost nothing, but if you later 21 say \pagewiselinenumbers, you get margin switching, with real pagewise 22 line numbers.

603 \DeclareOption{pagewise}{\setpagewiselinenumbers
604  \realpagewiselinenumbers}

605

606 \DeclareOption{running}{\setrunninglinenumbers}

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

607 \DeclareOption{modulo}{\modulolinenumbers\relax}

The package option mathlines switches the behavior of the {linenomath} 3 environment with its star-form. Without this option, the {linenomath} 4 environment does not number the lines of the display, while the star-form 5 does. With this option, its just the opposite. 6

608 \DeclareOption{mathlines}{\linenumberdisplaymath}

displaymath now calls for wrappers of the standard LaTeX display math 7 environment. This was previously done by mlineno.sty. 8

609 \let\do@mlineno\relax
610 \DeclareOption{displaymath}{\let\do@mlineno\@empty}

The hyperref package, via nameref, requires three more groups in the sec- 9 ond argment of a \newlabel. Well, why shouldn’t it get them? (New 10 v3.07) The presence of the nameref package is now detected automatically 11 \AtBeginDocument. (/New v3.07) (Fixed in v3.09) We try to be smart, 12 and test \AtBeginDocument if the nameref package is loaded, but hyperref 13 postpones the loading of nameref too, so this is all in vain. 14

611 \DeclareOption{hyperref}{\PackageWarningNoLine{lineno}{%
612  Option [hyperref] is obsolete.
613 \MessageBreak The hyperref package is detected automatically.}}

614

615 \AtBeginDocument{%
616 \@ifpackageloaded{nameref}{%
617  \def\@LN@ExtraLabelItems{{}{}{}}}{}}

(New v4.1) 15

8.4 A note on calling so many options 16

The number of package options may stimulate worrying about how to enter 17 all the options that one would like to use—they may not fit into one line. 18 Fortunately, you can safely break code lines after the commas separating the 19 option names in the \usepackage command (no comment marks needed). 20

1 8.5 Execute options

2 We stop declaring options and execute the ones that are called by the user. 3 (/New v4.1)

618 \ProcessOptions

4 (New v4.1) Now we know whether edtable.sty is wanted and (if it is) with 5 which options it is to be called.

619 \if@LN@edtable \RequirePackage{edtable}[2004/10/12] \fi

6 (/New v4.1)

7 9 The final touch

8 There is one deadcycle for each line number.

620 \advance\maxdeadcycles 100

621

622 \endinput

9 10 The user commands

10 The user commands to turn on and off line numbering are

11 \linenumbers

12 Turn on line numbering in the current mode.

13 \linenumbers*

14 and reset the line number to 1.

15 \linenumbers[⟨number ⟩]

16 and start with ⟨number⟩.

17 \nolinenumbers

18 Turn off line numbering.

19 \runninglinenumbers*[⟨number ⟩]

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

\pagewiselinenumbers  1

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

\resetlinenumber[⟨number ⟩]  4

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

\setrunninglinenumbers  6

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

\setpagewiselinenumbers  8

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

\switchlinenumbers*  10

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

\leftlinenumbers*  13
\rightlinenumbers*  14

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

\runningpagewiselinenumbers  18

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

\realpagewiselinenumbers  24
Reverses the effect of \runningpagewiselinenumbers.  25
\modulolinenumbers[⟨number ⟩]  26

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

 1 \firstlinenumber

2 \firstlinenumber{⟨filino⟩} brings about that (after it) line num-3 bers less than ⟨filino⟩ do not appear in the margin. Moreover, 4 with \modulolinenumbers[⟨number⟩], just the line numbers which 5 are ⟨filino⟩ plus a multiple of ⟨number⟩ are printed.—If you had 6 \firstlinenumber{⟨pos⟩} with some ⟨pos⟩ > 0 and want to switch 7 to printing multiples of, e.g., 4, you best do \modulolinenumbers[4]

 8  and \firstlinenumber{0}
 9 \linenumberdisplaymath

10 Number the lines of a display math in a {linenomath} environment, 11 but do not in a {linenomath*} environment. This is used by the 12 package option [mathlines].

13 \nolinenumberdisplaymath

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

16 \linelabel

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

19  \pageref{⟨foo⟩}.

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

24  {\linenumbers ⟨text⟩ \par}

25

26  \begin{linenumbers}

27 ⟨text⟩

28  \end{linenumbers}

29

30  ⟨paragraph⟩ {\linenumbers\par}

31

32  \linenumbers

33 ⟨text⟩ \par

34  \nolinenumbers

35

36  \linenumbers
37  ⟨paragraph⟩ {\nolinenumbers\par}

(New v4.00) However, the examples containing ⟨paragraph⟩ show what you 1

should not do, at least if you use \pagebreak, \nopagebreak, \vspace, \\*  2

or \\[⟨space⟩]—cf. section 7. 3

The same care should be applied to the “wizard” devices \ifLineNumbers 4 (section 3) and \PostponeVadjust (section 7.1). (/New v4.00) 5

10.1 Customization hooks 6

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

\thelinenumber  9

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

    \arabic{linenumber}  12
\makeLineNumberLeft  13

This macro is used to attach a line number to the left of the text page. 14 This macro should fill an \hbox to 0pt which will be placed at the 15 left margin of the page, with the reference point aligned to the line to 16 which it should give a number. Please use the macro \LineNumber to 17 refer to the line number. 18 The default definition is 19

    \hss\linenumberfont\LineNumber\hskip\linenumbersep  20
\makeLineNumberRight  21

Like \makeLineNumberLeft, but for line numbers on the right margin. 22 The default definition is 23

    \linenumberfont\hskip\linenumbersep\hskip\textwidth  24
    \hbox to\linenumberwidth{\hss\LineNumber}\hss  25
\linenumberfont  26

This macro is initialized to 27

    \normalfont\tiny\sffamily  28
\linenumbersep  29

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

1 \linenumberwidth

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

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

5 default value is 10pt.

6 \theLineNumber (for wizards)

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

11 \makeLineNumber (for wizards)

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

14  \makeLineNumberLeft.

15 \LineNumber (for wizards)

16 This macro is called by \makeLineNumber to typeset the line num-17 ber. This hook is changed by the modulo mechanism and by

18  \firstlinenumber.