From: jafortun@vela.acs.oakland.edu (James Fortune)
Subject: More progress in linenumbering style for LaTeX thanks to c.t.t.
Summary: Real Soon Now
Keywords: pentastich, line numbering, odd/even pages
Organization: Oakland University, Rochester MI.
Date: Tue, 8 Sep 1992 04:23:02 GMT

Here is the latest attempt at a style file for numbering lines every
nth line with line numbers on the right for odd pages and line numbers
on the left for even pages.  Right now I am trying to get all the line
numbers printed before making the style file more selective.  A few of
the remaining problems:

1.  On page 82 of the TeXbook, Dr. Knuth says, "It's possible to put
    text into the left margin using \llap, or into the right margin
    using \rlap, because TeX does not insist that the contents of a
    box must be strictly confined within that box's boundaries."

    I've tried many different things and have still not been able to
    get the numbers into the right margin.  I'll keep trying things.
    I might have to reread some of the TeXbook :-).

2.  It now properly places all the footnotes and such in the right places.
    It also numbers the blank line between paragraphs (which is not what
    I want).  David Salomon says on p. 220 of TUGboat, Volume 11 (1990),
    No. 2, "Macro \countlines below assumes that each box on the page is
    a line of text and should be numbered.  Alternatively, if certain
    items on the page should not be numbered, they can be placed in
    vboxes, and \countlines revised to count only hboxes."

    Any ideas on how to implement this for blank lines?  The blank lines
    that get numbered usually follow a line ending with \\
    Perhaps I should have changed \parskip to get the effect I wanted.

3.  I need to complete the rest of the start/stop/everynlines logic.

4.  I'm getting two pages with blank lines numbered 1 and 2 before the
    document gets going.  This should go away when the start logic is
    in effect but it's a bit puzzling.  The .log file makes it look like
    those pages are done before the various .sty files are done loading.

Please feel free to comment, criticize, etc.  I will post a summary of
any responses that come directly to me.  Michal Jaegermann was the only
person responding to my lastest post.  This newsgroup is quite professional
and helped immensely in getting me started on this style file.

%---------------------------------------------------------------------------
% altnline.sty    Version  -2.0
%
% Disclaimer - Use at your own risk.
%
% Based upon the TUGboat article ``Output Routines: Examples and Techniques.''
% that appeared in Volume 11 (1990), Nos. 1, 3 and 4.
% 
% This version incorporates suggestions made by Michal Jaegermann, namely:
%
% * Include explicit reference to the TUGboat article
% * ``You can use \def in style files without violating LaTeX spirit.
%     If you are writing style it is assumed that you know what you are
%     doing. :-)'' --- I don't know what I'm doing so I left the \newcommand
%   and \renewcommand commands as is.
% * Internal macros from style file should be protected against name conflicts
%   with user stuff by inclusion of '@' character in names. --- This is quite
%   reasonable so command names that the user is not expected to use in a
%   LaTeX file have been changed to include the '@' character.
% * He sent a new appendline command that adds numbers on a left margin
%   only that was also inspired by the Salomon article.  He left it as an
%   exercise for me to modify it for two-sided printing.  I probably shouldn't 
%   have picked something from the double dangerous bend signs for my first
%   serious attempt at hacking on a style file :-).
% * A global linesSoFar so that lineCount may be set to linesSoFar to deal
%   with box255 being broken from the bottom up.  countlines (or its
%   equivalent) advances linesSoFar by a count of lines on a current page.
% * Use of a decremented remainder variable to determine when to add a number
%   to a line. --- I'm trying to get all the lines first.
% * He advised not trying to combine the countlines and duplicate operations
%   into a single loop.  ``I am afraid that you really have to first count
%   how many lines you have on a page and later use this value to produce
%   line numbers.''
%
\font\sevenrm=cmr7

