\documentclass[a4paper,12pt]{article}%D
\usepackage{lineno}%D

\title{
\texttt{\itshape
lineno.sty \ v2.02 1997/03/17
}\\\ \\
A \LaTeX\ style option to attach                  
\\        Line numbers on paragraphs
}\author{
Stephan I. B\"ottcher                    
}\date{
stephan@alzt.tau.ac.il                   
\\              boettcher@desy.de                     
\\}


\def~{\verb~}
\catcode`\<\catcode`\~
\def<#1>{$\langle${\itshape#1}\/$\rangle$}
\catcode`\|\catcode`\~
\def|#1{{\ttfamily\string#1}}
\newenvironment{code}
{\par\runninglinenumbers
\modulolinenumbers[1]
\linenumbersep.3em
\footnotesize
\def\linenumberfont
{\normalfont\tiny\itshape}}
{}

\begin{document}%D
\pagewiselinenumbers
\maketitle
\tableofcontents
\sloppy



\section{
Introduction
}
This package provides line numbers on paragraphs.
After \TeX\ has broken a paragraph into lines there will
be line numbers attached to them, with the possibility to
make references  through the \LaTeX\ ~\ref~, ~\pageref~
cross reference mechanism.  This includes four issues:
\begin{itemize}
\item   attach a line number on each line,
\item   create references to a line number,
\item   control line numbering mode,
\item   count the lines and print the numbers.
\end{itemize}
The first two points are implemented through patches to
the output routine.  The third by redefining ~\par~, ~\@par~
and ~\@@par~.  The counting is easy, as long as you want
the line numbers run through the text.  If they shall
start over at the top of each page, the aux-file as well
as \TeX s memory have to carry a load for each counted line.

I wrote this package for my wife Petra, who needs it for
transcriptions of interviews.  This allows her to
precisely refer to passages in the text.  It works well
together with ~\marginpar~s, but not with displaymath. 

This style option is written for \LaTeXe.
\begin{code}\begin{verbatim}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lineno}
  [1997/03/17 line numbers on paragraphs (c) SIB v2.02]
\end{verbatim}
\end{code}

\section{
Put the line numbers to the lines
}
The line numbers have to be attached by the output
routine.  We simply set the ~\interlinepenalty~ to -100000.
The output routine will be called after each line in the
paragraph,  except the last,  where we trigger by ~\par~.
The ~\linenopenalty~ is small enough to compensate a bunch of
penalties (e.g., with ~\samepage~).
\begin{code}\begin{verbatim}
\newcount\linenopenalty\linenopenalty=-100000
\mathchardef\linenopenaltypar=25000
\end{verbatim}
\end{code}
So let's make a hook to ~\output~,  the direct way. The \LaTeX\ 
macro ~\@reinserts~ puts the footnotes back on the page.
\begin{code}\begin{verbatim}
\output=\expandafter{%
        \expandafter \LineNoTest
        \expandafter \if@tempswa
                       \the\output
                     \else
                       \@reinserts
                       \MakeLineNo
                     \fi
                     }
\end{verbatim}
\end{code}
The float mechanism inserts ~\interlinepenalty~s during
~\output~.  So carefully reset it before going on.  Else
we get doubled line numbers on every float placed in
horizontal mode, e.g, from ~\linelabel~.  

Sorry, neither a ~\linelabel~ nor a ~\marginpar~ should
insert a penalty, else the following linenumber
could go to the next page. Nor should any other
float.  So let us suppress the ~\interlinepenalty~ 
altogether with the ~\@nobreak~ switch.

Since (ltspace.dtx, v1.2p)[1996/07/26], the ~\@nobreaktrue~ does
it's job globally.  We need to do it locally here.
\begin{code}\begin{verbatim}
\def\LineNoTest{%
  \ifnum\interlinepenalty<-\linenopenaltypar
     \advance\interlinepenalty-\linenopenalty
     \my@nobreaktrue
     \fi
  \@tempswatrue
  \ifnum\outputpenalty>-\linenopenaltypar\else
     \ifnum\outputpenalty>-175000\relax
       \@tempswafalse
       \fi
     \fi
  }
 
\def\my@nobreaktrue{\let\if@nobreak\iftrue}
\end{verbatim}
\end{code}
We have to return all the page to the current page, and
add a box with the line number, without adding
breakpoints, glue or space.  The depth of our line number
should be equal to the previous depth of the page, in
case the page breaks here,  and the box has to be moved up
by that depth.  

