全自动段落编号

全自动段落编号

我不确定我是否可以延长我的上一个问题所以我在这里问。

我想要一种自动对段落进行编号的方法,乌尔丽克·菲舍尔提供了一个极好的解决方案。我想扩展此解决方案以减少手动干预。提供的方法明确要求暂时关闭段落编号。我确信可以修改\section和命令\subsection\subsubsection但我无法使其编译,更不用说工作了。理想情况下,我想学习如何对这些命令以及可能我不会段落编号的其他命令(例如标题和脚注)执行此操作。

MWE 1:人工干预(有效)

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

可以工作但需要人工干预

MWE 2:无需人工干预(需要修复)

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

导致编译错误。

我尝试过\addto\addtocmd但这些也导致编译错误。我最初遇到了不同的错误,因为我没有开始和结束一个组。

我见过这个问题但我无法适应解决方案。我发现但我也适应不了。

我找不到这方面的初学者指南。我估计没有,因为这不是一个真正的初学者主题。我也找不到钩子的列表。@Ulrike 的原始答案使用了para/begin但我不知道是否有section/begin或等效的。

答案1

你已经接近目标了。试试这个代码。它将与编号和未编号的分段命令一起使用。

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}

未编号部分

在此处输入图片描述

答案2

Simon Dispa 的答案很有效!但不幸的是,当使用 fancyhdr 包时,它就崩溃了。

我将 Simon 的作品与我在网上找到的一些其他东西结合起来(无法再找到……)。

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

自动段落编号和花式页眉的示例。

相关内容