2

I'm not sure whether I'm allowed to extend my previous question so I'm asking here.

I wanted a way to number paragraphs automatically and Ulrike Fischer provided an excellent solution. I want to extend this solution to require less manual intervention. The provided method explicitly requires temporarily switching the paragraph numbering off. I am sure it is possible to amend the \section and \subsection and \subsubsection commands but I have been unable to make it compile let alone work. Ideally I'd like to learn how to do this for these commands and probably others where I won't paragraph numbering such as captions and footnotes.

MWE 1: Manual intervention (works)

\documentclass[11pt]{article}

\usepackage{etoolbox}
\usepackage{lipsum}

\newbool{myparbool}
\booltrue{myparbool}
\newcounter{mypar}
\AddToHook{para/begin}
{\ifbool{myparbool}{\stepcounter{mypar}\llap{\P\themypar\quad}}{}}

\begin{document}

\boolfalse{myparbool}
\section{First}
\booltrue{myparbool}

All paragraphs should be numbered in the left margin but sections and subsections should not have paragraph numbers. This works but needs explicit manual control.

\lipsum[1]

\boolfalse{myparbool}

\subsection{Second}
\booltrue{myparbool}
\lipsum[2]
\boolfalse{myparbool}

\subsubsection{Third}
\booltrue{myparbool}
\lipsum[3]
\boolfalse{myparbool}

\section{Fourth}
\booltrue{myparbool}
\lipsum[4]
\end{document}

working but with manual intervention

MWE 2: No manual intervention (needs fixing)

\documentclass[11pt]{article}

\usepackage{etoolbox}
\usepackage{lipsum}

\newbool{myparbool}
\booltrue{myparbool}
\newcounter{mypar}
\AddToHook{para/begin}
{\ifbool{myparbool}{\stepcounter{mypar}\llap{\P\themypar\quad}}{}}

% New stuff that doesn't work: want to patch the \section, \subsection and \subsubsection commands

\usepackage{xpatch}
\newbool{parboolstatus}

\xpretocmd{\section}{%
  \bgroup%
  \ifbool{myparbool}%then
    {\setbool{parboolstatus}{true}}%else
    {\setbool{parboolstatus}{false}}
  \setbool{myparbool}{false}%
}{}{}
\xapptocmd{\section}{%
  \ifbool{parboolstatus}%then
    {\setbool{myparbool}{true}}%else
    {\setbool{myparbool}{false}}%
}{}{}
\apptocmd{\@xsect}{\egroup}{}{}

\begin{document}
\section{First}
All paragraphs should be numbered in the left margin but sections should not have paragraph numbers.

\lipsum[1]
\subsection{Second}
\lipsum[2]
\subsubsection{Third}
\lipsum[3]
\section{Fourth}
\lipsum[4]
\end{document}

Results in a compilation error.

I have tried \addto and \addtocmd but these also resulted in compilation errors. I had different errors initially because I wasn't beginning and ending a group.

I have seen this question but I can't adapt the solution. I found this but I can't adapt that either.

I haven't been able to find a beginners guide to this. I expect there isn't one because it's not really a beginner's topic. I can't find a list of the hooks either. @Ulrike's original answer uses para/begin but I don't know if there's a section/begin or equivalent.

4
  • 1
    Well yes, read TeXbook and the other books if you want to learn. List of hooks can be found in source2e.pdf and the places mentioned in it. Since this is a command try reading ltcmdhooks-doc, although if there isn't any patching is an option
    – user202729
    Commented May 7, 2022 at 0:12
  • 1
    You still need some intricate knowledge of TeX/the specific command definitions to do the patching though, since not all of the work is done in that single command (e.g. usually if the command parses star/optional argument etc. it will define auxiliary arguments to do additional work etc.)
    – user202729
    Commented May 7, 2022 at 0:18
  • Thank you @user202729 The documents you list weren't on my system. I have now downloaded them. I will use them as a reference. Programming LaTeX and TeX seems hard to learn. It's hard to know where to start and where to break into the circle.
    – Doc Octal
    Commented May 8, 2022 at 19:17
  • 1

2 Answers 2

1

You were close. Try this code. Will work with numbered and unnumbered sectional commands.

a

\documentclass[11pt]{article}

\usepackage{etoolbox}
\usepackage{lipsum}

