summaryrefslogtreecommitdiff
path: root/obsolete/macros/latex/contrib/numline/numline.sty
blob: 5739c6ca663d080a7bd03e55d6c55137e9295216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     author          = "Michal Jaegermann and James Fortune",
%%%     version         = "1.3",
%%%     date            = "06 Aug 1995",
%%%     filename        = "numline.sty",
%%%     address         = "10923 36Av, Edmonton,
%%%                        Alberta, CANADA T6J 0B7",
%%%     telephone       = "403 438 7012",
%%%     email           = "michal@ellpspace.math.ualberta.ca
%%%                        jafortun@vela.acs.oakland.edu (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "line numbers",
%%%     supported       = "yes (sort of)",
%%%     abstract        = "LaTeX style file for putting line numbers on
%%%                        margins of at least some documents which will
%%%                        survive such treatment.  It works modifying
%%%                        LaTeX output routine, so do not expect that
%%%                        this will work on anything but simple text!
%%%                        If it does then you are very lucky person.
%%%                        On the other hand is equally happy with
%%%                        LaTeX2e as with LaTeX 2.09
%%%
%%%                        In some situations you should be able to
%%%                        adjust it for special needs but do not expect
%%%                        miracles.  For plain TeX try EDMAC by Dominik
%%%                        Wujastyk.
%%%
%%%                        This style provides:
%%%                          - continuous line numbering for the whole
%%%                            document or, if \lnumpagetrue, restarted
%%%                            on every page
%%%                          - numbers on alternate margins when 'twoside'
%%%                            document option is in force
%%%                          - ajustable interval between line numbers
%%%                            given by a value of \lineCountInterval counter
%%%                          - settable ranges of numbered lines
%%%                            with \nlinesBoundary macro
%%%                          - changing line witdth together with indent.sty
%%%                        See comments and examples below.",
%%%  }
%%% ====================================================================
%
% HISTORY:
%
% Based upon the TUGboat article ``Output Routines: Examples and Techniques.''
% by David Salomon that appeared in Volume 11 (1990), Nos. 1, 3 and 4.
%
% Started by James Fortune as altnline.sty with some help and advice
% from MJ which later hacked it for quite a while.
%
% Changes for version 1.0
%
% * Protect internal macros by including '@' in their names.
% * Split countlines and duplicate operations from a single loop
%   (does not work that way).
%
% Changes for version 1.1 (mj)
%
% * Added logic to print line numbers only every \lineCountInterval
% * Added a way to set up ONE range of numbered lines; use \nlinesBoundary
%   to do that. (It is possible to sprinkle your text with \nlinesBoundary
%   commands and they may even do the right thing if you will put them
%   in right places - but this is a hack.)
% * Miscellaneous cleanup
%
% Changes for 1.2 (mj)
% * Prevent 'Output routine didn't use all of \box255' messages
% * Add code to handle, in a fashion, indentation generated by indent.sty
% * Add switch to restart numbering on every page
% * Make switching enumeration margins dependent on 'twoside' option
%
% Changes for 1.3 (mj)
% * None whatsoever to the code
% * Confirmed that it works to the same extent as before :-) with LaTeX2e.
% * More dire warninigs comments explaining how to make a document
%   work with numline.sty when it doesn't.

% USER INTERFACE:
%
% In your document you have an access to:
%   * a conditional \lnumpage which can be set with \lnumpagetrue
%     or \lnumpagefalse (default); when true line numbers are reset
%     on every page;
%   * a counter \lineCountInterval (default 5) to set interval between
%     line numbers
%   * a command \nlinesBoundary to set boundaries for line numbering;
%     example of use:
%          \nlinesBoundary{12}, or \nlinesBoundary[50]{12}
%     with \nlinesBoundary[<optional upper limit>]<lower limit>
% Any negative number for an upper limit means number to the very end.
% This is a default!

% DIRE WARNINGS:
%
% This IS a hack doing things which is not supposed to do by messing
% with LaTeX output routine.  It works as expected with versions of
% LaTeX I tested it, but your mileage may vary.  In particular, if
% some your other package does something with \output then you may
% have problems forcing such two to cooperate.
%
% You may still use this on more documents than it looks like at the
% first blush.  If you will see ``Incomplete breakup'' message consult
% DREADED INCOMPLETE BREAKUP section (it follows sample input) for
% some day saving hints.

% INDENTING:
% Any changes in line width caused by indent.sty have the same effect.
% So \begin{indentation}{2em}{2em} and \begin{indentation}{4em}{0pt}
% are equivalent.  Positioning of indented lines can be only performed
% by modifications in a definition of \@duplicate.  Check examples
% below.

%
% various counters
%
\newcount\lineCountInterval % used to determine how often lin@Count is printed
\lineCountInterval=5
%
\newcount\lin@CountInit     % used to establish lineCount of first line on page
\lin@CountInit=\z@
\newcount\lin@CountStart    % don't print line numbers below this value
\lin@CountStart=\@ne
\newcount\lin@CountFinish   % don't print line numbers higher than this
\lin@CountFinish=\m@ne
%
\newcount\lin@sSoFar
% remainder from division of \lin@sSoFar by \lineCountInterval
\newcount\total@Rem
% current value of remainder used to produce line numbers
\newcount\lin@rem

