완전 자동으로 단락 번호 매기기

완전 자동으로 단락 번호 매기기

연장이 허용되는지 잘 모르겠습니다.이전 질문그래서 여기에 묻습니다.

나는 단락에 자동으로 번호를 매기는 방법을 원했고울리케 피셔탁월한 솔루션을 제공했습니다. 수동 개입이 덜 필요하도록 이 솔루션을 확장하고 싶습니다. 제공된 메서드에서는 단락 번호 매기기를 일시적으로 꺼야 합니다. \sectionand \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 패키지를 사용하면 문제가 발생합니다.

나는 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}

자동 단락 번호 매기기 및 멋진 헤더의 예.

관련 정보