\newbool{myparbool}
\booltrue{myparbool}
\newcounter{mypar}
\AddToHook{para/begin}
{\ifbool{myparbool}{\stepcounter{mypar}\llap{\P\themypar\quad}}{}}

%**************************** added <<<<<<<<<<
\makeatletter
\pretocmd{\@ssect}{\boolfalse{myparbool}}{}{}
\apptocmd{\@ssect}{\booltrue{myparbool}}{}{}
\pretocmd{\@sect}{\boolfalse{myparbool}}{}{}
\apptocmd{\@sect}{\booltrue{myparbool}}{}{}
\makeatother

\begin{document}

    \section{First}
    
    All paragraphs should be numbered in the left margin but sections and subsections should not have paragraph numbers. This works but needs explicit manual control.
    
    \lipsum[1]
    

    \subsection{Second}

    \lipsum[2]
    
    \subsubsection{Third}

    \lipsum[3]
    
    \section*{Fourth}

    \lipsum[4]
\end{document}

Un-numbered section

enter image description here

2
  • (this clearly will not allow \section*.)
    – user202729
    Commented May 7, 2022 at 0:15
  • This works beautifully thank you. I'd forgotten about starred section commands.
    – Doc Octal
    Commented May 8, 2022 at 19:21
1

The answer of Simon Dispa works great! But unfortunately it breaks down when using the fancyhdr package.

I combined the work of Simon with some other things I found online (and can't find back again...).

\documentclass[]{article}

%%%% Packages %%%% 
\usepackage{lipsum}   % Package for lorum ipsum text.

\usepackage{fmtcount} % For converting counter to integer

\usepackage{fancyhdr} % For fancy headers/footers

\usepackage{etoolbox} % For more programming capabilities: pretocmd and apptocmd commands

%%%% Settings %%%%

\reversemarginpar     % Put the margin on the left

% Create a new counter, set it to 0
\newcounter{parcount}
\setcounter{parcount}{0}

% Create a new command "parnum".
% When invoked it adds to 'everypar' (i.e. every paragraph) the margin, after incrementing the counter by one.
\newcommand\parnum{
    \everypar{%
        \refstepcounter{parcount}%
        \marginpar[\hspace{1.5cm}\decimal{parcount}]{}%
}}

% Creates a command "noparnum" that de-activates the behaviour of \parnum
\newcommand\noparnum{\everypar{}}

%%% Reset paragraph counter when a new section is started.
\AddToHook{cmd/section/before}{\setcounter{parcount}{0}}


\makeatletter  % Hack to use @-commands in a non-style file.
% The pretocmd prepends the \noparnum to the ¿section? command so that sections don't have a paragraph number
% The apptocmd append the \parnum so that the paragraphs do have a paragraph number.
\pretocmd{\@ssect}{\noparnum\vspace{0.3cm}}{}{}
\apptocmd{\@ssect}{\parnum}{}{}
\pretocmd{\@sect}{\noparnum\vspace{0.3cm}}{}{}
\apptocmd{\@sect}{\parnum}{}{}
\makeatother % Hack to use @-commands in a non-style file.

\begin{document}

% Two settings for fancyhdr
\pagestyle{fancy}
\fancyhead[R]{{\textbf{Fancy header text}}}

    \section{First}
    All paragraphs should be numbered in the left margin but sections and 
    subsections should not have paragraph numbers. 
    
    \lipsum[1]
    
    \subsection{Susbsection}

    \lipsum[2]
    
    \subsubsection{subsubsection}

    \lipsum[3]
    
    \section*{Unnumbered section}

    \lipsum[4]
\end{document}

Example of the automatic paragraph numbering and fancy header.

3
  • Thanks @Stijn and welcome to tex.SE. One of the things I love about SE is that contributors take the time and effort to add extra answers. I always use titleps instead of fancyhdr so I didn't know there was a problem. > things I found online (and can't find back again...) I do that all the time.
    – Doc Octal
    Commented Jul 17, 2022 at 20:40
  • @DocOctal thanks for the tip on titleps, I don't know it. I'll look into it!
    – Stijn
    Commented Jul 19, 2022 at 11:53
  • titleps is part of the titlesec package and there is a guide for fancyhdr users
    – Doc Octal
    Commented Jul 19, 2022 at 20:11

You must log in to answer this question.

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