%
% A command to set boundaries for line numbering.
%
\def\nlinesBoundary{\@ifnextchar[{\nlin@sBoundary}{\nlin@sBoundary[\m@ne]}}
\edef\nlin@sBoundary[#1]#2{%
  \global\lin@CountFinish=#1\relax
  \global\lin@CountStart=#2\relax}

%
% if's
%
\newif\iflnumpage % if true restart line numbers on every page
%
\newif\ifa@@lnum  % do we really want to print that line number?

%
% make page easier to break into lines
%
\def\zero@ToSp{\parskip=\@ne sp plus\@ne pt
  \def\vfil{\vskip1sp plus1fil}
  \def\vfill{\vskip1sp plus1fill}
  \abovedisplayshortskip=\@ne sp plus3pt
  \postdisplaypenalty=\@ne
  \interlinepenalty=\@ne}

\zero@ToSp

% It is possible to change a "look" of numbering by modifications
% of this macro; in particular calculations below allow to start
% with \lin@CountInit different from 0

\def\resetlin@sSoFar{%
  \global\lin@sSoFar=\lin@CountInit
  \total@Rem=\lin@CountInit
  \divide\total@Rem by\lineCountInterval
  \multiply\total@Rem by-\lineCountInterval
  \global\advance\total@Rem by\lin@CountInit}

\resetlin@sSoFar

\newcount\lin@Count
\newif\ifAnyleft \newcount\pen
\def\count@lines{%
  \global\lin@Count=\lin@sSoFar
  \loop \Anyleftfalse
    \ifdim\lastskip=\z@ \ifdim\lastkern=\z@ \ifnum\lastpenalty=\z@
      \setbox\z@=\lastbox
    \ifvoid\z@
    \else \Anylefttrue \global\advance\lin@Count by\@ne
                       \global\advance\total@Rem by\@ne
                       \ifnum\total@Rem=\lineCountInterval
                          \global\total@Rem=\z@
                       \fi\fi
    \else \Anylefttrue \unpenalty \fi
    \else \Anylefttrue \unkern \fi
    \else \Anylefttrue \unskip \fi
  \ifAnyleft \repeat}

\def\@duplicate{%
  \loop \Anyleftfalse
    \ifdim\lastskip=\z@ \ifdim\lastkern=\z@ \ifnum\lastpenalty=\z@
      \global\setbox\z@=\lastbox \ifvoid\z@ % end of breakup loop
    \else \Anylefttrue                  % box present
      \ifnum\lin@rem=\z@                % if remainder non-zero we do not care
        \lin@rem=\lineCountInterval
        \ifnum\lin@Count<\lin@CountStart
          \a@@lnumfalse
        \else
          \ifnum\lin@CountFinish<\z@    % no upper bound
            \a@@lnumtrue                % print it
          \else
            \ifnum\lin@Count>\lin@CountFinish
              \a@@lnumfalse
            \else
              \a@@lnumtrue              % this is the ticket
            \fi
          \fi
        \fi
      \else
         \a@@lnumfalse
      \fi
% Position shorter lines to keep indentation.  Only a line length
% is important
% This prints indented lines flush right margin
      \setbox\z@=\vbox{\hbox to\textwidth{\hfil\box\z@}}
% This for centered lines
%     \setbox\z@=\vbox{\hbox to\textwidth{\hfil\box\z@\hfil\relax}}
% For other variants you are on your own!!
      \ifa@@lnum
         \append@line
      \else
         \global\setbox\@ne=\vbox{\box\z@\unvbox\@ne}
      \fi
      \advance\lin@Count\m@ne
      \advance\lin@rem by\m@ne\fi
    \else \Anylefttrue                  % penalty present
      \pen=\lastpenalty
      \global\setbox\@ne=\vbox{\penalty\pen\unvbox\@ne}\unpenalty\fi
    \else \Anylefttrue                  % kern present
      \dimen\z@=\lastkern
      \global\setbox\@ne=\vbox{\kern\dimen\z@\unvbox\@ne}\unkern \fi
    \else \Anylefttrue                  % skip present
      \skip\z@=\lastskip
      \global\setbox\@ne=\vbox{\vskip\skip\z@\unvbox\@ne}\unskip \fi
    \ifAnyleft \repeat}

\def\append@line{%  attach line number
  \setbox2=\vbox to \z@{\smash{\footnotesize\the\lin@Count}}
  \wd2=25pt                           % that much of space for a line number
  \if@twoside                         % we are printing twoside
    \ifodd\count\z@                   % on the right margin for odd pages
       \setbox\z@=\hbox{\box\z@\rlap{\rule{\wd2}{\z@}\box2}}
    \else                             % and on the left for even
       \setbox\z@=\hbox{\llap{\box2}\box\z@}
    \fi
  \else                               % one sided printing
    \setbox\z@=\hbox{\llap{\box2}\box\z@} % left margin only
  \fi
  \global\setbox\@ne=\vbox{\box\z@\unvbox\@ne}}% add this to a reconstructed
                                               % page

% Rainer showed me how to save the previous \output.  Thanks Rainer. (jaf)
\newtoks\LaTeX@output
\LaTeX@output = \expandafter{\the\output}

\newbox\brk

\output={\ifnum\outputpenalty <-\@M
    \output={\LaTeX@output}   % Use old output definition if page
                              % is not to be output
  \else
    \lin@Count=\lin@sSoFar
    \setbox\brk=\vbox{\unvcopy\@cclv\count@lines}
             \global\setbox\@ne=\vbox{}
             \lin@rem=\total@Rem
    \iflnumpage
      \resetlin@sSoFar
    \else
      \global\lin@sSoFar=\lin@Count
    \fi
    \setbox\brk=\vbox{\unvcopy\@cclv\@duplicate}
    \ifdim\ht\brk>\z@\message{Incomplete breakup}\fi
    \ht\@ne=\z@\dp\@ne=\z@
    \global\setbox\@cclv=\vbox to\vsize{\unvbox\@ne}% attempt to get glues back
    \@makecol\@opcol
  \fi
\setbox\brk=\box\@cclv
\deadcycles=\z@
}

\endinput

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Save up to \end{document} in a separate file and use as a playground
%% for testing

\ifx\documentclass\undefined
\documentstyle[numline%  %% This is LaTeX 2.09
%,indent%                %% uncomment this line if you have indent.sty
]{article}
\else
\documentclass{article}  %% LaTeX2e header
\usepackage{numline%
%,indent%                %% uncomment this line if you have indent.sty
}
\fi

\newcommand\testtext{%
This is the test.  This is the test.  This is the test.  This is the
test.  This is the test.  This is the test.  This is the test.  This
is the test.  This is the test.  This is the test.  This is the test.
}
\let\oldSection=\section
%\renewcommand{\section}[1]{\vbox{\vspace{3.5ex}\oldSection{#1}}}
\def\section#1 \label#2{\vbox{\vspace{3.5ex}\oldSection{#1}\label{#2}}}

%\lnumpagetrue % Uncomment this to restart lines numbers at every page
%\lineCountInterval=1 % default is 5
\begin{document}

% This gives only a length of indented line - not a position.
% See comments in numline.sty

\section{Section one}
\label{sec-start}

\testtext

% This gives only a length of indented line - not a position.
% See comments in numline.sty
% Uncomment following four lines if you are using indent.sty
%indent%\begin{indentation}{3em}{0cm}
%indent%  \noindent\testtext
%indent%  \testtext
%indent%\end{indentation}

\testtext\testtext\testtext\testtext\testtext\testtext
\begin{minipage}{\textwidth}
  \begin{displaymath}
    2 \times 2 = 4
  \end{displaymath}
\end{minipage}

\testtext
\par\noindent\parbox{\textwidth}{%
  \begin{displaymath}
    2 \times 2 = 4
  \end{displaymath}
}

\testtext\testtext\testtext\testtext\testtext\testtext\testtext\testtext

\section{Another section}
\label{sec-too}

\testtext\testtext\testtext\testtext\testtext

\par\noindent\parbox{\textwidth}{
  \begin{displaymath}
    \left(
      \begin{array}{c}
        a \\ b
      \end{array}
    \right[
  \end{displaymath}
}
\testtext\testtext\testtext\testtext\testtext\testtext

\end{document}

DREADED INCOMPLETE BREAKUP AND OTHER PASTIMES

The code for line numbering works by splitting an already set page
into separate lines and by putting resulting boxes together with their
line numbers.  One difficulty with that is that is not always clear on
a typeset page what constitutes a ``line''.  The other, related to
some extent, is that TeX does not have tools to accomplish required
split in every situation; you will see then ``Incomplete breakup''
message and part of your input will vanish from dvi file.

It is usually possible to work around this limitation by hiding
troublesome elements in boxes. \minipage and \parbox will serve very
well. These two methods do not have the same effect and, depending on
circumstances, one or another may be preferable.  Test input provides
an example for displayed formulas.

Sectioning commands will likely cause trouble as well. You may write
something like ``\vbox{\vspace{3.5ex}\section{Another section}}'' in
place of an original ``\section{Another section}''.  These are NOT
equivalent, but when you are numbering lines presumably you are not
creating a masterpiece of a typography and such replacement should be
good enough.

In a longer document meant to be typed with and without numbered lines
changes like above can be troublesome.  Instead you can try re-defini-
tions which can be used conditionally. Here is how ``\section'' can be
redone:

\let\oldSection=\section
\renewcommand{\section}[1]{\vbox{\vspace{3.5ex}\oldSection{#1}}}

(making this work with optional arguments is left as an exercise :-).
This still will not work on section titles with labels.  One can use
then a redefinition like in the sample text:

\let\oldSection=\section
\def\section#1 \label#2{\vbox{\vspace{3.5ex}\oldSection{#1}\label{#2}}}

Warning: This approach requires rather specific section headers. Every
``\section{...}'' has to be followed by ``\label{...}'' separated by
white space and/or one new-line.

Go and number your lines.