![全自動段落編號](https://rvso.com/image/449642/%E5%85%A8%E8%87%AA%E5%8B%95%E6%AE%B5%E8%90%BD%E7%B7%A8%E8%99%9F.png)
我不確定我是否可以延長我的期限上一個問題所以我在這裡問。
我想要一種自動對段落進行編號的方法烏爾里克·費雪提供了一個極好的解決方案。我想擴展這個解決方案以減少手動幹預。所提供的方法明確要求暫時關閉段落編號。我確信可以修改\section
and\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
你很接近。試試這個程式碼。將使用編號和未編號的截面指令。
\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 包時它會崩潰。
我將西蒙的工作與我在網上找到的其他一些東西結合起來(並且無法再找到...)。
\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}