Latest news 2024-07-03: Ebook sale (1st – 31st July 2024): short story cybercrime fiction Unsocial Media free; short story cybercrime fiction Smile for the Camera free; short story noir crime fiction I’ve Heard the Mermaid Sing free; crime fiction novel The Private Enemy US$1.99; illustrated children’s story The Foolish Hedgehog US$0.99.

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:


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


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):



Three paragraphs each starting with a number.

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:


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:


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:

texdef -t latex @afterheading

I get (reformatted for clarity):

       {\setbox \z@ \lastbox }%
So \@afterheading can be redefined to use our new \numberedparagraph command:

      \everypar{\numberedparagraph}% <- modification
    \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:


\usepackage{lipsum}% dummy text

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


\section{Sample Section}


Some\label{sample} sample text.

\subsection{Sample Subsection}


\section{Another Section}



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).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs