Debian Codegraph @

texmf-dist/doc/latex/lineno/lineno.tex

raw
1 \def\fileversion{v5.9} \def\filedate{2026/03/08}
2
3 \title{\texttt{\itshape
4 lineno.sty \ \fileversion\ \filedate
5 \unskip}\\\ \\
6 A \LaTeX\ package to attach
7 \\ line numbers to paragraphs
8 \unskip}\author{%
9 Stephan I. Böttcher
10 \\ Uwe Lück
11 \\ Karl Wette
12 \unskip}\date{%
13 \url{https://github.com/latex-lineno/lineno}
14 \\}
15
16 \documentclass[a4paper,12pt]{article}%D
17 \usepackage{lineno}%D
18 \usepackage{hyperref}%D
19 \catcode`\_\active\let_~
20 \def~{\verb~}
21 \let\lessthan<
22 \def\angle#1{$\langle${\itshape#1}\/$\rangle$}
23 \catcode`\|\active
24 \def|#1{\ttfamily\string#1}
25 \newenvironment{code}
26 {\par\runninglinenumbers
27 \modulolinenumbers[1]%
28 \linenumbersep.3em
29 \footnotesize
30 \def\linenumberfont
31 {\normalfont\tiny\itshape}}
32 {}
33 {\makeatletter \gdef\scs#1{\texttt
34 {\protect\@backslashchar#1}}}
35 \def\old{\par\footnotesize}
36 {\catcode`\/\active
37 \gdef\path{\begingroup\catcode`\/\active
38 \let/\slash\dopath}
39 \gdef\dopath#1{\slash\unpenalty#1\endgroup}}
40
41 \begin{document}%D
42 \pagewiselinenumbers
43 \maketitle
44 \pagestyle{headings}
45 \tableofcontents
46 \sloppy
47
48 \section{%
49 Introductions
50 \unskip}
51
52 (New v4.00) Parts of former first section
53 have been rendered separate subsections for package
54 version_v4.00. (/New v4.00)
55
56 \subsection{%
57 Introduction to versions $\textrm{v}\lessthan4$
58 \unskip}
59
60 This package provides line numbers on paragraphs.
61 After \TeX\ has broken a paragraph into lines there will
62 be line numbers attached to them, with the possibility to
63 make references through the \LaTeX\ ~\ref~, ~\pageref~
64 cross reference mechanism. This includes four issues:
65 \begin{itemize}
66 \item attach a line number on each line,
67 \item create references to a line number,
68 \item control line numbering mode,
69 \item count the lines and print the numbers.
70 \end{itemize}
71 The first two points are implemented through patches to
72 the output routine. The third by redefining ~\par~, ~\@par~
73 and ~\@@par~. The counting is easy, as long as you want
74 the line numbers run through the text. If they shall
75 start over at the top of each page, the aux-file as well
76 as \TeX s memory have to carry a load for each counted line.
77
78 I wrote this package for my wife Petra, who needs it for
79 transcriptions of interviews. This allows her to
80 precisely refer to passages in the text. It works well
81 together with ~\marginpar~s, but not too well with displaymath.
82 ~\footnote~s are a problem, especially when they
83 are split, but we may get there.
84 (New v4.00 UL) Version v4.00 overcomes the problem, I believe.
85 (/UL /New v4.00)
86
87 lineno.sty works
88 surprisingly well with other packages, for
89 example, ~wrapfig.sty~. So please try if it
90 works with whatever you need, and if it does,
91 please tell me, and if it does not, tell me as
92 well, so I can try to fix it.
93
94 \subsection{%
95 Introduction to versions v4.00ff. (UL)
96 \unskip}
97
98 ~lineno.sty~ has been maintained by Stephan until version_v3.14.
99 From version_v4.00 onwards, maintenance is shifting towards
100 Uwe Lück (UL), who is the author of v4\dots code and of v4\dots
101 changes in documentation. This came about as follows.
102
103 Since late 2002, Christian Tapp and Uwe Lück have employed
104 ~lineno.sty~ for their ~ednotes.sty~, a package supporting
105 critical editions, while you find ~ednotes.sty~ and surrounding files in
106 CTAN folder \path{macros/latex/contrib/ednotes}.
107
108 Soon, some weaknesses of ~lineno.sty~ showed up, mainly since
109 Christian's critical editions (using ~ednotes.sty~) needed lots
110 of ~\linelabel~s and footnotes. (These weaknesses are due to
111 weaknesses of \LaTeX's ~\marginpar~ mechanism that Stephan
112 used for ~\linelabel~.) So we changed some ~lineno.sty~
113 definitions in some extra files, which moreover offered new
114 features. We sent these files to Stephan, hoping he would take
115 the changes into ~lineno.sty~. However, he was too short of time.
116
117 Writing a TUGboat article on Ednotes in 2004, we hoped to
118 reduce the number of files in the Ednotes bundle and so asked
119 Stephan again. Now he generously offered maintenance to me, so
120 I could execute the changes on my own.
121
122 The improvements are as follows:
123 \begin{itemize}\item
124 [(i)] Footnotes placement approaches intentions better
125 (footnotes formerly liked to pile up at late pages).
126 \item
127 [(ii)] The number of ~\linelabel~s in one paragraph is no longer
128 limited to 18.
129 \item
130 [(iii)] ~\pagebreak~, ~\nopagebreak~, ~\vspace~, and the star
131 and optional versions of ~\\~ work as one would expect
132 (section_\ref{s:MVadj}). %% Added for v4.1
133 \item
134 [(iv)] A command is offered which chooses the first line number
135 to be printed in the margin
136 (subsection_\ref{ss:Mod}). %% Added for v4.1
137 \item
138 [(v)] (New v4.1) \LaTeX\ tabular environments (optionally)
139 get line numbers as well, and you can refer to them in the
140 usual automatic way. (It may be considered a shortcoming that,
141 precisely, \emph{rows} are numbered, not lines.---See
142 subsection_\ref{ss:Tab}.)
143 \item
144 [(vi)] We are moving towards referring to math items
145 (subsection_\ref{ss:MathRef} and the hooks in
146 subsection_\ref{ss:LL}).
147 (/New v4.1)
148 \end{itemize}
149 (Thanks to Stephan for making this possible!)
150
151
152 Ednotes moreover profits from Stephan's offer with regard
153 to the documentation of our code which yielded these
154 improvements formerly. This documentation now becomes
155 printable, being part of the ~lineno.sty~ documentation.
156
157 Of course, Stephan's previous ~lineno.sty~ versions were a great
158 and ingenious work and exhibit greatest \TeX pertise. I never
159 could have done this. I learnt a lot in studying the code when
160 Christian pointed out strange output results and error
161 messages, and there are still large portions of ~lineno.sty~
162 which I don't understand (consider only pagewise numbering of
163 lines). Fortunately, Stephan has offered future help if
164 needed.---My code for attaching line numbers to \emph{tabular
165 environments} (as mentioned above, now still in
166 ~edtable.sty~) %% %% TODO
167 developed from macros which Stephan and Christian experimented
168 with in December 2002. Stephan built the basics.
169 (However, I then became too proud to follow his advice only to
170 use and modify ~longtable.sty~.)
171
172 There are some issues concerning use of counters on which I
173 don't agree with Stephan and where I would like to change the
174 code if ~lineno.sty~ is ``mine'' as Stephan offered. However,
175 Stephan is afraid of compatibility problems from which, in
176 particular, his wife could suffer in the near future. So he
177 demanded that I change as little as possible for my first
178 version. Instead of executing changes that I plan I just offer
179 my opinions at the single occasions. I hope to get in touch
180 this way with users who consider subtle features vital which I
181 consider strange.
182
183 On the other hand, the sections on improvements of the
184 implementation have been blown up very much and may be tiring
185 and litte understandable for mere \emph{users}. These users
186 may profit from the present presentation just by jumping to
187 sections_\ref{s:Opts} and_\ref{s:UserCmds}. There is a user's
188 guide ulineno.tex which may be even more helpful, but it has
189 not been updated for a while. %% TODO
190
191 \subsection{%
192 Availability
193 \unskip}
194
195 In case you have found the present file otherwise than from
196 CTAN: A recent version and documentation of this package
197 should be available from CTAN folder
198 \path{macros/latex/contrib/lineno}.
199 Or mail to one of the addresses at top of file.
200
201 \subsection{%
202 Introductory code
203 \unskip}
204
205 This style option is written for \LaTeXe, November 1994 or later,
206 since we need the ~\protected@write~ macro.
207
208 (New v4.00) And we use ~\newcommand*~ for
209 controlling length of user macro arguments, which has been
210 available since December 1994.
211 (/New v4.00)
212 \begin{code}\begin{verbatim}
213 \NeedsTeXFormat{LaTeX2e}[1994/12/01]
214 \ProvidesPackage{lineno}
215 [\filedate\space line numbers on paragraphs \fileversion]
216 \RequirePackage{etoolbox}
217 \RequirePackage{kvoptions}
218 \end{verbatim}
219 \end{code}
220
221
222 \section{%
223 Put the line numbers to the lines
224 \unskip}
225
226 (New v4.00) This section contained the most
227 basic package code previously. For various purposes of
228 version_4\dots, much of these basics have been to be modified.
229 Much of my (UL's) reasoning on these modifications has been to
230 be reported. Sorry, the present section has been blown up
231 awfully thus and contains ramifications that may be difficult
232 to trace. We add some ~\subsection~ commands in order to cope
233 with the new situation. (/New v4.00)
234
235 \subsection{%
236 Basic code of \texttt{lineno.sty} \scs{output}
237 \unskip}\label{ss:output}
238
239 The line numbers have to be attached by the output
240 routine. We simply set the ~\interlinepenalty~ to $-100000$.
241 The output routine will be called after each line in the
242 paragraph, except the last, where we trigger by ~\par~.
243 The ~\linenopenalty~ is small enough to compensate a bunch of
244 penalties (e.g., with ~\samepage~).
245
246 (New v3.04) Longtable uses
247 ~\penalty~$-30000$. The lineno penalty range was
248 shrunk to $-188000 \dots -32000$. (/New v3.04)
249 (New v4.00) New values are listed below (11111f.). (/New v4.00)
250 \begin{code}\begin{verbatim}
251 \newcount\linenopenalty\linenopenalty=-100000
252 \end{verbatim}
253 \end{code}
254 (UL) Hm. It is never needed below
255 that this is a counter. ~\def\linenopenalty{-100000\relax}~
256 would do. (I guess this consumes more memory, but it
257 is more important to save counters than to save memory.)
258 I was frightened by ~-\linenopenalty~ below, but indeed
259 \TeX\ interprets the string ~--100000~ as 100000.
260 Has any user or extension package writer ever called
261 ~\linenopenalty=xxx~, or could I really change this?---The
262 counter is somewhat faster than the macro. Together with the
263 compatibility question this seems to support keeping the
264 counter. (???)
265 (/UL)
266 \begin{code}\begin{verbatim}
267 \mathchardef\linenopenaltypar=32000
268 \end{verbatim}
269 \end{code}
270 So let's make a hook to ~\output~, the direct way. The \LaTeX\
271 macro ~\@reinserts~ puts the footnotes back on the page.
272
273 (New v3.01) ~\@reinserts~ badly
274 screws up split footnotes. The bottom part is
275 still on the recent contributions list, and the
276 top part will be put back there after the bottom
277 part. Thus, since lineno.sty does not play well
278 with ~\inserts~ anyway, we can safely experiment
279 with ~\holdinginserts~, without making things
280 much worse.
281
282 Or that's what I thought, but: Just activating
283 ~\holdinginserts~ while doing the ~\par~ will
284 not do the trick: The ~\output~ routine may be
285 called for a real page break before all line
286 numbers are done, and how can we get control
287 over ~\holdinginserts~ at that point?
288
289 Let's try this: When the ~\output~ routine is
290 run with ~\holdinginserts=3~ for a real page
291 break, then we reset ~\holdinginserts~ and
292 restart ~\output~.
293
294 Then, again, how do we keep the remaining
295 ~\inserts~ while doing further line numbers?
296
297 If we find ~\holdinginserts~=$-3$ we activate it again
298 after doing ~\output~. (/New v3.01)
299
300 (New v3.02) To work with
301 multicol.sty, the original output routine is now
302 called indirectly, instead of being replaced.
303 When multicol.sty changes ~\output~, it is a
304 toks register, not the real thing. (/New v3.02)
305
306 (New v4.00) Two further complications are added.
307 \begin{itemize}\item
308 [(i)] Problems with footnotes formerly resulted from
309 \LaTeX's ~\@reinserts~ in ~\@specialoutput~ which Stephan's
310 ~\linelabel~ called via the ~\marginpar~ mechanism.
311 \item
312 [(ii)] \LaTeX\ commands using ~\vadjust~ formerly didn't work
313 as one would have hoped. The problem is as follows:
314 Printing the line number results from
315 a box that the output routine inserts at the place of the
316 ~\interlinepenalty~. ~\vadjust~ items appear \emph{above} the
317 ~\interlinepenalty~ (\TeX book p._105). So ~\pagebreak~, e.g.,
318 formerly sent the line number to the next page, while the
319 penalty from ~\nopagebreak~ could not tie the following line,
320 since it was screened off by the line number box.---Our trick
321 is putting the ~\vadjust~ items into a list macro from which
322 the output routine transfers them into the vertical list,
323 below the line number box.
324 \end{itemize}
325 In this case_(ii), like in case_(i), footnotes would suffer
326 if ~\holdinginserts~ were non-positive. Indeed, in both
327 cases_(i) and_(ii) we tackle the footnote problem by extending
328 that part of Stephan's output routine that is active when
329 ~\holdinginserts~ is positive. This extension writes the line
330 number ~\newlabel~ to the .aux file (which was formerly done
331 under $~\holdinginserts~=-3$) and handles the ~\vadjust~
332 items.---To trigger ~\output~ and its ~\linelabel~ or, resp.,
333 ~\vadjust~ part, the list of signal penalties started
334 immediately before is increased here (first for ~\linelabel~,
335 second for postponed ~\vadjust~ items):
336 \begin{code}\begin{verbatim}
337 \mathchardef\@Mllbcodepen=11111
338 \mathchardef\@Mppvacodepen=11112
339 \end{verbatim}
340 \end{code}
341 (/New v4.00) (New v4.2) David Kastrup urges to use a private
342 name instead of ~\the\output~ (LaTeX-L-list). Otherwise an
343 ~\output~ routine loaded later and using ~\newtoks\output~
344 again may get lost entirely. So we change use of ~\@LN@output~,
345 using it for the former purpose. Reference to what appeared
346 with the name of ~\output~ here lasts for a few lines and then
347 is given away.
348 \begin{code}\begin{verbatim}
349 \let\@tempa\output
350 \newtoks\output
351 \let\@LN@output\output
352 \output=\expandafter{\the\@tempa}
353 \end{verbatim}
354 \end{code}
355 Now we add two cases to Stephan's output routine. (New v4.00)
356 \begin{code}\begin{verbatim}
357 \@tempa={%
358 \end{verbatim}
359 \end{code}
360 (/New 4.2)
361 \begin{code}\begin{verbatim}
362 \LineNoTest
363 \if@tempswa
364 \end{verbatim}
365 \end{code}
366 (New v4.00)
367 We insert recognition of waiting ~\linelabel~ items---
368 \begin{code}\begin{verbatim}
369 \ifnum\outputpenalty=-\@Mllbcodepen
370 \WriteLineNo
371 \end{verbatim}
372 \end{code}
373 ---and of waiting ~\vadjust~ items:
374 \begin{code}\begin{verbatim}
375 \else
376 \ifnum\outputpenalty=-\@Mppvacodepen
377 \PassVadjustList
378 \else
379 \end{verbatim}
380 \end{code}
381 (/New v4.00) (New v4.2) Outsource ``Standard'' output
382 ---which occurs so rarely---to subsection_\ref{ss:LLO}:
383 \begin{code}\begin{verbatim}
384 \LineNoLaTeXOutput
385 \end{verbatim}
386 \end{code}
387 (/New v4.2) (New v4.00)
388 Two new ~\fi~s for the ~\linelabel~ and ~\vadjust~ tests---
389 \begin{code}\begin{verbatim}
390 \fi
391 \fi
392 \end{verbatim}
393 \end{code}
394 ---and the remaining is
395 Stephan's code again:
396 (/New v4.00)
397 \begin{code}\begin{verbatim}
398 \else
399 \MakeLineNo
400 \fi
401 }
402 \end{verbatim}
403 \end{code}
404 (New v4.00) Our new macros
405 ~\WriteLineNo~ and ~\PassVadjustList~ will be dealt with in
406 sections_\ref{s:LNref} and_\ref{ss:PVadj}. (/New v4.00)
407
408 \subsection{%
409 \scs{LineNoTest}
410 \unskip}
411
412 The float mechanism inserts ~\interlinepenalty~s during
413 ~\output~. So carefully reset it before going on. Else
414 we get doubled line numbers on every float placed in
415 horizontal mode, e.g, from ~\linelabel~.
416
417 Sorry, neither a ~\linelabel~ nor a ~\marginpar~ should
418 insert a penalty, else the following line number
419 could go to the next page. Nor should any other
420 float. So let us suppress the ~\interlinepenalty~
421 altogether with the ~\@nobreak~ switch.
422
423 Since (ltspace.dtx, v1.2p)[1996/07/26], the ~\@nobreaktrue~ does
424 it's job globally. We need to do it locally here.
425 \begin{code}\begin{verbatim}
426 \def\LineNoTest{%
427 \let\@@par\@@@par
428 \ifnum\interlinepenalty<-\linenopenaltypar
429 \advance\interlinepenalty-\linenopenalty
430 \@LN@nobreaktrue
431 \fi
432 \@tempswatrue
433 \ifnum\outputpenalty>-\linenopenaltypar\else
434 \ifnum\outputpenalty>-188000\relax
435 \@tempswafalse
436 \fi
437 \fi
438 }
439
440 \def\@LN@nobreaktrue{\let\if@nobreak\iftrue} % renamed v4.33
441 \end{verbatim}
442 \end{code}
443 (UL) I thought here were
444 another case of the save stack problem explained in \TeX book,
445 p._301, namely through both local and global changing
446 ~\if@nobreak~. However, ~\@LN@nobreak~ is called during
447 ~\@LN@output~ only, while ~\@nobreaktrue~ is called by \LaTeX's
448 ~\@startsection~ only. The latter never happens during
449 ~\@LN@output~. So there is no local value of ~\if@nobreak~ on
450 save stack when ~\@nobreaktrue~ acts, since ~\the\@LN@output~
451 (where ~\@LN@output~ is a new name for the original ~\output~)
452 is executed within a group (\TeX book p._21).
453 (/UL)
454
455 \subsection{%
456 Other output routines (v4.2)
457 \unskip}\label{ss:LLO}
458
459 I had thought of dealing with bad interference of footnotes
460 (and ~\enlargethispage~) with (real) ~\marginpar~s and floats
461 \emph{here}. Yet this is done in
462 \[
463 ~http://~\mbox{[CTAN]}
464 ~/macros/latex/contrib/tamefloats/tameflts.sty~
465 \]
466 now, and I prefer striving for compatibility with the latter.
467 (See there for expanding on the problem.)
468 This requires returning the special absolute value of
469 ~\holdinginserts~ that ~lineno.sty~ finds at the end of a newly
470 typeset paragraph---now done in subsection_\ref{ss:calls}
471 (~\linenumberpar~).
472 The former ~\LineNoHoldInsertsTest~ has been filled into here.
473 Note: when the following code is invoked, we have
474 ~\if@tempswa~_ =_~\iftrue~.
475 WARNING: I am still not sure whether the present code is good
476 for cooperating with other packages that use ~\holdinginserts~.
477 \begin{code}\begin{verbatim}
478 \def\LineNoLaTeXOutput{%
479 \ifnum \holdinginserts=\thr@@ % v4.33 without \@tempswafalse
480 \global\holdinginserts-\thr@@
481 \unvbox\@cclv
482 \ifnum \outputpenalty=\@M \else \penalty\outputpenalty \fi
483 \else
484 \if@twocolumn \let\@makecol\@LN@makecol \fi
485 \the\@LN@output % finally following David Kastrup's advice.
486 \ifnum \holdinginserts=-\thr@@
487 \global\holdinginserts\thr@@ \fi
488 \fi
489 }
490 \end{verbatim}
491 \end{code}
492 \textit{More on dealing with output routines from other
493 packages:}
494 Since ~lineno.sty~'s output routine is called at least once
495 for each output line, I think it should be in \TeX's
496 original ~\output~, while output routines dealing with
497 building pages and with floats etc.\ should be filled into
498 registers addressed by ~\output~ after ~\newtoks\output~.
499 Therefore \begin{enumerate}
500 \item
501 ~tameflts.sty~ should be loaded \emph{after} ~lineno.sty~;
502 \item
503 if a class changes ~\output~ (APS journal class revtex4,
504 e.g.), ~lineno.sty~ should be loaded by ~\RequirePackage~
505 [here presumably following some options in
506 brackets]~{lineno}~ \emph{preceding} ~\documentclass~.
507 \item
508 If you actually maintain such a class, please consider
509 loading ~lineno.sty~ on some draft option. The bunch of
510 lineno's package options may be a problem, but perhaps the
511 purpose of your class is offering only very few of lineno's
512 options anyway, maybe just one.
513 \end{enumerate}
514 The latter may also be needed with classes that don't follow
515 David Kastrup's rule on changing ~\output~.
516
517 \subsection{%
518 \scs{MakeLineNo}: Actually attach line number
519 \unskip}\label{ss:MLN}
520
521 We have to return all the page to the current page, and
522 add a box with the line number, without adding
523 breakpoints, glue or space. The depth of our line number
524 should be equal to the previous depth of the page, in
525 case the page breaks here, and the box has to be moved up
526 by that depth.
527
528 The ~\interlinepenalty~ comes after the ~\vadjust~ from a
529 ~\linelabel~, so we increment the line number \emph{after}
530 printing it. The macro ~\makeLineNumber~ produces the
531 text of the line number, see section \ref{appearance}.
532
533 (UL) I needed a while to understand
534 the sentence on incrementing. Correctly: writing the
535 ~\newlabel~ to the .aux file is triggered by the signal
536 penalty that ~\end@float~ inserts via ~\vadjust~.
537 However, this could be changed by our new ~\PostponeVadjust~.
538 After ~\c@linenumber~ has been introduced as a \LaTeX\
539 counter, it might be preferable that it behaved like standard
540 \LaTeX\ counters which are incremented shortly before printing.
541 But this may be of little practical relevance in this case,
542 as ~\c@linenumber~ is driven in a very non-standard
543 way.---However still, this behaviour of ~\c@linenumber~
544 generates a problem with our ~edtable.sty~.
545 (/UL).
546
547 Finally we put in the natural ~\interlinepenalty~, except
548 after the last line.
549
550 (New v3.10) Frank Mittelbach points out that box255 may be
551 less deep than the last box inside, so he proposes to
552 measure the page depth with ~\boxmaxdepth=\maxdimen~.
553 (/New v3.10)
554
555 (UL, New v4.00) We also resume the matter of
556 ~\vadjust~ items that was started in section_\ref{ss:output}.
557
558 \TeX\ puts only nonzero interline
559 penalties into the vertical list (\TeX book p._105), while
560 ~lineno.sty~ formerly replaced the signal interline penalty by
561 something closing with an explicit penalty of the value that
562 the interline penalty would have without ~lineno.sty~.
563 This is usually 0. Now, explicit vertical penalties can be
564 very nasty with respect to ~\nopagebreak~, e.g., a low (even
565 positive) ~\widowpenalty~ may force a widow where you
566 explicitly tried to forbid it by ~\nopagebreak~
567 (see explanation soon below).
568 The ~\nopagebreak~ we create here would never work if all
569 those zero penalties were present.---On
570 the other hand, we cannot just omit Stephan's zero penalties,
571 because \TeX\ puts a penalty of 10000 after what ~lineno.sty~
572 inserts (\TeX book p._125). This penalty must be overridden
573 to allow page breaks between ordinary lines. To revive
574 ~\nopagebreak~, we therefore replace those zero (or low)
575 penalties by penalties that the user demanded by
576 ~\nopagebreak~.---This mechanism is not perfect and does not
577 exactly restore the original \LaTeX\ working of ~\pagebreak~
578 and ~\nopagebreak~. Viz., if there are several vertical
579 penalties after a line which were produced by closely sitting
580 ~\[no]pagebreak~s, without ~lineno.sty~ the lowest penalty would
581 be effective (cf._\TeX book exercise_14.10). Our mechanism, by
582 contrast, chooses the \emph{last} user-set penalty of the line
583 as the effective one. It would not be very difficult to come
584 more close to the original mechanism, but until someone urges
585 us we will cling to the present simple way. You may consider an
586 advantage of the difference between our mechanism and the
587 original one that the user here can actually override low
588 penalties by ~\nopagebreak~, which may be what a lay \LaTeX\
589 user would expect.
590 (/UL, /New v4.00)
591 \begin{code}\begin{verbatim}
592 \def\MakeLineNo{%
593 \@LN@maybe@normalLineNumber % v4.31
594 \boxmaxdepth\maxdimen\setbox\z@\vbox{\unvbox\@cclv}%
595 \@tempdima\dp\z@ \unvbox\z@
596 \sbox\@tempboxa{\hb@xt@\z@{\makeLineNumber}}%
597 \end{verbatim}
598 \end{code}
599 (New v4.00) Previously,
600 \begin{old}\begin{verbatim}
601 % \stepcounter{linenumber}%
602 \end{verbatim}
603 \end{old}
604 followed. (Of course, there was no
605 comment mark; I put it there to make
606 reading the actual code easy.)
607
608 (New v4.22: improved) Why not just
609 \[~\global\advance\c@linenumber\@ne~?\]
610 ~\stepcounter~ additionally resets ``subordinate''
611 counters, but which could these (usefully) be?
612 Again, may be column counters with ~edtable.sty~!?
613
614 But then, our ~edtable.sty~ and its ~longtable~ option
615 should use it as well. So use a shorthand supporting
616 uniformity. You can even use it as a hook for choosing
617 ~\global\advance\c@linenumber\@ne~ instead of our choice.
618 (/New v4.22)
619 \begin{code}\begin{verbatim}
620 \stepLineNumber
621 \end{verbatim}
622 \end{code}
623 (New v4.4) Now
624 \begin{code}\begin{verbatim}
625 \ht\@tempboxa\z@ \@LN@depthbox
626 \end{verbatim}
627 \end{code}
628 appends the box containing the line number without changing
629 ~\prevdepth~---see end of section.
630 Now is the time for inserting the $\dots$ (/New v4.4)
631 ~\vadjust~ items. We cannot do this much later, because
632 their right place is above the artificial interline
633 penalty which Stephan's code will soon insert
634 (cf._\TeX book p._105). The next command is just ~\relax~
635 if no ~\vadjust~ items have been accumulated for the
636 current line. Otherwise it is a list macro inserting
637 the ~\vadjust~ items and finally resetting itself.
638 (This is made in section_\ref{ss:PVadj} below.)
639 If the final item is a penalty, it is stored so it can
640 compete with other things about page breaking.
641 \begin{code}\begin{verbatim}
642 \@LN@do@vadjusts
643 \count@\lastpenalty
644 \end{verbatim}
645 \end{code}
646 At this place,
647 \begin{old}\begin{verbatim}
648 % \ifnum\outputpenalty=-\linenopenaltypar\else
649 \end{verbatim}
650 \end{old}
651 originally followed. We need something \emph{before} the
652 ~\else~:
653 \begin{code}\begin{verbatim}
654 \ifnum\outputpenalty=-\linenopenaltypar
655 \ifnum\count@=\z@ \else
656 \end{verbatim}
657 \end{code}
658 So final ~\pagebreak[0]~ or ~\nopagebreak[0]~ has no
659 effect---but this will make a difference after headings only,
660 where nobody should place such a thing anyway.
661 \begin{code}\begin{verbatim}
662 \xdef\@LN@parpgbrk{%
663 \penalty\the\count@
664 \global\let\noexpand\@LN@parpgbrk
665 \noexpand\@LN@screenoff@pen}% v4.4
666 \end{verbatim}
667 \end{code}
668 That penalty will replace former ~\kern\z@~ in
669 ~\linenumberpar~, see subsection_\ref{ss:calls}.---A
670 few days earlier, I tried to send just a penalty value.
671 However, the ~\kern\z@~ in ~\linenumberpar~ is crucial,
672 as I then found out. See below.---The final penalty is
673 repeated, but this does no harm. (It would not be very
674 difficult to avoid the repeating, but it may even be
675 less efficient.) It may be repeated due to the previous
676 ~\xdef~, but it may be repeated as well below in the
677 present macro where artificial interline penalty is to
678 be overridden.
679 \begin{code}\begin{verbatim}
680 \fi
681 \else
682 \end{verbatim}
683 \end{code}
684 (/New v4.00)
685 \begin{code}\begin{verbatim}
686 \@tempcnta\outputpenalty
687 \advance\@tempcnta -\linenopenalty
688 \end{verbatim}
689 \end{code}
690 (New v4.00)
691 \begin{old}\begin{verbatim}
692 % \penalty\@tempcnta
693 \end{verbatim}
694 \end{old}
695 followed previously. To give ~\nopagebreak~ a chance,
696 we do
697 \begin{code}\begin{verbatim}
698 \penalty \ifnum\count@<\@tempcnta \@tempcnta \else \count@ \fi
699 \end{verbatim}
700 \end{code}
701 instead.---In ~linenox0.sty~, the ~\else~ thing once was omitted.
702 Sergei Mariev's complaint (thanks!) showed that it is vital
703 (see comment before ~\MakeLineNo~).
704 The remaining ~\fi~ from previous package version closes the
705 ~\ifnum\outputpenalty~\dots
706 (/New v4.00)
707 \begin{code}\begin{verbatim}
708 \fi
709 }
710 \end{verbatim}
711 \end{code}
712 (New v4.00)
713 \begin{code}\begin{verbatim}
714 \newcommand\stepLineNumber{\stepcounter{linenumber}}
715 \end{verbatim}
716 \end{code}
717 For reason, see use above. (/New v4.00)
718
719 (New v4.4) The depth preserving trick is drawn here from
720 ~\MakeLineNo~ because it will be used again in
721 section_\ref{ss:calls}.
722 (v5.3) Handle special value of ~\prevdepth=-1000pt~. (/v5.3)
723 \begin{code}\begin{verbatim}
724 \def\@LN@depthbox{%
725 \ifdim\@tempdima=-1000pt
726 % \nointerlineskip is already set so we don't need set it again
727 % (and we shouldn't back up)
728 \else
729 \dp\@tempboxa=\@tempdima
730 \nointerlineskip
731 \kern-\@tempdima
732 \fi
733 \box\@tempboxa}
734 \end{verbatim}
735 \end{code}
736 (/New v4.4)
737
738 \section{%
739 Control line numbering
740 \unskip}
741 \subsection{%
742 Inserting \scs{output} calls %% own subsec. v4.4.
743 \unskip}\label{ss:calls}
744 The line numbering is controlled via ~\par~. \LaTeX\
745 saved the \TeX-primitive ~\par~ in ~\@@par~. We push it
746 one level further out, and redefine ~\@@par~ to insert
747 the ~\interlinepenalty~ needed to trigger the
748 line numbering. And we need to allow pagebreaks after a
749 paragraph.
750
751 New (2.05beta): the prevgraf test. A paragraph that ends with a
752 displayed equation, a ~\noindent\par~ or ~wrapfig.sty~ produce empty
753 paragraphs. These should not get a spurious line number via
754 ~\linenopenaltypar~.
755 \begin{code}\begin{verbatim}
756 \let\@@@par\@@par
757 \newcount\linenoprevgraf
758 \end{verbatim}
759 \end{code}
760 (UL) And needs ~\linenoprevgraf~
761 to be a counter? Perhaps there may be a paragraph having
762 thousands of lines, so ~\mathchardef~ doesn't suffice (really??).
763 A macro ending on ~\relax~ might suffice, but would be
764 somewhat slow. I think I will use ~\mathchardef~ next time.
765 Or has any user used ~\linenoprevgraf~? (/UL)
766 \begin{code}\begin{verbatim}
767 \def\linenumberpar{%
768 \ifvmode \@@@par \else
769 \ifinner \@@@par \else
770 \xdef\@LN@outer@holdins{\the\holdinginserts}% v4.2
771 \advance \interlinepenalty \linenopenalty
772 \linenoprevgraf \prevgraf
773 \global \holdinginserts \thr@@
774 \@@@par
775 \ifnum\prevgraf>\linenoprevgraf
776 \penalty-\linenopenaltypar
777 \fi
778 \end{verbatim}
779 \end{code}
780 (New v4.00)
781 \begin{old}\begin{verbatim}
782 % \kern\z@
783 \end{verbatim}
784 \end{old}
785 was here previously. What for?
786 According to \TeX book p._125, Stephan's
787 interline penalty is changed into 10000. At the end of a
788 paragraph, the ~\parskip~ would follow that penalty of 10000,
789 so there could be a page break neither at the
790 ~\parskip~ nor at the ~\baselineskip~ (\TeX book p._110)---so
791 there could never be a page break between two paragraphs.
792 So something must screen off the 10000 penalty.
793 Indeed, the ~\kern~ is a place to break.
794 (Stephan once knew this: see `allow pagebreaks' above.)
795
796 Formerly, I tried to replace ~\kern\z@~ by
797 \begin{old}\begin{verbatim}
798 % \penalty\@LN@parpgpen\relax
799 \end{verbatim}
800 \end{old}
801 ---but this allows a page break after heading. So:
802 \begin{code}\begin{verbatim}
803 \@LN@parpgbrk
804 \end{verbatim}
805 \end{code}
806
807 These and similar changes were formerly done by ~linenox1.sty~.
808 (/New v4.00)
809
810 (New v4.4)
811 A ~\belowdisplayskip~ may precede the previous when the paragraph
812 ends on a display-math; or there may be a ~\topsep~ from a list, etc.
813 ~\addvspace~ couldn't take account for it with ~\kern\z@~
814 here. v4.32 therefore moved the space down -- with at least two
815 bad consequences.
816 Moreover, David Josef Dev observes that ~\kern\z@~ may
817 inappropriately yield column depth 0pt.
818 For these reasons, we introduce ~\@LN@screenoff@pen~ below.
819 (/New v4.4)
820 \begin{code}\begin{verbatim}
821 \global\holdinginserts\@LN@outer@holdins % v4.2
822 \advance\interlinepenalty -\linenopenalty
823 \fi % from \ifinner ... \else
824 \fi} % from \ifvmode ... \else
825 \end{verbatim}
826 \end{code}
827 (New v4.00, v4.4) Initialize ~\@LN@parpgbrk~, accounting
828 for earlier space and for appropriate columndepth.
829 We use former ~\MakeLineNo~'s depth-preverving trick
830 ~\@LN@depthbox~ again:
831 \begin{code}\begin{verbatim}
832 \def\@LN@screenoff@pen{%
833 \ifdim\lastskip=\z@
834 \@tempdima\prevdepth \setbox\@tempboxa\null
835 \@LN@depthbox \fi}
836
837 \global\let\@LN@parpgbrk\@LN@screenoff@pen
838 \end{verbatim}
839 \end{code}
840 (/New v4.4, v4.00)
841 \subsection{%
842 Turning on/off %% own subsec. v4.4.
843 \unskip}\label{ss:OnOff}
844 The basic commands to enable and disable line numbers.
845 ~\@par~ and ~\par~ are only touched, when they are ~\let~
846 to ~\@@@par~/~\linenumberpar~. The line number may be
847 reset to 1 with the star-form, or set by an optional
848 argument ~[~\angle{number}~]~.
849
850 (New v4.00) We add ~\ifLineNumbers~ etc.\ since
851 a number of our new adjustments need to know whether
852 linenumbering is active. This just provides a kind of
853 shorthand for ~\ifx\@@par\linenumberpar~; moreover it is
854 more stable: who knows what may happen to ~\@@par~?---A
855 caveat: ~\ifLineNumbers~ may be wrong. E.g., it may be
856 ~\iffalse~ where it acts, while a ~\linenumbers~ a few
857 lines below---in the same paragraph---brings about that
858 the line where the ~\ifLineNumbers~ appears gets a
859 marginal number.
860 (New v4.3) Just noticed: Such tricks have been
861 disallowed with v4.11, see subsections_\ref{ss:LL}
862 and_\ref{ss:OnOff}.---Moreover, the switching between
863 meanings of ~\linelabel~ for a possible error message
864 as of v4.11 is removed. Speed is difficult to esteem
865 and also depends on applications. Just use the most
866 simple code you find. (/New v4.3)
867 \begin{code}\begin{verbatim}
868 \newif\ifLineNumbers \LineNumbersfalse
869 \end{verbatim}
870 \end{code}
871 (/New v4.00)
872 \begin{code}\begin{verbatim}
873 \def\linenumbers{%
874 \LineNumberstrue % v4.00
875 \xdef\@LN@outer@holdins{\the\holdinginserts}% v4.3
876 \end{verbatim}
877 \end{code}
878 (New v4.3) The previous line is for ~{linenomath}~
879 in a first numbered paragraph. (/New v4.3)
880 \begin{code}\begin{verbatim}
881 \let\@@par\linenumberpar
882 % \let\linelabel\@LN@linelabel % v4.11, removed v4.3
883 \ifx\@par\@@@par\let\@par\linenumberpar\fi
884 \ifx\par\@@@par\let\par\linenumberpar\fi
885 \@LN@maybe@moduloresume % v4.31
886 \@ifnextchar[{\resetlinenumber}%]
887 {\@ifstar{\resetlinenumber}{}}%
888 }
889
890 \def\nolinenumbers{%
891 \LineNumbersfalse % v4.00
892 \let\@@par\@@@par
893 % \let\linelabel\@LN@LLerror % v4.11, removed v4.3
894 \ifx\@par\linenumberpar\let\@par\@@@par\fi
895 \ifx\par\linenumberpar\let\par\@@@par\fi
896 }
897 \end{verbatim}
898 \end{code}
899 (New v4.00) Moreover, it is useful to switch to
900 ~\nolinenumbers~ in ~\@arrayparboxrestore~. We postpone this
901 to section_\ref{ss:ReDef} where we'll have an appending macro
902 for doing this. (/New v4.00)
903
904 What happens with a display math? Since ~\par~ is not executed,
905 when breaking the lines before a display, they will not get
906 line numbers. Sorry, but I do not dare to change
907 ~\interlinepenalty~ globally, nor do I want to redefine
908 the display math environments here.
909 \begin{displaymath}
910 display \ math
911 \end{displaymath}
912 See the subsection below, for a wrapper environment to make
913 it work. But that requires to wrap each and every display
914 in your \LaTeX\ source %%.
915 (see option ~displaymath~ in subsections_\ref{ss:v3opts}
916 and_\ref{ss:display} for some relief [UL]).
917
918 The next two commands are provided to turn on line
919 numbering in a specific mode. Please note the difference:
920 for pagewise numbering, ~\linenumbers~ comes first to
921 inhibit it from seeing optional arguments, since
922 re-/presetting the counter is useless.
923 \begin{code}\begin{verbatim}
924 \def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}
925 \def\runninglinenumbers{\setrunninglinenumbers\linenumbers}
926 \end{verbatim}
927 \end{code}
928 Finally, it is a \LaTeX\ style, so we provide for the use
929 of environments, including the suppression of the
930 following paragraph's indentation.
931
932 (UL) I am drawing the following
933 private thoughts of Stephan's to publicity so that others may
934 think about them---or to remind myself of them in an efficient
935 way. (/UL)
936 \begin{old}\begin{verbatim}
937 % TO DO: add \par to \linenumbers, if called from an environment.
938 % To DO: add an \@endpe hack if \linenumbers are turned on
939 % in horizontal mode. {\par\parskip\z@\noindent} or
940 % something.
941 \end{verbatim}
942 \end{old}
943 (UL) However, I rather think that ~\linenumbers~ and %% v4.31
944 ~\nolinenumbers~ should execute a ~\par~ already. (Then the
945 ~\par~s in the following definitions should be removed.) (/UL)
946 \begin{code}\begin{verbatim}
947 \@namedef{linenumbers*}{\par\linenumbers*}
948 \@namedef{runninglinenumbers*}{\par\runninglinenumbers*}
949
950 \def\endlinenumbers{\par\@endpetrue}
951 \let\endrunninglinenumbers\endlinenumbers
952 \let\endpagewiselinenumbers\endlinenumbers
953 \expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers
954 \expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers
955 \let\endnolinenumbers\endlinenumbers
956 \end{verbatim}
957 \end{code}
958
959 \subsection{%
960 Display math
961 \unskip}\label{ss:DM}
962
963 Now we tackle the problem to get display math working.
964 There are different options.
965 \begin{enumerate}\item[
966 1.] Precede every display math with a ~\par~.
967 Not too good.
968 \item[
969 2.] Change ~\interlinepenalty~ and associates globally.
970 Unstable.
971 \item[
972 3.] Wrap each display math with a ~{linenomath}~
973 environment.
974 \end{enumerate}
975 We'll go for option 3. See if it works:
976 \begin{linenomath}
977 \begin{equation}
978 display \ math
979 \end{equation}
980 \end{linenomath}
981 The star form ~{linenomath*}~ should also number the lines
982 of the display itself,
983 \begin{linenomath*}
984 \begin{eqnarray}
985 multi && line \\
986 display && math \\
987 &
988 \begin{array}{c}
989 with \\
990 array
991 \end{array}
992 &
993 \end{eqnarray}
994 \end{linenomath*}
995 including multline displays.
996
997 First, here are two macros to turn
998 on linenumbering on paragraphs preceeding displays, with
999 numbering the lines of the display itself, or without.
1000 The ~\ifx..~ tests if line numbering is turned on. It
1001 does not harm to add these wrappers in sections that are
1002 not numbered. Nor does it harm to wrap a display
1003 twice, e.q, in case you have some ~{equation}~s wrapped
1004 explicitely, and later you redefine ~\equation~ to do it
1005 automatically.
1006
1007 (New v4.3) To avoid the spurious line number above a
1008 display in vmode, I insert ~\ifhmode~. (/New v4.3)
1009 \begin{code}\begin{verbatim}
1010 \newcommand\linenomathNonumbers{%
1011 \ifLineNumbers
1012 \ifnum\interlinepenalty>-\linenopenaltypar
1013 \global\holdinginserts\thr@@
1014 \advance\interlinepenalty \linenopenalty
1015 \ifhmode % v4.3
1016 \advance\predisplaypenalty \linenopenalty
1017 \fi
1018 \fi
1019 \fi
1020 \ignorespaces
1021 }
1022
1023 \newcommand\linenomathWithnumbers{%
1024 \ifLineNumbers
1025 \ifnum\interlinepenalty>-\linenopenaltypar
1026 \global\holdinginserts\thr@@
1027 \advance\interlinepenalty \linenopenalty
1028 \ifhmode % v4.3
1029 \advance\predisplaypenalty \linenopenalty
1030 \fi
1031 \advance\postdisplaypenalty \linenopenalty
1032 \advance\interdisplaylinepenalty \linenopenalty
1033 \fi
1034 \fi
1035 \ignorespaces
1036 }
1037 \end{verbatim}
1038 \end{code}
1039 The ~{linenomath}~ environment has two forms, with and
1040 without a star. The following two macros define the
1041 environment, where the stared/non-stared form does/doesn't number the
1042 lines of the display or vice versa.
1043 \begin{code}\begin{verbatim}
1044 \newcommand\linenumberdisplaymath{%
1045 \def\linenomath{\linenomathWithnumbers}%
1046 \@namedef{linenomath*}{\linenomathNonumbers}%
1047 }
1048
1049 \newcommand\nolinenumberdisplaymath{%
1050 \def\linenomath{\linenomathNonumbers}%
1051 \@namedef{linenomath*}{\linenomathWithnumbers}%
1052 }
1053
1054 \def\endlinenomath{%
1055 \ifLineNumbers % v4.3
1056 \global\holdinginserts\@LN@outer@holdins % v4.21
1057 \fi
1058 \global % v4.21 support for LaTeX2e earlier than 1996/07/26.
1059 \@ignoretrue
1060 }
1061 \expandafter\let\csname endlinenomath*\endcsname\endlinenomath
1062 \end{verbatim}
1063 \end{code}
1064 The default is not to number the lines of a display. But
1065 the package option ~mathlines~ may be used to switch
1066 that behavior.
1067 \begin{code}\begin{verbatim}
1068 \nolinenumberdisplaymath
1069 \end{verbatim}
1070 \end{code}
1071
1072 \section{%
1073 Line number references
1074 \unskip}\label{s:LNref}
1075 \subsection{%
1076 Internals %% New subsec. v4.3.
1077 \unskip}
1078 The only way to get a label to a line number in a
1079 paragraph is to ask the output routine to mark it.
1080
1081 (New v4.00) The following two paragraphs don't hold any
1082 longer, see below. (/New v4.00)
1083 \begin{old}\begin{verbatim}
1084 % We use the marginpar mechanism to hook to ~\output~ for a
1085 % second time. Marginpars are floats with number $-1$, we
1086 % fake marginpars with No $-2$. Originally, every negative
1087 % numbered float was considered to be a marginpar.
1088 %
1089 % The float box number ~\@currbox~ is used to transfer the
1090 % label name in a macro called ~\@LNL@~\angle{box-number}.
1091 \end{verbatim}
1092 \end{old}
1093 A ~\newlabel~ is written to the aux-file. The reference
1094 is to ~\theLineNumber~, \emph{not} ~\thelinenumber~.
1095 This allows to hook in, as done below for pagewise line
1096 numbering.
1097
1098 (New v3.03) The ~\@LN@ExtraLabelItems~ are added for a hook
1099 to keep packages like ~{hyperref}~ happy. (/New v3.03)
1100
1101 (New v4.00)
1102 We fire the ~\marginpar~ mechanism, so we leave \LaTeX's
1103 ~\@addmarginpar~ untouched.
1104 \begin{old}\begin{verbatim}
1105 % \let\@LN@addmarginpar\@addmarginpar
1106 % \def\@addmarginpar{%
1107 % \ifnum\count\@currbox>-2\relax
1108 % \expandafter\@LN@addmarginpar
1109 % \else
1110 % \@cons\@freelist\@currbox
1111 % \protected@write\@auxout{}{%
1112 % \string\newlabel
1113 % {\csname @LNL@\the\@currbox\endcsname}%
1114 % {{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%
1115 % \fi}
1116 \end{verbatim}
1117 \end{old}
1118 OK, we keep Stephan's ~\@LN@ExtraLabelItems~:
1119 (/New v4.00)
1120 \begin{code}\begin{verbatim}
1121 \let\@LN@ExtraLabelItems\@empty
1122 \end{verbatim}
1123 \end{code}
1124 (New v4.00)
1125 We imitate the ~\marginpar~ mechanism without using the
1126 ~\@freelist~ boxes. ~\linelabel~ will indeed place a signal
1127 penalty (~\@Mllbcodepen~, new), and it will put a label into
1128 some list macro ~\@LN@labellist~. A new part of the output
1129 routine will take the labels from the list and will write
1130 ~\newlabel~s to the .aux file.
1131
1132 The following is a version of \LaTeX's ~\@xnext~.
1133 \begin{code}\begin{verbatim}
1134 \def\@LN@xnext#1\@lt#2\@@#3#4{\def#3{#1}\gdef#4{#2}}
1135 \end{verbatim}
1136 \end{code}
1137 This takes an item ~#1~ from a list ~#4~ into ~#3~;
1138 to be used as ~\expandafter\@LN@xnext#4\@@#3#4~.
1139 Our lists use ~\@lt~ after each item for separating.
1140 Indeed, there will be another list macro which can
1141 appear as argument ~#4~, this will be used for moving
1142 ~\vadjust~ items (section_\ref{ss:PVadj}).
1143 The list for ~\linelabel~s is the following:
1144 \begin{code}\begin{verbatim}
1145 \global\let\@LN@labellist\@empty
1146 \end{verbatim}
1147 \end{code}
1148 The next is the new part of the output routine writing the
1149 ~\newlabel~ to the .aux file. Since it is no real page output,
1150 the page is put back to top of the main vertical list.
1151 \begin{code}\begin{verbatim}
1152 \def\WriteLineNo{%
1153 \unvbox\@cclv
1154 \expandafter \@LN@xnext \@LN@labellist \@@
1155 \@LN@label \@LN@labellist
1156 \protected@write\@auxout{}{\string\newlabel{\@LN@label}%
1157 {{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%
1158 }
1159 \end{verbatim}
1160 \end{code}
1161 (/New v4.00)
1162
1163 \subsection{%
1164 The \scs{linelabel} command
1165 \unskip}\label{ss:LL}
1166 To refer to a place in line ~\ref{~\angle{foo}~}~ at page
1167 ~\pageref{~\angle{foo}~}~ you place a ~\linelabel{~\angle{foo}~}~ at
1168 that place.
1169
1170 \linelabel{demo}
1171 \marginpar{\tiny\raggedright
1172 See if it works: This paragraph
1173 starts on page \pageref{demo}, line
1174 \ref{demo}.
1175 \unskip}%
1176 (New v4.11)
1177 \begin{old}\begin{verbatim}
1178 % If you use this command outside a ~\linenumbers~
1179 % paragraph, you will get references to some bogus
1180 % line numbers, sorry. But we don't disable the command,
1181 % because only the ~\par~ at the end of a paragraph may
1182 % decide whether to print line numbers on this paragraph
1183 % or not. A ~\linelabel~ may legally appear earlier than
1184 % ~\linenumbers~.
1185 \end{verbatim}
1186 \end{old}
1187 This trick is better not allowed---see subsections_\ref{ss:LL}
1188 and_\ref{ss:OnOff}.
1189 (/New v4.11)
1190
1191 ~\linelabel~
1192 \begin{old}\begin{verbatim}
1193 %, via a fake float number $-2$, %% new mechanism v4.00
1194 \end{verbatim}
1195 \end{old}
1196 puts a
1197 ~\penalty~ into a ~\vadjust~, which triggers the
1198 pagebuilder after putting the current line to the main
1199 vertical list. A ~\write~ is placed on the main vertical
1200 list, which prints a reference to the current value of
1201 ~\thelinenumber~ and ~\thepage~ at the time of the
1202 ~\shipout~.
1203
1204 A ~\linelabel~ is allowed only in outer horizontal mode.
1205 In outer vertical mode we start a paragraph, and ignore
1206 trailing spaces (by fooling ~\@esphack~).
1207
1208 (New v4.00) We aim at relaxing the previous condition.
1209 We insert a hook ~\@LN@mathhook~ and a shorthand
1210 ~\@LN@postlabel~ to support the ~mathrefs~ option which
1211 allows ~\linelabel~ in math mode.
1212
1213 The next paragraph is no longer valid.
1214 \begin{old}\begin{verbatim}
1215 % The argument of ~\linelabel~ is put into a macro with a
1216 % name derived from the number of the allocated float box.
1217 % Much of the rest is dummy float setup.
1218 \end{verbatim}
1219 \end{old}
1220 (/New v4.00)
1221
1222 (New v4.11)
1223 \begin{old}\begin{verbatim}
1224 % \def\linelabel#1{%
1225 \end{verbatim}
1226 \end{old}
1227 I forgot ~\linenumbers~ today, costed me hours or so.
1228 \begin{code}\begin{verbatim}
1229 \def\@LN@LLerror{\PackageError{lineno}{%
1230 \string\linelabel\space without \string\linenumbers}{%
1231 Just see documentation. (New feature v4.11)}\@gobble}
1232 \end{verbatim}
1233 \end{code}
1234 (New v4.3) Here some things have changed for v4.3.
1235 The previous ~#1~ has been replaced by ~\@gobble~.
1236 Ensuing, the ~\linelabel~ error message is re-implemented.
1237 I find it difficult to compare efficiency of slight
1238 alternatives---so choose an easy one. Explicit switching
1239 in ~\linenumbers~ and ~\nolinenumbers~ is an additional
1240 command that may better be avoided.
1241 \begin{code}\begin{verbatim}
1242 \newcommand\linelabel{%
1243 \ifLineNumbers \expandafter \@LN@linelabel
1244 \else \expandafter \@LN@LLerror \fi}
1245
1246 \gdef\@LN@linelabel#1{%
1247 \end{verbatim}
1248 \end{code}
1249 ~\gdef~ for hyperref ``symbolically''. (/New v4.11)
1250 \begin{code}\begin{verbatim}
1251 \ifx\protect\@typeset@protect
1252 \end{verbatim}
1253 \end{code}
1254 $\gets$ And a ~\linelabel~ should never be replicated in a
1255 mark or a TOC entry. (/New v4.3)
1256 \begin{code}\begin{verbatim}
1257 \ifvmode
1258 \ifinner \else
1259 \leavevmode \@bsphack \@savsk\p@
1260 \fi
1261 \else
1262 \@bsphack
1263 \fi
1264 \ifhmode
1265 \ifinner
1266 \@parmoderr
1267 \else
1268 \end{verbatim}
1269 \end{code}
1270 (New v4.00)
1271 \begin{code}\begin{verbatim}
1272 \@LN@postlabel{#1}%
1273 \end{verbatim}
1274 \end{code}
1275 \begin{old}\begin{verbatim}
1276 % \@floatpenalty -\@Mii
1277 % \@next\@currbox\@freelist
1278 % {\global\count\@currbox-2%
1279 % \expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%
1280 % {\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%
1281 % \begingroup
1282 % \setbox\@currbox \color@vbox \vbox \bgroup \end@float
1283 % \endgroup
1284 % \@ignorefalse \@esphack
1285 \end{verbatim}
1286 \end{old}
1287 (/New v4.00)
1288 \begin{code}\begin{verbatim}
1289 \@esphack
1290 \end{verbatim}
1291 \end{code}
1292 (New v4.00)
1293 The ~\@ignorefalse~ was appropriate before because the
1294 ~\@Esphack~ in ~\end@float~ set ~\@ignoretrue~. Cf._\LaTeX's
1295 ~\@xympar~. (/New v4.00)
1296 \begin{code}\begin{verbatim}
1297 \fi
1298 \else
1299 \end{verbatim}
1300 \end{code}
1301 (New v4.00)
1302 \begin{code}\begin{verbatim}
1303 \@LN@mathhook{#1}%
1304 \end{verbatim}
1305 \end{code}
1306 \begin{old}\begin{verbatim}
1307 % \@parmoderr
1308 \end{verbatim}
1309 \end{old}
1310 Instead of complaining, you may just do your job.
1311 (/New v4.00)
1312 \begin{code}\begin{verbatim}
1313 \fi
1314 \fi
1315 }
1316 \end{verbatim}
1317 \end{code}
1318 (New v4.00) The shorthand just does what happened
1319 with ~linenox0.sty~ before ~ednmath0.sty~ (New v4.1:
1320 now ~mathrefs~ option) appeared, and
1321 the hook is initialized to serve the same purpose.
1322 So errors come just where Stephan had built them in,
1323 and this is just the \LaTeX\ ~\marginpar~ behaviour.
1324 \begin{code}\begin{verbatim}
1325 \def\@LN@postlabel#1{\g@addto@macro\@LN@labellist{#1\@lt}%
1326 \vadjust{\penalty-\@Mllbcodepen}}
1327 \def\@LN@mathhook#1{\@parmoderr}
1328 \end{verbatim}
1329 \end{code}
1330 (/New v4.00)
1331
1332 \modulolinenumbers[3]
1333 \firstlinenumber{1}
1334 \section{%
1335 The appearance of the line numbers
1336 \unskip}\label{appearance}
1337 \subsection{%
1338 Basic code %% own subsec. v4.2.
1339 \unskip}
1340
1341 The line numbers are set as ~\tiny\sffamily\arabic{linenumber}~,
1342 $10pt$ left of the text. With options to place it
1343 right of the text, or . . .
1344
1345 . . . here are the hooks:
1346 \begin{code}\begin{verbatim}
1347 \def\makeLineNumberLeft{%
1348 \hss\linenumberfont\LineNumber\hskip\linenumbersep}
1349
1350 \def\makeLineNumberRight{%
1351 \linenumberfont\hskip\linenumbersep\hskip\columnwidth
1352 \hb@xt@\linenumberwidth{\hss\LineNumber}\hss}
1353
1354 \def\linenumberfont{\normalfont\tiny\sffamily}
1355
1356 \newdimen\linenumbersep
1357 \newdimen\linenumberwidth
1358
1359 \linenumbersep=10pt
1360 \linenumberwidth=10pt
1361 \end{verbatim}
1362 \end{code}
1363 Margin switching requires ~pagewise~ numbering mode, but
1364 choosing the left or right margin for the numbers always
1365 works.
1366 \begin{code}\begin{verbatim}
1367 \def\switchlinenumbers{\@ifstar
1368 {\let\makeLineNumberOdd\makeLineNumberRight
1369 \let\makeLineNumberEven\makeLineNumberLeft}%
1370 {\let\makeLineNumberOdd\makeLineNumberLeft
1371 \let\makeLineNumberEven\makeLineNumberRight}%
1372 }
1373
1374 \def\setmakelinenumbers#1{\@ifstar
1375 {\let\makeLineNumberRunning#1%
1376 \let\makeLineNumberOdd#1%
1377 \let\makeLineNumberEven#1}%
1378 {\ifx\c@linenumber\c@runninglinenumber
1379 \let\makeLineNumberRunning#1%
1380 \else
1381 \let\makeLineNumberOdd#1%
1382 \let\makeLineNumberEven#1%
1383 \fi}%
1384 }
1385
1386 \def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}
1387 \def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}
1388
1389 \leftlinenumbers*
1390 \end{verbatim}
1391 \end{code}
1392 ~\LineNumber~ is a hook which is used for the modulo stuff.
1393 It is the command to use for the line number, when you
1394 customize ~\makeLineNumber~. Use ~\thelinenumber~ to
1395 change the outfit of the digits.
1396
1397
1398 We will implement two modes of operation:
1399 \begin{itemize}
1400 \item numbers ~running~ through (parts of) the text
1401 \item ~pagewise~ numbers starting over with one on top of
1402 each page.
1403 \end{itemize}
1404 Both modes have their own count register, but only one is
1405 allocated as a \LaTeX\ counter, with the attached
1406 facilities serving both.
1407 \begin{code}\begin{verbatim}
1408 \newcounter{linenumber}
1409 \newcount\c@pagewiselinenumber
1410 \let\c@runninglinenumber\c@linenumber
1411 \end{verbatim}
1412 \end{code}
1413 Only the running mode counter may be reset, or preset,
1414 for individual paragraphs. The pagewise counter must
1415 give a unique anonymous number for each line.
1416
1417 (New v4.3) ~\newcounter{linenumber}~
1418 was the only ~\newcounter~ in the whole package, and
1419 formerly I was near using ~\newcount~ instead. Yet
1420 ~\newcounter~ may be quite useful for ~\includeonly~.
1421 It also supports resetting ``subcounters'', but what
1422 could these be? Well, ~edtable~ might introduce a
1423 subcounter for columns.
1424 (Note that \LaTeX's setting commands would work with
1425 ~\newcount\c@linenumber~ already, apart from this.
1426 And perhaps sometimes ~\refstepcounter{linenumber}~
1427 wouldn't work---cf._my discussion of ~\stepcounter~ in
1428 subsection_\ref{ss:MLN}, similarly ~\refstep...~ would
1429 be quite useless.
1430 Even the usual redefinitions of ~\thelinenumber~ would
1431 work. It is nice, on the other hand, that
1432 ~\thelinenumber~ is predefined here. \LaTeX's
1433 initialization of the value perhaps just serves making
1434 clear \LaTeX\ counters should always be changed
1435 globally.---Shortened and improved the discussion here.)
1436 (/New v4.3)
1437
1438 (New v4.22)
1439 ~\c@linenumber~ usually is---globally---incremented by
1440 ~\stepcounter~ (at present), so resetting it locally would
1441 raise the save stack problem of \TeX book p._301, moreover
1442 it would be is useless, there is no hope of keeping the
1443 values local (but see subsection_\ref{ss:ILN}). So I insert
1444 ~\global~: (/New v4.22)
1445 \begin{code}\begin{verbatim}
1446 \newcommand*\resetlinenumber[1][\@ne]{%
1447 \global % v4.22
1448 \c@runninglinenumber#1\relax}
1449 \end{verbatim}
1450 \end{code}
1451 (New v4.00)
1452 \begin{old}\begin{verbatim}
1453 % \newcommand\resetlinenumber[1][1]{\c@runninglinenumber#1}
1454 \end{verbatim}
1455 \end{old}
1456 Added ~\relax~, being quite sure that this does no harm
1457 and is quite important, as with ~\setcounter~ etc.
1458 I consider this a bug fix (although perhaps no user has
1459 ever had a problem with this). (/New v4.00)
1460
1461 (v4.22: I had made much fuss about resetting subordinate
1462 counters here---removed, somewhat postponed.)
1463
1464
1465 \subsection{%
1466 Running line numbers
1467 \unskip}
1468
1469 Running mode is easy, ~\LineNumber~ and ~\theLineNumber~
1470 produce ~\thelinenumber~, which defaults to
1471 ~\arabic{linenumber}~, using the ~\c@runninglinenumber~
1472 counter. This is the default mode of operation.
1473 \begin{code}\begin{verbatim}
1474 \def\makeRunningLineNumber{\makeLineNumberRunning}
1475
1476 \def\setrunninglinenumbers{%
1477 \def\theLineNumber{\thelinenumber}%
1478 \let\c@linenumber\c@runninglinenumber
1479 \let\makeLineNumber\makeRunningLineNumber
1480 }
1481
1482 \setrunninglinenumbers\resetlinenumber
1483 \end{verbatim}
1484 \end{code}
1485
1486 \subsection{%
1487 Pagewise line numbers
1488 \unskip}\label{ss:PW}
1489
1490 Difficult, if you think about it. The number has to be
1491 printed when there is no means to know on which page it
1492 will end up, except through the aux-file. My solution
1493 is really expensive, but quite robust.
1494
1495 With version ~v2.00~ the hashsize requirements are
1496 reduced, because we do not need one controlsequence for
1497 each line any more. But this costs some computation time
1498 to find out on which page we are.
1499
1500 ~\makeLineNumber~ gets a hook to log the line and page
1501 number to the aux-file. Another hook tries to find out
1502 what the page offset is, and subtracts it from the counter
1503 ~\c@linenumber~. Additionally, the switch
1504 ~\ifoddNumberedPage~ is set true for odd numbered pages,
1505 false otherwise.
1506 \begin{code}\begin{verbatim}
1507 \def\setpagewiselinenumbers{%
1508 \let\theLineNumber\thePagewiseLineNumber
1509 \let\c@linenumber\c@pagewiselinenumber
1510 \let\makeLineNumber\makePagewiseLineNumber
1511 }
1512
1513 \def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber
1514 \ifoddNumberedPage
1515 \makeLineNumberOdd
1516 \else
1517 \makeLineNumberEven
1518 \fi
1519 }
1520 \end{verbatim}
1521 \end{code}
1522 Each numbered line gives a line to the aux file
1523 \begin{verse}
1524 ~\@LN{~\angle{line}~}{~\angle{page}~}~
1525 \end{verse}
1526 very similar to the ~\newlabel~ business, except that we need
1527 an arabic representation of the page number, not what
1528 there might else be in ~\thepage~.
1529 \begin{code}\begin{verbatim}
1530 \def\logtheLineNumber{\protected@write\@auxout{}{%
1531 \end{verbatim}
1532 \end{code}
1533 (New v4.00) (UL)
1534 As Daniel Doherty observed, the earlier line
1535 \begin{old}\begin{verbatim}
1536 % \string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}
1537 \end{verbatim}
1538 \end{old}
1539 here may lead into an infinite loop when the user resets
1540 the page number (think of ~\pagenumbering~, e.g.).
1541 Stephan and I brief\/ly discussed the matter and decided
1542 to introduce a ``physical''-page counter to which
1543 ~\logtheLineNumber~ refers. It was Stephan's idea to use
1544 ~\cl@page~ for reliably augmenting the ``physical''-page
1545 counter. However, this relies on the output routine once
1546 doing ~\stepcounter{page}~. Before Stephan's
1547 suggestion, I had thought of appending the stepping to
1548 \LaTeX's ~\@outputpage~.---So the macro definition ends
1549 as follows.
1550 \begin{code}\begin{verbatim}
1551 \string\@LN{\the\c@linenumber}{%
1552 \end{verbatim}
1553 \end{code}
1554 (New v4.2)
1555 The `truepage' counter must start with ~\c@~ so it works
1556 with ~\include~, and the ~\@addtoreset~ below is needed
1557 for the same purpose.
1558 \begin{code}\begin{verbatim}
1559 \noexpand\the\c@LN@truepage}}}
1560
1561 \newcount\c@LN@truepage
1562 \g@addto@macro\cl@page{\global\advance\c@LN@truepage\@ne}
1563 \@addtoreset{LN@truepage}{@ckpt}
1564 \end{verbatim}
1565 \end{code}
1566 (/New v4.2) I had thought of offering more
1567 features of a \LaTeX\ counter. However, the user should
1568 better \emph{not} have access to this counter. ~\c@page~
1569 should suffice as a pagewise master counter.---To be sure,
1570 along the present lines the user \emph{can} manipulate
1571 ~\c@LN@truepage~ by ~\stepcounter{page}~. E.g., she might
1572 do this in order to manually insert a photograph. Well,
1573 seems not to harm.
1574
1575 The above usage of ~\g@addto@macro~ and ~\cl@page~ may be
1576 not as stable as Stephan intended. His proposal used
1577 ~\xdef~ directly. But he used ~\cl@page~ as well, and who
1578 knows \dots{} And as to ~\g@addto@macro~, I have introduced
1579 it for list macros anyway.
1580 (/UL) (/New v4.00)
1581
1582 From the aux-file we get one macro ~\LN@P~\angle{page} for each
1583 page with line numbers on it. This macro calls four other
1584 macros with one argument each. These macros are
1585 dynamically defined to do tests and actions, to find out
1586 on which page the current line number is located.
1587
1588 We need sort of a pointer to the first page with line
1589 numbers, initiallized to point to nothing:
1590 \begin{code}\begin{verbatim}
1591 \def\LastNumberedPage{first}
1592 \def\LN@Pfirst{\nextLN\relax}
1593 \end{verbatim}
1594 \end{code}
1595 The four dynamic macros are initiallized to reproduce
1596 themselves in an ~\xdef~
1597 \begin{code}\begin{verbatim}
1598 \let\lastLN\relax % compare to last line on this page
1599 \let\firstLN\relax % compare to first line on this page
1600 \let\pageLN\relax % get the page number, compute the line number
1601 \let\nextLN\relax % move to the next page
1602 \end{verbatim}
1603 \end{code}
1604 During the end-document run through the aux-files, we
1605 disable ~\@LN~. I may put in a check here later, to give
1606 a rerun recommendation.
1607 \begin{code}\begin{verbatim}
1608 \AtEndDocument{\let\@LN\@gobbletwo}
1609 \end{verbatim}
1610 \end{code}
1611 Now, this is the tricky part. First of all, the whole
1612 definition of ~\@LN~ is grouped, to avoid accumulation
1613 on the save stack. Somehow ~\csname~\angle{cs}~\endcsname~ pushes
1614 an entry, which stays after an ~\xdef~ to that \angle{cs}.
1615
1616 If ~\LN@P~\angle{page} is undefined, initialize it with the
1617 current page and line number, with the
1618 \emph{pointer-to-the-next-page} pointing to nothing. And
1619 the macro for the previous page will be redefined to point
1620 to the current one.
1621
1622 If the macro for the current page already exists, just
1623 redefine the \emph{last-line-number} entry.
1624
1625 Finally, save the current page number, to get the pointer to the
1626 following page later.
1627 \begin{code}\begin{verbatim}
1628 \def\@LN#1#2{{\expandafter\@@LN
1629 \csname LN@P#2C\@LN@column\expandafter\endcsname
1630 \csname LN@PO#2\endcsname
1631 {#1}{#2}}}
1632
1633 \def\@@LN#1#2#3#4{\ifx#1\relax
1634 \ifx#2\relax\gdef#2{#3}\fi
1635 \expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1%
1636 \xdef#1{\lastLN{#3}\firstLN{#3}%
1637 \pageLN{#4}{\@LN@column}{#2}\nextLN\relax}%
1638 \else
1639 \def\lastLN##1{\noexpand\lastLN{#3}}%
1640 \xdef#1{#1}%
1641 \fi
1642 \xdef\LastNumberedPage{#4C\@LN@column}}
1643 \end{verbatim}
1644 \end{code}
1645 The previous page macro gets its pointer to the
1646 current one, replacing the ~\relax~ with the cs-token
1647 ~\LN@P~\angle{page}.
1648 \begin{code}\begin{verbatim}
1649 \def\@@@LN#1#2{{\def\nextLN##1{\noexpand\nextLN\noexpand#2}%
1650 \xdef#1{#1}}}
1651 \end{verbatim}
1652 \end{code}
1653 Now, to print a line number, we need to find the page,
1654 where it resides. This will most probably be the page where
1655 the last one came from, or maybe the next page. However, it can
1656 be a completely different one. We maintain a cache,
1657 which is ~\let~ to the last page's macro. But for now
1658 it is initialized to expand ~\LN@first~, where the poiner
1659 to the first numbered page has been stored in.
1660 \begin{code}\begin{verbatim}
1661 \def\NumberedPageCache{\LN@Pfirst}
1662 \end{verbatim}
1663 \end{code}
1664 To find out on which page the current ~\c@linenumber~ is,
1665 we define the four dynamic macros to do something usefull
1666 and execute the current cache macro. ~\lastLN~ is run
1667 first, testing if the line number in question may be on a
1668 later page. If so, disable ~\firstLN~, and go on to the
1669 next page via ~\nextLN~.
1670 \begin{code}\begin{verbatim}
1671 \def\testLastNumberedPage#1{\ifnum#1<\c@linenumber
1672 \let\firstLN\@gobble
1673 \fi}
1674 \end{verbatim}
1675 \end{code}
1676 Else, if ~\firstLN~ finds out that we need an earlier
1677 page, we start over from the beginning. Else, ~\nextLN~
1678 will be disabled, and ~\pageLN~ will run
1679 ~\gotNumberedPage~ with four arguments: the first line
1680 number on this column, the page number, the column
1681 number, and the first line on the page.
1682 \begin{code}\begin{verbatim}
1683 \def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber
1684 \def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%
1685 \else
1686 \let\nextLN\@gobble
1687 \def\pageLN{\gotNumberedPage{#1}}%
1688 \fi}
1689 \end{verbatim}
1690 \end{code}
1691 We start with ~\pageLN~ disabled and ~\nextLN~ defined to
1692 continue the search with the next page.
1693 \begin{code}\begin{verbatim}
1694 \long\def \@gobblethree #1#2#3{}
1695
1696 \def\testNumberedPage{%
1697 \let\lastLN\testLastNumberedPage
1698 \let\firstLN\testFirstNumberedPage
1699 \let\pageLN\@gobblethree
1700 \let\nextLN\testNextNumberedPage
1701 \NumberedPageCache
1702 }
1703 \end{verbatim}
1704 \end{code}
1705 When we switch to another page, we first have to make
1706 sure that it is there. If we are done with the last
1707 page, we probably need to run \TeX\ again, but for the
1708 rest of this run, the cache macro will just return four
1709 zeros. This saves a lot of time, for example if you have
1710 half of an aux-file from an aborted run, in the next run
1711 the whole page-list would be searched in vain again and
1712 again for the second half of the document.
1713
1714 If there is another page, we iterate the search.
1715 \begin{code}\begin{verbatim}
1716 \def\testNextNumberedPage#1{\ifx#1\relax
1717 \global\def\NumberedPageCache{\gotNumberedPage0000}%
1718 \PackageWarningNoLine{lineno}%
1719 {Line number reference failed, re-run to get it right}%
1720 \else
1721 \global\let\NumberedPageCache#1%
1722 \fi
1723 \testNumberedPage
1724 }
1725 \end{verbatim}
1726 \end{code}
1727 \linelabel{demo2}
1728 \marginpar{\tiny\raggedright
1729 Let's see if it finds the label
1730 on page \pageref{demo},
1731 line \ref{demo}, and back here
1732 on page \pageref{demo2}, line
1733 \ref{demo2}.
1734 \unskip}%
1735 To separate the official hooks from the internals there is
1736 this equivalence, to hook in later for whatever purpose:
1737 \begin{code}\begin{verbatim}
1738 \let\getLineNumber\testNumberedPage
1739 \end{verbatim}
1740 \end{code}
1741 So, now we got the page where the number is on. We
1742 establish if we are on an odd or even page, and calculate
1743 the final line number to be printed.
1744 \begin{code}\begin{verbatim}
1745 \newif\ifoddNumberedPage
1746 \newif\ifcolumnwiselinenumbers
1747 \columnwiselinenumbersfalse
1748
1749 \def\gotNumberedPage#1#2#3#4{\oddNumberedPagefalse
1750 \ifodd \if@twocolumn #3\else #2\fi\relax\oddNumberedPagetrue\fi
1751 \advance\c@linenumber\@ne
1752 \ifcolumnwiselinenumbers
1753 \subtractlinenumberoffset{#1}%
1754 \else
1755 \subtractlinenumberoffset{#4}%
1756 \fi
1757 }
1758 \end{verbatim}
1759 \end{code}
1760 You might want to run the pagewise mode with running line
1761 numbers, or you might not. It's your choice:
1762 \begin{code}\begin{verbatim}
1763 \def\runningpagewiselinenumbers{%
1764 \let\subtractlinenumberoffset\@gobble
1765 }
1766
1767 \def\realpagewiselinenumbers{%
1768 \def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%
1769 }
1770
1771 \realpagewiselinenumbers
1772 \end{verbatim}
1773 \end{code}
1774 For line number references, we need a protected call to
1775 the whole procedure, with the requested line number stored
1776 in the ~\c@linenumber~ counter. This is what gets printed
1777 to the aux-file to make a label:
1778 \begin{code}\begin{verbatim}
1779 \def\thePagewiseLineNumber{\protect
1780 \getpagewiselinenumber{\the\c@linenumber}}%
1781 \end{verbatim}
1782 \end{code}
1783 And here is what happens when the label is refered to:
1784 \begin{code}\begin{verbatim}
1785 \def\getpagewiselinenumber#1{{%
1786 \c@linenumber #1\relax\testNumberedPage
1787 \thelinenumber
1788 }}
1789 \end{verbatim}
1790 \end{code}
1791 %
1792 A summary of all per line expenses:
1793 \begin{description}\item
1794 [CPU:] The ~\output~ routine is called for each line,
1795 and the page-search is done.
1796 \item
1797 [DISK:] One line of output to the aux-file for each
1798 numbered line
1799 \item
1800 [MEM:] One macro per page. Great improvement over v1.02,
1801 which had one control sequence per line in
1802 addition. It blew the hash table after some five
1803 thousand lines.
1804 \end{description}
1805
1806 \subsection{%
1807 Twocolumn mode (New v3.06)
1808 \unskip}
1809
1810 Twocolumn mode requires another patch to the ~\output~
1811 routine, in order to print a column tag to the .aux
1812 file.
1813 \begin{code}\begin{verbatim}
1814 \AtBeginDocument{% v4.2, revtex4.cls (e.g.).
1815 % <- TODO v4.4+: Or better in \LineNoLaTeXOutput!?
1816 \let\@LN@orig@makecol\@makecol
1817 }
1818
1819 \def\@LN@makecol{%
1820 \@LN@orig@makecol
1821 \setbox\@outputbox \vbox{%
1822 \boxmaxdepth \@maxdepth
1823 \protected@write\@auxout{}{%
1824 \string\def\string\@LN@column{\if@firstcolumn1\else2\fi}%
1825 }%
1826 \box\@outputbox
1827 }% \vbox
1828 } %% TODO cf. revtexln.sty.
1829
1830 \def\@LN@column{1}
1831
1832 \ifdefined\IfFormatAtLeastTF
1833 \IfFormatAtLeastTF{2025-06-01}{
1834 % disable \@LN@makecol
1835 \AtBeginDocument{%
1836 \let\@LN@makecol\@makecol
1837 }
1838 % replace with hook code
1839 \AddToHook{build/column/after}{%
1840 \setbox\@outputbox \vbox to\ht\@outputbox{%
1841 \boxmaxdepth \@maxdepth
1842 \protected@write\@auxout{}{%
1843 \string\def\string\@LN@column{\if@firstcolumn1\else2\fi}%
1844 }%
1845 \unvbox\@outputbox
1846 }%
1847 }
1848 }{
1849 }
1850 \fi
1851 \end{verbatim}
1852 \end{code}
1853
1854 \subsection{%
1855 Numbering modulo $m$, starting at $f$
1856 \unskip}\label{ss:Mod}
1857
1858 Most users want to have only one in five lines numbered.
1859 ~\LineNumber~ is supposed to produce the outfit of the
1860 line number attached to the line, while ~\thelinenumber~
1861 is used also for references, which should appear even if
1862 they are not multiples of five.
1863
1864 (New v4.00) Moreover, some users want to
1865 control which line number should be printed first. Support
1866 of this is now introduced here---see ~\firstlinenumber~
1867 below.---~numline.sty~ by Michael Jaegermann and
1868 James Fortune offers controlling which \emph{final}
1869 line numbers should not be printed. What is
1870 it good for? We ignore this here until some user demands
1871 it.---Peter Wilson's ~ledmac.sty~ offers much different
1872 choices of line numbers to be printed, due to Wayne Sullivan.
1873 (/New v4.00)
1874
1875 (New v4.22) ~\c@linenumbermodulo~ is rendered a
1876 fake counter, as discussed since v4.00. So it can
1877 no longer be set by ~\setcounter~. ~\modulolinenumbers~
1878 serves this purpose. Well, does anybody want to do
1879 what worked with ~\addtocounter~? (Then please tell
1880 me.)---At least, ~\value~ still works. For the same
1881 purpose I rename the fake `firstlinenumber' counter
1882 ~\n@...~ to ~\c@...~. (/New v4.22)
1883 \begin{old}\begin{verbatim}
1884 % \newcount\c@linenumbermodulo % removed for v4.22
1885 \end{verbatim}
1886 \end{old}
1887
1888
1889 (New v4.00) \par
1890 ~\themodulolinenumber~ waits for being declared
1891 ~\LineNumber~ by ~\modulolinenumbers~. (This has
1892 been so before, no change.) Here is how it
1893 looked before:
1894 \begin{old}\begin{verbatim}
1895 % \def\themodulolinenumber{{\@tempcnta\c@linenumber
1896 % \divide\@tempcnta\c@linenumbermodulo
1897 % \multiply\@tempcnta\c@linenumbermodulo
1898 % \ifnum\@tempcnta=\c@linenumber\thelinenumber\fi
1899 % }}
1900 \end{verbatim}
1901 \end{old}
1902 (UL) This was somewhat slow. This arithmetic
1903 happens at every line. This time I tend to declare an extra
1904 line counter (as opposed to my usual recommendations to use
1905 counters as rarely as possible) which is stepped every line.
1906 It could be incremented in the same way as ~\c@LN@truepage~
1907 is incremented via ~\cl@page~! This is another point in favour
1908 of ~{linenumber}~ being a \LaTeX\ counter!
1909 When this new counter equals ~\c@linenumbermodulo~, it is reset,
1910 and ~\thelinenumber~ is executed.---It gets much slower by my
1911 support of controlling the first line number below. I should
1912 improve this.---On
1913 the other hand, time expense means very little nowadays,
1914 while the number of \TeX\ counters still is limited.
1915
1916 For the same purpose, moreover, attaching the line number
1917 box could be intercepted earlier (in ~\MakeLineNo~),
1918 without changing ~\LineNumber~. However, this may be
1919 bad for the latter's announcement as a wizard interface
1920 in section_\ref{s:UserCmds}.
1921 (/UL)
1922
1923 Here is the new code. It is very near to my ~lnopatch.sty~
1924 code which introduced the first line number feature
1925 before.---I add starting with a ~\relax~ which is so often
1926 recommended---without understanding this really. At least,
1927 it will not harm.---Former group braces appear as
1928 ~\begingroup~/~\endgroup~ here.
1929 \begin{code}\begin{verbatim}
1930 \def\themodulolinenumber{\relax
1931 \ifnum\c@linenumber<\c@firstlinenumber \else
1932 \begingroup
1933 \@tempcnta\c@linenumber
1934 \advance\@tempcnta-\c@firstlinenumber
1935 \divide\@tempcnta\c@linenumbermodulo
1936 \multiply\@tempcnta\c@linenumbermodulo
1937 \advance\@tempcnta\c@firstlinenumber
1938 \ifnum\@tempcnta=\c@linenumber \thelinenumber \fi
1939 \endgroup
1940 \fi
1941 }
1942 \end{verbatim}
1943 \end{code}
1944 (/New v4.00)
1945
1946 The user command to set the modulo counter:
1947 (New v4.31) \dots\ a star variant is introduced to implement
1948 Hillel Chayim Yisraeli's idea to print the first line number
1949 after an interruption of the edited text by some editor's
1950 text, regardless of the modulo. If it is 1, it is printed only
1951 with ~\firstlinenumber{1}~. I.e., you use ~\modulolinenumbers*~
1952 for the new feature, without the star you get the simpler
1953 behaviour that we have had so far. And you can switch back
1954 from the refined behaviour to the simple one by using
1955 ~\modulolinenumbers~ without the star.---This enhancement
1956 is accompanied by a new package option ~modulo*~ which just
1957 executes ~\modulolinenumbers*~
1958 (subsection_\ref{ss:v3opts}).---`With ~\firstlinenumber{1}~'
1959 exactly means: `1' is printed if and only if the last
1960 ~\firstlinenumber~ before or in the paragraph that follows
1961 the ``interruption'' has argument `1' (or something
1962 \emph{expanding} to `1', or (to) something that \TeX\
1963 ``reads'' as 1, e.g.: a \TeX\ count register storing
1964 1).---At present, this behaviour may be unsatisfactory with
1965 pagewise line-numbering $\dots$ I'll make an experimental
1966 extra package if someone complains \dots
1967 \begin{code}\begin{verbatim}
1968 \newcommand\modulolinenumbers{%
1969 \@ifstar
1970 {\def\@LN@maybe@moduloresume{%
1971 \global\let\@LN@maybe@normalLineNumber
1972 \@LN@normalLineNumber}%
1973 \@LN@modulolinenos}%
1974 {\let\@LN@maybe@moduloresume\relax \@LN@modulolinenos}%
1975 }
1976
1977 \global\let\@LN@maybe@normalLineNumber\relax
1978 \let\@LN@maybe@moduloresume\relax
1979 \gdef\@LN@normalLineNumber{%
1980 \ifnum\c@linenumber=\c@firstlinenumber \else
1981 \ifnum\c@linenumber>\@ne
1982 \def\LineNumber{\thelinenumber}%
1983 \fi
1984 \fi
1985 \end{verbatim}
1986 \end{code}
1987 ~\def~ instead of ~\let~ enables taking account of a
1988 redefinition of ~\thelinenumber~ in a present numbering
1989 environment (e.g.).
1990 \begin{code}\begin{verbatim}
1991 \global\let\@LN@maybe@normalLineNumber\relax}
1992 \end{verbatim}
1993 \end{code}
1994 Instead of changing ~\LineNumber~ directly by
1995 ~LN@moduloresume~, these tricks enable ~\modulolinenumbers*~
1996 to act as locally as I can make it. I don't know how to
1997 avoid that the output routine switches back to the normal
1998 modulo behaviour by a global change. (An ~\aftergroup~ may
1999 fail in admittedly improbable cases.)
2000 \begin{code}\begin{verbatim}
2001 \newcommand*\@LN@modulolinenos[1][\z@]{%
2002 \end{verbatim}
2003 \end{code}
2004 The definition of this macro is that of the former
2005 ~\modulolinenumbers~. (/New v4.31)
2006 \begin{code}\begin{verbatim}
2007 \let\LineNumber\themodulolinenumber
2008 \ifnum#1>\@ne
2009 \chardef % v4.22, note below
2010 \c@linenumbermodulo#1\relax
2011 \else\ifnum#1=\@ne
2012 \end{verbatim}
2013 \end{code}
2014 \begin{old}\begin{verbatim}
2015 % \def\LineNumber{\thelinenumber}%
2016 \end{verbatim}
2017 \end{old}
2018 (New v4.00) I am putting something here to enable
2019 ~\firstlinenumber~ with $~\c@linenumbermodulo~=1$.
2020 With ~lnopatch.sty~, a trick was offered for this purpose.
2021 It is now obsolete.
2022
2023 \begin{code}\begin{verbatim}
2024 \def\LineNumber{\@LN@ifgreat\thelinenumber}%
2025 \end{verbatim}
2026 \end{code}
2027 (/New v4.00)
2028 \begin{code}\begin{verbatim}
2029 \fi\fi
2030 }
2031 \end{verbatim}
2032 \end{code}
2033 (New v4.00) The default of ~\@LN@ifgreat~ is
2034 \begin{code}\begin{verbatim}
2035 \let\@LN@ifgreat\relax
2036 \end{verbatim}
2037 \end{code}
2038 The previous changes as soon as ~\firstlinenumber~ is used:
2039 \begin{code}\begin{verbatim}
2040 \newcommand*\firstlinenumber[1]{%
2041 \chardef\c@firstlinenumber#1\relax
2042 \end{verbatim}
2043 \end{code}
2044 No counter, little values allowed only---OK?---(UL)
2045 The change is local---OK? The good thing is that
2046 ~\global\firstlinenumber{~\angle{number}~}~ works. Moreover,
2047 ~\modulolinenumbers~ acts locally as well. (/UL)
2048
2049 (New v4.31)
2050 \begin{code}\begin{verbatim}
2051 \let\@LN@ifgreat\@LN@ifgreat@critical}
2052
2053 \def\@LN@ifgreat@critical{%
2054 \ifnum\c@linenumber<\c@firstlinenumber
2055 \expandafter \@gobble
2056 \fi}%
2057 \end{verbatim}
2058 \end{code}
2059 (/New v4.31)
2060
2061 The default
2062 value of ~\c@firstlinenumber~ %% v4.31
2063 is 0. This is best for what one would expect from modulo
2064 printing.
2065 \begin{code}\begin{verbatim}
2066 \let\c@firstlinenumber=\z@
2067 \end{verbatim}
2068 \end{code}
2069
2070 For usage and effects of ~\modulolinenumbers~ and %% v4.31
2071 ~\firstlinenumbers~, please consult section_\ref{s:UserCmds}.
2072 Two details on ~\firstlinenumbers~ here:
2073 (i)_~\firstlinenumber~ acts on a paragraph if and only if
2074 (a)_the paragraph is broken into lines ``in line-numbering
2075 mode'' (after ~\linenumbers~, e.g.);
2076 (b)_it is the last occurrence of a ~\firstlinenumbers~
2077 before or in the paragraph.
2078 (The practical applications of this that I can imagine
2079 don't seem appealing to me.)
2080 Cf._the explanation above of how ~\modulolinenumbers~ and
2081 ~\firstlinenumbers~ interact---for this and for (ii),
2082 which is concerned with possible arguments for
2083 ~\firstlinenumbers~.
2084
2085 Note that the line numbers of the present section
2086 demonstrate the two devices. (/New v4.00)
2087 \begin{code}\begin{verbatim}
2088 \chardef\c@linenumbermodulo=5 % v4.2; ugly?
2089 \modulolinenumbers[1]
2090 \end{verbatim}
2091 \end{code}
2092 (New v4.22) The new implementation through ~\chardef~
2093 decreases the functionality and raises certain compatibility
2094 problems. I face this without fear. The maximum modulo value
2095 is now ~255~. I expect that this suffices for usual applications.
2096 However, some users have ``abused'' ~lineno.sty~ to get
2097 ~ednotes.sty~ features without line numbers, so have set the
2098 modulo to a value beyond the total number of lines in their
2099 edition. This ought to be replaced by
2100 ~\let\makeLineNumber\relax~. (/New v4.22)
2101
2102 \section{%
2103 Package options
2104 \unskip}\label{s:Opts}
2105
2106 (New v4.1)
2107 The last heading formerly was the heading of what is now
2108 subsection_\ref{ss:v3opts}. The options declared there were
2109 said to execute user commands only. This was wrong already
2110 concerning ~displaymath~ and ~hyperref~. At least, however,
2111 these options were no or almost no occasion to skip definitions
2112 or allocations. This is different with the options that we now
2113 insert.
2114
2115
2116 \subsection{%
2117 Extended referencing to line numbers. (v4.2)
2118 \unskip}
2119 This subsection explains and declares package option ~addpageno~. %% v4.31
2120
2121 If a line to whose number you refer by ~\ref~ is not on the
2122 present page, it may be useful to add the number of the page
2123 on which the line occurs---and perhaps it should not be added
2124 otherwise. In general, you could use the Standard \LaTeX\
2125 package varioref for this. However, the latter usually
2126 produces verbose output like `on the preceding page'---
2127 unless costumized---, while in critical editions, e.g., one
2128 may prefer just adding the page number and some mark on the
2129 left of the line number, irrespectively of how far the page is
2130 apart etc. To support this, package option ~addpageno~
2131 provides a command ~\vpagelineref~ to be used in place of
2132 ~\ref~. This produces, e.g., `34.15' when referring to line_15
2133 on page_34 while the present page is not 34. You can customize
2134 the outcome, see the package file ~vplref.sty~ where the code
2135 and further details are. You may conceive of
2136 ~\vpagelineref~ as a certain customization of varioref's
2137 ~\vref~.
2138
2139 This implies that option ~addpageno~ requires the files
2140 ~vplref.sty~ and ~varioref.sty~. ~addpageno~ automatically
2141 loads both of them. Yet you can also load ~varioref.sty~
2142 on your own to use its package options.
2143
2144 Of course, you might better introduce a shorter command name
2145 for ~\vpagelineref~ for your work, while we cannot predict
2146 here what shorthand will fit your work. E.g.,
2147 ~\newcommand{\lref}{\vpagelineref}~.
2148
2149 If you really want to add the page number in \emph{any} case,
2150 use, e.g., some ~\myref~ instead of ~\ref~, after
2151 \[~newcommand*{\myref}{\pageref{#1}.\ref{#1}}~\]
2152 or what you like. You don't need the ~addpageno~ option in
2153 this case.
2154
2155 ~addpageno~ is due to a suggestion by Sergei Mariev.
2156 \begin{code}\begin{verbatim}
2157 \DeclareVoidOption{addpageno}{%
2158 \AtEndOfPackage{\RequirePackage{vplref}[2005/04/25]}}
2159 \end{verbatim}
2160 \end{code}
2161 \subsection{%
2162 \scs{linelabel} in math mode
2163 \unskip}\label{ss:MathRef}
2164
2165 We have made some first steps towards allowing ~\linelabel~ in
2166 math mode. Because our code for this is presently experimental,
2167 we leave it to the user to decide for the experiment by calling
2168 option ~mathrefs~. We are in a hurry now and thus leave the
2169 code, explanations, and discussion in the separate package
2170 ~ednmath0.sty~. Maybe we later find the time to improve the
2171 code and move the relevant content of ~ednmath0.sty~ to here.
2172 The optimal situation would be to define ~\linelabel~ from
2173 the start so it works in math mode, omitting the ~mathrefs~
2174 option.
2175
2176 Actually, this package even provides adjustments for analogously
2177 allowing ~ednotes.sty~ commands in math mode. Loading the package
2178 is postponed to ~\AtBeginDocument~ when we know whether these
2179 adjustments are needed.
2180 \begin{code}\begin{verbatim}
2181 \DeclareVoidOption{mathrefs}{\AtBeginDocument
2182 {\RequirePackage{ednmath0}[2004/08/20]}}
2183 \end{verbatim}
2184 \end{code}
2185
2186 \subsection{%
2187 Arrays, tabular environments (Revised v4.11)
2188 \unskip}\label{ss:Tab}
2189
2190 This subsection explains and declares package options %% v4.31
2191 ~edtable~, ~longtable~, and ~nolongtablepatch~.
2192
2193 The standard \LaTeX\ tabular environments come as single
2194 boxes, so the ~lineno.sty~ versions before v4.00 treated them as
2195 (parts of) single lines, printing (at most) one line number
2196 beside each and stepping the line number counter once only.
2197 Moreover, ~\linelabel~s got lost. Of course, tables are
2198 usually so high that you will want to treat each row like a
2199 line. (Christian Tapp even desires that the lines of table
2200 entries belonging to a single row are treated like ordinary
2201 lines.) Footnotes get lost in such environments as well, which
2202 was bad for ~ednotes.sty~.
2203
2204 We provide adjustments to count lines, print their numbers
2205 etc.\ as desired at least for \emph{some} \LaTeX\ tabular
2206 environments. (Like with other details, ``some'' is to some
2207 extent explained in ~edtable.sty~.) We do this similarly as
2208 with option ~mathrefs~ before. We leave code
2209 and explanations in the separate package ~edtable.sty~.
2210 (For wizards: this package provides adjustments for
2211 ~ednotes.sty~ as well. However, in the present case we don't try
2212 to avoid them unless ~ednotes.sty~ is loaded.)
2213 Package option ~edtable~
2214 defines---by loading ~edtable.sty~---an environment ~{edtable}~
2215 which is able to change some \LaTeX\ tabular environments
2216 with the desired effects. (v4.11: ~edtable.sty~ v1.3 counts
2217 \LaTeX's ~{array}~ [etc.\@] as a ``tabular environment'' as
2218 well.)
2219
2220 The ~{edtable}~ environment doesn't help with ~longtable.sty~,
2221 however. To make up for this, ~{longtable}~ is adjusted in a
2222 different way---and this happens only when another ~lineno.sty~
2223 option ~longtable~ is called. In this case, option ~edtable~
2224 needn't be called explicitly: option ~longtable~ works as if
2225 ~edtable~ had been called.
2226
2227 Now, we are convinced that vertical spacing around
2228 ~{longtable}~ works wrongly---see \LaTeX\ bugs database
2229 tools/3180 and 3485, or see explanations in the package
2230 ~ltabptch.sty~ (which is to be obtained from CTAN folder
2231 \path{macros/latex/ltabptch}). Our conviction is so strong
2232 that the ~longtable~ option loads---after ~longtable.sty~---the
2233 patch package ~ltabptch.sty~. If the user doesn't want this
2234 (maybe preferring her own arrangement with the vertical
2235 spacing), she can forbid it by calling ~nolongtablepatch~.
2236
2237 The following code just collects some choices, which are
2238 then executed in section_\ref{ss:ExOpt}. We use an ~\if...~
2239 without ~\newif~ since ~\if...true~ and ~\if...false~
2240 would occur at most two times and only within the present
2241 package. (~\AtEndOfClass{\RequirePackage{edtable}}~
2242 could be used instead, I just overlooked this. Now I don't
2243 change it because it allows to change the version requirement
2244 at one place only.)
2245 \begin{code}\begin{verbatim}
2246 \let\if@LN@edtable\iffalse
2247
2248 \DeclareVoidOption{edtable}{\let\if@LN@edtable\iftrue}
2249
2250 \DeclareVoidOption{longtable}{\let\if@LN@edtable\iftrue
2251 \PassOptionsToPackage{longtable}{edtable}}
2252
2253 \DeclareVoidOption{nolongtablepatch}{%
2254 \PassOptionsToPackage{nolongtablepatch}{edtable}}
2255 \end{verbatim}
2256 \end{code}
2257 (/New v4.1)
2258
2259 \subsection{%
2260 Switch among settings
2261 \unskip}\label{ss:v3opts}
2262
2263 There is a bunch of package options that execute %% v4.2
2264 user commands only.
2265
2266 Options ~left~ (~right~) put the line numbers on the left
2267 (right) margin. This works in all modes. ~left~ is the
2268 default.
2269 \begin{code}\begin{verbatim}
2270 \DeclareVoidOption{left}{\leftlinenumbers*}
2271
2272 \DeclareVoidOption{right}{\rightlinenumbers*}
2273 \end{verbatim}
2274 \end{code}
2275 Option ~switch~ (~switch*~) puts the line numbers on the
2276 outer (inner) margin of the text. This requires running
2277 the pagewise mode, but we turn off the page offset
2278 subtraction, getting sort of running numbers again. The
2279 ~pagewise~ option may restore true pagewise mode later.
2280 \begin{code}\begin{verbatim}
2281 \DeclareVoidOption{switch}{\setpagewiselinenumbers
2282 \switchlinenumbers
2283 \runningpagewiselinenumbers}
2284
2285 \DeclareVoidOption{switch*}{\setpagewiselinenumbers
2286 \switchlinenumbers*%
2287 \runningpagewiselinenumbers}
2288 \end{verbatim}
2289 \end{code}
2290 In twocolumn mode, we can switch the line numbers to
2291 the outer margin, and/or start with number 1 in each
2292 column. Margin switching is covered by the ~switch~
2293 options.
2294 \begin{code}\begin{verbatim}
2295 \DeclareVoidOption{columnwise}{\setpagewiselinenumbers
2296 \columnwiselinenumberstrue
2297 \realpagewiselinenumbers}
2298 \end{verbatim}
2299 \end{code}
2300 The options ~pagewise~ and ~running~ select the major
2301 line number mechanism. ~running~ line numbers refer to a real
2302 counter value, which can be reset for any paragraph,
2303 even getting multiple paragraphs on one page starting
2304 with line number one. ~pagewise~ line numbers get a
2305 unique hidden number within the document, but with the
2306 opportunity to establish the page on which they finally
2307 come to rest. This allows the subtraction of the page
2308 offset, getting the numbers starting with 1 on top of each
2309 page, and margin switching in twoside formats becomes
2310 possible. The default mode is ~running~.
2311
2312 The order of declaration of the options is important here
2313 ~pagewise~ must come after ~switch~, to overide running
2314 pagewise mode. ~running~ comes last, to reset the running
2315 line number mode, e.g, after selecting margin switch mode
2316 for ~pagewise~ running. Once more, if you specify all
2317 three of the options ~[switch,pagewise,running]~, the
2318 result is almost nothing, but if you later say
2319 ~\pagewiselinenumbers~, you get margin switching, with
2320 real pagewise line numbers.
2321
2322 \begin{code}\begin{verbatim}
2323 \DeclareVoidOption{pagewise}{\setpagewiselinenumbers
2324 \realpagewiselinenumbers}
2325
2326 \DeclareVoidOption{running}{\setrunninglinenumbers}
2327 \end{verbatim}
2328 \end{code}
2329 The option ~modulo~ causes only those linenumbers to be
2330 printed which are multiples of five.
2331 \begin{code}\begin{verbatim}
2332 \DeclareVoidOption{modulo}{\modulolinenumbers\relax}
2333 \end{verbatim}
2334 \end{code}
2335 Option ~modulo*~ modifies ~modulo~ in working like
2336 ~\modulolinenumbers*~---see section_\ref{s:UserCmds}.
2337 \begin{code}\begin{verbatim}
2338 \DeclareVoidOption{modulo*}{\modulolinenumbers*\relax}
2339 \end{verbatim}
2340 \end{code}
2341 The package option ~mathlines~ switches the behavior of
2342 the ~{linenomath}~ environment with its star-form.
2343 Without this option, the ~{linenomath}~ environment does
2344 not number the lines of the display, while the star-form
2345 does. With this option, its just the opposite.
2346
2347 \begin{code}\begin{verbatim}
2348 \DeclareVoidOption{mathlines}{\linenumberdisplaymath}
2349 \end{verbatim}
2350 \end{code}
2351 ~displaymath~ now calls for wrappers of the standard
2352 \LaTeX\ display math environment. This was previously
2353 done by ~mlineno.sty~.
2354
2355 (New v4.3) Option `displaymath' becomes default according
2356 to Erik \mbox{Luijten}'s suggestion. I was finally convinced
2357 of this as soon as I discovered how to avoid a spurious line
2358 number above ~\begin{linenomath}~ (subsection_\ref{ss:DM}).
2359 ~\endlinenomath~ provides ~\ignorespaces~, so what could go
2360 wrong now?
2361 \begin{code}\begin{verbatim}
2362 \DeclareVoidOption{displaymath}{%
2363 \PackageWarningNoLine{lineno}{Option [displaymath] is obsolete}}
2364 \end{verbatim}
2365 \end{code}
2366 (/New v4.3)
2367
2368 (New v5.3) Options `sep' and `width' set ~\linenumbersep~ (the separation of
2369 the line number to the text) and ~\linenumberwidth~ (the width of the line
2370 number box on the right margin) respectively; see section_\ref{s:CustHooks}.
2371 \begin{code}\begin{verbatim}
2372 \DeclareStringOption[\linenumbersep]{sep}
2373 \DeclareStringOption[\linenumberwidth]{width}
2374 \AtBeginDocument{%
2375 \linenumbersep=\lineno@sep%
2376 \linenumberwidth=\lineno@width%
2377 }
2378 \end{verbatim}
2379 \end{code}
2380 (/New v5.3)
2381
2382 \subsection{%
2383 Compatibility with \texttt{hyperref} %% own subsec. v4.3.
2384 \unskip}
2385 The ~hyperref~ package, via ~nameref~, requires three more
2386 groups in the second argment of a ~\newlabel~. Well, why
2387 shouldn't it get them? (New v3.07) The presence of the
2388 ~nameref~ package is now detected automatically
2389 ~\AtBeginDocument~. (/New v3.07) (Fixed in v3.09) We try
2390 to be smart, and test ~\AtBeginDocument~ if the ~nameref~
2391 package is loaded, but ~hyperref~ postpones the loading of
2392 ~nameref~ too, so this is all in vain.
2393
2394 (New v4.3) But we can also test at the first ~\linelabel~.
2395 Regarding the error-message for misplaced ~\linelabel~ from v4.11:
2396 previously, ~\linenumbers~ rendered ~\linelabel~ the genuine
2397 version of ~\linelabel~ from the start on. This doesn't work
2398 now, since ~\@LN@linelabel~ may change its meaning after the
2399 first ~\linenumbers~ and before a next one (if there is some).
2400 (/New v4.3)
2401 \begin{code}\begin{verbatim}
2402 \DeclareVoidOption{hyperref}{%
2403 \PackageWarningNoLine{lineno}{Option [hyperref] is obsolete}}
2404
2405 \AtBeginDocument{%
2406 \@ifpackageloaded{nameref}{%
2407 \end{verbatim}
2408 \end{code}
2409 (New v4.3) ``Global'' is merely ``symbolic'' ~\AtBeginDoc...~.
2410 If ~nameref~ is not detected here, the next ~\@LN@linelabel~
2411 will do almost the same, then globally indeed.
2412 \begin{code}\begin{verbatim}
2413 \gdef\@LN@ExtraLabelItems{{}{}{}}%
2414 }{%
2415 \global\let\@LN@@linelabel\@LN@linelabel
2416 \gdef\@LN@linelabel{%
2417 \end{verbatim}
2418 \end{code}
2419 ~\@ifpackageloaded~ is ``preamble only'', its---very
2420 internal---preamble definition is replicated here:
2421 \begin{code}\begin{verbatim}
2422 \expandafter
2423 \ifx\csname [email protected]\endcsname\relax \else
2424 \gdef\@LN@ExtraLabelItems{{}{}{}}%
2425 \fi
2426 \end{verbatim}
2427 \end{code}
2428 Now aim at the ``usual'' behaviour:
2429 \begin{code}\begin{verbatim}
2430 \global\let\@LN@linelabel\@LN@@linelabel
2431 \global\let\@LN@@linelabel\relax
2432 \@LN@linelabel
2433 }%
2434 }%
2435 }
2436 \end{verbatim}
2437 \end{code}
2438 (New 5.4) Option to allow ~\linelabel~ with ~\nolinenumbers~.
2439 \begin{code}\begin{verbatim}
2440 \DeclareVoidOption{nolinelabelerror}{\def\@LN@LLerror{\relax}}
2441 \end{verbatim}
2442 \end{code}
2443 (/New 5.4)
2444 (/New v4.3)
2445
2446 (New v4.1)
2447 \subsection{%
2448 A note on calling so many options
2449 \unskip}
2450
2451 The number of package options may stimulate worrying about how to
2452 \emph{enter} all the options that one would like to use---they may
2453 not fit into one line. Fortunately, you can safely break code lines
2454 after the commas separating the option names in the ~\usepackage~
2455 command (no comment marks needed).
2456
2457 \subsection{%
2458 Execute options
2459 \unskip}\label{ss:ExOpt}
2460
2461 We stop declaring options and execute the ones that are
2462 called by the user. (/New v4.1)
2463 \begin{code}\begin{verbatim}
2464 \ProcessKeyvalOptions*
2465 \end{verbatim}
2466 \end{code}
2467 (New v4.1) Now we know whether ~edtable.sty~ is wanted
2468 and (if it is) with which options it is to be called.
2469 \begin{code}\begin{verbatim}
2470 \if@LN@edtable \RequirePackage{edtable}[2005/03/07] \fi
2471 \end{verbatim}
2472 \end{code}
2473 (/New v4.1)
2474
2475 \section{%
2476 Package extensions
2477 \label{s:Xt}\unskip}
2478
2479 Some of the extensions in this section were previously supplied
2480 in separate ~.sty~ files.
2481
2482 \subsection{%
2483 \texttt{displaymath}
2484 \unskip}\label{ss:display}
2485 (New v4.3) From now on, you no longer need to type
2486 the ~{linenomath}~ environment with the ~\[~, ~{equation}~,
2487 and ~{eqnarray}~ environments---and you no longer need to
2488 use the former package option ~displaymath~ for this feature.
2489 (/New v4.3)
2490
2491 The standard \LaTeX\ display math environments are
2492 wrapped in a ~{linenomath}~ environment.
2493
2494 (New 3.05) The ~[fleqn]~ option of the standard
2495 \LaTeX\ classes defines the display math
2496 environments such that line numbers appear just
2497 fine. Thus, we need not do any tricks when
2498 ~[fleqn]~ is loaded, as indicated by presents of
2499 the ~\mathindent~ register. (/New 3.05)
2500
2501 (New 3.05a) for ~{eqnarray}~s we rather keep the
2502 old trick. (/New 3.05a)
2503
2504 (New 3.08) Wrap ~\[~ and ~\]~ into ~{linenomath}~,
2505 instead of ~{displaymath}~. Also save the definition
2506 of ~\equation~, instead of replicating the current
2507 \LaTeX\ definition. (/New 3.08)
2508 \begin{code}\begin{verbatim}
2509 \@ifundefined{mathindent}{
2510
2511 \let\LN@displaymath\[%
2512 \let\LN@enddisplaymath\]%
2513 \renewcommand\[{\begin{linenomath}\LN@displaymath}%
2514 \renewcommand\]{\LN@enddisplaymath\end{linenomath}}%
2515 \end{verbatim}
2516 \end{code}
2517
2518 \begin{code}\begin{verbatim}
2519 \let\LN@equation\equation
2520 \let\LN@endequation\endequation
2521 \renewenvironment{equation}%
2522 {\linenomath\LN@equation}%
2523 {\LN@endequation\endlinenomath}%
2524
2525 }{}% \@ifundefined{mathindent} -- 3rd arg v4.2, was \par!
2526
2527 \let\LN@eqnarray\eqnarray
2528 \let\LN@endeqnarray\endeqnarray
2529 \renewenvironment{eqnarray}%
2530 {\linenomath\LN@eqnarray}%
2531 {\LN@endeqnarray\endlinenomath}%
2532 \end{verbatim}
2533 \end{code}
2534 (UL) Indeed. The \LaTeX\ macros are saved for
2535 unnumbered mode, which is detected by ~\linenomath~.
2536 (/UL)
2537
2538 \subsection{%
2539 Line numbers in internal vertical mode
2540 \unskip}\label{ss:ILN}
2541
2542 The command ~\internallinenumbers~ adds line numbers in
2543 internal vertical mode, but with limitations: we assume
2544 fixed baseline skip.
2545
2546 (v4.22) v3.10 provided a global (~\global\advance~)
2547 as well as a local version (star-form, using
2548 ~\c@internallinenumber~). ~\resetlinenumbers~ acted
2549 locally and was here used with the global version---save
2550 stack danger, \TeX book p._301---in v4.00 I
2551 disabled the global version therefore. Now I find that
2552 it is better to keep a global version, and the now global
2553 ~\resetlinenumbers~ is perfect for this. The global version
2554 allows continuing the ``internal'' numbers in the ensuing
2555 ``external'' text, and---unless reset by brackets
2556 argument---continuing the above series of line numbers.
2557 As with v3.10, the local version always starts with
2558 line number one. A new ~\@LN@iglobal~ steps ~\global~ly
2559 in the global version, otherwise it is ~\relax~.
2560 (I also remove all my stupid discussions as of v4.00.
2561 And I use ~\newcommand~.) (v4.22)
2562 \begin{code}\begin{verbatim}
2563 \let\@LN@iglobal\global % v4.22
2564
2565 \newcommand\internallinenumbers{\setrunninglinenumbers
2566 \let\@@par\internallinenumberpar
2567 \ifx\@par\@@@par\let\@par\internallinenumberpar\fi
2568 \ifx\par\@@@par\let\par\internallinenumberpar\fi
2569 \ifx\@par\linenumberpar\let\@par\internallinenumberpar\fi
2570 \ifx\par\linenumberpar\let\par\internallinenumberpar\fi
2571 \@ifnextchar[{\resetlinenumber}%]
2572 {\@ifstar{\let\c@linenumber\c@internallinenumber
2573 \let\@LN@iglobal\relax % v4.22
2574 \c@linenumber\@ne}{}}%
2575 }
2576
2577 \let\endinternallinenumbers\endlinenumbers
2578 \@namedef{internallinenumbers*}{\internallinenumbers*}
2579 \expandafter\let\csname endinternallinenumbers*\endcsname\endlinenumbers
2580
2581 \newcount\c@internallinenumber
2582 \newcount\c@internallinenumbers
2583
2584 \newcommand\internallinenumberpar{%
2585 \ifvmode\@@@par\else\ifinner\@@@par\else\@@@par
2586 \begingroup
2587 \c@internallinenumbers\prevgraf
2588 \setbox\@tempboxa\hbox{\vbox{\makeinternalLinenumbers}}%
2589 \ht\@tempboxa\z@
2590 \ifdim\prevdepth=-1000pt
2591 % \nointerlineskip is already set so we don't need set it again
2592 % (and we shouldn't back up)
2593 \else
2594 \dp\@tempboxa\prevdepth
2595 \nobreak\vskip-\prevdepth
2596 \nointerlineskip
2597 \fi
2598 \box\@tempboxa
2599 \endgroup
2600 \fi\fi
2601 }
2602
2603 \newcommand\makeinternalLinenumbers{%
2604 \ifnum\c@internallinenumbers>\z@ % v4.2
2605 \hb@xt@\z@{\makeLineNumber}%
2606 \@LN@iglobal % v4.22
2607 \advance\c@linenumber\@ne
2608 \advance\c@internallinenumbers\m@ne
2609 \expandafter\makeinternalLinenumbers\fi
2610 }
2611 % TODO v4.4+: star: line numbers right!? cf. lnocapt.sty
2612 \end{verbatim}
2613 \end{code}
2614
2615 \subsection{%
2616 Line number references with offset
2617 \unskip}
2618
2619 This extension defines macros to refer to line
2620 numbers with an offset, e.g., to refer to a line
2621 which cannot be labeled directly (display math).
2622 This was formerly knows as ~rlineno.sty~.
2623
2624 To refer to a pagewise line number with offset:
2625 \begin{quote}
2626 ~\linerefp[~\angle{OFFSET}~]{~\angle{LABEL}~}~
2627 \end{quote}
2628 To refer to a running line number with offset:
2629 \begin{quote}
2630 ~\linerefr[~\angle{OFFSET}~]{~\angle{LABEL}~}~
2631 \end{quote}
2632 To refer to a line number labeled in the same mode as currently
2633 selected:
2634 \begin{quote}
2635 ~\lineref[~\angle{OFFSET}~]{~\angle{LABEL}~}~
2636 \end{quote}
2637 \begin{code}\begin{verbatim}
2638 \newcommand\lineref{%
2639 \ifx\c@linenumber\c@runninglinenumber
2640 \expandafter\linerefr
2641 \else
2642 \expandafter\linerefp
2643 \fi
2644 }
2645
2646 \newcommand*\linerefp[2][\z@]{{%
2647 \let\@thelinenumber\thelinenumber
2648 \edef\thelinenumber{\advance\c@linenumber#1\relax
2649 \noexpand\@thelinenumber}%
2650 \ref{#2}%
2651 }}
2652 \end{verbatim}
2653 \end{code}
2654 This goes deep into \LaTeX's internals.
2655 \begin{code}\begin{verbatim}
2656 \newcommand*\linerefr[2][\z@]{{%
2657 \def\@@linerefadd{\advance\c@linenumber#1}%
2658 \expandafter\@setref\csname r@#2\endcsname
2659 \@linerefadd{#2}%
2660 }}
2661
2662 \newcommand*\@linerefadd[2]{\c@linenumber=#1\@@linerefadd\relax
2663 \thelinenumber}
2664 \end{verbatim}
2665 \end{code}
2666
2667 \subsection{%
2668 Numbered quotation environments
2669 \unskip}
2670
2671 The ~{numquote}~ and ~{numquotation}~
2672 environments are like ~{quote}~ and
2673 ~{quotation}~, except there will be line
2674 numbers.
2675
2676 An optional argument gives the number to count
2677 from. A star ~*~ (inside or outside the closing
2678 ~}~) prevent the reset of the line numbers.
2679 Default is to count from one.
2680
2681 (v4.22: A local version using ~\c@internallinenumber~
2682 might be useful, see subsection_\ref{ss:ILN}.) %% TODO v4.4+
2683 \begin{code}\begin{verbatim}
2684 \newcommand\quotelinenumbers
2685 {\@ifstar\linenumbers{\@ifnextchar[\linenumbers{\linenumbers*}}}
2686
2687 \newdimen\quotelinenumbersep
2688 \quotelinenumbersep=\linenumbersep
2689 \let\quotelinenumberfont\linenumberfont
2690
2691 \newcommand\numquotelist
2692 {\leftlinenumbers
2693 \linenumbersep\quotelinenumbersep
2694 \let\linenumberfont\quotelinenumberfont
2695 \addtolength{\linenumbersep}{-\@totalleftmargin}%
2696 \quotelinenumbers
2697 }
2698
2699 \newenvironment{numquote} {\quote\numquotelist}{\endquote}
2700 \newenvironment{numquotation} {\quotation\numquotelist}{\endquotation}
2701 \newenvironment{numquote*} {\quote\numquotelist*}{\endquote}
2702 \newenvironment{numquotation*}{\quotation\numquotelist*}{\endquotation}
2703 \end{verbatim}
2704 \end{code}
2705
2706 \subsection{%
2707 Frame around a paragraph
2708 \unskip}
2709
2710 The ~{bframe}~ environment draws a frame around
2711 some text, across page breaks, if necessary.
2712
2713 This works only for plain text paragraphs,
2714 without special height lines. All lines must be
2715 ~\baselineskip~ apart, no display math.
2716 \begin{code}\begin{verbatim}
2717 \newenvironment{bframe}
2718 {\par
2719 \@tempdima\columnwidth
2720 \advance\@tempdima 2\bframesep
2721 \setbox\bframebox\hb@xt@\columnwidth{%
2722 \hskip-\bframesep
2723 \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
2724 \advance\@tempdima-2\bframerule
2725 \hskip\@tempdima
2726 \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
2727 \hskip-\bframesep
2728 }%
2729 \hbox{\hskip-\bframesep
2730 \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%
2731 \nointerlineskip
2732 \copy\bframebox
2733 \nobreak
2734 \kern-\baselineskip
2735 \runninglinenumbers
2736 \def\makeLineNumber{\copy\bframebox\hss}%
2737 }
2738 {\par
2739 \ifdim\prevdepth=-1000pt \else
2740 \kern-\prevdepth
2741 \fi
2742 \kern\bframesep
2743 \nointerlineskip
2744 \@tempdima\columnwidth
2745 \advance\@tempdima 2\bframesep
2746 \hbox{\hskip-\bframesep
2747 \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%
2748 }
2749
2750 \newdimen\bframerule
2751 \bframerule=\fboxrule
2752
2753 \newdimen\bframesep
2754 \bframesep=\fboxsep
2755
2756 \newbox\bframebox
2757 \end{verbatim}
2758 \end{code}
2759
2760 \subsection{%
2761 \textsf{amsmath} patches
2762 \unskip}\label{ss:AMSMath}
2763
2764 (New v5.0) Patches ~amsmath~ to work with ~lineno~. These patches used to be
2765 supplied by the ~linenoamsmath~ package. See ~linenoamsmathdemo.pdf~ for a
2766 demonstration.
2767 (/New v5.0)
2768
2769 (New v5.1) ~lineno~ tries to use \LaTeX's hook management system to patch
2770 ~amsmath~, so that the two packages may be loaded independently. This requires
2771 the October 2020 release of \LaTeX. As a fallback for older releases, ~lineno~
2772 tests whether ~amsmath~ had already been loaded (by testing for the presence
2773 of the ~gather~ command) and if so applies the patches; otherwise if ~amsmath~
2774 has not been loaded, no patches are applied, and a warning is issued.
2775 (/New v5.1)
2776
2777 (New v5.2) Fix ~lineno~ to work with ~amsmath~'s ~\allowdisplaybreaks~
2778 option. A side effect is that now ~\\*~ suppresses a line number on that
2779 line. This is because ~\\*~ prohibits a page break after a given line, and
2780 ~lineno~ basically works by hijacking page breaks. It's probably not possible
2781 to fix this without losing the behaviour of ~\\*~.
2782 (/New v5.2)
2783
2784 \begin{code}\begin{verbatim}
2785 \ifdefined\AddToHook
2786 \def\@LN@amsmath@patches#1{\AddToHook{package/amsmath/after}{#1}}
2787 \else
2788 \ifdefined\endgather
2789 \def\@LN@amsmath@patches#1{#1}
2790 \else
2791 \PackageWarningNoLine{lineno}%
2792 {`amsmath' must be loaded before `lineno' for patches to be applied}
2793 \def\@LN@amsmath@patches#1{\relax}
2794 \fi
2795 \fi
2796
2797 \@LN@amsmath@patches{
2798 \newcommand*\@LN@amsmath@patch[1]{%
2799 \cspreto{#1}{\linenomath}%
2800 \cspreto{#1*}{\linenomath}%
2801 \csappto{end#1}{\endlinenomath}%
2802 \csappto{end#1*}{\endlinenomath}%
2803 }
2804 \newcount\@LN@amsmath@ams@eqpen
2805 \cspreto{math@cr@}{%
2806 %%% Uncommenting the following 2 lines restores the line number on a line
2807 %%% ended with \\*, by making \\* act just like \\. This is probably
2808 %%% undesirable, however, so these lines are disabled.
2809 % \global\@eqpen%
2810 % \ifnum\dspbrk@lvl <\z@ \interdisplaylinepenalty \else -\@getpen\dspbrk@lvl \fi%
2811 \advance\@eqpen\@LN@amsmath@ams@eqpen\relax%
2812 }
2813 \newcommand*\@LN@amsmath@patch@ams[1]{%
2814 \cspreto{#1}{%
2815 \linenomath%
2816 \postdisplaypenalty=0%
2817 \global\@LN@amsmath@ams@eqpen\interdisplaylinepenalty%
2818 }%
2819 \cspreto{#1*}{%
2820 \linenomath%
2821 \postdisplaypenalty=0%
2822 \global\@LN@amsmath@ams@eqpen\interdisplaylinepenalty%
2823 }%
2824 \csappto{end#1}{%
2825 \global\@LN@amsmath@ams@eqpen\z@%
2826 \endlinenomath%
2827 }%
2828 \csappto{end#1*}{%
2829 \global\@LN@amsmath@ams@eqpen\z@%
2830 \endlinenomath%
2831 }%
2832 }
2833 \@LN@amsmath@patch{equation}
2834 \@LN@amsmath@patch@ams{multline}
2835 \@LN@amsmath@patch@ams{gather}
2836 \@LN@amsmath@patch@ams{align}
2837 \@LN@amsmath@patch@ams{alignat}
2838 \@LN@amsmath@patch@ams{flalign}
2839 \let\@LN@amsmath@ams@mmeasure\mmeasure@
2840 \def\mmeasure@#1{%
2841 \global\@LN@amsmath@ams@eqpen\z@%
2842 \begingroup%
2843 \interdisplaylinepenalty=0%
2844 \@LN@amsmath@ams@mmeasure{#1\\}%
2845 \endgroup%
2846 \global\@LN@amsmath@ams@eqpen\interdisplaylinepenalty%
2847 }
2848 }
2849 \end{verbatim}
2850 \end{code}
2851
2852
2853 \section{%
2854 Move \scs{vadjust} items (New v4.00)
2855 \unskip}\label{s:MVadj}
2856
2857 This section completes reviving ~\pagebreak~, ~\nopagebreak~,
2858 ~\vspace~, and the star and optional form of ~\\~. This was
2859 started in section_\ref{ss:output} and resumed in
2860 section_\ref{ss:MLN} and subsection_\ref{ss:calls}.
2861 The problem was explained in section_\ref{ss:output}:
2862 ~\vadjust~ items come out at a bad position, and the
2863 \LaTeX\ commands named before work with ~\vadjust~ indeed.
2864 Our solution was sketched there as well.
2865
2866 According to the caveat in subsection_\ref{ss:OnOff} concerning
2867 ~\ifLineNumbers~, the \LaTeX\ commands enumerated may go
2868 wrong if you switch line numbering inside or at the end of
2869 a paragraph.
2870
2871 \subsection{%
2872 Redefining \scs{vadjust}
2873 \unskip}\label{ss:PVadj}
2874
2875 ~\vadjust~ will temporarily be changed into the following
2876 command.
2877 \begin{code}\begin{verbatim}
2878 \def\PostponeVadjust#1{%
2879 \global\let\vadjust\@LN@@vadjust
2880 \end{verbatim}
2881 \end{code}
2882 This undoes a ~\global\let\vadjust\PostponeVadjust~ which will
2883 start each of the refined \LaTeX\ commands. The ~\global~s
2884 are most probably superfluous. They might be useful should one
2885 ~\vadjust~ appear in a group starting after the change of
2886 ~\vadjust~ into ~\PostponeVadjust~.
2887 (UL) Even the undoing may be superfluous, cf._discussion
2888 in section_\ref{ss:ReDef} below. (UL)
2889 \begin{code}\begin{verbatim}
2890 \vadjust{\penalty-\@Mppvacodepen}%
2891 \g@addto@macro\@LN@vadjustlist{#1\@lt}%
2892 }
2893 \let\@LN@@vadjust\vadjust
2894 \global\let\@LN@vadjustlist\@empty
2895 \global\let\@LN@do@vadjusts\relax
2896 \end{verbatim}
2897 \end{code}
2898 These ~\global~s are just to remind that
2899 all the changes of the strings after ~\let~ should be
2900 ~\global~ (\TeX book p._301). ~\@LN@vadjustlist~ collects
2901 the ~\vadjust~ items of a paragraph. ~\PassVadjustList~
2902 tears one ~\vadjust~ item for the current line out of
2903 ~\@LN@vadjustlist~ and puts it into ~\@LN@do@vadjusts~.
2904 The latter is encountered each line in ~\MakeLineNo~
2905 (section_\ref{ss:MLN}), while those \LaTeX\ ~\vadjust~
2906 commands will come rather rarely. So I decided that
2907 ~\@LN@do@vadjust~ is ~\relax~ until a ~\vadjust~ item
2908 is waiting. In the latter case, ~\@LN@do@vadjusts~
2909 is turned into a list macro which resets itself to
2910 ~\relax~ when the other contents have been placed in
2911 the vertical list.---~\PassVadjustList~ is invoked by
2912 the output routine (section_\ref{ss:output}), so the
2913 ~\box255~ must be put back.
2914 \begin{code}\begin{verbatim}
2915 \def\PassVadjustList{%
2916 \unvbox\@cclv
2917 \expandafter \@LN@xnext \@LN@vadjustlist \@@
2918 \@tempa \@LN@vadjustlist
2919 \ifx\@LN@do@vadjusts\relax
2920 \gdef\@LN@do@vadjusts{\global\let\@LN@do@vadjusts\relax}%
2921 \fi
2922 \expandafter \g@addto@macro \expandafter \@LN@do@vadjusts
2923 \expandafter {\@tempa}%
2924 }
2925 \end{verbatim}
2926 \end{code}
2927
2928 \subsection{%
2929 Redefining the \LaTeX\ commands
2930 \unskip}\label{ss:ReDef}
2931
2932 Now we change ~\pagebreak~ etc.\
2933 so that they use ~\PostponeVadjust~ in place of ~\vadjust~.
2934 We try to do this as independently as possible of the
2935 implementation of the \LaTeX\ commands to be redefined.
2936 Therefore, we don't just copy macro definition code from any
2937 single implementation (say, latest \LaTeX) and insert our
2938 changes, but attach a conditional
2939 ~\global\let\vadjust\PostponeVadjust~
2940 to their left ends in a way which should work rather
2941 independantly of their actual code.
2942 However, ~\vadjust~ should be the primitive again after
2943 execution of the command. So the ~\global\let...~ may be used
2944 only if it's guaranteed that a ~\vadjust~ is near.---(UL)
2945 Sure? In line numbering mode, probably each ~\vadjust~
2946 coming from a \LaTeX\ command should be ~\PostponeVadjust~.
2947 ~\marginpar~s and floats seem to be the only cases which
2948 are not explicitly dealt with in the present section.
2949 This would be a way to avoid ~\@LN@nobreaktrue~!
2950 Of course, the ~\vadjust~s that the present package uses
2951 then must be replaced by ~\@LN@@vadjust~.---Maybe
2952 next time. (/UL)
2953
2954 The next command and something else will be added to the
2955 \LaTeX\ commands we are concerned with here.
2956 \begin{code}\begin{verbatim}
2957 \DeclareRobustCommand\@LN@changevadjust{%
2958 \ifvmode\else\ifinner\else
2959 \global\let\vadjust\PostponeVadjust
2960 \fi\fi
2961 }
2962 \end{verbatim}
2963 \end{code}
2964 (UL) What about math mode? Math display? Warn? (/UL)
2965
2966 ~\@tempa~ will now become a two place macro which adds first
2967 argument (single token), enclosed by ~\ifLineNumbers~\,\dots
2968 ~\fi~ to the left of second argument. As long as we need it,
2969 we can't use the star form of ~\DeclareRobustCommand~ or
2970 the like, because AMS-\LaTeX\ uses ~\@tempa~ for ~\@ifstar~.
2971 (New v4.41) And for the same reason, that ~\CheckCommand*~
2972 had to be raised! (/New v4.41)
2973 \begin{code}\begin{verbatim}
2974 \CheckCommand*\@parboxrestore{\@arrayparboxrestore\let\\\@normalcr}
2975
2976 \def\@tempa#1#2{%
2977 \expandafter \def \expandafter#2\expandafter{\expandafter
2978 \ifLineNumbers\expandafter#1\expandafter\fi#2}%
2979 }
2980 \end{verbatim}
2981 \end{code}
2982 (UL) This ~\ifLineNumber~ can be fooled by
2983 ~\linenumbers~ ahead etc. It might be better to place
2984 a signal penalty in any case and let the output routine
2985 decide what to do.
2986 (/UL)
2987
2988 We use the occasion to switch off linenumbers where they
2989 don't work anyway and where we don't want them,
2990 especially in footnotes:
2991 \begin{code}\begin{verbatim}
2992 \@tempa\nolinenumbers\@arrayparboxrestore
2993 \end{verbatim}
2994 \end{code}
2995 We hope this suffices $\dots$ let's check one thing
2996 at least: [(New v4.41) see ~\CheckCommand~ above (/New v4.41)]
2997
2998 Now for the main theme of the section.
2999 The next lines assume that ~\vspace~, ~\pagebreak~, and
3000 ~\nopagebreak~ use ~\vadjust~ whenever they occur outside
3001 vertical mode; moreover, that they don't directly read
3002 an argument. Indeed ~\pagebreak~ and ~\nopagebreak~ first
3003 call something which tests for a left bracket ahead,
3004 while ~\vspace~ first tests for a star.
3005 \begin{code}\begin{verbatim}
3006 \@tempa\@LN@changevadjust\vspace
3007 \@tempa\@LN@changevadjust\pagebreak
3008 \@tempa\@LN@changevadjust\nopagebreak
3009 \end{verbatim}
3010 \end{code}
3011 ~\\~, however, uses ~\vadjust~ only in star or optional form.
3012 We relax independency of implementation in assuming
3013 that ~\@normalcr~ is the fragile version of ~\\~
3014 (and we use ~\@ifstar~!).
3015 (Using a copy of ~\\~ would be safer, but an ugly repetition
3016 of ~\protect~.)
3017 \begin{code}\begin{verbatim}
3018 \protected\def\\{%
3019 \ifLineNumbers
3020 \expandafter \@LN@cr
3021 \else
3022 \expandafter \@normalcr
3023 \fi
3024 }
3025 \def\@LN@cr{%
3026 \@ifstar{\@LN@changevadjust\@normalcr*}%
3027 {\@ifnextchar[{\@LN@changevadjust\@normalcr}\@normalcr}%
3028 }
3029 \end{verbatim}
3030 \end{code}
3031 Moreover we hope that ~\newline~ never leads to a ~\vadjust~,
3032 although names of some commands invoked by ~\\~ contain
3033 ~newline~. At last, this seems to have been OK since 1989 or
3034 even earlier.
3035
3036 \modulolinenumbers[1]
3037 \firstlinenumber{0}
3038 Let's have a few tests.\vspace*{.5\baselineskip}
3039 Testing ~\pagebreak~ and ~\nopagebreak~ would be too expensive
3040 here, but---oops!---we have just experienced a successful
3041 ~\vspace*{.5\baselineskip}~. A
3042 ~\\*[.5\baselineskip]~\\*[.5\baselineskip] may look even more
3043 drastical, but this time we are happy about it. Note that the
3044 line numbers have moved with the lines. Without our changes,
3045 one line number\vadjust{\kern.5\baselineskip} would have
3046 ``anticipated'' the move of the next line, just as you can
3047 observe it now.
3048 (/New v4.00)
3049
3050 \switchlinenumbers
3051
3052 \subsection{%
3053 Reminder on obsoleteness
3054 \unskip}
3055
3056 (New v4.1) We have completed inclusion of the earlier
3057 extension packages ~linenox0.sty~, ~linenox1.sty~, ~lnopatch.sty~,
3058 and ~linenoamsmath~. If one of them is loaded, though,
3059 we produce an error message before something weird happens.
3060 We avoid ~\newif~ because the switchings occur so rarely.
3061 \begin{code}\begin{verbatim}
3062 \AtBeginDocument{%
3063 \let\if@LN@obsolete\iffalse
3064 \@ifpackageloaded{linenox0}{\let\if@LN@obsolete\iftrue}\relax
3065 \@ifpackageloaded{linenox1}{\let\if@LN@obsolete\iftrue}\relax
3066 \@ifpackageloaded{lnopatch}{\let\if@LN@obsolete\iftrue}\relax
3067 \@ifpackageloaded{linenoamsmath}{\let\if@LN@obsolete\iftrue}\relax
3068 \if@LN@obsolete
3069 \PackageError{lineno}{Obsolete extension package(s)}{%
3070 As of \fileversion, `lineno' includes the functionality of \MessageBreak
3071 `linenox0', `linenox1', `lnopatch', and `linenoamsmath'; \MessageBreak
3072 these packages are therefore obsolete and must not be loaded.}%
3073 \fi
3074 }
3075 \end{verbatim}
3076 \end{code}
3077
3078 \modulolinenumbers[1]
3079 \section{%
3080 The final touch
3081 \unskip}
3082
3083 There is one deadcycle for each line number.
3084 \begin{code}\begin{verbatim}
3085 \advance\maxdeadcycles 100
3086
3087 \endinput
3088 \end{verbatim}
3089 \end{code}
3090
3091 \section{%
3092 The user commands
3093 \unskip}\label{s:UserCmds}
3094
3095 The user commands to turn on and off line numbering
3096 are
3097 \begin{description}\item
3098 [|\linenumbers] \ \par
3099 Turn on line numbering in the current mode.
3100
3101 \item
3102 [|\linenumbers*] \ \par$\qquad$
3103 and reset the line number to 1.
3104 \def\NL{\angle{number}]}\item
3105 [|\linenumbers[\NL] \ \par$\qquad$
3106 and start with \angle{number}.
3107 \item
3108 [|\nolinenumbers] \ \par
3109 Turn off line numbering.
3110 \item
3111 [|\runninglinenumbers*[\NL] \ \par
3112 Turn on ~running~ line numbers, with the same optional
3113 arguments as ~\linenumbers~. The numbers are running
3114 through the text over pagebreaks. When you turn
3115 numbering off and on again, the numbers will continue,
3116 except, of cause, if you ask to reset or preset the
3117 counter.
3118 \item
3119 [|\pagewiselinenumbers] \ \par
3120 Turn on ~pagewise~ line numbers. The lines on each
3121 page are numbered beginning with one at the first
3122 ~pagewise~ numbered line.
3123 \item
3124 [|\resetlinenumber[\NL] \ \par
3125 Reset ~[~Set~]~ the line number to 1
3126 ~[~\angle{number}~]~.
3127 \item
3128 [|\setrunninglinenumbers] \ \par
3129 Switch to ~running~ line number mode. Do \emph{not}
3130 turn it on or off.
3131 \item
3132 [|\setpagewiselinenumbers] \ \par
3133 Switch to ~pagewise~ line number mode. Do \emph{not}
3134 turn it on or off.
3135 \item
3136 [|\switchlinenumbers*] \ \par
3137 Causes margin switching in pagewise modes. With the
3138 star, put the line numbers on the inner margin.
3139 \item
3140 [|\leftlinenumbers*] \ \par
3141 \item
3142 [|\rightlinenumbers*] \ \par
3143 Set the line numbers in the left/right margin. With the
3144 star this works for both modes of operation, without
3145 the star only for the currently selected mode.
3146 \item
3147 [|\runningpagewiselinenumbers] \ \par
3148 When using the pagewise line number mode, do not
3149 subtract the page offset. This results in running
3150 line numbers again, but with the possibility to switch
3151 margins. Be careful when doing line number
3152 referencing, this mode status must be the same while
3153 setting the paragraph and during references.
3154 \item
3155 [|\realpagewiselinenumbers] \ \par
3156 Reverses the effect of ~\runningpagewiselinenumbers~.
3157 \item
3158 [|\modulolinenumbers[\NL] \ \par
3159 Give a number only to lines which are multiples of
3160 ~[~\angle{number}~]~. If \angle{number} is not specified, the
3161 current value in the counter ~linenumbermodulo~ is
3162 retained. \angle{number}=1 turns this off without changing
3163 ~linenumbermodulo~. The counter is initialized to 5.
3164 \item
3165 [|\modulolinenumbers*[\NL] \ \par
3166 Like ~\modulolinenumbers~, the only difference being
3167 that the first line number after a ~\linenumbers~
3168 (or ~\runninglinenumbers~, ~\pagewiselinenumbers~,
3169 ~\quotelinenumbers~) is printed regardless of the
3170 modulo---yet `1' is printed only after (or \dots)
3171 ~\firstlinenumber{1}~.
3172 This also applies to the first line of a
3173 ~{linenumbers}~ or respective environment.
3174 See subsection_\ref{ss:Mod} for another explanation.
3175 The behaviour may be unsatisfactory with pagewise
3176 line-numbering.
3177 \item
3178 [|\firstlinenumber] \ \par
3179 ~\firstlinenumber{~\angle{filino}~}~ brings about that
3180 (after it) line numbers less than \angle{filino} do
3181 \emph{not} appear in the margin. Moreover, with
3182 ~\modulolinenumbers[~\angle{number}~]~, just the line
3183 numbers which are \angle{filino} plus a multiple of
3184 \angle{number} are printed.---If you had
3185 ~\firstlinenumber{~\angle{pos}~}~ with some $\mbox{\angle{pos}}>0$
3186 and want to switch to printing multiples of, e.g.,
3187 4, you best do ~\modulolinenumbers[4]~ and
3188 ~\firstlinenumber{0}~. (See subsection_\ref{ss:Mod}
3189 for technical details.)
3190 \item
3191 [|\linenumberdisplaymath] \ \par
3192 Number the lines of a display math in a ~{linenomath}~
3193 environment, but do not in a ~{linenomath*}~
3194 environment. This is used by the package option
3195 ~[mathlines]~.
3196 \item
3197 [|\nolinenumberdisplaymath] \ \par
3198 Do not Number the lines of a display math in a
3199 ~{linenomath}~ environment, but do in a
3200 ~{linenomath*}~ environment. This is the default.
3201 \item
3202 [|\linelabel] \ \par
3203 Set a ~\linelabel{~\angle{foo}~}~ to the line number where
3204 this commands is in. Refer to it with the \LaTeX\
3205 referencing commands ~\ref{~\angle{foo}~}~ and
3206 ~\pageref{~\angle{foo}~}~.
3207 \end{description}
3208 The commands can be used globally, locally within groups
3209 or as environments. It is important to know that they
3210 take action only when the ~\par~ is executed. The
3211 ~\end{~\angle{mode}~linenumbers}~ commands provide a ~\par~.
3212 Examples:
3213 \begin{verse}
3214 ~{\linenumbers~ \angle{text} ~\par}~ \\
3215 \ \\
3216 ~\begin{linenumbers}~ \\
3217 \angle{text} \\
3218 ~\end{linenumbers}~ \\
3219 \ \\
3220 \angle{paragraph} ~{\linenumbers\par}~ \\
3221 \ \\
3222 ~\linenumbers~ \\
3223 \angle{text} ~\par~ \\
3224 ~\nolinenumbers~ \\
3225 \ \\
3226 ~\linenumbers~ \\
3227 \angle{paragraph} ~{\nolinenumbers\par}~ \\
3228 \end{verse}
3229 (New v4.00)
3230 However, the examples containing \angle{paragraph} show what you
3231 should \emph{not} do, at least if you use ~\pagebreak~,
3232 ~\nopagebreak~, ~\vspace~, ~\\*~ or
3233 ~\\[~\angle{space}~]~---cf._section_\ref{s:MVadj}.
3234
3235 The same care should be applied to the ``wizard'' devices
3236 ~\ifLineNumbers~ (subsection_\ref{ss:OnOff}) and
3237 ~\PostponeVadjust~ (section_\ref{ss:PVadj}).
3238 (/New v4.00)
3239
3240 (New v4.11) Oh, and the commands and environments of
3241 section_{s:Xt} are missing. Sorry, I am in a hurry now.
3242 May be next time.% %% TODO v4.4+
3243 ---And the environments ~{linenomath}~ and ~{linenomath*}~should
3244 get an own paragraph. In short, each math display, equation,
3245 or ~{eqnarray}~ should be ``wrapped'' in one of ~{linenomath}~
3246 and ~{linenomath*}~.
3247
3248 \subsection{%
3249 Customization hooks
3250 \unskip}\label{s:CustHooks}
3251
3252 There are several hooks to customize the appearance of the
3253 line numbers, and some low level hooks for special
3254 effects.
3255 \begin{description}\item
3256 [|\thelinenumber] \ \par
3257 This macro should give the representation of the line
3258 number in the \LaTeX-counter ~linenumber~. The
3259 default is provided by \LaTeX: \par$\qquad$
3260 ~\arabic{linenumber}~
3261 \item
3262 [|\makeLineNumberLeft] \ \par
3263 This macro is used to attach a line number to the left
3264 of the text page. This macro should fill an ~\hbox to 0pt~
3265 which will be placed at the left margin of the
3266 page, with the reference point aligned to the line to
3267 which it should give a number. Please use the macro
3268 ~\LineNumber~ to refer to the line number.
3269
3270 The default definition is \par$\qquad$
3271 ~\hss\linenumberfont\LineNumber\hskip\linenumbersep~
3272 \item
3273 [|\makeLineNumberRight] \ \par
3274 Like ~\makeLineNumberLeft~, but for line numbers on
3275 the right margin.
3276
3277 The default definition is \par$\qquad$
3278 ~\linenumberfont\hskip\linenumbersep\hskip\columnwidth~ \par$\qquad$
3279 ~\hbox to\linenumberwidth{\hss\LineNumber}\hss~
3280 \item
3281 [|\linenumberfont] \ \par
3282 This macro is initialized to \par$\qquad$
3283 ~\normalfont\tiny\sffamily~
3284 \item
3285 [|\linenumbersep] \ \par
3286 This dimension register sets the separation of the
3287 line number to the text. Default value is ~10pt~.
3288 \item
3289 [|\linenumberwidth] \ \par
3290 This dimension register sets the width of the line
3291 number box on the right margin. The distance of the
3292 right edge of the text to the right edge of the line
3293 number is ~\linenumbersep~ + ~\linenumberwidth~. The
3294 default value is ~10pt~.
3295 \item
3296 [|\theLineNumber] (for wizards) \ \par
3297 This macro is called for printing a ~\newlabel~ entry
3298 to the aux-file. Its definition depends on the mode.
3299 For running line numbers it's just ~\thelinenumber~,
3300 while in pagewise mode, the page offset subtraction
3301 is done in here.
3302 \item
3303 [|\makeLineNumber] (for wizards) \ \par
3304 This macro produces the line numbers. The definition
3305 depends on the mode. In the running line numbers
3306 mode it just expands ~\makeLineNumberLeft~.
3307 \item
3308 [|\LineNumber] (for wizards) \ \par
3309 This macro is called by ~\makeLineNumber~ to typeset
3310 the line number. This hook is changed by the modulo
3311 mechanism
3312 and by ~\firstlinenumber~.
3313 \end{description}
3314 \end{document}%D