The ~\interlinepenalty~ comes after the ~\vadjust~ from a
~\linelabel~,  so we increment the line number \emph{after}
printing it. The macro ~\makeLineNumber~ produces the
text of the line number, see section \ref{appearance}.
Finally we put in the natural ~\interlinepenalty~, except
after the last line. 
\begin{code}\begin{verbatim}
\def\MakeLineNo{\@tempdima\dp\@cclv \unvbox\@cclv
   \sbox\@tempboxa{\hbox to\z@{\makeLineNumber}}%
   \stepcounter{linenumber}%
   \dp\@tempboxa=\@tempdima\ht\@tempboxa=\z@
   \nointerlineskip\kern-\@tempdima\box\@tempboxa
   \ifnum\outputpenalty=-\linenopenaltypar\else
       \@tempcnta\outputpenalty
       \advance\@tempcnta -\linenopenalty
       \penalty\@tempcnta
   \fi
   }
\end{verbatim}
\end{code}



\section{
Control line numbering
}
The line numbering is controlled via ~\par~.  \LaTeX\
saved the \TeX-primitive ~\par~ in ~\@@par~.  We push it
one level further out, and redefine ~\@@par~ to insert
the ~\interlinepenalty~ needed to trigger the
line numbering. And we need to allow pagebreaks after a
paragraph. 
\begin{code}\begin{verbatim}
\let\@@@par\@@par
 
\def\linenumberpar{\ifvmode\@@@par\else\ifinner\@@@par\else
     \advance\interlinepenalty \linenopenalty
        \@@@par
        \penalty-\linenopenaltypar
        \kern\z@
     \advance\interlinepenalty -\linenopenalty
     \fi\fi
     }
\end{verbatim}
\end{code}
The basic commands to enable and disable line numbers.
~\@par~ and ~\par~ are only touched, when they are ~\let~ 
to ~\@@@par~/~\linenumberpar~.  The line number may be
reset to 1 with the star-form, or set by an optional
argument ~[~<number>~]~. 
\begin{code}\begin{verbatim}
\def\linenumbers{\let\@@par\linenumberpar
     \ifx\@par\@@@par\let\@par\linenumberpar\fi
     \ifx\par\@@@par\let\par\linenumberpar\fi
     \@ifnextchar[{\resetlinenumber}%]
                 {\@ifstar{\resetlinenumber}{}}%
     }
 
\def\nolinenumbers{\let\@@par\@@@par
  \ifx\@par\linenumberpar\let\@par\@@@par\fi
  \ifx\par\linenumberpar\let\par\@@@par\fi
  }
\end{verbatim}
\end{code}
What happens with a display?  Since ~\par~ is not executed,
when breaking the lines before a display, they will not get
line numbers.  Sorry, but I do not dare to change
~\interlinepenalty~ globally, nor do I want to redefine
the display math environments.  Do that, if you really need it.
$$
display \ math
$$
The next two commands are provided to turn on line
numbering in a specific mode. Please note the difference:
for pagewise numbering, ~\linenumbers~ comes first to
inhibit it from seeing optional arguments, since
re-/presetting the counter is useless. 
\begin{code}\begin{verbatim}
\def\pagewiselinenumbers{\linenumbers\setpagewiselinenumbers}
\def\runninglinenumbers{\setrunninglinenumbers\linenumbers}
\end{verbatim}
\end{code}
Finally, it is a \LaTeX\ style, so we provide for the use
of environments, including the suppression of the
following paragraphs indentation.
\begin{code}\begin{verbatim}
\@namedef{linenumbers*}{\par\linenumbers*}
\@namedef{runninglinenumbers*}{\par\runninglinenumbers*}
 
\def\endlinenumbers{\par\@endpetrue}
\let\endrunninglinenumbers\endlinenumbers
\let\endpagewiselinenumbers\endlinenumbers
\expandafter\let\csname endlinenumbers*\endcsname\endlinenumbers
\expandafter\let\csname endrunninglinenumbers*\endcsname\endlinenumbers
\end{verbatim}
\end{code}



\section{
Line number references
}
The only way to get a label to a line number in a
paragraph is to ask the output routine to mark it.

We use the marginpar mechanism to hook to ~\output~ for a
second time.  Marginpars are floats with number $-1$, we
fake marginpars with No $-2$. Originally, every negative
numbered float was considered to be a marginpar.

