lineno.sty 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. \iffalse; awk '/S[H]ELL/' lineno.sty|sh;exit;\fi
  2. %%% To pretty-print this file, feed it to a unix shell!
  3. %%%
  4. %%% $Id: lineno.sty,v 3.11 2002/01/26 23:40:55 stephan Exp $
  5. %%%
  6. %%% Copyright 1995--2001 Stephan I. B"ottcher <stephan@nevis.columbia.edu>
  7. %%%
  8. %%% This program can be redistributed and/or modified under the terms
  9. %%% of the LaTeX Project Public License Distributed from CTAN
  10. %%% archives in directory macros/latex/base/lppl.txt; either
  11. %%% version 1 of the License, or any later version.
  12. %%%
  13. % \documentclass[a4paper,12pt]{article}%D
  14. % \usepackage{lineno}%D
  15. %
  16. % \title{
  17. % \texttt{\itshape
  18. % lineno.sty \ v3.08b 2002/02/27
  19. % }\\\ \\
  20. % A \LaTeX\ package to attach
  21. % \\ Line numbers to paragraphs
  22. % }\author{
  23. % Stephan I. B\"ottcher
  24. % }\date{
  25. % stephan@nevis.columbia.edu
  26. %% \\ Stephan.Boettcher@desy.de
  27. %% \\ Stephan.Boettcher@cern.ch
  28. % \\}
  29. %
  30. %
  31. % \def~{\verb~}
  32. % \catcode`\<\catcode`\~
  33. % \def<#1>{$\langle${\itshape#1}\/$\rangle$}
  34. % \catcode`\|\catcode`\~
  35. % \def|#1{{\ttfamily\string#1}}
  36. % \newenvironment{code}
  37. % {\par\runninglinenumbers
  38. % \modulolinenumbers[1]
  39. % \linenumbersep.3em
  40. % \footnotesize
  41. % \def\linenumberfont
  42. % {\normalfont\tiny\itshape}}
  43. % {}
  44. %
  45. % \begin{document}%D
  46. %% \DocInput{lineno.doc}%D
  47. % \pagewiselinenumbers
  48. % \maketitle
  49. % \tableofcontents
  50. % \sloppy
  51. %
  52. %
  53. %
  54. % \section{
  55. % Introduction
  56. % }
  57. % This package provides line numbers on paragraphs.
  58. % After \TeX\ has broken a paragraph into lines there will
  59. % be line numbers attached to them, with the possibility to
  60. % make references through the \LaTeX\ ~\ref~, ~\pageref~
  61. % cross reference mechanism. This includes four issues:
  62. % \begin{itemize}
  63. % \item attach a line number on each line,
  64. % \item create references to a line number,
  65. % \item control line numbering mode,
  66. % \item count the lines and print the numbers.
  67. % \end{itemize}
  68. % The first two points are implemented through patches to
  69. % the output routine. The third by redefining ~\par~, ~\@par~
  70. % and ~\@@par~. The counting is easy, as long as you want
  71. % the line numbers run through the text. If they shall
  72. % start over at the top of each page, the aux-file as well
  73. % as \TeX s memory have to carry a load for each counted line.
  74. %
  75. % I wrote this package for my wife Petra, who needs it for
  76. % transcriptions of interviews. This allows her to
  77. % precisely refer to passages in the text. It works well
  78. % together with ~\marginpar~s, but not to well with displaymath.
  79. % ~\footnote~s are a problem, especially when they
  80. % are split, but we may get there.
  81. %
  82. % lineno.sty works
  83. % surprisingly well with other packages, for
  84. % example, ~wrapfig.sty~. So please try if it
  85. % works with whatever you need, and if it does,
  86. % please tell me, and if it does not, tell me as
  87. % well, so I can try to fix it.
  88. %
  89. % This style option is written for \LaTeXe, later than November 1994,
  90. % since we need the ~\protected@write~ macro.
  91. \NeedsTeXFormat{LaTeX2e}[1994/11/04]
  92. \ProvidesPackage{lineno}
  93. [2002/01/27 line numbers on paragraphs v3.08b]
  94. %% v1.00 1995/03/31 SIB: first release for Petras interview transcriptions
  95. %% v1.01 1995/10/28 SIB: added ~pagewise~ mode
  96. %% v1.02 1995/11/15 SIB: added ~modulo~ option
  97. %% v1.03 1995/12/05 SIB: pagewise: try to reduce the hash-size requirements
  98. %% v2.00 1995/12/06 SIB: .. it works, new user interface
  99. %% v2.01 1996/09/17 SIB: put into CVS
  100. %% v2.02 1997/03/17 SIB: add: \@reinserts, for footnotes
  101. %% v2.04 1998/03/09 SIB: add: linenomath environment
  102. %% v2.05 1998/04/26 SIB: add: prevgraf test
  103. %% v2.06 1999/03/02 SIB: LPPL added
  104. %% v3.00 1999/06/11 SiB: include the extension in the main file
  105. %% v3.01 1999/08/28 SiB: \@reinserts -> \holdinginserts
  106. %% v3.02 2000/03/10 SiB: \@LN@output
  107. %% v3.03 2000/07/01 SiB: \@LN@ExtraLabelItems, hyperref
  108. %% v3.04 2000/12/17 SiB: longtable compatibility.
  109. %% v3.05 2001/01/02 SiB: [fleqn] detection.
  110. %% v3.05a 2001/01/04 SiB: [fleqn] detection reverted for eqnarray.
  111. %% v3.06 2001/01/17 SiB: [twocolumn] mode support.
  112. %% v3.07 2001/07/30 SiB: [hyperref] option obsoleted.
  113. %% v3.08 2001/08/02 SiB: linenomath wrapping for \[ \]
  114. %% v3.08a 2001/08/04 SiB: linenomath wrapping for \[ \] fixed
  115. %% v3.08b 2002/01/27 SiB: enquotation typo fix
  116. %%
  117. %% Acknowledgements:
  118. %% v3.06: Donald Arseneau, pointed to mparhack.sty.
  119. %% v3.07+: Frank Mittelbach, points out inconsistencies in the
  120. %% user interface.
  121. %
  122. % \section{
  123. % Put the line numbers to the lines
  124. % }
  125. % The line numbers have to be attached by the output
  126. % routine. We simply set the ~\interlinepenalty~ to -100000.
  127. % The output routine will be called after each line in the
  128. % paragraph, except the last, where we trigger by ~\par~.
  129. % The ~\linenopenalty~ is small enough to compensate a bunch of
  130. % penalties (e.g., with ~\samepage~).
  131. %
  132. % (New v3.04) Longtable uses
  133. % ~\penaly~-30000. The lineno penalty range was
  134. % shrunk to $-188000 \dots -32000$. (/New v3.04)
  135. \newcount\linenopenalty\linenopenalty=-100000
  136. \mathchardef\linenopenaltypar=32000
  137. % So let's make a hook to ~\output~, the direct way. The \LaTeX\
  138. % macro ~\@reinserts~ puts the footnotes back on the page.
  139. %
  140. % (New v3.01) ~\@reinserts~ badly
  141. % screws up split footnotes. The bottom part is
  142. % still on the recent contributions list, and the
  143. % top part will be put back there after the bottom
  144. % part. Thus, since lineno.sty does not play well
  145. % with ~\inserts~ anyway, we can safely experiment
  146. % with ~\holdinginserts~, without making things
  147. % much worse.
  148. %
  149. % Or that's what I thought, but: Just activating
  150. % ~\holdinginserts~ while doing the ~\par~ will
  151. % not do the trick: The ~\output~ routine may be
  152. % called for a real page break before all line
  153. % numbers are done, and how can we get control
  154. % over ~\holdinginserts~ at that point?
  155. %
  156. % Let's try this: When the ~\output~ routine is
  157. % run with ~\holdinginserts=3~ for a real page
  158. % break, then we reset ~\holdinginserts~ and
  159. % restart ~\output~.
  160. %
  161. % Then, again, how do we keep the remaining
  162. % ~\inserts~ while doing further line numbers?
  163. %
  164. % If we find ~\holdinginserts~=-3 we activate it again
  165. % after doing ~\output~. (/New v3.01)
  166. %
  167. % (New v3.02) To work with
  168. % multicol.sty, the original output routine is now
  169. % called indirectly, instead of being replaced.
  170. % When multicol.sty changes ~\output~, it is a
  171. % toks register, not the real thing. (/New v3.02)
  172. \let\@LN@output\output
  173. \newtoks\output
  174. \output=\expandafter{\the\@LN@output}
  175. \@LN@output={%
  176. \LineNoTest
  177. \if@tempswa
  178. \LineNoHoldInsertsTest
  179. \if@tempswa
  180. \if@twocolumn\let\@makecol\@LN@makecol\fi
  181. \the\output
  182. \ifnum\holdinginserts=-3
  183. \global\holdinginserts 3
  184. \fi
  185. \else
  186. \global\holdinginserts-3
  187. \unvbox\@cclv
  188. \ifnum\outputpenalty=10000\else
  189. \penalty\outputpenalty
  190. \fi
  191. \fi
  192. \else
  193. \MakeLineNo
  194. \fi
  195. }
  196. % The float mechanism inserts ~\interlinepenalty~s during
  197. % ~\output~. So carefully reset it before going on. Else
  198. % we get doubled line numbers on every float placed in
  199. % horizontal mode, e.g, from ~\linelabel~.
  200. %
  201. % Sorry, neither a ~\linelabel~ nor a ~\marginpar~ should
  202. % insert a penalty, else the following linenumber
  203. % could go to the next page. Nor should any other
  204. % float. So let us suppress the ~\interlinepenalty~
  205. % altogether with the ~\@nobreak~ switch.
  206. %
  207. % Since (ltspace.dtx, v1.2p)[1996/07/26], the ~\@nobreaktrue~ does
  208. % it's job globally. We need to do it locally here.
  209. \def\LineNoTest{%
  210. \let\@@par\@@@par
  211. \ifnum\interlinepenalty<-\linenopenaltypar
  212. \advance\interlinepenalty-\linenopenalty
  213. \my@nobreaktrue
  214. \fi
  215. \@tempswatrue
  216. \ifnum\outputpenalty>-\linenopenaltypar\else
  217. \ifnum\outputpenalty>-188000\relax
  218. \@tempswafalse
  219. \fi
  220. \fi
  221. }
  222. \def\my@nobreaktrue{\let\if@nobreak\iftrue}
  223. \def\LineNoHoldInsertsTest{%
  224. \ifnum\holdinginserts=3\relax
  225. \@tempswafalse
  226. \fi
  227. }
  228. % We have to return all the page to the current page, and
  229. % add a box with the line number, without adding
  230. % breakpoints, glue or space. The depth of our line number
  231. % should be equal to the previous depth of the page, in
  232. % case the page breaks here, and the box has to be moved up
  233. % by that depth.
  234. %
  235. % The ~\interlinepenalty~ comes after the ~\vadjust~ from a
  236. % ~\linelabel~, so we increment the line number \emph{after}
  237. % printing it. The macro ~\makeLineNumber~ produces the
  238. % text of the line number, see section \ref{appearance}.
  239. %
  240. % Finally we put in the natural ~\interlinepenalty~, except
  241. % after the last line.
  242. \def\MakeLineNo{\@tempdima\dp\@cclv \unvbox\@cclv
  243. \sbox\@tempboxa{\hbox to\z@{\makeLineNumber}}%
  244. \stepcounter{linenumber}%
  245. \dp\@tempboxa=\@tempdima\ht\@tempboxa=\z@
  246. \nointerlineskip\kern-\@tempdima\box\@tempboxa
  247. \ifnum\outputpenalty=-\linenopenaltypar\else
  248. \@tempcnta\outputpenalty
  249. \advance\@tempcnta -\linenopenalty
  250. \penalty\@tempcnta
  251. \fi
  252. }
  253. %
  254. %
  255. % \section{
  256. % Control line numbering
  257. % }
  258. % The line numbering is controlled via ~\par~. \LaTeX\
  259. % saved the \TeX-primitive ~\par~ in ~\@@par~. We push it
  260. % one level further out, and redefine ~\@@par~ to insert
  261. % the ~\interlinepenalty~ needed to trigger the
  262. % line numbering. And we need to allow pagebreaks after a
  263. % paragraph.
  264. %
  265. % New (2.05beta): the prevgraf test. A paragraph that ends with a
  266. % displayed equation, a ~\noindent\par~ or ~wrapfig.sty~ produce empty
  267. % paragraphs. These should not get a spurious line number via
  268. % ~\linenopenaltypar~.
  269. \let\@@@par\@@par
  270. \newcount\linenoprevgraf
  271. \def\linenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else
  272. \advance\interlinepenalty \linenopenalty
  273. \linenoprevgraf\prevgraf
  274. \global\holdinginserts3%
  275. \@@@par
  276. \ifnum\prevgraf>\linenoprevgraf
  277. \penalty-\linenopenaltypar
  278. \fi
  279. \kern\z@
  280. \global\holdinginserts0%
  281. \advance\interlinepenalty -\linenopenalty
  282. \fi\fi
  283. }
  284. % The basic commands to enable and disable line numbers.
  285. % ~\@par~ and ~\par~ are only touched, when they are ~\let~
  286. % to ~\@@@par~/~\linenumberpar~. The line number may be
  287. % reset to 1 with the star-form, or set by an optional
  288. % argument ~[~<number>~]~.
  289. \def\linenumbers{\let\@@par\linenumberpar
  290. \ifx\@par\@@@par\let\@par\linenumberpar\fi
  291. \ifx\par\@@@par\let\par\linenumberpar\fi
  292. \@ifnextchar[{\resetlinenumber}%]
  293. {\@ifstar{\resetlinenumber}{}}%
  294. }
  295. \def\nolinenumbers{\let\@@par\@@@par
  296. \ifx\@par\linenumberpar\let\@par\@@@par\fi
  297. \ifx\par\linenumberpar\let\par\@@@par\fi
  298. }
  299. % What happens with a display math? Since ~\par~ is not executed,
  300. % when breaking the lines before a display, they will not get
  301. % line numbers. Sorry, but I do not dare to change
  302. % ~\interlinepenalty~ globally, nor do I want to redefine
  303. % the display math environments here.
  304. % \begin{displaymath}
  305. % display \ math
  306. % \end{displaymath}
  307. % See the subsection below, for a wrapper enviroment to make
  308. % it work. But that requires to wrap each and every display
  309. % in your LaTeX source.
  310. %
  311. % The next two commands are provided to turn on line
  312. % numbering in a specific mode. Please note the difference:
  313. % for pagewise numbering, ~\linenumbers~ comes first to
  314. % inhibit it from seeing optional arguments, since
  315. % re-/presetting the counter is useless.
  316. \def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}
  317. \def\runninglinenumbers{\setrunninglinenumbers\linenumbers}
  318. % Finally, it is a \LaTeX\ style, so we provide for the use
  319. % of environments, including the suppression of the
  320. % following paragraph's indentation.
  321. %%% TO DO: add \par to \linenumbers, if called from an environment.
  322. %%% To DO: add an \@endpe hack if \linenumbers are turned on
  323. %%% in horizontal mode. {\par\parskip\z@\noindent} or
  324. %%% something.
  325. \@namedef{linenumbers*}{\par\linenumbers*}
  326. \@namedef{runninglinenumbers*}{\par\runninglinenumbers*}
  327. \def\endlinenumbers{\par\@endpetrue}
  328. \let\endrunninglinenumbers\endlinenumbers
  329. \let\endpagewiselinenumbers\endlinenumbers
  330. \expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers
  331. \expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers
  332. \let\endnolinenumbers\endlinenumbers
  333. %
  334. % \subsection{
  335. % Display math
  336. % }
  337. %
  338. % Now we tackle the problem to get display math working.
  339. % There are different options.
  340. % \begin{enumerate}\item[
  341. % 1.] Precede every display math with a ~\par~.
  342. % Not too good.
  343. % \item[
  344. % 2.] Change ~\interlinepenalty~ and associates globally.
  345. % Unstable.
  346. % \item[
  347. % 3.] Wrap each display math with a ~{linenomath}~
  348. % environment.
  349. % \end{enumerate}
  350. % We'll go for option 3. See if it works:
  351. % \begin{linenomath}
  352. % \begin{equation}
  353. % display \ math
  354. % \end{equation}
  355. % \end{linenomath}
  356. % The star form ~{linenomath*}~ should also number the lines
  357. % of the display itself,
  358. % \begin{linenomath*}
  359. % \begin{eqnarray}
  360. % multi && line \\
  361. % display && math \\
  362. % &
  363. % \begin{array}{c}
  364. % with \\
  365. % array
  366. % \end{array}
  367. % &
  368. % \end{eqnarray}
  369. % \end{linenomath*}
  370. % including multline displays.
  371. %
  372. % First, here are two macros to turn
  373. % on linenumbering on paragraphs preceeding displays, with
  374. % numbering the lines of the display itself, or without.
  375. % The ~\ifx..~ tests if line numbering is turned on. It
  376. % does not harm to add these wrappers in sections that are
  377. % not numbered. Nor does it harm to wrap a display
  378. % twice, e.q, in case you have some ~{equation}~s wrapped
  379. % explicitely, and later you redefine ~\equation~ to do it
  380. % automatically.
  381. \newcommand\linenomathNonumbers{%
  382. \ifx\@@par\@@@par\else
  383. \ifnum\interlinepenalty>-\linenopenaltypar
  384. \global\holdinginserts3%
  385. \advance\interlinepenalty \linenopenalty
  386. \advance\predisplaypenalty \linenopenalty
  387. \fi
  388. \fi
  389. \ignorespaces
  390. }
  391. \newcommand\linenomathWithnumbers{%
  392. \ifx\@@par\@@@par\else
  393. \ifnum\interlinepenalty>-\linenopenaltypar
  394. \global\holdinginserts3%
  395. \advance\interlinepenalty \linenopenalty
  396. \advance\predisplaypenalty \linenopenalty
  397. \advance\postdisplaypenalty \linenopenalty
  398. \advance\interdisplaylinepenalty \linenopenalty
  399. \fi
  400. \fi
  401. \ignorespaces
  402. }
  403. % The ~{linenomath}~ environment has two forms, with and
  404. % without a star. The following two macros define the
  405. % environment, where the stared/non-stared form does/doesn't number the
  406. % lines of the display or vice versa.
  407. \newcommand\linenumberdisplaymath{%
  408. \def\linenomath{\linenomathWithnumbers}%
  409. \@namedef{linenomath*}{\linenomathNonumbers}%
  410. }
  411. \newcommand\nolinenumberdisplaymath{%
  412. \def\linenomath{\linenomathNonumbers}%
  413. \@namedef{linenomath*}{\linenomathWithnumbers}%
  414. }
  415. \def\endlinenomath{%
  416. \global\holdinginserts0
  417. \@ignoretrue
  418. }
  419. \expandafter\let\csname endlinenomath*\endcsname\endlinenomath
  420. % The default is not to number the lines of a display. But
  421. % the package option ~mathlines~ may be used to switch
  422. % that behavior.
  423. \nolinenumberdisplaymath
  424. %
  425. %
  426. % \section{
  427. % Line number references
  428. % }
  429. % The only way to get a label to a line number in a
  430. % paragraph is to ask the output routine to mark it.
  431. %
  432. % We use the marginpar mechanism to hook to ~\output~ for a
  433. % second time. Marginpars are floats with number $-1$, we
  434. % fake marginpars with No $-2$. Originally, every negative
  435. % numbered float was considered to be a marginpar.
  436. %
  437. % The float box number ~\@currbox~ is used to transfer the
  438. % label name in a macro called ~\@LNL@~<box-number>.
  439. %
  440. % A ~\newlabel~ is written to the aux-file. The reference
  441. % is to ~\theLineNumber~, \emph{not} ~\thelinenumber~.
  442. % This allows to hook in, as done below for pagewise line
  443. % numbering.
  444. %
  445. % (New v3.03) The ~\@LN@ExtraLabelItems~ are added for a hook
  446. % to keep packages like ~{hyperref}~ happy. (/New v3.03)
  447. \let\@LN@addmarginpar\@addmarginpar
  448. \def\@addmarginpar{%
  449. \ifnum\count\@currbox>-2\relax
  450. \expandafter\@LN@addmarginpar
  451. \else
  452. \@cons\@freelist\@currbox
  453. \protected@write\@auxout{}{%
  454. \string\newlabel
  455. {\csname @LNL@\the\@currbox\endcsname}%
  456. {{\theLineNumber}{\thepage}\@LN@ExtraLabelItems}}%
  457. \fi}
  458. \let\@LN@ExtraLabelItems\@empty
  459. % \subsection{
  460. % The linelabel command
  461. % }
  462. % To refer to a place in line ~\ref{~<foo>~}~ at page
  463. % ~\pageref{~<foo>~}~ you place a ~\linelabel{~<foo>~}~ at
  464. % that place.
  465. %
  466. % \linelabel{demo}
  467. % \marginpar{\tiny\raggedright
  468. % See if it works: This paragraph
  469. % starts on page \pageref{demo}, line
  470. % \ref{demo}.
  471. % }%
  472. % If you use this command outside a ~\linenumbers~
  473. % paragraph, you will get references to some bogus
  474. % line numbers, sorry. But we don't disable the command,
  475. % because only the ~\par~ at the end of a paragraph may
  476. % decides whether to print line numbers on this paragraph
  477. % or not. A ~\linelabel~ may legally appear earlier than
  478. % ~\linenumbers~.
  479. %
  480. % ~\linelabel~, via a fake float number $-2$, puts a
  481. % ~\penalty~ into a ~\vadjust~, which triggers the
  482. % pagebuilder after putting the current line to the main
  483. % vertical list. A ~\write~ is placed on the main vertical
  484. % list, which prints a reference to the current value of
  485. % ~\thelinenumber~ and ~\thepage~ at the time of the
  486. % ~\shipout~.
  487. %
  488. % A ~\linelabel~ is allowed only in outer horizontal mode.
  489. % In outer vertical mode we start a paragraph, and ignore
  490. % trailing spaces (by fooling ~\@esphack~).
  491. %
  492. % The argument of ~\linelabel~ is put into a macro with a
  493. % name derived from the number of the allocated float box.
  494. % Much of the rest is dummy float setup.
  495. \def\linelabel#1{%
  496. \ifvmode
  497. \ifinner \else
  498. \leavevmode \@bsphack \@savsk\p@
  499. \fi
  500. \else
  501. \@bsphack
  502. \fi
  503. \ifhmode
  504. \ifinner
  505. \@parmoderr
  506. \else
  507. \@floatpenalty -\@Mii
  508. \@next\@currbox\@freelist
  509. {\global\count\@currbox-2%
  510. \expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%
  511. {\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%
  512. \begingroup
  513. \setbox\@currbox \color@vbox \vbox \bgroup \end@float
  514. \endgroup
  515. \@ignorefalse \@esphack
  516. \fi
  517. \else
  518. \@parmoderr
  519. \fi
  520. }
  521. % \modulolinenumbers[3]
  522. % \section{
  523. % The appearance of the line numbers
  524. % }\label{appearance}
  525. % The line numbers are set as ~\tiny\sffamily\arabic{linenumber}~,
  526. % $10pt$ left of the text. With options to place it
  527. % right of the text, or . . .
  528. %
  529. % . . . here are the hooks:
  530. \def\makeLineNumberLeft{\hss\linenumberfont\LineNumber\hskip\linenumbersep}
  531. \def\makeLineNumberRight{\linenumberfont\hskip\linenumbersep\hskip\columnwidth
  532. \hbox to\linenumberwidth{\hss\LineNumber}\hss}
  533. \def\linenumberfont{\normalfont\tiny\sffamily}
  534. \newdimen\linenumbersep
  535. \newdimen\linenumberwidth
  536. \linenumberwidth=10pt
  537. \linenumbersep=10pt
  538. % Margin switching requires ~pagewise~ numbering mode, but
  539. % choosing the left or right margin for the numbers always
  540. % works.
  541. \def\switchlinenumbers{\@ifstar
  542. {\let\makeLineNumberOdd\makeLineNumberRight
  543. \let\makeLineNumberEven\makeLineNumberLeft}%
  544. {\let\makeLineNumberOdd\makeLineNumberLeft
  545. \let\makeLineNumberEven\makeLineNumberRight}%
  546. }
  547. \def\setmakelinenumbers#1{\@ifstar
  548. {\let\makeLineNumberRunning#1%
  549. \let\makeLineNumberOdd#1%
  550. \let\makeLineNumberEven#1}%
  551. {\ifx\c@linenumber\c@runninglinenumber
  552. \let\makeLineNumberRunning#1%
  553. \else
  554. \let\makeLineNumberOdd#1%
  555. \let\makeLineNumberEven#1%
  556. \fi}%
  557. }
  558. \def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}
  559. \def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}
  560. \leftlinenumbers*
  561. % ~\LineNumber~ is a hook which is used for the modulo stuff.
  562. % It is the command to use for the line number, when you
  563. % customizes ~\makeLineNumber~. Use ~\thelinenumber~ to
  564. % change the outfit of the digits.
  565. %
  566. %
  567. % We will implement two modes of operation:
  568. % \begin{itemize}
  569. % \item numbers ~running~ through (parts of) the text
  570. % \item ~pagewise~ numbers starting over with one on top of
  571. % each page.
  572. % \end{itemize}
  573. % Both modes have their own count register, but only one is
  574. % allocated as a \LaTeX\ counter, with the attached
  575. % facilities serving both.
  576. \newcounter{linenumber}
  577. \newcount\c@pagewiselinenumber
  578. \let\c@runninglinenumber\c@linenumber
  579. % Only the running mode counter may be reset, or preset,
  580. % for individual paragraphs. The pagewise counter must
  581. % give a unique anonymous number for each line.
  582. \newcommand\resetlinenumber[1][1]{\c@runninglinenumber#1}
  583. % \subsection{
  584. % Running line numbers
  585. % }
  586. % Running mode is easy, ~\LineNumber~ and ~\theLineNumber~
  587. % produce ~\thelinenumber~, which defaults to
  588. % ~\arabic{linenumber}~, using the ~\c@runninglinenumber~
  589. % counter. This is the default mode of operation.
  590. \def\makeRunningLineNumber{\makeLineNumberRunning}
  591. \def\setrunninglinenumbers{%
  592. \def\theLineNumber{\thelinenumber}%
  593. \let\c@linenumber\c@runninglinenumber
  594. \let\makeLineNumber\makeRunningLineNumber
  595. }
  596. \setrunninglinenumbers\resetlinenumber
  597. %
  598. %
  599. % \subsection{
  600. % Pagewise line numbers
  601. % }
  602. % Difficult, if you think about it. The number has to be
  603. % printed when there is no means to know on which page it
  604. % will end up, except through the aux-file. My solution
  605. % is really expensive, but quite robust.
  606. %
  607. % With version ~v2.00~ the hashsize requirements are
  608. % reduced, because we do not need one controlsequence for
  609. % each line any more. But this costs some computation time
  610. % to find out on which page we are.
  611. %
  612. % ~\makeLineNumber~ gets a hook to log the line and page
  613. % number to the aux-file. Another hook tries to find out
  614. % what the page offset is, and subtracts it from the counter
  615. % ~\c@linenumber~. Additionally, the switch
  616. % ~\ifoddNumberedPage~ is set true for odd numbered pages,
  617. % false otherwise.
  618. \def\setpagewiselinenumbers{%
  619. \let\theLineNumber\thePagewiseLineNumber
  620. \let\c@linenumber\c@pagewiselinenumber
  621. \let\makeLineNumber\makePagewiseLineNumber
  622. }
  623. \def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber
  624. \ifoddNumberedPage
  625. \makeLineNumberOdd
  626. \else
  627. \makeLineNumberEven
  628. \fi
  629. }
  630. % Each numbered line gives a line to the aux file
  631. % \begin{verse}
  632. % ~\@LN{~<line>~}{~<page>~}~
  633. % \end{verse}
  634. % very similar to the ~\newlabel~ business, except that we need
  635. % an arabic representation of the page number, not what
  636. % there might else be in ~\thepage~.
  637. \def\logtheLineNumber{\protected@write\@auxout{}{%
  638. \string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}
  639. % From the aux-file we get one macro ~\LN@P~<page> for each
  640. % page with line numbers on it. This macro calls four other
  641. % macros with one argument each. These macros are
  642. % dynamically defined to do tests and actions, to find out
  643. % on which page the current line number is located.
  644. %
  645. % We need sort of a pointer to the first page with line
  646. % numbers, initiallized to point to nothing:
  647. \def\LastNumberedPage{first}
  648. \def\LN@Pfirst{\nextLN\relax}
  649. % The four dynamic macros are initiallized to reproduce
  650. % themselves in an ~\xdef~
  651. \let\lastLN\relax % compare to last line on this page
  652. \let\firstLN\relax % compare to first line on this page
  653. \let\pageLN\relax % get the page number, compute the linenumber
  654. \let\nextLN\relax % move to the next page
  655. % During the end-document run through the aux-files, we
  656. % disable ~\@LN~. I may put in a check here later, to give
  657. % a rerun recommendation.
  658. \AtEndDocument{\let\@LN\@gobbletwo}
  659. % Now, this is the tricky part. First of all, the whole
  660. % definition of ~\@LN~ is grouped, to avoid accumulation
  661. % on the save stack. Somehow ~\csname~<cs>~\endcsname~ pushes
  662. % an entry, which stays after an ~\xdef~ to that <cs>.
  663. %
  664. % If ~\LN@P~<page> is undefined, initialize it with the
  665. % current page and line number, with the
  666. % \emph{pointer-to-the-next-page} pointing to nothing. And
  667. % the macro for the previous page will be redefined to point
  668. % to the current one.
  669. %
  670. % If the macro for the current page already exists, just
  671. % redefine the \emph{last-line-number} entry.
  672. %
  673. % Finally, save the current page number, to get the pointer to the
  674. % following page later.
  675. \def\@LN#1#2{{\expandafter\@@LN
  676. \csname LN@P#2C\@LN@column\expandafter\endcsname
  677. \csname LN@PO#2\endcsname
  678. {#1}{#2}}}
  679. \def\@@LN#1#2#3#4{\ifx#1\relax
  680. \ifx#2\relax\gdef#2{#3}\fi
  681. \expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1
  682. \xdef#1{\lastLN{#3}\firstLN{#3}\pageLN{#4}{\@LN@column}{#2}\nextLN\relax}%
  683. \else
  684. \def\lastLN##1{\noexpand\lastLN{#3}}%
  685. \xdef#1{#1}%
  686. \fi
  687. \xdef\LastNumberedPage{#4C\@LN@column}}
  688. % The previous page macro gets its pointer to the
  689. % current one, replacing the ~\relax~ with the cs-token
  690. % ~\LN@P~<page>.
  691. \def\@@@LN#1#2{{\def\nextLN##1{\noexpand\nextLN\noexpand#2}%
  692. \xdef#1{#1}}}
  693. % Now, to print a line number, we need to find the page,
  694. % where it resides. This will most probably be the page where
  695. % the last one came from, or maybe the next page. However, it can
  696. % be a completely different one. We maintain a cache,
  697. % which is ~\let~ to the last page's macro. But for now
  698. % it is initialized to expand ~\LN@first~, where the poiner
  699. % to the first numbered page has been stored in.
  700. \def\NumberedPageCache{\LN@Pfirst}
  701. % To find out on which page the current ~\c@linenumber~ is,
  702. % we define the four dynamic macros to do something usefull
  703. % and execute the current cache macro. ~\lastLN~ is run
  704. % first, testing if the line number in question may be on a
  705. % later page. If so, disable ~\firstLN~, and go on to the
  706. % next page via ~\nextLN~.
  707. \def\testLastNumberedPage#1{\ifnum#1<\c@linenumber
  708. \let\firstLN\@gobble
  709. \fi}
  710. % Else, if ~\firstLN~ finds out that we need an earlier
  711. % page, we start over from the beginning. Else, ~\nextLN~
  712. % will be disabled, and ~\pageLN~ will run
  713. % ~\gotNumberedPage~ with four arguments: the first line
  714. % number on this column, the page number, the column
  715. % number, and the first line on the page.
  716. \def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber
  717. \def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%
  718. \else
  719. \let\nextLN\@gobble
  720. \def\pageLN{\gotNumberedPage{#1}}%
  721. \fi}
  722. % We start with ~\pageLN~ disabled and ~\nextLN~ defined to
  723. % continue the search with the next page.
  724. \long\def \@gobblethree #1#2#3{}
  725. \def\testNumberedPage{%
  726. \let\lastLN\testLastNumberedPage
  727. \let\firstLN\testFirstNumberedPage
  728. \let\pageLN\@gobblethree
  729. \let\nextLN\testNextNumberedPage
  730. \NumberedPageCache
  731. }
  732. % When we switch to another page, we first have to make
  733. % sure that it is there. If we are done with the last
  734. % page, we probably need to run \TeX\ again, but for the
  735. % rest of this run, the cache macro will just return four
  736. % zeros. This saves a lot of time, for example if you have
  737. % half of an aux-file from an aborted run, in the next run
  738. % the whole page-list would be searched in vain again and
  739. % again for the second half of the document.
  740. %
  741. % If there is another page, we iterate the search.
  742. \def\testNextNumberedPage#1{\ifx#1\relax
  743. \global\def\NumberedPageCache{\gotNumberedPage0000}%
  744. \PackageWarningNoLine{lineno}%
  745. {Linenumber reference failed,
  746. \MessageBreak rerun to get it right}%
  747. \else
  748. \global\let\NumberedPageCache#1%
  749. \fi
  750. \testNumberedPage
  751. }
  752. % \linelabel{demo2}
  753. % \marginpar{\tiny\raggedright
  754. % Let's see if it finds the label
  755. % on page \pageref{demo},
  756. % line \ref{demo}, and back here
  757. % on page \pageref{demo2}, line
  758. % \ref{demo2}.
  759. % }%
  760. % To separate the official hooks from the internals there is
  761. % this equivalence, to hook in later for whatever purpose:
  762. \let\getLineNumber\testNumberedPage
  763. % So, now we got the page where the number is on. We
  764. % establish if we are on an odd or even page, and calculate
  765. % the final line number to be printed.
  766. \newif\ifoddNumberedPage
  767. \newif\ifcolumnwiselinenumbers
  768. \columnwiselinenumbersfalse
  769. \def\gotNumberedPage#1#2#3#4{\oddNumberedPagefalse
  770. \ifodd \if@twocolumn #3\else #2\fi\relax\oddNumberedPagetrue\fi
  771. \advance\c@linenumber 1\relax
  772. \ifcolumnwiselinenumbers
  773. \subtractlinenumberoffset{#1}%
  774. \else
  775. \subtractlinenumberoffset{#4}%
  776. \fi
  777. }
  778. % You might want to run the pagewise mode with running line
  779. % numbers, or you might not. It's your choice:
  780. \def\runningpagewiselinenumbers{%
  781. \let\subtractlinenumberoffset\@gobble
  782. }
  783. \def\realpagewiselinenumbers{%
  784. \def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%
  785. }
  786. \realpagewiselinenumbers
  787. % For line number references, we need a protected call to
  788. % the whole procedure, with the requested line number stored
  789. % in the ~\c@linenumber~ counter. This is what gets printed
  790. % to the aux-file to make a label:
  791. \def\thePagewiseLineNumber{\protect
  792. \getpagewiselinenumber{\the\c@linenumber}}%
  793. % And here is what happens when the label is refered to:
  794. \def\getpagewiselinenumber#1{{%
  795. \c@linenumber #1\relax\testNumberedPage
  796. \thelinenumber
  797. }}
  798. % %
  799. % A summary of all per line expenses:
  800. % \begin{description}\item
  801. % [CPU:] The ~\output~ routine is called for each line,
  802. % and the page-search is done.
  803. % \item
  804. % [DISK:] One line of output to the aux-file for each
  805. % numbered line
  806. % \item
  807. % [MEM:] One macro per page. Great improvement over v1.02,
  808. % which had one control sequence per line in
  809. % addition. It blew the hash table after some five
  810. % thousand lines.
  811. % \end{description}
  812. %
  813. %
  814. %
  815. % \subsection{
  816. % Twocolumn mode (New v3.06)
  817. % }
  818. %
  819. % Twocolumn mode requires another patch to the ~\output~
  820. % routine, in order to print a column tag to the .aux
  821. % file.
  822. \let\@LN@orig@makecol\@makecol
  823. \def\@LN@makecol{%
  824. \@LN@orig@makecol
  825. \setbox\@outputbox \vbox{%
  826. \boxmaxdepth \@maxdepth
  827. \protected@write\@auxout{}{%
  828. \string\@LN@col{\if@firstcolumn1\else2\fi}%
  829. }%
  830. \box\@outputbox
  831. }% \vbox
  832. }
  833. \def\@LN@col#1{\def\@LN@column{#1}}
  834. \@LN@col{1}
  835. %
  836. %
  837. %
  838. % \subsection{
  839. % Numbering modulo 5
  840. % }
  841. % Most users want to have only one in five lines numbered.
  842. % ~\LineNumber~ is supposed to produce the outfit of the
  843. % line number attached to the line, while ~\thelinenumber~
  844. % is used also for references, which should appear even if
  845. % they are not multiples of five.
  846. \newcount\c@linenumbermodulo
  847. \def\themodulolinenumber{{\@tempcnta\c@linenumber
  848. \divide\@tempcnta\c@linenumbermodulo
  849. \multiply\@tempcnta\c@linenumbermodulo
  850. \ifnum\@tempcnta=\c@linenumber\thelinenumber\fi
  851. }}
  852. % The user command to set the modulo counter:
  853. \newcommand\modulolinenumbers[1][0]{%
  854. \let\LineNumber\themodulolinenumber
  855. \ifnum#1>1\relax
  856. \c@linenumbermodulo#1\relax
  857. \else\ifnum#1=1\relax
  858. \def\LineNumber{\thelinenumber}%
  859. \fi\fi
  860. }
  861. \setcounter{linenumbermodulo}{5}
  862. \modulolinenumbers[1]
  863. %
  864. % \switchlinenumbers
  865. % \modulolinenumbers[1]
  866. % \section{
  867. % Package options
  868. % }
  869. % There is a bunch of package options, all of them
  870. % executing only user commands (see below).
  871. %
  872. % Options ~left~ (~right~) put the line numbers on the left
  873. % (right) margin. This works in all modes. ~left~ is the
  874. % default.
  875. \DeclareOption{left}{\leftlinenumbers*}
  876. \DeclareOption{right}{\rightlinenumbers*}
  877. % Option ~switch~ (~switch*~) puts the line numbers on the
  878. % outer (inner) margin of the text. This requires running
  879. % the pagewise mode, but we turn off the page offset
  880. % subtraction, getting sort of running numbers again. The
  881. % ~pagewise~ option may restore true pagewise mode later.
  882. \DeclareOption{switch}{\setpagewiselinenumbers
  883. \switchlinenumbers
  884. \runningpagewiselinenumbers}
  885. \DeclareOption{switch*}{\setpagewiselinenumbers
  886. \switchlinenumbers*%
  887. \runningpagewiselinenumbers}
  888. % In twocolumn mode, we can switch the line numbers to
  889. % the outer margin, and/or start with number 1 in each
  890. % column. Margin switching is covered by the ~switch~
  891. % options.
  892. \DeclareOption{columnwise}{\setpagewiselinenumbers
  893. \columnwiselinenumberstrue
  894. \realpagewiselinenumbers}
  895. % The options ~pagewise~ and ~running~ select the major
  896. % linenumber mechanism. ~running~ line numbers refer to a real
  897. % counter value, which can be reset for any paragraph,
  898. % even getting multiple paragraphs on one page starting
  899. % with line number one. ~pagewise~ line numbers get a
  900. % unique hidden number within the document, but with the
  901. % opportunity to establish the page on which they finally
  902. % come to rest. This allows the subtraction of the page
  903. % offset, getting the numbers starting with 1 on top of each
  904. % page, and margin switching in twoside formats becomes
  905. % possible. The default mode is ~running~.
  906. %
  907. % The order of declaration of the options is important here
  908. % ~pagewise~ must come after ~switch~, to overide running
  909. % pagewise mode. ~running~ comes last, to reset the running
  910. % line number mode, e.g, after selecting margin switch mode
  911. % for ~pagewise~ running. Once more, if you specify all
  912. % three of the options ~[switch,pagewise,running]~, the
  913. % result is almost nothing, but if you later say
  914. % ~\pagewiselinenumbers~, you get margin switching, with
  915. % real pagewise line numbers.
  916. %
  917. \DeclareOption{pagewise}{\setpagewiselinenumbers
  918. \realpagewiselinenumbers}
  919. \DeclareOption{running}{\setrunninglinenumbers}
  920. % The option ~modulo~ causes only those linenumbers to be
  921. % printed which are multiples of five.
  922. \DeclareOption{modulo}{\modulolinenumbers\relax}
  923. % The package option ~mathlines~ switches the behavior of
  924. % the ~{linenomath}~ environment with its star-form.
  925. % Without this option, the ~{linenomath}~ environment does
  926. % not number the lines of the display, while the star-form
  927. % does. With this option, its just the opposite.
  928. %
  929. %%% 1999-06-10: renamed ~displaymath~ to ~mathlines~.
  930. \DeclareOption{mathlines}{\linenumberdisplaymath}
  931. % ~displaymath~ now calls for wrappers of the standard
  932. % LaTeX display math environment. This was previously
  933. % done by ~mlineno.sty~.
  934. \let\do@mlineno\relax
  935. \DeclareOption{displaymath}{\let\do@mlineno\@empty}
  936. % The ~hyperref~ package, via ~nameref~, requires three more
  937. % groups in the second argment of a ~\newlabel~. Well, why
  938. % shouldn't it get them? (New v3.07) The presencs of the
  939. % ~nameref~ package is now detected automatically
  940. % ~\AtBeginDocument~. (/New v3.07)
  941. \DeclareOption{hyperref}{\PackageWarningNoLine{lineno}{%
  942. Option [hyperref] is obsolete.
  943. \MessageBreak The hyperref package is detected automatically.}}
  944. \AtBeginDocument{%
  945. \@ifpackageloaded{nameref}{%
  946. \def\@LN@ExtraLabelItems{{}{}{}}}}
  947. \ProcessOptions
  948. % \subsection{
  949. % Package Extensions
  950. % }
  951. %
  952. % The extensions in this section were previously supplied
  953. % in seperate ~.sty~ files.
  954. %
  955. % \subsubsection{
  956. % $display math$
  957. % }
  958. %
  959. % The standard \LaTeX\ display math environments are
  960. % wrapped in a ~{linenomath}~ environment.
  961. %
  962. % (New 3.05) The ~[fleqn]~ option of the standard
  963. % \LaTeX\ classes defines the display math
  964. % environments such that line numbers appear just
  965. % fine. Thus, we need not do any tricks when
  966. % ~[fleqn]~ is loaded, as indicated by presents of
  967. % the ~\mathindent~ register. (/New 3.05)
  968. %
  969. % (New 3.05a) for ~{eqnarray}~s we rather keep the
  970. % old trick. (/New 3.05a)
  971. %
  972. % (New 3.08) Wrap ~\[~ and ~\]~ into ~{linenomath}~,
  973. % instead of ~{displaymath}~. Also save the definition
  974. % of ~\equation~, instead of replicating the current
  975. % \LaTeX\ definition. (/New 3.08)
  976. \ifx\do@mlineno\@empty
  977. \@ifundefined{mathindent}{
  978. \let\LN@displaymath\[
  979. \let\LN@enddisplaymath\]
  980. \renewcommand\[{\begin{linenomath}\LN@displaymath}
  981. \renewcommand\]{\LN@enddisplaymath\end{linenomath}}
  982. \let\LN@equation\equation
  983. \let\LN@endequation\endequation
  984. \renewenvironment{equation}
  985. {\linenomath\LN@equation}
  986. {\LN@endequation\endlinenomath}
  987. }% \@ifundefined{mathindent}
  988. \let\LN@eqnarray\eqnarray
  989. \let\LN@endeqnarray\endeqnarray
  990. \renewenvironment{eqnarray}
  991. {\linenomath\LN@eqnarray}
  992. {\LN@endeqnarray\endlinenomath}
  993. \fi
  994. % \subsubsection{
  995. % Line numbers in internal vertical mode
  996. % }
  997. %
  998. % The command ~\internallinenumbers~ adds line numbers in
  999. % internal vertical mode, but with limitations: we assume
  1000. % fixed baseline skip.
  1001. \def\internallinenumbers{\setrunninglinenumbers
  1002. \let\@@par\internallinenumberpar
  1003. \ifx\@par\@@@par\let\@par\internallinenumberpar\fi
  1004. \ifx\par\@@@par\let\par\internallinenumberpar\fi
  1005. \ifx\@par\linenumberpar\let\@par\internallinenumberpar\fi
  1006. \ifx\par\linenumberpar\let\par\internallinenumberpar\fi
  1007. \@ifnextchar[{\resetlinenumber}%]
  1008. {\@ifstar{\let\c@linenumber\c@internallinenumber
  1009. \c@linenumber\@ne}{}}%
  1010. }
  1011. \let\endinternallinenumbers\endlinenumbers
  1012. \@namedef{internallinenumbers*}{\internallinenumbers*}
  1013. \expandafter\let\csname endinternallinenumbers*\endcsname\endlinenumbers
  1014. \newcount\c@internallinenumber
  1015. \newcount\c@internallinenumbers
  1016. \def\internallinenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else\@@@par
  1017. \begingroup
  1018. \c@internallinenumbers\prevgraf
  1019. \setbox\@tempboxa\hbox{\vbox{\makeinternalLinenumbers}}%
  1020. \dp\@tempboxa\prevdepth
  1021. \ht\@tempboxa\z@
  1022. \nobreak\vskip-\prevdepth
  1023. \nointerlineskip\box\@tempboxa
  1024. \endgroup
  1025. \fi\fi
  1026. }
  1027. \def\makeinternalLinenumbers{\ifnum\c@internallinenumbers>0\relax
  1028. \hbox to\z@{\makeLineNumber}\global\advance\c@linenumber\@ne
  1029. \advance\c@internallinenumbers\m@ne
  1030. \expandafter\makeinternalLinenumbers\fi
  1031. }
  1032. % \subsubsection{
  1033. % Line number references with offset
  1034. % }
  1035. %
  1036. % This extension defines macros to refer to line
  1037. % numbers with an offset, e.g., to refer to a line
  1038. % which cannot be labeled directly (display math).
  1039. % This was formerly knows as ~rlineno.sty~.
  1040. %
  1041. % To refer to a pagewise line number with offset:
  1042. % \begin{quote}
  1043. % ~\linerefp[~<OFFSET>~]{~<LABEL>~}~
  1044. % \end{quote}
  1045. % To refer to a running line number with offset:
  1046. % \begin{quote}
  1047. % ~\linerefr[~<OFFSET>~]{~<LABEL>~}~
  1048. % \end{quote}
  1049. % To refer to a line number labeled in the same mode as currently
  1050. % selected:
  1051. % \begin{quote}
  1052. % ~\lineref[~<OFFSET>~]{~<LABEL>~}~
  1053. % \end{quote}
  1054. \newcommand\lineref{%
  1055. \ifx\c@linenumner\c@runninglinenumner
  1056. \expandafter\linerefr
  1057. \else
  1058. \expandafter\linerefp
  1059. \fi
  1060. }
  1061. \newcommand\linerefp[2][\z@]{{%
  1062. \let\@thelinenumber\thelinenumber
  1063. \edef\thelinenumber{\advance\c@linenumber#1\relax\noexpand\@thelinenumber}%
  1064. \ref{#2}%
  1065. }}
  1066. % This goes deep into \LaTeX s internals.
  1067. \newcommand\linerefr[2][\z@]{{%
  1068. \def\@@linerefadd{\advance\c@linenumber#1}%
  1069. \expandafter\@setref\csname r@#2\endcsname
  1070. \@linerefadd{#2}%
  1071. }}
  1072. \newcommand\@linerefadd[2]{\c@linenumber=#1\@@linerefadd\relax
  1073. \thelinenumber}
  1074. % \subsubsection{
  1075. % Numbered quotation environments
  1076. % }
  1077. %
  1078. % The ~{numquote}~ and ~{numquotation}~
  1079. % environments are like ~{quote}~ and
  1080. % ~{quotation}~, except there will be line
  1081. % numbers.
  1082. %
  1083. % An optional argument gives the number to count
  1084. % from. A star ~*~ (inside or outside the closing
  1085. % ~}~) prevent the reset of the line numbers.
  1086. % Default is to count from one.
  1087. \newcommand\quotelinenumbers
  1088. {\@ifstar\linenumbers{\@ifnextchar[\linenumbers{\linenumbers*}}}
  1089. \newdimen\quotelinenumbersep
  1090. \quotelinenumbersep=\linenumbersep
  1091. \let\quotelinenumberfont\linenumberfont
  1092. \newcommand\numquotelist
  1093. {\leftlinenumbers
  1094. \linenumbersep\quotelinenumbersep
  1095. \let\linenumberfont\quotelinenumberfont
  1096. \addtolength{\linenumbersep}{-\@totalleftmargin}%
  1097. \quotelinenumbers
  1098. }
  1099. \newenvironment{numquote} {\quote\numquotelist}{\endquote}
  1100. \newenvironment{numquotation} {\quotation\numquotelist}{\endquotation}
  1101. \newenvironment{numquote*} {\quote\numquotelist*}{\endquote}
  1102. \newenvironment{numquotation*}{\quotation\numquotelist*}{\endquotation}
  1103. % \subsubsection{
  1104. % Frame around a paragraph
  1105. % }
  1106. %
  1107. % The ~{bframe}~ environment draws a frame around
  1108. % some text, across page breaks, if necessary.
  1109. %
  1110. % This works only for plain text paragraphs,
  1111. % without special height lines. All lines must be
  1112. % ~\baselineskip~ apart, no display math.
  1113. \newenvironment{bframe}
  1114. {\par
  1115. \@tempdima\textwidth
  1116. \advance\@tempdima 2\bframesep
  1117. \setbox\bframebox\hbox to\textwidth{%
  1118. \hskip-\bframesep
  1119. \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
  1120. \advance\@tempdima-2\bframerule
  1121. \hskip\@tempdima
  1122. \vrule\@width\bframerule\@height\baselineskip\@depth\bframesep
  1123. \hskip-\bframesep
  1124. }%
  1125. \hbox{\hskip-\bframesep
  1126. \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%
  1127. \nointerlineskip
  1128. \copy\bframebox
  1129. \nobreak
  1130. \kern-\baselineskip
  1131. \runninglinenumbers
  1132. \def\makeLineNumber{\copy\bframebox\hss}%
  1133. }
  1134. {\par
  1135. \kern-\prevdepth
  1136. \kern\bframesep
  1137. \nointerlineskip
  1138. \@tempdima\textwidth
  1139. \advance\@tempdima 2\bframesep
  1140. \hbox{\hskip-\bframesep
  1141. \vrule\@width\@tempdima\@height\bframerule\@depth\z@}%
  1142. }
  1143. \newdimen\bframerule
  1144. \bframerule=\fboxrule
  1145. \newdimen\bframesep
  1146. \bframesep=\fboxsep
  1147. \newbox\bframebox
  1148. % \section{
  1149. % The final touch
  1150. % }
  1151. % There is one deadcycle for each line number.
  1152. \advance\maxdeadcycles 100
  1153. \endinput
  1154. % \section{
  1155. % The user commands
  1156. % }
  1157. % The user command to turn on and off line numbering
  1158. % are
  1159. % \begin{description}\item
  1160. % [|\linenumbers] \ \par
  1161. % Turn on line numbering in the current mode.
  1162. % \item
  1163. % [|\linenumbers*] \ \par$\qquad$
  1164. % and reset the line number to 1.
  1165. % \def\NL{<number>]}\item
  1166. % [|\linenumbers[\NL] \ \par$\qquad$
  1167. % and start with <number>.
  1168. % \item
  1169. % [|\nolinenumbers] \ \par
  1170. % Turn off line numbering.
  1171. % \item
  1172. % [|\runninglinenumbers*[\NL] \ \par
  1173. % Turn on ~running~ line numbers, with the same optional
  1174. % arguments as ~\linenumbers~. The numbers are running
  1175. % through the text over pagebreaks. When you turn
  1176. % numbering off and on again, the numbers will continue,
  1177. % except, of cause, if you ask to reset or preset the
  1178. % counter.
  1179. % \item
  1180. % [|\pagewiselinenumbers] \ \par
  1181. % Turn on ~pagewise~ line numbers. The lines on each
  1182. % page are numbered beginning with one at the first
  1183. % ~pagewise~ numbered line.
  1184. % \item
  1185. % [|\resetlinenumber[\NL] \ \par
  1186. % Reset ~[~Set~]~ the line number to 1
  1187. % ~[~<number>~]~.
  1188. % \item
  1189. % [|\setrunninglinenumbers] \ \par
  1190. % Switch to ~running~ line number mode. Do \emph{not}
  1191. % turn it on or off.
  1192. % \item
  1193. % [|\setpagewiselinenumbers] \ \par
  1194. % Switch to ~pagewise~ line number mode. Do \emph{not}
  1195. % turn it on or off.
  1196. % \item
  1197. % [|\switchlinenumbers*] \ \par
  1198. % Causes margin switching in pagewise modes. With the
  1199. % star, put the line numbers on the inner margin.
  1200. % \item
  1201. % [|\leftlinenumbers*] \ \par
  1202. % \item
  1203. % [|\rightlinenumbers*] \ \par
  1204. % Set the line numbers in the left/right margin. With the
  1205. % star this works for both modes of operation, without
  1206. % the star only for the currently selected mode.
  1207. % \item
  1208. % [|\runningpagewiselinenumbers] \ \par
  1209. % When using the pagewise line number mode, do not
  1210. % subtract the page offset. This results in running
  1211. % line numbers again, but with the possibility to switch
  1212. % margins. Be careful when doing line number
  1213. % referencing, this mode status must be the same while
  1214. % setting the paragraph and during references.
  1215. % \item
  1216. % [|\realpagewiselinenumbers] \ \par
  1217. % Reverses the effect of ~\runningpagewiselinenumbers~.
  1218. % \item
  1219. % [|\modulolinenumbers[\NL] \ \par
  1220. % Give a number only to lines which are multiples of
  1221. % ~[~<number>~]~. If <number> is not specified, the
  1222. % current value in the counter ~linenumbermodulo~ is
  1223. % retained. <number>=1 turns this off without changing
  1224. % ~linenumbermodulo~. The counter is initialized to 5.
  1225. % \item
  1226. % [|\linenumberdisplaymath] \ \par
  1227. % Number the lines of a display math in a ~{linenomath}~
  1228. % environment, but do not in a ~{linenomath*}~
  1229. % environment. This is used by the package option
  1230. % ~[mathlines]~.
  1231. % \item
  1232. % [|\nolinenumberdisplaymath] \ \par
  1233. % Do not Number the lines of a display math in a
  1234. % ~{linenomath}~ environment, but do in a
  1235. % ~{linenomath*}~ environment. This is the default.
  1236. % \item
  1237. % [|\linelabel] \ \par
  1238. % Set a ~\linelabel{~<foo>~}~ to the line number where
  1239. % this commands is in. Refer to it with the \LaTeX\
  1240. % referencing commands ~\ref{~<foo>~}~ and
  1241. % ~\pageref{~<foo>~}~.
  1242. % \end{description}
  1243. % The commands can be used globally, locally within groups
  1244. % or as environments. It is important to know that they
  1245. % take action only when the ~\par~ is executed. The
  1246. % ~\end{~<mode>~linenumbers}~ commands provide a ~\par~.
  1247. % Examples:
  1248. % \begin{verse}
  1249. % ~{\linenumbers~ <text> ~\par}~ \\
  1250. % \ \\
  1251. % ~\begin{linenumbers}~ \\
  1252. % <text> \\
  1253. % ~\end{linenumbers}~ \\
  1254. % \ \\
  1255. % <paragraph> ~{\linenumbers\par}~ \\
  1256. % \ \\
  1257. % ~\linenumbers~ \\
  1258. % <text> ~\par~ \\
  1259. % ~\nolinenumbers~ \\
  1260. % \ \\
  1261. % ~\linenumbers~ \\
  1262. % <paragraph> ~{\nolinenumbers\par}~ \\
  1263. % \end{verse}
  1264. %
  1265. %
  1266. % \subsection{
  1267. % Customization hooks
  1268. % }
  1269. % There are several hooks to customize the appearance of the
  1270. % line numbers, and some low level hooks for special
  1271. % effects.
  1272. % \begin{description}\item
  1273. % [|\thelinenumber] \ \par
  1274. % This macro should give the representation of the line
  1275. % number in the \LaTeX-counter ~linenumber~. The
  1276. % default is provided by \LaTeX: \par$\qquad$
  1277. % ~\arabic{linenumber}~
  1278. % \item
  1279. % [|\makeLineNumberLeft] \ \par
  1280. % This macro is used to attach a line number to the left
  1281. % of the text page. This macro should fill an ~\hbox to 0pt~
  1282. % which will be placed at the left margin of the
  1283. % page, with the reference point aligned to the line to
  1284. % which it should give a number. Please use the macro
  1285. % ~\LineNumber~ to refer to the line number.
  1286. %
  1287. % The default definition is \par$\qquad$
  1288. % ~\hss\linenumberfont\LineNumber\hskip\linenumbersep~
  1289. % \item
  1290. % [|\makeLineNumberRight] \ \par
  1291. % Like ~\makeLineNumberLeft~, but for line numbers on
  1292. % the right margin.
  1293. %
  1294. % The default definition is \par$\qquad$
  1295. % ~\linenumberfont\hskip\linenumbersep\hskip\textwidth~ \par$\qquad$
  1296. % ~\hbox to\linenumberwidth{\hss\LineNumber}\hss~
  1297. % \item
  1298. % [|\linenumberfont] \ \par
  1299. % This macro is initialized to \par$\qquad$
  1300. % ~\normalfont\tiny\sffamily~
  1301. % \item
  1302. % [|\linenumbersep] \ \par
  1303. % This dimension register sets the separation of the
  1304. % linenumber to the text. Default value is ~10pt~.
  1305. % \item
  1306. % [|\linenumberwidth] \ \par
  1307. % This dimension register sets the width of the line
  1308. % number box on the right margin. The distance of the
  1309. % right edge of the text to the right edge of the line
  1310. % number is ~\linenumbersep~ + ~\linenumberwidth~. The
  1311. % default value is ~10pt~.
  1312. % \item
  1313. % [|\theLineNumber] (for wizards) \ \par
  1314. % This macro is called for printing a ~\newlabel~ entry
  1315. % to the aux-file. Its definition depends on the mode.
  1316. % For running line numbers it's just ~\thelinenumber~,
  1317. % while in pagewise mode, the page offset subtraction
  1318. % is done in here.
  1319. % \item
  1320. % [|\makeLineNumber] (for wizards) \ \par
  1321. % This macro produces the line numbers. The definition
  1322. % depends on the mode. In the running line numbers
  1323. % mode it just expands ~\makeLineNumberLeft~.
  1324. % \item
  1325. % [|\LineNumber] (for wizards) \ \par
  1326. % This macro is called by ~\makeLineNumber~ to typeset
  1327. % the line number. This hook is changed by the modulo
  1328. % mechanism.
  1329. % \end{description}
  1330. % \end{document}%D
  1331. ------------------------------------------------------------------------------
  1332. echo "expect errors for unknown commands 'iffalse' and 'fi'";# SHELL
  1333. awk '/A[W]K/' lineno.sty | awk -f - lineno.sty >lineno.tex; # SHELL
  1334. latex lineno; latex lineno; latex lineno; latex lineno; # SHELL
  1335. awk '/DOC A [W] K/' lineno.sty | awk -f - lineno.sty >lineno.doc; # DOC SH
  1336. BEGIN{DOC=-1; # AWK DOC A W K
  1337. BEGINCODE = "\\begin{code}\\begin{verbatim}"; # AWK
  1338. ENDCODE = "\\end{verbatim}\n\\end{code}"; } # AWK
  1339. BEGINCODE = "% \\begin{macrocode}"; # DOC A W K
  1340. ENDCODE = "% \\end{macrocode}"; } # DOC A W K
  1341. /^[ \t]*$/ { ECNT++; next; } # AWK DOC A W K
  1342. /\\documentclass/{ sub("article","ltxdoc") } # DOC A W K
  1343. /%D$/ { sub("^%* *",""); sub("%D$",""); # DOC A W K
  1344. print > "lineno.drv"; next } # DOC A W K
  1345. /^%%/ { next; } # AWK DOC A W K
  1346. /^%/ { if (!DOC) { print ENDCODE; } # AWK DOC A W K
  1347. DOC=1; ECNT=0; # AWK DOC A W K
  1348. sub("^% *",""); # AWK
  1349. sub("^% *","% "); # DOC A W K
  1350. print; next; } # AWK DOC A W K
  1351. DOC<0 { next } # AWK DOC A W K
  1352. /^-+-$/ { if (!DOC) print ENDCODE; exit } # AWK DOC A W K
  1353. { if (DOC) { ECNT=DOC=0; print BEGINCODE; } # AWK DOC A W K
  1354. while (ECNT>0) { print " "; ECNT--; } # AWK DOC A W K
  1355. print $0; } # AWK DOC A W K