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.

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