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:
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):
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]
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
{
after every sectioning command, is more complicated. Most classes
use:
\numberedsection
}
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.
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.
You can download or view a solution.
For the More Adventurous:
Modify the definition of \numberedparagraph
so that it
uses
to put the paragraph numbers in the margins.
This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).