About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.

Exercise 21: Numbered Paragraphs (Solution)

This is a solution to Exercise 21.

For the more adventurous part of the exercise, the \numberedparagraph command can be defined as:

\newcommand*{\numberedparagraph}{%
  \refstepcounter{para}\marginpar{\thepara.}}
If you want to reset the paragraph numbering at the start of every page, remember to use \newcounter{para}[page].
\documentclass{article}

\usepackage{lipsum}
\usepackage{etoolbox}

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

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

\makeatletter

\preto\section{\everypar{}}
\preto\subsection{\everypar{}}
\preto\subsubsection{\everypar{}}

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

\makeatother

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

Cross-reference to paragraph~\ref{sample} on page~\pageref{sample}.

\end{document}

Download numberedpar.tex or numberedpar.pdf.

© 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