5

I am trying to create a LaTeX document that will automatically number paragraphs without the need to use \paragraph - the reason for this is that I am processing text that will be written by others who do not use LaTeX.

Edit: this is why the solution at paragraph numbering does not address my particular problem as it requires that \paragraph is replaced by \item. This gives me the same problem as far as I can tell.

I have searched for several solutions which have helped me make progress and also warned that the use of \everypar can be problematic... The current situation is that I almost have a solution but a new \section or \subsection causes the subsequent paragraph to drop the numbering as shown in the attached image. Further paragraphs are numbered correctly.

Alternative numbering solutions would be acceptable to me - I have no real preference for whether the paragraph numbers flow consecutively through all sections or follow a within-section pattern.

My minimal working example is as follows (note I have a need to use the \titlesec package to make the document look pretty). I am really scratching my head about this one - any help or pointers will be gratefully received!

\documentclass[11pt]{article}
\usepackage{lipsum}
\usepackage{titlesec}    
\setcounter{secnumdepth}{3}

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

\let\oldep\everypar\newtoks\everypar  
\oldep{\the\everypar\everypar{\numberedparagraph}}

\begin{document}

\everypar{\numberedparagraph}

\section{Section 1}

\lipsum[1-2]

\section{Section 2}

\lipsum[4-5]

\subsection{A Subsection}

\lipsum[6-7]

\end{document}

MWE

5
  • See question tex.stackexchange.com/questions/305220/paragraph-numbering?rq=1
    – bmv
    Commented Dec 21, 2017 at 12:48
  • Thanks for having a look at this. I had already looked at that question and I think my situation is different. The solution linked to just seems to replace the need to use \paragraph with a need to use \item so it gives me the same problem really...?
    – quabbage
    Commented Dec 21, 2017 at 12:55
  • 1
    Perhaps Non-Hierarchical Paragraph Numbering may help. It uses \everypar but addresses \section etc. Commented Dec 21, 2017 at 13:09
  • 1
    I don't think it's a dup since OP wants an automated method (that is, use \everypar without interruption by sectioning commands). Commented Dec 21, 2017 at 13:12
  • @NicolaTalbot that link looks promising - thank you. It seems that I have to redefine \@afterheading. This sounds a bit scary but I will try to figure it out :)
    – quabbage
    Commented Dec 21, 2017 at 13:21

1 Answer 1

5

Thank you to @NicolaTalbot for pointing me in the right direction.

The solution was to redefine \@afterheading. This then wasn't quite right as the indentation of paragraph numbers was a bit awry so I solved that by setting the paragraph indentation to 0.

Corrected code and image follow:

\documentclass[11pt]{article}
\usepackage{lipsum}
\usepackage{titlesec}
\setcounter{secnumdepth}{3}

\setlength{\parindent}{0em} % indent subsequent paragraphs

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

\let\oldep\everypar\newtoks\everypar  
\oldep{\the\everypar\everypar{\numberedparagraph}}


\makeatletter %
\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
  }%
}


\begin{document}



\everypar{\numberedparagraph}

\section{Section 1}

\lipsum[1-2]

\section{Section 2}

\lipsum[4-5]

\subsection{A Subsection}

\lipsum[6-7]

\end{document}

enter image description here

2
  • 2
    You have extra spaced because of line endings without percent: → tex.stackexchange.com/questions/7453/… Commented Dec 21, 2017 at 19:14
  • @Schweinebacke thanks for the tip. This isn't something I currently understand but I will read the link, cheers.
    – quabbage
    Commented Dec 22, 2017 at 11:14

Not the answer you're looking for? Browse other questions tagged .