The float box number ~\@currbox~ is used to transfer the
label name in a macro called ~\@LNL@~<box-number>.

A ~\newlabel~ is written to the aux-file.  The reference
is to ~\theLineNumber~, \emph{not} ~\thelinenumber~.
This allows to hook in, as done below for pagewise line
numbering. 

\begin{code}\begin{verbatim}
\let\@LN@addmarginpar\@addmarginpar
\def\@addmarginpar{%
   \ifnum\count\@currbox>-2\relax
     \@LN@addmarginpar
   \else
     \@cons\@freelist\@currbox
     \protected@write\@auxout{}{%
         \string\newlabel
            {\csname @LNL@\the\@currbox\endcsname}%
            {{\theLineNumber}{\thepage}}}%
   \fi}
\end{verbatim}
\end{code}
\subsection{
The linelabel command
}
To refer to a place in line ~\ref{~<foo>~}~ at page
~\pageref{~<foo>~}~ you place a ~\linelabel{~<foo>~}~ at
that place.

\linelabel{demo}%
\marginpar{\tiny\raggedright
It works: This paragraph starts on page
\pageref{demo}, line \ref{demo}. 
}
If you use this command outside a ~\linenumbers~
paragraph, you will get references to some junky
line numbers, sorry.  But we don't disable the command,
because only the ~\par~ at the end of a paragraph  may
decides whether to print line numbers on this paragraph
or not.  A ~\linelabel~ may appear earlier than
~\linenumbers~.

~\linelabel~, via a fake float number $-2$, puts a
~\penalty~ into a ~\vadjust~, which triggers the
pagebuilder after putting the current line to the main
vertical list.  A ~\write~ is placed on the main vertical
list, which prints a reference to the current value of
~\thelinenumber~ and ~\thepage~ at the time of the
~\shipout~.

A ~\linelabel~ is allowed only in outer horizontal or
vertical modes.

