Vollautomatische Nummerierung von Absätzen

Vollautomatische Nummerierung von Absätzen

Ich bin mir nicht sicher, ob ich meinevorherige Fragedeshalb frage ich hier.

Ich wollte eine Möglichkeit, Absätze automatisch zu nummerieren undUlrike Fischerbot eine hervorragende Lösung. Ich möchte diese Lösung erweitern, damit weniger manuelle Eingriffe erforderlich sind. Die bereitgestellte Methode erfordert ausdrücklich das vorübergehende Ausschalten der Absatznummerierung. Ich bin sicher, dass es möglich ist, die Befehle und \sectionzu ändern , aber ich konnte es nicht kompilieren, geschweige denn funktionieren lassen. Idealerweise würde ich gerne lernen, wie man das für diese Befehle und wahrscheinlich auch für andere macht, bei denen ich keine Absatznummerierung vornehme, wie z. B. Überschriften und Fußnoten.\subsection\subsubsection

MWE 1: Manueller Eingriff (Werke)

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

funktioniert, jedoch mit manuellem Eingriff

MWE 2: Kein manueller Eingriff (muss behoben werden)

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

Führt zu einem Kompilierungsfehler.

Ich habe es versucht \addto, aber \addtocmdauch diese führten zu Kompilierungsfehlern. Ich hatte anfangs andere Fehler, weil ich keine Gruppe begann und beendete.

Ich habe gesehendiese Frageaber ich kann die Lösung nicht anpassen. Ich fandDasaber das kann ich auch nicht anpassen.

Ich konnte keinen Anfängerleitfaden dazu finden. Ich nehme an, es gibt keinen, weil es nicht wirklich ein Anfängerthema ist. Ich kann auch keine Liste der Hooks finden. @Ulrikes ursprüngliche Antwort verwendet, para/beginaber ich weiß nicht, ob es einen section/beginoder ein Äquivalent gibt.

Antwort1

Sie waren nah dran. Versuchen Sie diesen Code. Funktioniert mit nummerierten und unnummerierten Abschnittsbefehlen.

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}

Nicht nummerierter Abschnitt

Bildbeschreibung hier eingeben

Antwort2

Die Antwort von Simon Dispa funktioniert super! Aber leider bricht sie bei Verwendung des fancyhdr-Pakets zusammen.

Ich habe die Arbeit von Simon mit einigen anderen Dingen kombiniert, die ich online gefunden habe (und nicht wieder finden kann …).

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

Beispiel für die automatische Absatznummerierung und die schicke Überschrift.

verwandte Informationen