From: jafortun@vela.acs.oakland.edu (James Fortune)
Newsgroups: comp.text.tex
Subject: LaTeX style file: altnline.sty
Summary: Suggestions encouraged
Keywords: line numbering, pentastich, source code listings
Date: 20 Dec 92 20:54:57 GMT
Organization: Oakland University, Rochester Michigan.

Here is the latest working version of altnline.sty.  It currently prints
every line number in the left margin on even pages and in the right margin
on odd pages.  It should work with any other style file involving input
routines provided that altnline is listed after the other style.  This is
because altnline.sty makes a copy of the current output routine and uses
it for when the output routine is invoked for purposes other that outputting
a page.  Footnotes can be used without messing up the numbering scheme.
I have not tried to see if \section{This Section} will work.

The problems that I am working on are:

1) It counts a blank line following \\[1em] as a line (not what I want for
   my application).

2) I sometimes use hanging indentation for short quotes.  altnline.sty
   wipes out the indentation and prints the number to the left of where
   the other numbers are (on odd pages).

3) I expect that putting in the logic for adjusting line numbers, printing
   every nth line, and turning on and off the line numbering mechanism
   will not be difficult although it could be more difficult that it looks
   because I do not fully understand box manipulation yet.  I would like
   to avoid the countdown register proposed by Michal Jaegermann if possible.

The limits of the output routine still include the caveats in the original
TugBoat article such as float environments, etc.

If anyone has any suggestions regarding the above problems (or any others)
please email me.  I am getting very close to the deadline for the book
that needs this style file and might not be able to hack on it much once
the book is done.  I DON'T want to hand type the numbers in <cringe>
especially when the style file is so close to completion.  Also, if anyone
more qualified than myself (most of you) would like to take over improving
on this style file once the book is done let me know.

-----------------------------------------------------------------------------
% altnline.sty    Version -1.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

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

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

\zero@ToSp

% This logic has not been implemented yet.
\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{\box0\rlap{\rule{\wd2}{0pt}\box2}}
     \else\setbox0=\hbox{\llap{\box2} \box0}\fi           % prepend even pages
  \global\setbox1 =
      \vbox{ \box0 \unvbox1}}         % add this to a reconstructed page

\newbox\brk    % Use old output definition if page is not to be output
	\output={\ifnum\outputpenalty <-\@M \output={\LaTeX@output} \else
        \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\fi}
-----------------------------------------------------------------------------
-- 
James Fortune                  - Internet: Thousands of people mostly unaware
Oakland University             - of the sight and sound of each other, linked
jafortun@vela.acs.oakland.edu  - together by the bits of our souls that can
Disclaimer: Not O.U. opinions  - fit through computer keyboards.
Newsgroups: comp.text.tex