Latest news 2021-09-06: new blog post "Legacy Documents and TeX Live Docker Images".

## 6.5.2⁂Non-Hierarchical Paragraph Numbering

Suppose now that instead of the hierarchical structure illustrated above, you simply want all your paragraphs numbered sequentially. TeX has a mechanism for specifying code that should be performed at the start of each paragraph:

\everypar{code}

The numbering can be dealt with using a counter. For example, I could define a new counter called, say, para:

\newcounter{para} 

and I could define a command called, say, \numberedparagraph:

\newcommand*{\numberedparagraph}{%
\refstepcounter{para}\thepara.\space
}


This command needs to go at the start of each paragraph, but it's rather tiresome to do this manually, so \everypar can be used instead. For example (using the lipsum package [33] to generate dummy text):

\everypar{\numberedparagraph}
\lipsum[1-3]


produces:

Since the para counter is incremented using \refstepcounter, the paragraphs can be cross-referenced using the standard \label/\ref mechanism. [Master and slave counters] If the paragraph numbering needs to be reset every page, you can specify the page counter as the “master” counter when you define the para counter:

\newcounter{para}[page] 

In general you need to be careful about using page as a master counter, but in this case it's not a problem as this new para counter only gets incremented at the beginning of a paragraph so it doesn't conflict with TeX's output routine.

There is, however, a problem: some commands, such as the section commands, use \everypar to reset the paragraph behaviour. So, for example, a \chapter or \section command will override an earlier use of \everypar. It's also unlikely that you'll want the chapter and section headings to have a paragraph number. This last issue is easily dealt with by hooking into the sectioning commands using one of the etoolbox commands described in §2.1.2 Hook Management:

\preto\chapter{\everypar{}}
\preto\section{\everypar{}}
\preto\subsection{\everypar{}}
\preto\subsubsection{\everypar{}}
\preto\paragraph{\everypar{}}
\preto\subparagraph{\everypar{}}


The first issue, redoing \everypar{\numberedsection} after every sectioning command, is more complicated. Most classes use:

within the definition of the sectioning commands. This command uses \everypar to suppress the indentation of the first paragraph following the heading, and within the argument of \everypar there is another \everypar that resets the paragraph hook back to empty, which ensures that subsequent paragraphs are indented.

As in the previous section, either the \show command or the texdef script can be used to show the original definition of \@afterheading. For example, if I run:

I get (reformatted for clarity):

\@afterheading:
macro:->\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty\@M
\if@afterindent
\else
{\setbox \z@ \lastbox }%
\fi
\else
\clubpenalty\@clubpenalty
\everypar{}%
\fi
}

So \@afterheading can be redefined to use our new \numberedparagraph command:

\renewcommand{\@afterheading}{%
\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty\@M
\if@afterindent
\else
{\setbox\z@\lastbox}%
\fi
\else
\clubpenalty\@clubpenalty
\everypar{\numberedparagraph}% <- modification
\fi
\numberedparagraph% <- modification
}%
}


Remember that this code uses internal commands, so either use \makeatletter and \makeatother or place the code in a package or class.

Exercise 21. Numbered Paragraphs

Modify the following document code so that the paragraphs are automatically numbered:

\documentclass{article}

\usepackage{lipsum}% dummy text

\author{Some One}
\title{Numbered Paragraphs Example}

\begin{document}
\maketitle

\section{Sample Section}

\lipsum[1-4]

Some\label{sample} sample text.
\lipsum[5-10]

\subsection{Sample Subsection}

\lipsum[11-15]

\section{Another Section}

\lipsum[16-30]

\end{document}


Also, add a cross-reference to the paragraph labelled sample.

Modify the definition of \numberedparagraph so that it uses