\newcommand\zero@ToSp{\parskip=1sp plus1pt
  \renewcommand\vfil{\vskip1sp plus1fil}
  \renewcommand\vfill{\vskip1sp plus1fill}
  \abovedisplayshortskip=1sp plus3pt
  \postdisplaypenalty=1
  \interlinepenalty=1}

\zero@ToSp

\newcount\lineCountInit  % used to establish lineCount of first line on page
\global\lineCountInit=0
\newcount\lineCountInterval  % used to determine how often lineCount is printed
\global\lineCountInterval=5
\newcount\lineCountStart  % don't print lineCount until lineCount reaches this
\global\lineCountStart=1
\newcount\lineCountFinish  % don't print lineCount after this
\global\lineCountFinish=400

\newcount\linesSoFar
\newcommand\resetlinesSoFar{%
\global\linesSoFar=\lineCountInit}

\resetlinesSoFar

\newcount\lineCount
\newcount\PagelineCount
\newif\ifAnyleft \newcount\pen
\newcommand\count@lines{%
  \global\PagelineCount=0
  \global\lineCount=\linesSoFar
  \loop \Anyleftfalse
    \ifdim\lastskip=0pt \ifdim\lastkern=0pt \ifnum\lastpenalty=0
      \setbox0=\lastbox \ifvoid0
    \else \Anylefttrue \global\advance\lineCount by 1 
                       \global\advance\PagelineCount by 1 \fi
    \else \Anylefttrue \unpenalty \fi
    \else \Anylefttrue \unkern \fi
    \else \Anylefttrue \unskip \fi
  \ifAnyleft \repeat}

\newcommand\@duplicate{%
  \loop \Anyleftfalse
    \ifdim\lastskip=0pt \ifdim\lastkern=0pt \ifnum\lastpenalty=0
      \global\setbox0=\lastbox \ifvoid0 % end of breakup loop
    \else \Anylefttrue                  % box present
      \append@line
      \global\setbox1=\vbox{\box0 \unvbox1}\fi
    \else \Anylefttrue                  % penalty present
      \pen=\lastpenalty
      \global\setbox1=\vbox{\penalty\pen\unvbox1}\unpenalty\fi
    \else \Anylefttrue                  % kern present
      \dimen0=\lastkern
      \global\setbox1=\vbox{\kern\dimen0 \unvbox1}\unkern \fi
    \else \Anylefttrue                  % skip present
      \skip0=\lastskip
      \global\setbox1=\vbox{\vskip\skip0 \unvbox1}\unskip \fi
    \ifAnyleft \repeat}

\newcommand\append@line{% put rest of lineCount logic in later
  \setbox2=\vbox to 0pt{\smash{\sevenrm\the\lineCount}}
  \global\advance\lineCount-1
  \wd2=20pt                           % that much of space for a line number
  \ifodd\count0\setbox0=\hbox{\rlap{\rule{\wd2}{0pt}\box0} \box2}
     \else\setbox0=\hbox{\llap{\box2} \box0}\fi           % prepend even pages
  \global\setbox1 =
      \vbox{ \box0 \unvbox1}}         % add this to a reconstructed page

\newbox\brk
\output={\global\PagelineCount=0
         \global\lineCount=\linesSoFar
         \setbox\brk=\vbox{\unvcopy255 \count@lines}
                  \global\setbox1=\vbox{}
         \setbox\brk=\vbox{\unvcopy255 \@duplicate}
         \ifdim\ht\brk>0pt \message{Incomplete breakup} \fi
         \ht1=0pt \dp1=0pt
         \global\setbox255=\vbox to\vsize{\unvbox1} % attempt to get glues back
         \global\advance\linesSoFar by \PagelineCount
         \@makecol
         \@outputpage}
%-----------------------------------------------------------------------------
-- 
James Fortune                  - There are many sides to every question
Oakland University             - until you know the truth.  Then there's
jafortun@vela.acs.oakland.edu  - only one.  --  C. S. Lewis
Disclaimer: Not O.U. opinions  -