The argument of ~\linelabel~ is put into a macro with a
name derived from the number of the allocated float box.
Much of the rest is dummy float setup.
\begin{code}\begin{verbatim}
\def\linelabel#1{%
   \ifvmode
       \ifinner \else \leavevmode \fi
     \fi
   \ifhmode
     \ifinner
       \@parmoderr
     \else
       \@bsphack\@floatpenalty -\@Mii
       \@next\@currbox\@freelist
           {\global\count\@currbox-2%
            \expandafter\gdef\csname @LNL@\the\@currbox\endcsname{#1}}%
           {\@floatpenalty\z@ \@fltovf \def\@currbox{\@tempboxa}}%
       \begingroup
           \setbox\@currbox \color@vbox \vbox \bgroup \end@float
       \endgroup
       \global \@ignorefalse \@esphack
     \fi
   \else
     \@parmoderr
   \fi
   }
\end{verbatim}
\end{code}
\modulolinenumbers[3]
\section{
The appearance of the line numbers
}\label{appearance}
The line numbers are set as ~\tiny\sffamily\arabic{linenumber}~,
$10pt$ left of the text.  You may add options to place it
right of the text, or . . .

. . . here are the hooks:
\begin{code}\begin{verbatim}
\def\makeLineNumberLeft{\hss\linenumberfont\LineNumber\hskip\linenumbersep}
 
\def\makeLineNumberRight{\linenumberfont\hskip\linenumbersep\hskip\textwidth
                         \hbox to\linenumberwidth{\hss\LineNumber}\hss}
 
\def\linenumberfont{\normalfont\tiny\sffamily}
 
\newdimen\linenumbersep
\newdimen\linenumberwidth
 
\linenumberwidth=10pt
\linenumbersep=10pt
\end{verbatim}
\end{code}
Oh well, some of the suggested `customization' has been
done,  particularly the placement of the numbers.  Margin
switching requires ~pagewise~ numbering mode, but choosing
the left or right margin for the numbers always works.
\begin{code}\begin{verbatim}
\def\switchlinenumbers{\@ifstar
    {\let\makeLineNumberOdd\makeLineNumberRight
     \let\makeLineNumberEven\makeLineNumberLeft}%
    {\let\makeLineNumberOdd\makeLineNumberLeft
     \let\makeLineNumberEven\makeLineNumberRight}%
    }
 
\def\setmakelinenumbers#1{\@ifstar
  {\let\makeLineNumberRunning#1%
   \let\makeLineNumberOdd#1%
   \let\makeLineNumberEven#1}%
  {\ifx\c@linenumber\c@runninglinenumber
      \let\makeLineNumberRunning#1%
   \else
      \let\makeLineNumberOdd#1%
      \let\makeLineNumberEven#1%
   \fi}%
  }
 
\def\leftlinenumbers{\setmakelinenumbers\makeLineNumberLeft}
\def\rightlinenumbers{\setmakelinenumbers\makeLineNumberRight}
 
\leftlinenumbers*
\end{verbatim}
\end{code}
~\LineNumber~ is a hook which is used for the modulo stuff.
It is the command to use for the line number, when you
customizes ~\makeLineNumber~.  Use ~\thelinenumber~ to
change the outfit of the digits.


We will implement two modes of operation:
\begin{itemize}
\item  numbers ~running~ through (parts of) the text
\item  ~pagewise~ numbers starting over with one on top of
each page.
\end{itemize}
Both modes have their own count register, but only one is
allocated as a \LaTeX\ counter, with the attached
facilities serving both.
\begin{code}\begin{verbatim}
\newcounter{linenumber}
\newcount\c@pagewiselinenumber
\let\c@runninglinenumber\c@linenumber
\end{verbatim}
\end{code}
Only the running mode counter may be reset, or preset,
for individual paragraphs.  The pagewise counter must
give a unique anonymous number for each line.
\begin{code}\begin{verbatim}
\newcommand\resetlinenumber[1][1]{\c@runninglinenumber#1}
\end{verbatim}
\end{code}
\subsection{
Running line numbers
}
Running mode is easy,  ~\LineNumber~ and ~\theLineNumber~
produce ~\thelinenumber~, which defaults to
~\arabic{linenumber}~, using the ~\c@runninglinenumber~
counter.  This is the default mode of operation.
\begin{code}\begin{verbatim}
\def\makeRunningLineNumber{\makeLineNumberRunning}
 
\def\setrunninglinenumbers{%
   \def\theLineNumber{\thelinenumber}%
   \let\c@linenumber\c@runninglinenumber
   \let\makeLineNumber\makeRunningLineNumber
   }
 
\setrunninglinenumbers\resetlinenumber
\end{verbatim}
\end{code}


\subsection{
Pagewise line numbers
}
Difficult, if you think about it.  The number has to be
printed when there is no means to know on which page it
will end up,  except through the aux-file.  My solution  
is really expensive, but quite robust.  

With version ~v2.00~ the hashsize requirements are
reduced, because we do not need one controlsequence for
each line any more.  But this costs some computation time
to find out on which page we are.

~\makeLineNumber~ gets a hook to log the line and page
number to the aux-file.  Another hook tries to find out
what the page offset is, and subtracts it from the counter
~\c@linenumber~.  Additionally, the switch
~\ifoddNumberedPage~ is set true for odd numbered pages,
false otherwise.
\begin{code}\begin{verbatim}
\def\setpagewiselinenumbers{%
   \let\theLineNumber\thePagewiseLineNumber
   \let\c@linenumber\c@pagewiselinenumber
   \let\makeLineNumber\makePagewiseLineNumber
   }
 
\def\makePagewiseLineNumber{\logtheLineNumber\getLineNumber
  \ifoddNumberedPage
     \makeLineNumberOdd
  \else
     \makeLineNumberEven
  \fi
  }
\end{verbatim}
\end{code}
Each numbered line gives a line to the aux file
\begin{verse}
~\@LN{~<line>~}{~<page>~}~
\end{verse}
very similar to the ~\newlabel~ business, except that we need
an arabic representation of the page number, not what
there might else be in ~\thepage~.
\begin{code}\begin{verbatim}
\def\logtheLineNumber{\protected@write\@auxout{}{%
   \string\@LN{\the\c@linenumber}{\noexpand\the\c@page}}}
\end{verbatim}
\end{code}
From the aux-file we get one macro ~\LN@P~<page> for each
page with line numbers on it.  This macro calls four other
macros with one argument each.  These macros are
dynamically defined to do tests and actions, to find out
on which page the current line number is located.

We need sort of a pointer to the first page with line
numbers, initiallized to point to nothing:
\begin{code}\begin{verbatim}
\def\LastNumberedPage{first} 
\def\LN@Pfirst{\nextLN\relax}
\end{verbatim}
\end{code}
The four dynamic macros are initiallized to reproduce
themselves in an ~\xdef~
\begin{code}\begin{verbatim}
\let\lastLN\relax  % compare to last line on this page
\let\firstLN\relax % compare to first line on this page
\let\pageLN\relax  % get the page number, compute the linenumber
\let\nextLN\relax  % move to the next page
\end{verbatim}
\end{code}
During the end-document run through the aux-files, we
disable ~\@LN~.  I may put in a check here later, to give
a rerun recommendation.  
\begin{code}\begin{verbatim}
\AtEndDocument{\let\@LN\@gobbletwo}
\end{verbatim}
\end{code}
Now, this is the tricky part.  First of all, the whole
definition of ~\@LN~ is grouped, to avoid accumulation
on the save stack. Somehow ~\csname~<cs>~\endcsname~ pushes
an entry, which stays after an ~\xdef~ to that <cs>.

If ~\LN@P~<page> is undefined, initialize it with the
current page and line number, with the
\emph{pointer-to-the-next-page} pointing to nothing.  And
the macro for the previous page will be redefined to point
to the current one. 

If the macro for the current page already exists, just
redefine the \emph{last-line-number} entry.

Finally, save the current page number, to get the pointer to the
following page later.
\begin{code}\begin{verbatim}
\def\@LN#1#2{{\expandafter\@@LN\csname LN@P#2\endcsname{#1}{#2}}}
\def\@@LN#1#2#3{\ifx#1\relax
    \expandafter\@@@LN\csname LN@P\LastNumberedPage\endcsname#1
    \xdef#1{\lastLN{#2}\firstLN{#2}\pageLN{#3}\nextLN\relax}%
  \else
    \def\lastLN##1{\noexpand\lastLN{#2}}%
    \xdef#1{#1}%
  \fi
  \gdef\LastNumberedPage{#3}}
\end{verbatim}
\end{code}
The previous page macro gets its pointer to the
current one, replacing the ~\relax~ with the cs-token
~\LN@P~<page>.  In case of page number mismatch, \TeX\
will trip here, because the argument string for ~\nextLN~
is not ~\relax~.  I think it's difficult to do a
reasonable intercept here, because this is running in an
~\xdef~.  Does ~\PackageError{}~ work in there?
\begin{code}\begin{verbatim}
\def\@@@LN#1#2{{\def\nextLN\relax{\noexpand\nextLN\noexpand#2}%
                \xdef#1{#1}}}
\end{verbatim}
\end{code}
Now, to print a line number, we need to find the page,
where it resides.  This will most probably be the one where
the last one came from or maybe the next.  However, it can
be a completely different one.  We maintain a cache,
which is let to the last accessed pages macro.  But for now
it is initialized to expand ~\LN@first~, where the poiner
to the first numbered page has been stored in. 
\begin{code}\begin{verbatim}
\def\NumberedPageCache{\LN@Pfirst}
\end{verbatim}
\end{code}
To find out on which page the current ~\c@linenumber~ is, 
we define the four dynamic macros to do something usefull
and execute the current cache macro.  ~\lastLN~ is run
first, testing if the line number in question may be on a
later page.  If so, disable ~\firstLN~, and go on to the
next page via ~\nextLN~.
\begin{code}\begin{verbatim}
\def\testLastNumberedPage#1{\ifnum#1<\c@linenumber
      \let\firstLN\@gobble
  \fi}
\end{verbatim}
\end{code}
Else, if ~\firstLN~ finds out that we need an earlier
page,  we start over from the beginning. Else, ~\nextLN~
will be disabled, and ~\pageLN~ will run
~\gotNumberedPage~ with two arguments: the first line
number on this page, and the page number.
\begin{code}\begin{verbatim}
\def\testFirstNumberedPage#1{\ifnum#1>\c@linenumber
     \def\nextLN##1{\testNextNumberedPage\LN@Pfirst}%
  \else
      \let\nextLN\@gobble
      \def\pageLN{\gotNumberedPage{#1}}%
  \fi}
\end{verbatim}
\end{code}
We start with ~\pageLN~ disabled and ~\nextLN~ defined to
continue the search with the next page.
\begin{code}\begin{verbatim}
\def\testNumberedPage{%
  \let\lastLN\testLastNumberedPage
  \let\firstLN\testFirstNumberedPage
  \let\pageLN\@gobble
  \let\nextLN\testNextNumberedPage
  \NumberedPageCache
  }
\end{verbatim}
\end{code}
When we switch to another page, we first have to make
sure that it is there.  If we are done with the last 
page, we probably need to run \TeX\ again, but for the
rest of this run, the cache macro will just return two
zeros. This saves a lot of time, for example if you have
half of an aux-file from an aborted run,  in the next run
the whole page-list would be searched in vain again and
again for the second half of the document.

If there is another page, we iterate the search. 
\begin{code}\begin{verbatim}
\def\testNextNumberedPage#1{\ifx#1\relax
     \global\def\NumberedPageCache{\gotNumberedPage00}%
     \PackageWarningNoLine{lineno}%
                    {Linenumber reference failed,
      \MessageBreak  rerun to get it right}%
   \else
     \global\let\NumberedPageCache#1%
   \fi
   \testNumberedPage
   }
\end{verbatim}
\end{code}
\linelabel{demo2}
\marginpar{\tiny\raggedright
Let's see if it finds the label
on page \pageref{demo}, 
line \ref{demo}, and back here
on page \pageref{demo2}, line
\ref{demo2}. 
}
To separate the official hooks from the internals there is
this equivalence, to hook in later for whatever purpose:
\begin{code}\begin{verbatim}
\let\getLineNumber\testNumberedPage
\end{verbatim}
\end{code}
So, now we got the page where the number is on.  We
establish if we are on an odd or even page, and calculate
the final line number to be printed.
\begin{code}\begin{verbatim}
\newif\ifoddNumberedPage
 
\def\gotNumberedPage#1#2{\oddNumberedPagefalse
  \ifodd#2\relax\oddNumberedPagetrue\fi
  \advance\c@linenumber 1\relax
  \subtractlinenumberoffset{#1}%
  }
\end{verbatim}
\end{code}
You might want to run the pagewise mode with running line
numbers, or you might not.  It's your choice:
\begin{code}\begin{verbatim}
\def\runningpagewiselinenumbers{%
  \let\subtractlinenumberoffset\@gobble
  }
 
\def\realpagewiselinenumbers{%
  \def\subtractlinenumberoffset##1{\advance\c@linenumber-##1\relax}%
  }
 
\realpagewiselinenumbers
\end{verbatim}
\end{code}
For line number references, we need a protected call to
the whole procedure, with the requested line number stored
in the ~\c@linenumber~ counter.  This is what gets printed
to the aux-file to make a label:
\begin{code}\begin{verbatim}
\def\thePagewiseLineNumber{\protect
       \getpagewiselinenumber{\the\c@linenumber}}%
\end{verbatim}
\end{code}
And here is what happens when the label is refered to:
\begin{code}\begin{verbatim}
\def\getpagewiselinenumber#1{{%
  \c@linenumber #1\relax\testNumberedPage
  \thelinenumber
  }}
\end{verbatim}
\end{code}
%
A summary of all per line expenses:
\begin{description}\item
[CPU:]  The ~\output~ routine is called for each line,
and the page-search is done.
\item
[DISK:] One line of output to the aux-file for each
numbered line
\item
[MEM:]  One macro per page. Great improvement over v1.02,
which had one control sequence per line in
addition.  It blew the hash table after some five
thousand lines. 
\end{description}



\subsection{
Numbering modulo 5
}
Most users want to have only one in five lines numbered.
~\LineNumber~ is supposed to produce the outfit of the
line number attached to the line,  while ~\thelinenumber~
is used also for references, which should appear even if
they are not multiples of five.   
\begin{code}\begin{verbatim}
\newcount\c@linenumbermodulo
 
\def\themodulolinenumber{{\@tempcnta\c@linenumber
  \divide\@tempcnta\c@linenumbermodulo
  \multiply\@tempcnta\c@linenumbermodulo
  \ifnum\@tempcnta=\c@linenumber\thelinenumber\fi
  }}
\end{verbatim}
\end{code}
The user command to set the modulo counter:
\begin{code}\begin{verbatim}
\newcommand\modulolinenumbers[1][0]{%
 \let\LineNumber\themodulolinenumber
 \ifnum#1>1\relax
   \c@linenumbermodulo#1\relax
 \else\ifnum#1=1\relax
   \def\LineNumber{\thelinenumber}%
 \fi\fi
 }
 
\setcounter{linenumbermodulo}{5}
\modulolinenumbers[1]
\end{verbatim}
\end{code}

\switchlinenumbers
\modulolinenumbers[1]
\section{
Package options
}
There is a bunch of package options, all of them
executing only user commands (see below).

Options ~left~ (~right~) put the line numbers on the left
(right) margin.  This works in all modes.  ~left~ is the
default.
\begin{code}\begin{verbatim}
\DeclareOption{left}{\leftlinenumbers*}
 
\DeclareOption{right}{\rightlinenumbers*}
\end{verbatim}
\end{code}
Option ~switch~ (~switch*~) puts the line numbers on the
outer (inner) margin of the text.   This requires running
the pagewise mode,  but we turn off the page offset
subtraction, getting sort of running numbers again.  The
~pagewise~ option may restore true pagewise mode later.
\begin{code}\begin{verbatim}
\DeclareOption{switch}{\setpagewiselinenumbers
                       \switchlinenumbers
                       \runningpagewiselinenumbers}
 
\DeclareOption{switch*}{\setpagewiselinenumbers
                        \switchlinenumbers*%
                        \runningpagewiselinenumbers}
\end{verbatim}
\end{code}
The options ~pagewise~ and ~running~ select the major
linenumber mechanism.  ~running~ line numbers refer to a real
counter value, which can be reset for any paragraph,
even getting  multiple paragraphs on one page starting
with line number one.  ~pagewise~ line numbers get a
unique hidden number within the document,  but with the
opportunity to establish the page on which they finally
come to rest.  This allows the subtraction of the page
offset, getting the numbers starting with 1 on top of each
page, and margin switching in twoside formats becomes
possible.  The default mode is ~running~.  

The order of declaration of the options is important here
~pagewise~ must come after ~switch~, to overide running
pagewise mode. ~running~ comes last, to reset the running
line number mode, e.g, after selecting margin switch mode
for ~pagewise~ running.  Once more, if you specify all
three of the options ~[switch,pagewise,running]~, the
result is almost nothing, but if you later say
~\pagewiselinenumbers~,  you get margin switching, with
real pagewise line numbers.

\begin{code}\begin{verbatim}
\DeclareOption{pagewise}{\setpagewiselinenumbers
                         \realpagewiselinenumbers}
 
\DeclareOption{running}{\setrunninglinenumbers}
\end{verbatim}
\end{code}
And finally, the option ~modulo~ causes only those
linenumbers to be printed which are multiples of five.
\begin{code}\begin{verbatim}
\DeclareOption{modulo}{\modulolinenumbers\relax}
 
\ProcessOptions
\end{verbatim}
\end{code}
\section{
The final touch
}
There is one deadcycle for each line number.
\begin{code}\begin{verbatim}
\advance\maxdeadcycles 100
 
\endinput
\end{verbatim}
\end{code}
\section{
The user commands
}
The user command to turn on and off line numbering 
are 
\begin{description}\item
[|\linenumbers]                                                       \ \par
Turn on line numbering in the current mode.
\item
[|\linenumbers*]                                              \ \par$\qquad$
and reset the line number to 1.
\def\NL{<number>]}\item
[|\linenumbers[\NL]                                           \ \par$\qquad$
and start with <number>.  
\item
[|\nolinenumbers]                                                     \ \par
Turn off line numbering.
\item
[|\runninglinenumbers*[\NL]                                           \ \par
Turn on ~running~ line numbers, with the same optional
arguments as ~\linenumbers~.  The numbers are running
through the text over pagebreaks.  When you turn
numbering off and on again, the numbers will continue,
except, of cause, if you ask to reset or preset the
counter.
\item
[|\pagewiselinenumbers]                                               \ \par
Turn on ~pagewise~ line numbers.  The lines on each
page are numbered beginning with one at the first
~pagewise~ numbered line.
\item
[|\resetlinenumber[\NL]                                               \ \par
Reset ~[~Set~]~ the line number to 1
~[~<number>~]~.
\item
[|\setrunninglinenumbers]                                             \ \par
Switch to ~running~ line number mode. Do \emph{not}
turn it on or off.
\item
[|\setpagewiselinenumbers]                                            \ \par
Switch to ~pagewise~ line number mode. Do \emph{not}
turn it on or off.
\item
[|\switchlinenumbers*]                                                \ \par
Causes margin switching in pagewise modes. With the
star,  put the line numbers on the inner margim.
\item
[|\leftlinenumbers*]                                                  \ \par
\item
[|\rightlinenumbers*]                                                 \ \par
Set the line numbers in the left/right margin. With the
star this works for both modes of operation, without
the star only for the currently selected mode. 
\item
[|\runningpagewiselinenumbers]                                        \ \par
When using the pagewise line number mode,  do not
subtract the page offset.  This results in running
line numbers again,  but with the possibility to switch
margins.  Be careful when doing line number
referencing,  this mode status must be the same while
setting the paragraph and during references.
\item
[|\realpagewiselinenumbers]                                           \ \par
Reverses the effect of ~\runningpagewiselinenumbers~.
\item
[|\modulolinenumbers[\NL]                                             \ \par
Give a number only to lines which are multiples of
~[~<number>~]~.  If <number> is not specified, the
current value in the counter ~linenumbermodulo~ is
retained.  <number>=1 turns this off without changing
~linenumbermodulo~.  The counter is initialized to 5.
\item
[|\linelabel]                                                         \ \par
Set a ~\linelabel{~<foo>~}~ to the line number where
this commands is in.  Refer to it with the \LaTeX\
referencing commands ~\ref{~<foo>~}~ and
~\pageref{~<foo>~}~.
\end{description}
The commands can be used globally, locally within groups
or as environments.  It is important to know that they 
take action only when the ~\par~ is executed.  The
~\end{~<mode>~linenumbers}~ commands provide a ~\par~.
Examples:
\begin{verse}
~{\linenumbers~  <text> ~\par}~                                         \\
\ \\
~\begin{linenumbers}~                                                   \\
<text>                                                              \\
~\end{linenumbers}~                                                     \\
\ \\
<paragraph> ~{\linenumbers\par}~                                        \\
\ \\
~\linenumbers~                                                          \\
<text> ~\par~                                                         \\
~\nolinenumbers~                                                        \\
\ \\
~\linenumbers~                                                          \\
<paragraph> ~{\nolinenumbers\par}~                                      \\
\end{verse}


\subsection{
Customization hooks
}
There are several hooks to customize the appearance of the
line numbers, and some low level hooks for special
effects. 
\begin{description}\item
[|\thelinenumber]                                                     \ \par
This macro should give the representation of the line
number in the \LaTeX-counter ~linenumber~.  The
default is provided by \LaTeX:                              \par$\qquad$
~\arabic{linenumber}~
\item
[|\makeLineNumberLeft]                                                \ \par
This macro is used to attach a line number to the left
of the text page.  This macro should fill an ~\hbox to 0pt~ 
which will be placed at the left margin of the
page, with the reference point aligned to the line to
which it should give a number.  Please use the macro
~\LineNumber~ to refer to the line number. 

The default definition is                                   \par$\qquad$
~\hss\linenumberfont\LineNumber\hskip\linenumbersep~
\item
[|\makeLineNumberRight]                                               \ \par
Like ~\makeLineNumberLeft~, but for line numbers on
the right margin.

The default definition is                                   \par$\qquad$
~\linenumberfont\hskip\linenumbersep\hskip\textwidth~    \par$\qquad$
~\hbox to\linenumberwidth{\hss\LineNumber}\hss~
\item
[|\linenumberfont]                                                    \ \par
This macro is initialized to                                \par$\qquad$
~\normalfont\tiny\sffamily~
\item
[|\linenumbersep]                                                     \ \par
This dimension register sets the separation of the
linenumber to the text. Default value is ~10pt~.
\item
[|\linenumberwidth]                                                   \ \par
This dimension register sets the width of the line
number box on the right margin.  The distance of the
right edge of the text to the right edge of the line
number is ~\linenumbersep~ + ~\linenumberwidth~. The
default value is ~10pt~.  
\item
[|\theLineNumber] (for wizards)                                       \ \par
This macro is called for printing a ~\newlabel~ entry
to the aux-file.  Its definition depends on the mode.
For running line numbers it's just ~\thelinenumber~,
while in pagewise mode, the page offset subtraction
is done in here.
\item
[|\makeLineNumber] (for wizards)                                      \ \par
This macro produces the line numbers.  The definition
depends on the mode.  In the running line numbers
mode it just expands ~\makeLineNumberLeft~.
\item
[|\LineNumber] (for wizards)                                          \ \par
This macro is called by ~\makeLineNumber~ to typeset
the line number.  This hook is changed by the modulo
mechanism.
\end{description}
\end{document}%D
