About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-12-13: Ebook sale (12th December 2024 – 1st January 2025): cybercrime fiction short stories Unsocial Media and Smile for the Camera free; crime fiction short story The Briefcase 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:

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

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:

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

\@afterheading

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

\@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.

You can download or view a solution.

For the More Adventurous:

Modify the definition of \numberedparagraph so that it uses

\marginpar[left]{text}

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