段落番号の完全自動設定

段落番号の完全自動設定

延長が認められるかどうかわかりません前の質問だからここで質問します。

段落番号を自動的に付ける方法が欲しかったのですが、ウルリケ・フィッシャー素晴らしいソリューションを提供してくれました。このソリューションを拡張して、手動による介入を少なくしたいと考えています。提供された方法では、段落番号を一時的にオフにすることを明示的に要求します。\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

近かったです。このコードを試してください。番号付きおよび番号なしのセクション コマンドで機能します。

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 パッケージを使用すると機能しなくなります。

私は、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}

自動段落番号と装飾的なヘッダーの例。

関連情報