![Numeração de parágrafos totalmente automática](https://rvso.com/image/449642/Numera%C3%A7%C3%A3o%20de%20par%C3%A1grafos%20totalmente%20autom%C3%A1tica.png)
Não tenho certeza se posso estender meupergunta anteriorentão estou perguntando aqui.
Eu queria uma maneira de numerar os parágrafos automaticamente eUlrike Fischerforneceu uma excelente solução. Quero estender esta solução para exigir menos intervenção manual. O método fornecido requer explicitamente a desativação temporária da numeração dos parágrafos. Tenho certeza de que é possível alterar os comandos \section
e \subsection
e, \subsubsection
mas não consegui compilá-lo e muito menos funcionar. Idealmente, eu gostaria de aprender como fazer isso para esses comandos e provavelmente para outros onde não irei numerar parágrafos, como legendas e notas de rodapé.
MWE 1: Intervenção manual (obras)
\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: Sem intervenção manual (precisa de conserto)
\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}
Resulta em um erro de compilação.
Eu tentei \addto
e \addtocmd
isso também resultou em erros de compilação. Tive erros diferentes inicialmente porque não estava começando e terminando um grupo.
Eu tenho vistoessa questãomas não consigo adaptar a solução. eu encontreiessemas também não consigo adaptar isso.
Não consegui encontrar um guia para iniciantes sobre isso. Espero que não haja um porque não é realmente um tópico para iniciantes. Também não consigo encontrar uma lista dos ganchos. A resposta original de @Ulrike usa para/begin
, mas não sei se existe section/begin
ou equivalente.
Responder1
Você estava perto. Experimente este código. Funcionará com comandos seccionais numerados e não numerados.
\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}
Seção não numerada
Responder2
A resposta de Simon Dispa funciona muito bem! Mas infelizmente ele falha ao usar o pacote fancyhdr.
Combinei o trabalho do Simon com algumas outras coisas que encontrei online (e não consigo encontrar novamente...).
\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}