![Numeración de párrafos completamente automática.](https://rvso.com/image/449642/Numeraci%C3%B3n%20de%20p%C3%A1rrafos%20completamente%20autom%C3%A1tica..png)
No estoy seguro de si puedo extender miPregunta anteriorentonces pregunto aquí.
Quería una manera de numerar párrafos automáticamente yUlrike Fischerproporcionó una excelente solución. Quiero ampliar esta solución para que requiera menos intervención manual. El método proporcionado requiere explícitamente desactivar temporalmente la numeración de párrafos. Estoy seguro de que es posible modificar los comandos \section
and \subsection
y, \subsubsection
pero no he podido compilarlo y mucho menos funcionar. Idealmente, me gustaría aprender a hacer esto para estos comandos y probablemente para otros en los que no numeraré párrafos, como títulos y notas al pie.
MWE 1: Intervención 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: Sin intervención manual (necesita reparación)
\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}
Da como resultado un error de compilación.
Lo intenté \addto
y \addtocmd
esto también resultó en errores de compilación. Inicialmente tuve diferentes errores porque no estaba comenzando ni terminando un grupo.
He vistoesta preguntapero no puedo adaptar la solución. encontréestepero tampoco puedo adaptar eso.
No he podido encontrar una guía para principiantes sobre esto. Supongo que no hay ninguno porque en realidad no es un tema para principiantes. Tampoco puedo encontrar una lista de los ganchos. La respuesta original de @Ulrike usa para/begin
pero no sé si existe un section/begin
equivalente.
Respuesta1
Estabas cerca. Pruebe este código. Funcionará con comandos seccionales numerados y no 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}
Sección sin numerar
Respuesta2
¡La respuesta de Simon Dispa funciona muy bien! Pero desafortunadamente se estropea cuando se usa el paquete fancyhdr.
Combiné el trabajo de Simon con algunas otras cosas que encontré en línea (y no puedo volver a encontrarlas...).
\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}