1

I'd like to patch the section command to add at the end a simple \label in a way that is as generic as possible (I don't want to assume the number of arguments of the section command for instance, so I can't use \let and redefine \section directly).

However, the patched version moves the title outside of the section:

enter image description here

\documentclass{article}

\usepackage{etoolbox}

\ifdef{\section}{ \ifpatchable*{\section}{ \apptocmd{\section}{{\label{DEF}}}{}{}% }{} }

\usepackage{hyperref} \usepackage{cleveref} \begin{document}

\section{Name of my section}

Hello everybody. \cref{DEF}

\end{document}

tobiasBora
  • 8,684
  • You can't append something to section. That will break its handling of arguments. Beside this: automatic labels are mostly useless. They change if you change or move the section command and then the references break. – Ulrike Fischer Jan 27 '22 at 17:24
  • @UlrikeFischer Really?? What is so special about section? And I'm not sure to understand what you mean when you say that automatic labels are useless: in practice, I'll generate a new label per section, and keep its name in a macro to use later on, more or less as advised here https://tex.stackexchange.com/a/600847/116348 – tobiasBora Jan 27 '22 at 17:29

1 Answers1

2

You assume that \section is the command that sets a numbered section. But that's not the case. Look at the definition of \section in article.cls:

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries}}

\section, in fact, calls \@startsection with a bunch of other arguments.

If you want to add a generic label to a numbered section, you can use your patch but for \@sect:

enter image description here

\documentclass{article}

\usepackage{etoolbox}

\makeatletter \apptocmd{@sect}{\label{DEF}}{}{}% \makeatother

\usepackage{hyperref} \usepackage{cleveref}

\begin{document}

\section{Name of my section}

Hello everybody. \cref{DEF}

\end{document}

Unnumbered section use \@ssect internally, so this will only hold for numbered sections. But that makes complete sense. What doesn't make sense in your request if fixing the \label independent of the \section. It will result in multiply-defined labels if you have more than one section.

Werner
  • 603,163
  • Thanks a lot! I also need to patch chapters, subsection, paragraphs... if they exist. What is the equivalent name for them? – tobiasBora Jan 27 '22 at 17:42
  • Concerning the label, in practice I change the label programatically in every section using a counter, the DEF is only for the example. – tobiasBora Jan 27 '22 at 17:43
  • @tobiasBora Follow @Werner's answer and look into the appropriate .cls code. – Peter Wilson Jan 27 '22 at 17:57
  • @PeterWilson I tried, but I could not even find \@ssect in this file ^^ But I guess \@startsection defines it internally, and therefore all subsections/paragraphs/... should use the same \@sect macro, am I right? What I can't find however is the chapter definition (for instance from memoir). I tried to look into this file, but can't even find \newcommand{\chapter} https://mirrors.ircam.fr/pub/CTAN/macros/latex/contrib/memoir/memoir.dtx – tobiasBora Jan 27 '22 at 18:14
  • 1
    @tobiasBora: My patch via \@sect covers all sectioning units from \section and lower, but not \chapter. For reference, \@startsection is defined in the LaTeX kernel latex.ltx, while the sectioning commands are defined in the respective class files - article.cls, book.cls, ... – Werner Jan 27 '22 at 18:23
  • 1
    ... and report.cls. If you're using a class that defines \chapter (like book and report), then you should be able to \appcmd{\@chapter}{\label{<label>}}{}{}. – Werner Jan 27 '22 at 18:24
  • Thank's a lot, I'll try! – tobiasBora Jan 27 '22 at 18:25
  • Just to let you know, on komascript I can't patch chapters like this, so I created another question here https://tex.stackexchange.com/questions/655029/reliable-way-to-patch-a-chapter-in-scrreport-and-article – tobiasBora Aug 24 '22 at 10:01