Debian Codegraph @

texmf-dist/source/latex-dev/latex-lab/latex-lab-sec-template.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-sec-template.dtx
4 %
5 % Copyright (C) 2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 %
15 % The latex-lab bundle is developed in the LaTeX2e GitHub.
16 % Issues may be reported at
17 %
18 % https://github.com/latex3/latex2e/issues
19 %
20 \def\ltlabsecIIdate{2026-04-24}
21 \def\ltlabsecIIversion{0.9d}
22 %<*driver>
23 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
24
25 \documentclass[kernel]{l3in2edoc}
26
27
28 \usepackage{amstext}
29 \usepackage{xcolor}
30
31 \EnableCrossrefs
32 \CodelineIndex
33
34 \usepackage{todonotes}
35
36 \begin{document}
37 \DocInput{latex-lab-sec-template.dtx}
38 \end{document}
39 %</driver>
40 %
41 % \fi
42 %
43 %
44 % \newcommand\key[1]{\texttt{#1}}
45 %
46 %
47 % \NewDocumentCommand\fmi{sO{}m}
48 % {\IfBooleanTF{#1}{\todo[inline,#2]{#3}}^^A
49 % {\todo[#2]{#3}}}
50 %
51 % \NewDocumentCommand\ufi{sO{}m}
52 % {\IfBooleanTF{#1}{\todo[inline,#2]{UFi:#3}}^^A
53 % {\todo[#2]{UFi:#3}}}
54 %
55 %
56 % \title{Reimplementation of \LaTeXe{}'s heading commands using templates}
57 % \author{\LaTeX{} Project\thanks{Initial implementation by Frank Mittelbach.}}
58 % \date{v\ltlabsecIIversion\ \ltlabsecIIdate}
59 %
60 % \maketitle
61 %
62 %
63 %
64 % \begin{abstract}
65 % \end{abstract}
66 %
67 %
68 % \tableofcontents
69 % \medskip
70 %
71 %
72 % \begin{documentation}
73 %
74 %
75 %
76 % \section{Introduction}
77 % This module reimplements tagging aware heading commands with templates.
78 % The new implementation is used automatically if \cs{DocumentMetadata} is used
79 % and replaces patches and redefinitions done in the \texttt{latex-lab-sec}.
80 % In a later step both modules will be merged.
81 %
82 % Most of the documentation is of interest only for class and package authors
83 % who want to setup their own heading commands but the new implementation changes
84 % also the user interface. This is documented first.
85 %
86 % \section{Changes to the user interface}
87 %
88 % \subsection{New user commands}
89 % \DescribeMacro\theheading
90 % This command expands inside a heading to the current number.
91 %
92 % \DescribeMacro\partmark
93 % This replaces the fix \verb+\markboth{}{}+ or similar in the standard \cs{part}
94 % definition.
95 %
96 %
97 % \subsection{The optional argument}
98 % The standard heading commands (re)defined by this module use the standard 2e syntax, e.g.,
99 % \begin{verbatim}
100 % \section[toc]{title}
101 % \end{verbatim}
102 % The star-form \verb+\section*{Title}+
103 % stops the numbering, toc, bookmark and running header
104 % as it was the way for the last 30-odd years.
105 % New is that the optional argument is handled as a key/val list if an equal sign at the outer level
106 % is detected, otherwise the argument is taken to be the value of the \key{shorttitle}
107 % and passed to the table of contents, the running headers, the bookmarks and used
108 % with \cs{nameref}. Note that this means, that
109 % \begin{verbatim}
110 % \section*[Title]{Title}
111 % \end{verbatim}
112 % will create a toc entry and a bookmark!
113 %
114 % The following keys can be used
115 % \begin{description}
116 % \item[\key{toc}] This sets the text for the table of contents. It also forces
117 % that the entry is created. So
118 % \begin{verbatim}
119 % \section*[toc=Introduction]{Introduction to this document}
120 % \end{verbatim}
121 % will create an unnumbered entry \enquote{Introduction} in the table of contents.
122 % An empty value (i.e., \texttt{toc=}) will suppress the toc entry.
123 %
124 % \item[\key{running}] This sets the text for the running header and forces the
125 % use of the mark command, so even with a starred section the running header will be updated.
126 % An empty value will suppress the call of the mark commands, so a header from a
127 % previous section will prevail.
128 %
129 % \item[\key{bookmark}] This sets the text for the bookmarks (the PDF outline)
130 % if, e.g., hyperref is loaded. As with the previous keys an empty value suppresses
131 % the bookmark entry.
132 %
133 % \item[\key{nameref}] This allows to set the text used for a crossreference
134 % with \cs{nameref}.
135 %
136 % \item[\key{label}] This sets a label, so is an alternative to using a
137 % \cs{label} command.
138 %
139 % \item[\key{subtitle}, \key{quote}] Theses keys will allow to pass a subtitle and
140 % a quote to the underlying code, but currently they are not yet used
141 % by the implementations of the standard \LaTeX{} heading commands.
142 %
143 % \item[\key{shorttitle}] This is the key which is used if the
144 % optional argument is not of key/val form. So,
145 % \begin{verbatim}
146 % \section[short]{long title}
147 % \end{verbatim}
148 % is the same as
149 % \begin{verbatim}
150 % \section[shortitle=short]{long title}
151 % \end{verbatim}
152 % The key sets the toc, running, bookmark and nameref text.
153 % If \key{shorttitle} is used together with any of the individual keys
154 % (\key{toc}, \key{running}, \key{bookmark}, \key{nameref}) then they overwrite the default
155 % value provided by \key{shorttitle}.
156 %
157 % \item[\key{numbered}, \key{unnumbered}] This allow to control
158 % the numbering without stopping toc, running head or bookmarks as it would happen
159 % with the starred version of the heading command.
160 %
161 % \item[template keys] Any other key present is assumed to be a key that should
162 % be passed to the heading instance to overwrite some of its
163 % settings. So, e.g.,
164 % \begin{verbatim}
165 % \section[placement=top,number-format=\fbox{\theheading}]{Title}
166 % \end{verbatim}
167 % will force the section to start a new page and put the number into an \cs{fbox}.
168 % For a list of supported keys, check the following parts of the documentation.
169 % \end{description}
170 %
171 % \subsection{Class support}
172 %
173 % The module redefines all heading commands of the three standard classes, \pkg{article},
174 % \pkg{report} and \pkg{book} to use the new implementation.
175 %
176 % Heading commands of other classes that are defined with \cs{@startsection} are
177 % supported (with some restrictions) through a compatibility layer described in the next section.
178 %
179 % The heading commands \cs{part} and \cs{chapter} are typically defined with
180 % \cs{secdef} and the internal command \cs{@part} and \cs{@chapter}. The module
181 % redefines \cs{secdef} to use the standard instances for these commands---this adds tagging support
182 % but \emph{changes the layout} until the class provides its own instances.
183 %
184 % For the ams-classes, \texttt{latex-lab-firstaid} contains instances for
185 % \cs{part} and \cs{chapter}. They come close to the original layout but are not
186 % exactly identical.
187 %
188 % Other heading commands defined with \cs{secdef} will error
189 % as the code has no real chance to know how to handle such a heading.
190 %
191 % Heading commands which use neither \cs{@startsection} or \cs{secdef} (e.g, the
192 % \cs{chapter} command of \pkg{memoir}, or the heading commands of \pkg{KOMA} classes)
193 % are not changed by this module and so will not be tagged correctly unless they add
194 % explicit tagging support.
195 %
196 % \subsection{Changing heading commands}\label{sec:startsection}
197 %
198 % It is possible to change heading commands by editing the instance they use.
199 % E.g., in the standard classes one could frame every heading number with
200 % \begin{verbatim}
201 % \EditInstance{heading}{section}
202 % {number-format=\fbox{\theheading}}
203 % \end{verbatim}
204 % The name of the instance (here \texttt{section}) is the same as the heading command.
205 %
206 % To support other classes which still setup their heading commands with
207 % \cs{@startsection} the code has a legacy compatibility layer: the
208 % \cs{@startsection} command has been redefined to setup instances on-the-fly
209 % at the first use of a heading command. These instances have names using the
210 % the first argument of \cs{@startsection} with an attached \texttt{-@startsection}.
211 % As they are created on-the-fly these internal instances can be only edited
212 % \emph{after} the first use of the heading command or by declaring (instead of editing)
213 % an instance with this name in the preamble. Also as the names of the instances
214 % are built from the first argument of \cs{@startsection}
215 % it is not possible to define two different heading commands
216 % of the same level with \cs{@startsection} as that would require two instances with
217 % different names. The next section describes how to lift this restrictions by converting
218 % such sectioning commands to use the new interfaces.
219 %
220 % \subsection[label=sec:convert]{Converting heading commands defined with \cs{@startsection}}
221 %
222 % To convert a heading command defined with \cs{@startsection} to the new interface suitable
223 % instances must be declared. The same needs to happen if one wants to alter
224 % the layout of a heading that was defined with \cs{@startsection} in the document preamble.
225 % How this can be done is demonstrated here with the \cs{section}
226 % command of the \pkg{ltugboat} class.
227 %
228 % \subsubsection{Step 1: Getting the template name and the instance values}
229 %
230 % The sectioning commands use two template \emph{types}, \texttt{heading} and \texttt{headformat}.
231 % Compile the following document
232 %
233 % \begin{verbatim}
234 % \DocumentMetadata{}
235 % \documentclass{ltugboat}
236 %
237 % \begin{document}
238 %
239 % \section{test} % <--- needed so that the instances get defined
240 % \ShowInstanceValues{heading}{section-@startsection}
241 % \ShowInstanceValues{headformat}{section-@startsection}
242 %
243 % \end{document}
244 % \end{verbatim}
245 %
246 % This will show the instance values in the log-file:
247 % \begin{verbatim}
248 % The instance 'section-@startsection' of type 'heading' has values:
249 % > level => 1
250 % > placement => normal
251 % > mark-cmd => \sectionmark {##1}
252 % > para-indent => false
253 % > before-vspace => 8.0pt plus 2.0pt minus 2.0pt
254 % > penalty => \c_max_int
255 % > after-penalty-vspace => 0pt
256 % > after-vspace => 4.0pt
257 % > start-code =>
258 % > final-code =>
259 % > heading-decls => \tubsecfmt
260 % > number-decls =>
261 % > title-decls =>
262 % > subtitle-decls =>
263 % > quote-decls =>
264 % > headformat-instance => section-@startsection
265 % > number-format => \theheading
266 % > contents-extra =>
267 % > name => section
268 % > from template => display.
269 %
270 %
271 % The instance 'section-@startsection' of type 'headformat' has values:
272 % > heading-indent => 0pt
273 % > title-format => \tubsecfmt {##1}
274 % > number-title-sep => 1em
275 % > from template => hang.
276 % \end{verbatim}
277 %
278 % \subsubsection{Step 2: Get the default template values}
279 %
280 % The last line in both lists show after the \texttt{from template} the names of the
281 % templates of each type. That can be used to get the default values of these templates.
282 % Compile the following document:%
283 % \begin{verbatim}
284 % \DocumentMetadata{}
285 % \documentclass{ltugboat}
286 %
287 % \begin{document}
288 % \ShowTemplateDefaults{heading}{display}
289 % \ShowTemplateDefaults{headformat}{hang}
290 % \end{document}
291 % \end{verbatim}
292 %
293 % This gives in the log and terminal:
294 % \begin{verbatim}
295 % The template 'display' of type 'heading' has default values:
296 % > level => 0
297 % > placement => normal
298 % > mark-cmd =>
299 % > para-indent => false
300 % > before-vspace => 0pt
301 % > penalty => \c_max_int
302 % > after-penalty-vspace => 0pt
303 % > after-vspace => 0pt
304 % > start-code =>
305 % > final-code =>
306 % > heading-decls => \normalfont
307 % > number-decls =>
308 % > title-decls =>
309 % > subtitle-decls =>
310 % > quote-decls =>
311 % > headformat-instance => hang
312 % > number-format => \theheading
313 % > contents-extra => .
314 %
315 % The template 'hang' of type 'headformat' has default values:
316 % > heading-indent => 0pt
317 % > title-format => ##1
318 % > number-title-sep => 1em.
319 % \end{verbatim}
320 %
321 % \subsection{Step 3: Declare the instances}
322 % By comparing the instance values with the default values one can see which values
323 % should be changed in the instance. The names of the new instances can be
324 % freely chosen best practice is to use the name
325 % of the heading command (without a backslash).
326 %
327 % This leads then to these declarations (note that in the \texttt{title-format} \verb+##1+ should
328 % be changed into \verb+#1+):
329 % \begin{verbatim}
330 % \DeclareInstance{heading}{section}{display} %#1=heading, #2=name, #3=template name
331 % {
332 % level = 1,
333 % mark-cmd = \sectionmark{#1}, % remove second hash
334 % before-vspace = 8.0pt plus 2.0pt minus 2.0pt,
335 % after-vspace = 4.0pt,
336 % heading-decls = \tubsecfmt,
337 % headformat-instance = section, % new name
338 % }
339 % \DeclareInstance{headformat}{section}{hang} %#1=heading, #2=name, #3=template name
340 % {
341 % title-format = \tubsecfmt {#1}
342 % }
343 % \end{verbatim}
344 %
345 % \subsubsection{Step 4 Redefine the \cs{section} command}
346 %
347 % Finally, the heading should be defined to use the new interface.
348 % \begin{verbatim}
349 % \DeclareDocumentCommand \section {s ={shorttitle}o m}
350 % { \ParseLaTeXeHeading {section} {#1} {#2} {#3} }
351 % \end{verbatim}
352 %
353 % \section{Setting up \LaTeXe{} heading commands in classes}
354 %
355 % Heading commands are managed by defining an instance of the
356 % \texttt{heading} template and then using through \cs{ParseLaTeXeHeading}, e.g.,
357 % \begin{verbatim}
358 % \DeclareDocumentCommand \part {s ={shorttitle}o m}
359 % { \ParseLaTeXeHeading {part} {#1} {#2} {#3} }
360 % \end{verbatim}
361 % The name of the \texttt{heading} instance is given in the first argument
362 % of \cs{ParseLaTeXeHeading} and it is recommended that classes use the
363 % same name as the heading command, to make it easier for users to identify the
364 % instance to edit if they want to adapt the command. Examples of instances
365 % that mimic the behavior of the heading commands in the standard classes can be found
366 % below. Section~\ref{label=sec:convert} shows how to get instances from commands
367 % previously defined with \cs{@startsection}.
368 %
369 % Legacy setup using \cs{@startsection} remains supported albeit
370 % not that performant and with some restrictions for the users,
371 % see section \ref{sec:startsection} above and in the documentation below.
372 %
373 % In contrast, \cs{secdef} is only rudimentarly supported. It delegates the
374 % layout to two commands which can contain arbitrary code (usually
375 % hardwired) so that there is no realistic chance to automatically take this
376 % apart and figure out what values should be used for what template
377 % parameters. We therefore redefine \cs{secdef} and
378 % map the standard commands \cs{part}
379 % and \cs{chapter} to the standard instance and error if other uses are detected.
380 % Classes using \cs{secdef} should setup suitable instances that
381 % mimic their current layout, an example can be found for the
382 % ams-classes in \texttt{latex-lab-firstaid.dtx}.
383 %
384 %
385 % \section{Open points}
386 %
387 % \begin{itemize}
388 % \item There is no good interface yet to change e.g. the font family of all heading
389 % commands in one go.
390 % \item Support for \texttt{titlesec}, see section \ref{label=sec:titlesec}.
391 % \end{itemize}
392 %
393 % \section{Template types and templates for headings}
394 %
395 % The template(s) for headings expect(s) a large number of
396 % positional arguments containing document user data. The
397 % document-level command, e.g., \cs{section} may not offer all of
398 % them directly, but may do so via a key value interface or not at
399 % all. If no interface is provided then the template is passed
400 % \cs{NoNalue} . If it is offered via a key value interface,
401 % the template receives whatever is set by the user (and
402 % \cs{NoNalue} otherwise).
403 %
404 % In my initial implementation I had only the main data as
405 % positional arguments, but I came to the conclusion that this
406 % scheme here is better going forward.
407 %
408 % \subsection{Template types}
409 %
410 % The template type \texttt{heading} has 10 data arguments, which is
411 % more than can be specified as positional arguments. For that
412 % reason argument \#9 holds 2 brace groups. The alternative would
413 % be to specify such arguments as keys, but for a number of reasons
414 % I think the approach to put seldom necessary data arguments all
415 % in the last positional argument is actually better (besides being faster).
416 %
417 % \begin{TemplateInterfaceDescription}{heading}
418 % \TemplateArgument{1}{key/value list to alter the default heading parameters}
419 % \TemplateArgument{2}{unnumbered heading?}
420 % \TemplateArgument{3}{main title of the heading}
421 % \TemplateArgument{4}{toc title}
422 % \TemplateArgument{5}{running title}
423 % \TemplateArgument{6}{bookmark title}
424 % \TemplateArgument{7}{nameref text}
425 % \TemplateArgument{8}{label for the heading in the form \cs{label}\{\meta{string}\}}
426 % \TemplateArgument{9}{\{ sub title \} \{ quotation \}}
427 % \TemplateSemantics
428 % Handles the layout and processing aspects of a heading.
429 %
430 % Whether or not the heading is numbered is governed through a
431 % boolean, expecting the result of an \texttt{s} specification of
432 % \cs{NewDocumentCommand} or equivalent, i.e., expects
433 % \cs{BooleanTrue} or \cs{BooleanFalse}.
434 %
435 % If the title data is also used for bookmarks, toc, running
436 % header, and cross references then it has to be given several times which
437 % can be arranged for by the parsing interface command that calls
438 % the template instance.
439 %
440 % If the bookmark, toc, or running argument is set to
441 % \enquote{empty} then the bookmark, toc or running header should
442 % be suppressed by the template. This enables the user on document
443 % level to explicitly specify, for example, \texttt{[bookmark=]} to
444 % suppress the bookmark.
445 %
446 % The \texttt{nameref} argument is not expected to be empty. Its value
447 % should always be used as given if named references are to be
448 % generated.
449 %
450 % The label argument either holds a \cs{label} command as provided
451 % by the user (or several, see implementation of
452 % \cs{ParseLaTeXeHeading}) or it is empty. I.e., it can be directly
453 % executed by a template at the right point where the reference
454 % counter (if any) has been set up without the need to check its
455 % content.
456 %
457 % Argument \#9 holds further user data (in brace groups) which are
458 % seldom implemented, but if they are the data is available in a
459 % positional argument, which then needs to be taken apart using
460 % \cs{@firstoftwo} (for subtitle) or \cs{@secondoftwo} (for a
461 % quotation). If no data is provided \cs{NoValue} is used to
462 % indicate that.
463 %
464 % \end{TemplateInterfaceDescription}
465 %
466 %
467 % \begin{TemplateInterfaceDescription}{headformat}
468 % \TemplateArgument{1}{key/value list to alter the default headformat parameters}
469 % \TemplateArgument{2}{formatted heading number}
470 % \TemplateArgument{3}{main title of the heading}
471 % \TemplateArgument{4}{subtitle}
472 % \TemplateArgument{5}{quotation}
473 % \TemplateSemantics
474 % Handles the layout of just the label, main title, subtitle, and
475 % quotation but not the spatial relation to previous and following text.
476 %
477 % Whether or not the heading is numbered is governed through a
478 % boolean, e.g., result of an \texttt{s} specification in
479 % \cs{NewDocumentCommand} or equivalent.
480 %
481 % If there is no subtitle or quotation then this is indicated
482 % with \cs{NoValue}.
483 %
484 % Note: instead of unnumbered + counter we could just have a single
485 % argument containing the number representation (or \cs{NoValue} if
486 % not used). The reason that there are two is that this allows us to
487 % continue to support \cs{@seccntformat}, but I'm not sure this is a
488 % good enough reason.
489 %
490 % \end{TemplateInterfaceDescription}
491 %
492 %
493 %
494 % \subsection{Templates}
495 %
496 %
497 % There are a number of keys that are expected to be recognized by
498 % all heading templates (though they may choose not to make use of
499 % them). These are listed below instead of being repeated on the
500 % actual templates.
501 %
502 % All other keys are either attached to the \texttt{heading} or to
503 % the \texttt{headformat} templates. Those that typically vary
504 % from heading instance to the next (e.g., \key{heading-decls}) are
505 % all declared in the
506 % \texttt{heading} templates even if they are actually only used
507 % within \texttt{headformat} templates. In other words,
508 % \texttt{headformat} templates have a number of implicit
509 % variables that they expect to be set.\footnote{Maybe questionable}
510 %
511 %
512 % \begin{TemplateDescription}{heading}{\meta{all}}
513 %
514 % \TemplateKey{name}{tokenlist}
515 % {Referencable name of the heading instance. String that
516 % is acceptable in csnames for use in building counter
517 % names, etc.}{}
518 % \TemplateKey{parent-name}{tokenlist}
519 % {Name of the next higher heading instance. If not
520 % given, then the internal heading level of the heading
521 % instance is set to \texttt{0}}{}
522 % \TemplateKey{reset-counter}{tokenlist}
523 % {Name of the heading instance that should reset the
524 % numbering of this heading level (if any)}{}
525 % \TemplateKey{level}{integer}
526 % {Sets the internal heading-level rather than deducing
527 % it from \key{parent-name}. Can be used to specify the
528 % top-level heading if not \texttt{0}, or all headings
529 % in legacy implementations, e.g., through \cs{@startsection}}{}
530 %
531 % \TemplateKey{placement}{choice}
532 % {Set the heading placement, i.e., the behavior of the
533 % heading with respect to page breaks. Allowed values
534 % are
535 % \texttt{page} (heading forms a page if its own),
536 % \texttt{top} (heading starts a new page),
537 % \texttt{normal} (heading can appear anywhere on the
538 % page). Further possibilities might be
539 % \texttt{rectopage} and \texttt{rectotop} if we
540 % implement that.}{\texttt{normal}}
541 %
542 % \TemplateKey{start-code}{tokenlist}
543 % {Default value is set by the \key{placement}
544 % key. Executed before the heading starts, so can issue, for
545 % example, a \cs{clearpage}}
546 % {}
547 %
548 % \TemplateKey{final-code}{tokenlist}
549 % {Default value is set by the \key{placement}
550 % key. Executed after the heading is typeset. Can set up code for
551 % putting the heading on a page by its own, or arrange for
552 % paragraph handling of a following paragraph, etc.}
553 % {}
554 %
555 % \TemplateKey{mark-cmd}{function(1)}
556 % {Function that receives the \meta{running} argument and
557 % creates a suitable mark insertion}
558 % {\texttt\textbackslash\meta{name}\texttt{mark}}
559 %
560 % \TemplateSemantics
561 %
562 % The above keys should be implemented by all heading templates.
563 %
564 % At the moment I have retained the \LaTeXe{} interfaces for marks,
565 % e.g., one has to set up \cs{chaptermark}, \cs{sectionmark},
566 % etc.\ but I'm not sure this should stay (even though it is
567 % certainly simpler from a compatibility perspective).
568 %
569 % All templates should set up \cs{theheading} to correspond to
570 % \cs{the\meta{name}}.
571 % \end{TemplateDescription}
572 %
573 %
574 % \begin{TemplateDescription}{heading}{display}
575 %
576 % \TemplateKey{para-indent}{boolean}
577 % {Should the paragraph after the heading be indented?}
578 % {false}
579 %
580 % \TemplateKey{before-vspace}{skip}
581 % {Vertical space before the heading if there is no page
582 % or column break. If there is one it vanishes.
583 % In particular this means it will not be used in the
584 % heading \texttt{placement}s \texttt{page} or \texttt{top}}
585 % {0pt}
586 %
587 % \TemplateKey{penalty}{integer}
588 % {Penalty to break before the heading. The default
589 % (\TeX's largest integer) indicates that no penalty was set in
590 % which case \cs{@secpenalty} is used.}
591 % {\number\maxdimen}
592 %
593 % \TemplateKey{after-penalty-vspace}{skip}
594 % {Vertical space before the heading but after the
595 % penalty for the heading. If the penalty results in a page or
596 % column break, this space remains at the top of the page}
597 % {0pt}
598 %
599 % \TemplateKey{after-vspace}{skip}
600 % {Vertical space after the heading}
601 % {0pt}
602 %
603 % \TemplateKey{heading-decls}{tokenlist}
604 % {Declarations (such as font or color settings) applied
605 % to all heading elements, i.e., number, title,
606 % subtitle, and quotation, if present.}
607 % {\cs{normalfont}}
608 %
609 % \TemplateKey{number-decls}{tokenlist}
610 % {Declarations (such as font or color settings) applied
611 % to the heading number, overwriting the setting
612 % of \key{heading-decls}.}
613 % {\meta{empty}}
614 %
615 % \TemplateKey{title-decls}{tokenlist}
616 % {Declarations (such as font or color settings) applied
617 % to the heading title, overwriting the setting
618 % of \key{heading-decls}.}
619 % {\meta{empty}}
620 %
621 % \TemplateKey{subtitle-decls}{tokenlist}
622 % {Declarations (such as font or color settings) applied
623 % to the heading subtitle, overwriting the setting
624 % of \key{heading-decls}.}
625 % {\meta{empty}}
626 %
627 % \TemplateKey{quote-decls}{tokenlist}
628 % {Declarations (such as font or color settings) applied
629 % to the heading quote, overwriting the setting
630 % of \key{heading-decls}.}
631 % {\meta{empty}}
632 %
633 % \TemplateKey{number-format}{function(1)}
634 % {Code that produces a formatted version of the
635 % heading number
636 % including any ornamentations. Its argument is the
637 % counter name for the head. However, instead of using
638 % a specific counter representations, e.g.,
639 % \cs{thesection}, it
640 % can refer to the counter representation for the
641 % current heading counter via \cs{theheading} and ignore
642 % the argument.}
643 % {\cs{theheading}}
644 %
645 % \TemplateKey{contents-extra}{tokenlist}
646 % {Code containing \cs{addcontents} calls to write to
647 % files like \texttt{.lot} or \texttt{.lot}}
648 % {\meta{empty}}
649 %
650 %
651 % \TemplateKey{headformat-instance}{instance}
652 % {Template instances of type \texttt{headformat}}
653 % {hang}
654 %
655 % \TemplateSemantics
656 %
657 % Several of the key names are simply bad and need revision!
658 % \end{TemplateDescription}
659 %
660 %
661 %
662 %
663 % \begin{TemplateDescription}{heading}{runin}
664 %
665 % \TemplateKey{before-vspace}{skip}
666 % {Vertical space before the heading if there is no page
667 % or column break. If there is one it vanishes.}
668 % {0pt}
669 %
670 % \TemplateKey{penalty}{integer}
671 % {Penalty to break before the heading. The default
672 % (\TeX's largest integer) indicates that no penalty was set in
673 % which case \cs{@secpenalty} is used.}
674 % {\number\maxdimen}
675 %
676 % \TemplateKey{after-penalty-vspace}{skip}
677 % {Vertical space before the heading but after the
678 % penalty for the heading. If the penalty results in a page or
679 % column break, this space remains at the top of the page. It is
680 % also applied if the heading is a \texttt{page} or \texttt{top} heading.}
681 % {0pt}
682 %
683 % \TemplateKey{after-vspace}{skip}
684 % {Vertical space after the heading}
685 % {0pt}
686 %
687 % \TemplateKey{heading-decls}{tokenlist}
688 % {Declarations (such as font or color settings) applied
689 % to all heading elements, i.e., number, title,
690 % subtitle, and quotation, if present.}
691 % {\cs{normalfont}}
692 %
693 % \TemplateKey{number-decls}{tokenlist}
694 % {Declarations (such as font or color settings) applied
695 % to the heading number, overwriting the setting
696 % of \key{heading-decls}.}
697 % {\meta{empty}}
698 %
699 % \TemplateKey{title-decls}{tokenlist}
700 % {Declarations (such as font or color settings) applied
701 % to the heading title, overwriting the setting
702 % of \key{heading-decls}.}
703 % {\meta{empty}}
704 %
705 % \TemplateKey{subtitle-decls}{tokenlist}
706 % {Declarations (such as font or color settings) applied
707 % to the heading subtitle, overwriting the setting
708 % of \key{heading-decls}.}
709 % {\meta{empty}}
710 %
711 % \TemplateKey{quote-decls}{tokenlist}
712 % {Declarations (such as font or color settings) applied
713 % to the heading quote, overwriting the setting
714 % of \key{heading-decls}.}
715 % {\meta{empty}}
716 %
717 % \TemplateKey{number-format}{function(1)}
718 % {Code that produces a formatted version of the
719 % heading number
720 % including any ornamentations. Its argument is the
721 % counter name for the head. However, instead of using
722 % a specific counter representations, e.g.,
723 % \cs{thesection}, it
724 % can refer to the counter representation for the
725 % current heading counter via \cs{theheading} and ignore
726 % the argument.}
727 % {\cs{theheading}}
728 %
729 % \TemplateKey{headformat-instance}{instance}
730 % {Template instances of type \texttt{headformat}}
731 % {hang}
732 %
733 % \TemplateSemantics
734 %
735 % Several of the key names are simply bad and need revision!
736 % \end{TemplateDescription}
737 %
738 %
739 % \begin{TemplateDescription}{headformat}{hang}
740 %
741 % \TemplateKey{heading-indent}{dimen}
742 % {Horizontal space before the heading (shifting it to
743 % the right in a LR typesetting context)}
744 % {0pt}
745 %
746 % \TemplateKey{title-format}{tokenlist}
747 % {Code executed directly before the heading is typeset
748 % and after the vertical spacing is done. The code takes an argument,
749 % e.g., \cs{MakeUppercase}}
750 % {\meta{\#1}}
751 %
752 % \TemplateKey{number-title-sep}{tokenlist}
753 % {Token list that can be used in the assignment to a
754 % \TeX{} dimension (can contain \texttt{em} or
755 % \texttt{ex} values) specifying the separation between
756 % title number and title text. Evaluated after fonts for
757 % title text have been set up, i.e., the \texttt{em} will be based
758 % on the then current font.}
759 % {1em}
760 %
761 % \TemplateSemantics
762 %
763 % Implements a heading layout where number and title start on the
764 % same line and the title is hanging off from that if the title
765 % has more than one line. It is what \LaTeX{} always used by
766 % default for \cs{section}, \cs{subsection}, and
767 % \cs{subsubsection}.
768 %
769 % Several of the key names are simply bad and need a revision!
770 % \end{TemplateDescription}
771 %
772 %
773 % \begin{TemplateDescription}{headformat}{runin}
774 %
775 % \TemplateKey{heading-indent}{dimen}
776 % {Horizontal space before the heading (shifting it to
777 % the right in a LR typesetting context)}
778 % {0pt}
779 %
780 % \TemplateKey{title-format}{tokenlist}
781 % {Code executed directly before the heading is typeset
782 % and after the vertical spacing is done. The code can take an argument,
783 % e.g., \cs{MakeUppercase}}
784 % {\meta{empty}}
785 %
786 % \TemplateKey{number-title-sep}{tokenlist}
787 % {Token list that can be used in the assignment to a
788 % \TeX{} dimension (can contain \texttt{em} or
789 % \texttt{ex} values) specifying the separation between
790 % title number and title text. Evaluated after fonts for
791 % title text have been set up, i.e., the \texttt{em} will be based
792 % on the then current font.
793 %
794 % In the hang template it is a horizontal dimension!}
795 % {1em}
796 %
797 % \TemplateSemantics
798 %
799 % Implements a heading layout where number and title start on the
800 % same line but then continue with the following paragraph on the
801 % same line (or the next if the title overflows, i.e., the title is
802 % run-in. It is what \LaTeX{} always used by default for
803 % \cs{paragraph} and \cs{subparagraph}.
804 %
805 % Several of the key names are simply bad and need a revision!
806 % \end{TemplateDescription}
807 %
808 %
809 % \begin{TemplateDescription}{headformat}{display}
810 %
811 % \TemplateKey{heading-indent}{dimen}
812 % {Horizontal space before the heading (shifting it to
813 % the right in a LR typesetting context)}
814 % {0pt}
815 % \TemplateKey{title-format}{tokenlist}
816 % {Code executed directly before the heading is typeset
817 % and after the vertical spacing is done. The code can take an argument,
818 % e.g., \cs{MakeUppercase}}
819 % {\meta{\#1}}
820 %
821 % \TemplateKey{number-title-sep}{tokenlist}
822 % {Token list that can be used in the assignment to a
823 % \TeX{} dimension (can contain \texttt{em} or
824 % \texttt{ex} values) specifying the separation between
825 % title number and title text. Evaluated after fonts for
826 % title text have been set up, i.e., the \texttt{em} will be based
827 % on the then current font.
828 % \endgraf
829 % In the display template it is a vertical dimension!}
830 % {20 pt}
831 %
832 % \TemplateSemantics
833 %
834 % Implements a heading layout where number and title are vertically
835 % separated. It is what \LaTeX{} always used by default for
836 % \cs{chapter} and \cs{part}.
837 %
838 % Several of the key names are simply bad and need a revision!
839 % \end{TemplateDescription}
840 %
841 %
842 %
843 %
844 % \subsection{Default instances}
845 %
846 % These instances are set up to mimic the design of the standard
847 % \LaTeX{} classes. For other classes they could be adjusted by
848 % changing the instance values and/or by basing them on a different
849 % template.
850 %
851 % \subsubsection{Instances of \texttt{heading} templates}
852 %
853 % \begin{description}
854 %
855 % \item[\texttt{part} (instance of \texttt{display} template)] Used for the
856 % \cs{part} command.
857 %
858 % \item[\texttt{chapter} (instance of \texttt{display} template)] Used for the
859 % \cs{chapter} command.
860 %
861 % \item[\texttt{section} (instance of \texttt{display} template)] Used for the
862 % \cs{section} command.
863 %
864 % \item[\texttt{subsection} (instance of \texttt{display} template)] Used for
865 % the \cs{subsection} command.
866 %
867 % \item[\texttt{subsubsection} (instance of \texttt{display} template)] Used for
868 % the \cs{subsubsection} command.
869 %
870 % \item[\texttt{paragraph} (instance of \texttt{runin} template)] Used for the
871 % \cs{paragraph} command.
872 %
873 % \item[\texttt{subparagraph} (instance of \texttt{runin} template)] Used for
874 % the \cs{subparagraph} command.
875 %
876 % \end{description}
877 %
878 %
879 % \subsubsection{Instances of \texttt{headformat} templates}
880 %
881 % \begin{description}
882 %
883 % \item[\texttt{display} (instance of \texttt{display} template)] Used in
884 % \texttt{heading} instance for \cs{part} and \cs{chapter}.
885 %
886 % \item[\texttt{hang} (instance of \texttt{hang} template)] Used in
887 % \texttt{heading} instance for \cs{section}, \cs{subsection}, and
888 % \cs{subsubsection}.
889 %
890 % \item[\texttt{paragraph} (instance of \texttt{runin} template)] Used in
891 % \texttt{heading} instance for \cs{paragraph}
892 %
893 % \item[\texttt{subparagraph} (instance of \texttt{runin} template)] Used in
894 % \texttt{heading} instance for \cs{subparagraph}
895 %
896 % \end{description}
897 %
898 %
899 %
900 %
901 % \section{Support for legacy classes and packages}
902 %
903 % \subsection{Support classes based on legacy \LaTeXe{} interfaces}
904 %
905 % \DescribeMacro\@startsection
906 % The \cs{@startsection} command has the following syntax:
907 % \begin{verbatim}
908 % \@startsection{name}{level}{indent}{beforeskip}{afterskip}{style}
909 % \end{verbatim}
910 % with a special logic that the sign of \meta{beforeskip} determines
911 % display or runin heading and that of \meta{afterskip} whether or
912 % not the next paragraph is indented.
913 %
914 % All arguments can be cleanly mapped to \texttt{heading} and
915 % \texttt{headformat} templates. Thus, if encountered suitable
916 % instances are declared unless already existing.
917 %
918 %
919 %
920 % \DescribeMacro\secdef
921 % In contrast \cs{secdef} only does the parsing and delegates the
922 % layout to two commands which can contain arbitrary code (usually
923 % hardwired) so that there is no realistic chance to take this
924 % apart and figure out what values should be used for what template
925 % parameters.
926 %
927 % We therefore do not attempt to model this, but instead just use
928 % the standard layout from \LaTeX's standard classes for
929 % \cs{chapter} and \cs{part} if we can identify that the \cs{secdef}
930 % is used to define one of these.
931 %
932 % Maybe we can try at some stage to get some static analysis tool
933 % going.
934 %
935 %
936 % \subsection[label=sec:titlesec]{Support for the \pkg{titlesec} package interfaces}
937 % TODO
938 %
939 % \noindent
940 % \DescribeMacro\titleformat
941 % The \cs{titleformat} declaration has the following syntax:
942 % \begin{verbatim}
943 % \titleformat{cmd}[shape]{format}{label}{sep}{before-code}[after-code]
944 % \end{verbatim}
945 %
946 % \noindent
947 % \DescribeMacro\titlespacing
948 % The \cs{titlespacing} declaration has the following syntax:
949 % \begin{verbatim}
950 % \titlespacing*{cmd}{left-sep}{before-vspace}{after-vspace}[right-sep]
951 % \end{verbatim}
952 %
953 % \section{Support for links}
954 %
955 % All heading commands (numbered and unnumbered) should contain support for
956 % active links directly. hyperref does not patch the new heading commands!
957 % As \cs{refstepcounter} is not used there is no automatic target.
958 %
959 % This means that all definitions should contain at an appropriate place
960 % a \cs{MakeLinkTarget} command. Typically numbered heading commands will use
961 % \verb+\MakeLinkTarget{+\meta{counter}\verb+}+ while unnumbered heading commands
962 % use \verb+\MakeLinkTarget[+\meta{counter}\verb+]{}+.
963 %
964 % The definition must take care that the target is not separated from the heading
965 % by a page break. It also should not affect spacing.
966 % The target should be placed so that a jump to the target gives
967 % a satisfying user experience, in most cases the best places is at the left margin
968 % a bit above the heading.
969 %
970 % The targets create also structure destinations that
971 % are used by the tagging code. It is therefore important that the targets
972 % are in the right place in relation to the tagging commands.
973 %
974 % \section{Bookmarks}
975 %
976 % Bookmarks are created by the \cs{addcontentsline} command, more precisely in the
977 % new latex-lab toc code a hook with arguments is inserted at the begin of the command
978 % which contains the command that creates the bookmark. The arguments of \cs{addcontentsline}
979 % and so also of the hook are the type of the toc,
980 % the level name and the (short-)title of the heading. To pass a differing
981 % bookmark text the code uses \cs{texorpdfstring} in the \cs{addcontentsline}.
982 % \ufi{check if this is still the implementation ...}
983 % \section{Tagging support}
984 %
985 % Tagging of heading commands has to do two tasks:
986 % \begin{itemize}
987 % \item surround the whole section (heading and text) with a \texttt{Sect} structure
988 % \item tag the heading with an Hn structure.
989 % \end{itemize}
990 % This is done with tagging sockets which are documented in the code and in \texttt{latex-lab-sec}.
991 %
992 % \section{Debugging support}
993 %
994 % \begin{function}{\DebugHeadingsOn,\DebugHeadingsOff, \head_debug_on:, \head_debug_off:}
995 %
996 % These commands enable/disable debugging messages.
997 %
998 % \end{function}
999 %
1000 %
1001 %
1002 % \end{documentation}
1003 %
1004 % \StopEventually{\setlength\IndexMin{200pt} \PrintIndex }
1005 %
1006 %
1007 %
1008 % \begin{implementation}
1009 %
1010 % \section{The Implementation}
1011 %
1012 %
1013 % \begin{macrocode}
1014 %<*package>
1015 %<@@=head>
1016 % \end{macrocode}
1017 %
1018 %
1019 % \begin{macrocode}
1020 \ProvidesExplPackage
1021 {latex-lab-testphase-sec-template}
1022 {\ltlabsecIIdate}
1023 {\ltlabsecIIversion}
1024 {heading implementation}
1025 % \end{macrocode}
1026 %
1027 % \changes{v0.9c}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
1028 % expansion in expl3 code}
1029 %
1030 % \bigskip
1031 %
1032 % \subsection{Debugging}
1033 %
1034 % \begin{variable}{\g_@@_debug_bool}
1035 %
1036 % \begin{macrocode}
1037 \bool_new:N \g_@@_debug_bool
1038 % \end{macrocode}
1039 % \end{variable}
1040 %
1041 %
1042 % \begin{macro}{\@@_debug:n,\@@_debug_typeout:n}
1043 %
1044 % \begin{macrocode}
1045 \cs_new_eq:NN \@@_debug:n \use_none:n
1046 \cs_new_eq:NN \@@_debug_typeout:n \use_none:n
1047 % \end{macrocode}
1048 % \end{macro}
1049 %
1050 % \begin{macro}{\head_debug_on:,\head_debug_off:,
1051 % \@@_debug_gset:}
1052 % \begin{macrocode}
1053 \cs_new_protected:Npn \head_debug_on:
1054 {
1055 \bool_gset_true:N \g_@@_debug_bool
1056 \@@_debug_gset:
1057 }
1058 % \end{macrocode}
1059 %
1060 % \begin{macrocode}
1061 \cs_new_protected:Npn \head_debug_off:
1062 {
1063 \bool_gset_false:N \g_@@_debug_bool
1064 \@@_debug_gset:
1065 }
1066 % \end{macrocode}
1067 %
1068 % \begin{macrocode}
1069 \cs_new_protected:Npn \@@_debug_gset:
1070 {
1071 \cs_gset_protected:Npe \@@_debug:n ##1
1072 { \bool_if:NT \g_@@_debug_bool {##1} }
1073 \cs_gset_protected:Npe \@@_debug_typeout:n ##1
1074 { \bool_if:NT \g_@@_debug_bool { \typeout{[head]~ ##1} } }
1075 }
1076 % \end{macrocode}
1077 % \end{macro}
1078 %
1079 %
1080 % \begin{macro}{\DebugHeadingsOn,\DebugHeadingsOff}
1081 %
1082 % \begin{macrocode}
1083 \cs_new_protected:Npn \DebugHeadingsOn { \head_debug_on: }
1084 \cs_new_protected:Npn \DebugHeadingsOff { \head_debug_off: }
1085 % \end{macrocode}
1086 %
1087 % \begin{macrocode}
1088 \DebugHeadingsOff
1089 % \end{macrocode}
1090 % \end{macro}
1091 %
1092 % \begin{macro}{\@@_show_arguments:nnnnnn}
1093 % Some debug data \ldots
1094 % \begin{macrocode}
1095 \cs_new:Npn \@@_show_arguments:nnnnn #1#2#3#4#5 {
1096 \@@_debug_typeout:n{---------~ Headformat~ instance~ arguments:}
1097 \@@_debug_typeout:n{1:~ keys~ =~ \exp_not:n{#1}}
1098 \@@_debug_typeout:n{2:~ number~ =~ \exp_not:n{#2}}
1099 \@@_debug_typeout:n{3:~ title~ =~ \exp_not:n{#3}}
1100 \@@_debug_typeout:n{4:~ subtitle~ =~ \exp_not:n{#4}}
1101 \@@_debug_typeout:n{5:~ quotation~ =~ \exp_not:n{#5}}
1102 }
1103 % \end{macrocode}
1104 % \end{macro}
1105 % \begin{macro}{\@@_show_arguments:nnnnnnnnn}
1106 % Some debug data \ldots
1107 % \begin{macrocode}
1108 \cs_new:Npn \@@_show_arguments:nnnnnnnnn #1#2#3#4#5#6#7#8#9 {
1109 \@@_debug_typeout:n{---------~ Heading~ instance~ arguments:}
1110 \@@_debug_typeout:n{1:~ keys~ =~ \exp_not:n{#1}}
1111 \@@_debug_typeout:n{2:~ unnumbered~ =~ \exp_not:n{#2}}
1112 \@@_debug_typeout:n{3:~ title~ =~ \exp_not:n{#3}}
1113 \@@_debug_typeout:n{4:~ toc~ =~ \exp_not:n{#4}}
1114 \@@_debug_typeout:n{5:~ running~ =~ \exp_not:n{#5}}
1115 \@@_debug_typeout:n{6:~ bookmark~ =~ \exp_not:n{#6}}
1116 \@@_debug_typeout:n{7:~ nameref~ =~ \exp_not:n{#7}}
1117 \@@_debug_typeout:n{8:~ label~ =~ \exp_not:n{#8}}
1118 \@@_debug_typeout:n{9:~ subtitle | quote ~ =~ \exp_not:n{#9}}
1119 }
1120 % \end{macrocode}
1121 % \end{macro}
1122 %
1123 % \subsection{Temp variable(s)}
1124 % \begin{variable}{\l_@@_tmpa_tl}
1125 % \begin{macrocode}
1126 \tl_new:N\l_@@_tmpa_tl
1127 % \end{macrocode}
1128 % \end{variable}
1129 %
1130 % \subsection{variable(s)}
1131 % These token lists are automatically declared by the key machinery.
1132 % \begin{macrocode}
1133 %\tl_new:N \l_@@_bookmark_tl
1134 %\tl_new:N \l_@@_running_tl
1135 %\tl_new:N \l_@@_toc_tl
1136 %\tl_new:N \l_@@_nameref_tl
1137 %\tl_new:N \l_@@_subtitle_tl
1138 %\tl_new:N \l_@@_quote_tl
1139 %\bool_new:N l_@@_unnumbered_bool
1140 % \end{macrocode}
1141 %
1142 % \begin{variable}{
1143 % \l_@@_label_tl,
1144 % \l_@@_instance_keys_tl,
1145 % \l_@@_typeset_number_tl,
1146 % \l_@@_saved_secnumdepth_tl,
1147 % \l_@@_title_tl,
1148 % \l_@@_placement_tl}
1149 %
1150 % But this token lists needs a declaration:
1151 %
1152 % \begin{macrocode}
1153 \tl_new:N\l_@@_label_tl
1154 \tl_new:N\l_@@_instance_keys_tl
1155 \tl_new:N\l_@@_typeset_number_tl
1156 \tl_new:N\l_@@_saved_secnumdepth_tl
1157 \tl_new:N \l_@@_title_tl
1158 \tl_new:N \l_@@_placement_tl
1159 % \end{macrocode}
1160 % \end{variable}
1161 %
1162 % \subsection{New user commands}
1163 %
1164 % \begin{macro}{\theheading}
1165 % This command expands to \cs{thechapter}, \cs{thesection} etc
1166 % in every heading command.
1167 % \begin{macrocode}
1168 \tl_new:N\theheading
1169 % \end{macrocode}
1170 % \end{macro}
1171 %
1172 % \begin{macro}{\partmark}
1173 % This replaces the fix \verb+\markboth{}{}+ or similar in the standard \cs{part}
1174 % definition. As the command is already defined in some classes (like \pkg{memoir}),
1175 % we provide it through a hook.
1176 % \begin{macrocode}
1177 \AddToHook{class/after}{\providecommand*\partmark[1]{\markboth{}{}}}
1178 % \end{macrocode}
1179 % \end{macro}
1180 %
1181 % \subsection{The \LaTeXe{} parsing interface}
1182 %
1183 % \LaTeXe{} provides heading commands with a starred form
1184 % (unnumbered) and one optional argument (to specify an alternative
1185 % toc/running head). We augment this slightly by supporting a key
1186 % value interface in the optional argument. This is handled by
1187 % defining the commands through \cs{ParseLaTeXeHeading}. This
1188 % should happen in the document class.
1189 %
1190 %
1191 % \begin{macro}{\ParseLaTeXeHeading}
1192 %
1193 % \cs{ParseLaTeXeHeading} arguments:
1194 % \begin{itemize}
1195 % \item[1:] instance name to use (of type \enquote{heading})
1196 % \item[2:] numbered? boolean
1197 % \item[3:] shorttitle or key/val for layout adjustments and individual
1198 % title settings
1199 % \item[4:] main title
1200 % \end{itemize}
1201 %
1202 % This command handles the document input that may be hidden in the
1203 % optional argument, i.e., special data for toc, running (header),
1204 % bookmark, label, and for more specialized headings subtitle and
1205 % quote. It also handles the legacy case that the optional argument is just
1206 % an alternate title to be used for toc, running, and bookmark
1207 % (through the key shorttitle to which it gets converted).
1208 %
1209 % \begin{macrocode}
1210 \cs_new_protected:Npn \ParseLaTeXeHeading #1 #2 #3 #4 {
1211 % \end{macrocode}
1212 %
1213 % \begin{macrocode}
1214 \@@_debug_typeout:n{==================================}
1215 \@@_debug_typeout:n{#1:~ \IfBooleanT{#2}{*}
1216 \IfValueT{#3}{[\exp_not:n{#3}]}
1217 {\exp_not:n{#4}}}
1218 % \end{macrocode}
1219 % We first check if the title argument contains a \cs{label}
1220 % command. If yes, we remove it and add it to \cs{l_@@_label_tl}
1221 % (and if more than one all of them) and the rest of the main title in
1222 % \cs{l_@@_title_tl} for later use. If there was no \cs{label}
1223 % command then \cs{l_@@_title_tl} is set to \texttt{\#4}.
1224 % \begin{macrocode}
1225 \@@_find_label:w #4 \label\q_no_value \@@_find_label:w
1226 % \end{macrocode}
1227 % By default toc, running, and bookmark use the data provided by
1228 % the main title. However, if the second argument is true (i.e., a
1229 % star was given) they are all suppressed (because this is the \LaTeXe{} logic).
1230 % \begin{macrocode}
1231 \IfBooleanTF{#2}
1232 {
1233 \tl_clear:N \l_@@_toc_tl
1234 \tl_clear:N \l_@@_running_tl
1235 \tl_clear:N \l_@@_bookmark_tl
1236 \bool_set_true:N \l_@@_unnumbered_bool
1237 }
1238 {
1239 \tl_set_eq:NN \l_@@_toc_tl \l_@@_title_tl
1240 \tl_set_eq:NN \l_@@_running_tl \l_@@_title_tl
1241 \tl_set_eq:NN \l_@@_bookmark_tl \l_@@_title_tl
1242 \bool_set_false:N \l_@@_unnumbered_bool
1243 }
1244 % \end{macrocode}
1245 % The nameref always starts out matching the main title.
1246 % \begin{macrocode}
1247 \tl_set_eq:NN \l_@@_nameref_tl \l_@@_title_tl
1248 % \end{macrocode}
1249 % Normally we don't have a subtitle or quote unless they are
1250 % explicitly given through keys, so we start with \cs{c_novalue_tl}
1251 % \begin{macrocode}
1252 \tl_set_eq:NN \l_@@_subtitle_tl \c_novalue_tl
1253 \tl_set_eq:NN \l_@@_quote_tl \c_novalue_tl
1254 % \end{macrocode}
1255 % If the optional argument is empty we set the
1256 % \cs{l_@@_instance_keys_tl} to empty, otherwise process the
1257 % key/val list setting the keys defined in the module \enquote{head}. This
1258 % overwrites the defaults specified above if keys like \enquote{toc} are in
1259 % the list. All the key/vals unknown by that module are put into
1260 % \cs{l_@@_instance_keys_tl} which may or may not make this token
1261 % list non-empty.
1262 %
1263 % If the user has a \key{label} key and also a \cs{label} in the
1264 % main title argument then the latter is ignored (no error checking
1265 % for that). We could alternatively set all labels we find,
1266 % perhaps that is the better approach?
1267 % \begin{macrocode}
1268 \IfNoValueTF { #3 }
1269 { \tl_clear:N \l_@@_instance_keys_tl }
1270 { \keys_set_known:nnN {heading} { #3 } \l_@@_instance_keys_tl }
1271 % \end{macrocode}
1272 %
1273 % Finally we call the heading instance using the collected
1274 % mandatory arguments. To simplify downstream processing we pass
1275 % the values not the container tokenlists resulting from the above
1276 % processing.\footnote {I think this is a common requirement and
1277 % perhaps \cs{UseInstance} should really o-expand all arguments it
1278 % receives.}
1279 % \begin{macrocode}
1280 \@@_debug_typeout:n{use~ 'heading'~ instance:~ #1 }
1281 \use:e {
1282 \exp_not:N \UseInstance{heading}{#1}
1283 { \exp_not:o \l_@@_instance_keys_tl }
1284 { \bool_if:NTF \l_@@_unnumbered_bool \BooleanTrue \BooleanFalse }
1285 { \exp_not:o \l_@@_title_tl}
1286 { \exp_not:o \l_@@_toc_tl }
1287 { \exp_not:o \l_@@_running_tl }
1288 { \exp_not:o \l_@@_bookmark_tl }
1289 { \exp_not:o \l_@@_nameref_tl }
1290 { \exp_not:o \l_@@_label_tl }
1291 { { \exp_not:o \l_@@_subtitle_tl }
1292 { \exp_not:o \l_@@_quote_tl } }
1293 }
1294 }
1295 % \end{macrocode}
1296 %
1297 % Syntax keys that can appear in the optional argument of headings
1298 % parsed by \cs{ParseLaTeXeHeading}. All other keys used there are
1299 % passed to the heading instance to overwrite instance setting.
1300 %
1301 % \begin{macrocode}
1302 \keys_define:nn {heading} {
1303 , shorttitle .meta:n =
1304 {toc = {#1} , running = {#1} , bookmark = {#1} , nameref= {#1} }
1305 , bookmark .tl_set:N = \l_@@_bookmark_tl
1306 , running .tl_set:N = \l_@@_running_tl
1307 , toc .tl_set:N = \l_@@_toc_tl
1308 , nameref .tl_set:N = \l_@@_nameref_tl
1309 %
1310 , numbered .bool_set_inverse:N = \l_@@_unnumbered_bool
1311 , numbered .default:n = true
1312 , unnumbered .bool_set:N = \l_@@_unnumbered_bool
1313 , unnumbered .default:n = true
1314 %
1315 , subtitle .tl_set:N = \l_@@_subtitle_tl
1316 , quote .tl_set:N = \l_@@_quote_tl
1317 % \end{macrocode}
1318 % We collect labels together with their \cs{label} command in case
1319 % there is more than one. This way we can later simply execute
1320 % \cs{l_@@_label_tl} without any status checks.
1321 % \begin{macrocode}
1322 , label .code:n = \tl_put_right:Nn \l_@@_label_tl { \label{#1} }
1323 }
1324 % \end{macrocode}
1325 %
1326 %
1327 % \end{macro}
1328 %
1329 % \begin{macro}{\@@_find_label:w}
1330 % A simple-minded check for an existing \cs{label} command in the
1331 % main title (could be done better I guess). We add
1332 % \cs{label}\cs{q_no_value} at the end of the argument, so that we
1333 % can be sure to find something.
1334 %
1335 % As currently implemented the spaces on both sides of a \cs{label}
1336 % command survive if it is removed. This may be an issue in which
1337 % case this may need some adjustments.
1338 % \begin{macrocode}
1339 \cs_new:Npn \@@_find_label:w #1 \label #2#3 \@@_find_label:w {
1340 % \end{macrocode}
1341 % If the token after \cs{label} is \cs{q_no_value} then the title
1342 % had no label and we set the two variables accordingly.
1343 % \begin{macrocode}
1344 \quark_if_no_value:nTF {#2}
1345 {
1346 \@@_debug_typeout:n{---~no~label }
1347 \tl_clear:N \l_@@_label_tl
1348 \tl_set:Nn\l_@@_title_tl {#1#3}
1349 }
1350 % \end{macrocode}
1351 % Otherwise, there was a real \cs{label} command and \texttt{\#2}
1352 % holds the label string.
1353 % \begin{macrocode}
1354 {
1355 \@@_debug_typeout:n{---~label~found~#2}
1356 \tl_set:Nn\l_@@_label_tl { \label{#2} }
1357 % \end{macrocode}
1358 % To construct the value for \cs{l_@@_title_tl} we have to get rid
1359 % of the two tokens we added at its end, this is done with
1360 % \cs{@@_find_label_aux:w}.
1361 % \begin{macrocode}
1362 \tl_set:Nn\l_@@_title_tl {#1}
1363 \@@_find_label_aux:w #3\@@_find_label_aux:w
1364 }
1365 \@@_debug_typeout:n{---~return:~ '\exp_not:o \l_@@_title_tl' }
1366 }
1367 % \end{macrocode}
1368 % There is at least one more \cs{label} now and the token after it
1369 % should be our \cs{q_no_value}. If not then the title argument had
1370 % at least 2 labels and we collect the newly found one and recurse.
1371 % \begin{macrocode}
1372 \cs_new:Npn \@@_find_label_aux:w #1 \label #2#3 \@@_find_label_aux:w {
1373 % \end{macrocode}
1374 % The \texttt{\#1} may not be everything we have to pick up but we
1375 % know for sure that it belongs to the title.
1376 % \begin{macrocode}
1377 \tl_put_right:Nn \l_@@_title_tl { #1 }
1378 % \end{macrocode}
1379 % Now let's see if we are at the end of the argument. If not pick
1380 % up the newly found \cs{label} and recurse on the remaining material.
1381 % \begin{macrocode}
1382 \quark_if_no_value:nF {#2}
1383 {
1384 \@@_debug_typeout:n{---~ extra~ label~ '#2'~ found }
1385 \tl_put_right:Nn \l_@@_label_tl { \label{#2} }
1386 \@@_find_label_aux:w #3\@@_find_label_aux:w
1387 }
1388 }
1389 % \end{macrocode}
1390 % \end{macro}
1391 %
1392 % \subsection{Templates}
1393 %
1394 % \subsubsection{Template types}
1395 %
1396 % \begin{templatetype}{heading}
1397 % Templates of type \texttt{heading} are used to produce headings. The
1398 % positional arguments are:
1399 % \begin{verbatim}
1400 % 1: key/val list
1401 % 2: unnumbered?
1402 % 3: title
1403 % 4: toc
1404 % 5: running
1405 % 6: bookmark
1406 % 7: nameref
1407 % 8: label(s)
1408 % 9: { subtitle } { quote }
1409 % \end{verbatim}
1410 % \begin{macrocode}
1411 \NewTemplateType{heading}{9}
1412 % \end{macrocode}
1413 % \end{templatetype}
1414 %
1415 % \begin{templatetype}{headformat}
1416 % Templates of type \texttt{headformat} are used to produce heading
1417 % layout from the formatted heading number (if any), the heading title,
1418 % subtitle and quotation. The
1419 % positional arguments are:
1420 % \begin{verbatim}
1421 % 1: key/val list for document-level customizations
1422 % 2: formatted heading number
1423 % 3: title
1424 % 4: subtitle
1425 % 5: quote
1426 % \end{verbatim}
1427 % \begin{macrocode}
1428 \NewTemplateType{headformat}{5}
1429 % \end{macrocode}
1430 % \end{templatetype}
1431 %
1432 % \subsubsection{heading templates interfaces}
1433 %
1434 % We have two heading templates: display and runin.
1435 %
1436 % \begin{template}{heading display}
1437 % The \texttt{display} template produces a display heading, i.e.,
1438 % one that has vertical space before and after it.
1439 % \begin{macrocode}
1440 \DeclareTemplateInterface{heading}{display}{9}
1441 {
1442 , name : tokenlist
1443 , parent-name : tokenlist
1444 , reset-counter : tokenlist
1445 , level : integer = 0
1446 % \end{macrocode}
1447 % The \key{placement} key sets default values for the keys
1448 % \key{start-code} and \key{final-code}.
1449 % \begin{macrocode}
1450 , placement : choice {page , top , normal } = normal
1451 , mark-cmd : function(1) =
1452 %
1453 % many more keys for layout settings are missing for now
1454 , para-indent : choice { true , false } = false
1455 % \end{macrocode}
1456 % We use \cs{c_max_int} as a fake penalty to indicate that no
1457 % penalty was given in a key.
1458 % \begin{macrocode}
1459 , before-vspace : skip = 0pt
1460 , penalty : integer = \c_max_int
1461 , after-penalty-vspace : skip = 0pt
1462 , after-vspace : skip = 0pt
1463 % \end{macrocode}
1464 % The next two keys are only there to overwrite the \key{placement}
1465 % settings with explicit code. Thus, their default value is set up
1466 % by the \key{placement} key (which has a default).
1467 % \begin{macrocode}
1468 , start-code : tokenlist = % no default values!
1469 , final-code : tokenlist = % no default values!
1470 % \end{macrocode}
1471 %
1472 % \begin{macrocode}
1473 , heading-decls : tokenlist = \normalfont
1474 , number-decls : tokenlist =
1475 , title-decls : tokenlist =
1476 , subtitle-decls : tokenlist =
1477 , quote-decls : tokenlist =
1478 % \end{macrocode}
1479 %
1480 % \begin{macrocode}
1481 , headformat-instance : tokenlist = hang
1482 , number-format : function(1) = \theheading
1483 , contents-extra : tokenlist =
1484 }
1485 % \end{macrocode}
1486 % \end{template}
1487 %
1488 %
1489 % \begin{template}{heading runin}
1490 % This template is similar to the \texttt{display} template but
1491 % implements a runin heading, i.e., one where the paragraph text
1492 % continues on the same line.
1493 %
1494 % Unfortunately, we can't really use \cs{DeclareTemplateCopy} to
1495 % set it up because with \cs{EditTemplateDefaults} we are not able
1496 % to alter the setup for the \key{placement} and \key{para-indent} keys
1497 % as that involves changing the implementation code. Thus with
1498 % \cs{DeclareTemplateCopy} we can copy the interface setup but the
1499 % code setup still needs to be done using \cs{DeclareTemplateCode}.
1500 %
1501 % \begin{macrocode}
1502 \DeclareTemplateCopy{heading}{runin}{display}
1503 % \end{macrocode}
1504 % \end{template}
1505 %
1506 %
1507 % \subsubsection{headformat templates interfaces}
1508 % We have three template: display, hang and runin.
1509 %
1510 % \begin{template}{headformat display}
1511 % The \texttt{display} template produces
1512 % \begin{macrocode}
1513 \DeclareTemplateInterface{headformat}{display}{5}
1514 {
1515 , heading-indent : length = 0pt
1516 , title-format : function(1) = #1
1517 , number-title-sep : tokenlist = 20pt
1518 }
1519 % \end{macrocode}
1520 % \end{template}
1521 %
1522 % \begin{template}{headformat hang}
1523 % The \texttt{hang} template produces
1524 % \begin{macrocode}
1525 \DeclareTemplateInterface{headformat}{hang}{5}
1526 {
1527 , heading-indent : length = 0pt
1528 , title-format : function(1) = #1
1529 , number-title-sep : tokenlist = 1em
1530 }
1531 % \end{macrocode}
1532 % \end{template}
1533 %
1534 %
1535 % \begin{template}{headformat runin}
1536 % The \texttt{runin} template produces
1537 % \begin{macrocode}
1538 \DeclareTemplateInterface{headformat}{runin}{5}
1539 {
1540 , heading-indent : length = 0pt
1541 , title-format : function(1) = #1
1542 , number-title-sep : tokenlist = 1em
1543 }
1544 % \end{macrocode}
1545 % \end{template}
1546 %
1547 % \subsubsection{heading templates code}
1548 %
1549 % \begin{template}{heading display}
1550 %
1551 % \begin{macrocode}
1552 \DeclareTemplateCode{heading}{display}{9}
1553 {
1554 , name = \l_@@_name_tl
1555 , level = \l_@@_level_int
1556 % next two not yet used
1557 , parent-name = \l_@@_pname_tl
1558 , reset-counter = \l_@@_reset_cnt_tl
1559 % \end{macrocode}
1560 % The \key{placement} key determines whether the heading can appear
1561 % anywhere (\texttt{normal}), automatically starts a new page or
1562 % column (\texttt{top}), or is on a page of its own (\texttt{page}.
1563 %
1564 % It is implemented by setting \cs{l_@@_start_code_tl} and
1565 % \cs{l_@@_final_code_tl}. These settings can be fine-tuned or
1566 % overwritten with the keys \key{start-code} and
1567 % \key{final-code}, if necessary.
1568 % \begin{macrocode}
1569 , placement = {
1570 ,page = \@@_debug_typeout:n{ A~ page~ heading }
1571 \tl_set:Nn \l_@@_placement_tl { page }
1572 ,top = \@@_debug_typeout:n{ A~ top~ heading }
1573 \tl_set:Nn \l_@@_placement_tl { top }
1574 ,normal = \@@_debug_typeout:n{ A~ normal~ heading }
1575 \tl_set:Nn \l_@@_placement_tl { normal }
1576 }
1577 , mark-cmd = \@@_mark_cmd:n
1578 % \end{macrocode}
1579 % For now we make use of the legacy coding for indentation of
1580 % the following paragraph.
1581 % \begin{macrocode}
1582 , para-indent = {
1583 ,true = \@afterindenttrue
1584 ,false = \@afterindentfalse
1585 }
1586 , before-vspace = \l_@@_before_skip
1587 , penalty = \l_@@_penalty_int
1588 , after-penalty-vspace = \l_@@_after_penalty_skip
1589 , after-vspace = \l_@@_after_skip
1590 , start-code = \l_@@_start_code_tl
1591 , final-code = \l_@@_final_code_tl
1592 % \end{macrocode}
1593 %
1594 % \begin{macrocode}
1595 , headformat-instance = \l_@@_headformat_instance_tl
1596 % \end{macrocode}
1597 %
1598 % \begin{macrocode}
1599 , heading-decls = \l_@@_heading_decls_tl
1600 , number-decls = \l_@@_number_decls_tl
1601 , title-decls = \l_@@_title_decls_tl
1602 , subtitle-decls = \l_@@_subtitle_decls_tl
1603 , quote-decls = \l_@@_quote_decls_tl
1604 , number-format = \@@_number_format:n
1605 , contents-extra = \l_@@_contents_extra_tl
1606 }
1607 % \end{macrocode}
1608 %
1609 % \begin{macrocode}
1610 {
1611 % \end{macrocode}
1612 %
1613 % \begin{macrocode}
1614 \tl_set_eq:Nc \theheading { the \l_@@_name_tl }
1615 % \end{macrocode}
1616 % We store the value of \texttt{secnumdepth} so that we can change it locally.
1617 % \begin{macrocode}
1618 \tl_set:Ne\l_@@_saved_secnumdepth_tl{\int_use:N\c@secnumdepth}
1619 % \end{macrocode}
1620 %
1621 % \begin{macrocode}
1622 \@@_show_arguments:nnnnnnnnn
1623 {#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}
1624 % \end{macrocode}
1625 % First evaluate any key setting done by the user in the
1626 % optional first argument.
1627 % \fmi{It would be nice if there is a variant of
1628 % \cs{SetTemplateKey} in which the template type and name are
1629 % implicit, e.g., \cs{SetCurrentTemplateKeys} because this is
1630 % usually what I need.}
1631 % \begin{macrocode}
1632 \tl_if_empty:oF {#1} { \SetTemplateKeys{heading}{display}{#1} }
1633 % \end{macrocode}
1634 % Based on the \key{placement} key we set up \cs{l_@@_start_code_tl}
1635 % and \cs{l_@@_final_code_tl}. These two variables can also be set
1636 % with the keys \key{start-code} and \key{final-code} in which case
1637 % we don't alter the definition.
1638 % \begin{macrocode}
1639 \tl_if_empty:oT \l_@@_start_code_tl
1640 { \str_case:VnF \l_@@_placement_tl
1641 {
1642 { page }
1643 { \tl_set:Nn \l_@@_start_code_tl
1644 % \end{macrocode}
1645 % Straight from the placement definition of \cs{part}.\fmi{Clearly not
1646 % \cs{@tempswa} and the page styles should be adjustable.}
1647 % \begin{macrocode}
1648 {
1649 \if@openright
1650 \cleardoublepage
1651 \else
1652 \clearpage
1653 \fi
1654 \thispagestyle{plain}%
1655 \if@twocolumn
1656 \onecolumn
1657 \@tempswatrue
1658 \else
1659 \@tempswafalse
1660 \fi
1661 \null\vfil
1662 }
1663 }
1664 { top }
1665 { \tl_set:Nn \l_@@_start_code_tl
1666 {
1667 \if@openright\cleardoublepage\else\clearpage\fi
1668 \thispagestyle{plain}%
1669 \global\@topnum\z@
1670 }
1671 }
1672 }
1673 { \tl_clear:N \l_@@_start_code_tl }
1674 }
1675 %
1676 \tl_if_empty:oT \l_@@_final_code_tl
1677 { \str_case:VnF \l_@@_placement_tl
1678 {
1679 { page }
1680 { \tl_set:Nn \l_@@_final_code_tl
1681 {
1682 \vfil\newpage
1683 \if@twoside
1684 \if@openright
1685 \null
1686 \thispagestyle{empty}%
1687 \newpage
1688 \fi
1689 \fi
1690 \if@tempswa
1691 \twocolumn
1692 \fi
1693 }
1694 }
1695 }
1696 { \tl_set:Nn \l_@@_final_code_tl { \@afterheading } }
1697 }
1698 % \end{macrocode}
1699 % Then we set up the penalty to use (might be given as a key value).
1700 % \begin{macrocode}
1701 \@@_determine_penalty:
1702 % \end{macrocode}
1703 %
1704 % \begin{macrocode}
1705 \@@_determine_number_typesetting:N #2
1706 % \end{macrocode}
1707 % Next comes the vertical spacing and penalty before the heading. This includes
1708 % running \cs{l_@@_start_code_tl} if it contains any code, e.g., to
1709 % start a new page.
1710 % \begin{macrocode}
1711 \@@_vertical_before_spacing:
1712 % \end{macrocode}
1713 % We are in vmode now and here is the point where we (with tagging) can close a previous Sect
1714 % structure and open the new one.
1715 % \begin{macrocode}
1716 \UseTaggingSocket{sec/end}{\int_use:N\l_@@_level_int}
1717 \UseTaggingSocket{sec/begin}
1718 {{\int_use:N\l_@@_level_int}{tag=\UseStructureName{sec/\int_use:N\l_@@_level_int}}}
1719 % \end{macrocode}
1720 % Up to this point everything is identical for both display and
1721 % runin headings. But from now on they have their own code.
1722 % We have dealt with argument \#1 and \#2 so now we can unbundle
1723 % argument \#9 so that it is easier to process downstream
1724 % \begin{macrocode}
1725 \@@_debug_typeout:n{use~ 'headformat'~instance:~
1726 \l_@@_headformat_instance_tl }
1727 \use:e {
1728 \UseInstance{headformat} { \l_@@_headformat_instance_tl }
1729 { \exp_not:o \UnusedTemplateKeys }
1730 { \exp_not:o { \l_@@_typeset_number_tl } }
1731 { \exp_not:n { #3 } }
1732 { \exp_not:o { \use_i:nn #9 } }
1733 { \exp_not:o { \use_ii:nn #9 } }
1734 }
1735 %
1736 % --- handle marks, toc-entry, bookmark, nameref, and label
1737 %
1738 \@@_handle_marks_etc:nnnnn {#4}{#5}{#6}{#7}{#8}
1739 %
1740 % --- post-heading handling (vertical)
1741 % \end{macrocode}
1742 % Restore \texttt{secnumdepth}
1743 % \begin{macrocode}
1744 \int_gset:Nn\c@secnumdepth{\l_@@_saved_secnumdepth_tl}
1745 \par \nobreak
1746 \skip_vertical:N \l_@@_after_skip
1747 % --- prepare next paragraph (defaults to \cs{@afterheading}
1748 %
1749 \l_@@_final_code_tl
1750 %
1751 \ignorespaces
1752 }
1753 % \end{macrocode}
1754 % \end{template}
1755 %
1756 %
1757 %
1758 % \begin{macro}{}
1759 %
1760 % \begin{macrocode}
1761 \AddToHook{begindocument}{
1762 \ifcsname if@openright\endcsname
1763 \else
1764 % \end{macrocode}
1765 % Need to hide this a little if it is in fact already defined!
1766 % \begin{macrocode}
1767 \expandafter\newif\csname if@openright\endcsname
1768 \fi
1769 }
1770 % \end{macrocode}
1771 % \end{macro}
1772 %
1773 % \begin{template}{heading runin}
1774 % The \texttt{runin} template is very similar to the
1775 % \texttt{display} one.
1776 % \begin{macrocode}
1777 \DeclareTemplateCode{heading}{runin}{9}
1778 {
1779 , name = \l_@@_name_tl
1780 , level = \l_@@_level_int
1781 % next two not yet used
1782 , parent-name = \l_@@_pname_tl
1783 , reset-counter = \l_@@_reset_cnt_tl
1784 % \end{macrocode}
1785 %
1786 % It wouldn't make sense to have page placement with a runin heading since
1787 % there would be nothing to run into.
1788 % \begin{macrocode}
1789 , placement = {
1790 page = \typeout{ ^^JA~ runin~ page~ placement~ heading~makes~no~sense
1791 (top~used)}
1792 \tl_set:Nn \l_@@_placement_tl { top }
1793 ,top = \@@_debug_typeout:n{ A~ top~ heading }
1794 \tl_set:Nn \l_@@_placement_tl { top }
1795 ,normal = \@@_debug_typeout:n{ A~ normal~ heading }
1796 \tl_set:Nn \l_@@_placement_tl { normal }
1797 }
1798 , mark-cmd = \@@_mark_cmd:n
1799 % \end{macrocode}
1800 % In a runin heading you can't set up paragraph indentation of the
1801 % following paragraph (since that one is \enquote{run in}. But we
1802 % accept the key and just spit out a warning.
1803 % \ufi{this types out messages for all run-in headers! Therefore disabled for now}
1804 % \begin{macrocode}
1805 , para-indent = {
1806 ,true = %\typeout{para-indent~ setting~ ignored}
1807 ,false = %\typeout{para-indent~ setting~ ignored}
1808 }
1809 , before-vspace = \l_@@_before_skip
1810 , penalty = \l_@@_penalty_int
1811 , after-penalty-vspace = \l_@@_after_penalty_skip
1812 , after-vspace = \l_@@_after_skip
1813 , start-code = \l_@@_start_code_tl
1814 , final-code = \l_@@_final_code_tl
1815 % \end{macrocode}
1816 %
1817 % \begin{macrocode}
1818 , headformat-instance = \l_@@_headformat_instance_tl
1819 % \end{macrocode}
1820 %
1821 % \begin{macrocode}
1822 , heading-decls = \l_@@_heading_decls_tl
1823 , number-decls = \l_@@_number_decls_tl
1824 , title-decls = \l_@@_title_decls_tl
1825 , subtitle-decls = \l_@@_subtitle_decls_tl
1826 , quote-decls = \l_@@_quote_decls_tl
1827 % \end{macrocode}
1828 %
1829 % \begin{macrocode}
1830 , number-format = \@@_number_format:n
1831 , contents-extra = \l_@@_contents_extra_tl
1832 }
1833 {
1834 \@@_show_arguments:nnnnnnnnn
1835 {#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}
1836 % \end{macrocode}
1837 % store the value of \texttt{secnumdepth} so that we can change it locally.
1838 % \begin{macrocode}
1839 \tl_set:Ne\l_@@_saved_secnumdepth_tl{\int_use:N\c@secnumdepth}
1840 % \end{macrocode}
1841 % define \cs{theheading}
1842 % \begin{macrocode}
1843 \tl_set_eq:Nc \theheading { the \l_@@_name_tl }
1844 \tl_if_empty:oF {#1} { \SetTemplateKeys{heading}{runin}{#1} }
1845 % \end{macrocode}
1846 %
1847 % \begin{macrocode}
1848 \tl_if_empty:oT \l_@@_start_code_tl
1849 {
1850 \str_case:Vn \l_@@_placement_tl
1851 {
1852 { top } { \tl_set:Nn \l_@@_start_code_tl {\clearpage } }
1853 }
1854 % \end{macrocode}
1855 % All other cases want an empty \cs{l_@@_start_code_tl} so nothing
1856 % to do.
1857 % \begin{macrocode}
1858 % { \tl_clear:N \l_@@_start_code_tl }
1859 }
1860 %
1861 % \end{macrocode}
1862 % Nothing at all to do (for now) for \cs{l_@@_final_code_tl}, it
1863 % should by default be empty in all heading placement. But maybe
1864 % we end up supporting further placements, so~\ldots
1865 % \begin{macrocode}
1866 % \tl_if_empty:oT \l_@@_final_code_tl
1867 % { \str_case:VnF \l_@@_placement_tl
1868 % {
1869 % { top } { \tl_clear:N \l_@@_final_code_tl }
1870 % }
1871 % { \tl_clear:N \l_@@_final_code_tl }
1872 % }
1873 % \end{macrocode}
1874 %
1875 % \begin{macrocode}
1876 \@@_determine_penalty:
1877 \@@_determine_number_typesetting:N #2
1878 \@@_vertical_before_spacing:
1879 % \end{macrocode}
1880 % We are in vmode now and here is the point where we (with tagging) can close a previous Sect
1881 % structure and open the new one. We also have to initialize the change in the paratagging here
1882 % as run-in titles typeset the heading in \cs{everypar}.
1883 % \begin{macrocode}
1884 \UseTaggingSocket{sec/end}{\int_use:N\l_@@_level_int}
1885 \UseTaggingSocket{sec/begin}
1886 {{\int_use:N\l_@@_level_int}{tag=\UseStructureName{sec/\int_use:N\l_@@_level_int}}}
1887 \UseTaggingSocket{sec/title/init}{\int_use:N\l_@@_level_int}
1888 \def \@svsechd {
1889 \@@_debug_typeout:n{use~ 'headformat'~instance:~
1890 \l_@@_headformat_instance_tl }
1891 \use:e {
1892 \UseInstance{headformat} { \l_@@_headformat_instance_tl }
1893 { \exp_not:o \UnusedTemplateKeys }
1894 { \exp_not:o { \l_@@_typeset_number_tl } }
1895 { \exp_not:n { #3 } }
1896 { \exp_not:o { \use_i:nn #9 } }
1897 { \exp_not:o { \use_ii:nn #9 } }
1898 }
1899 \@@_handle_marks_etc:nnnnn {#4}{#5}{#6}{#7}{#8}
1900 % \end{macrocode}
1901 % Restore \texttt{secnumdepth}
1902 % \begin{macrocode}
1903 \int_gset:Nn\c@secnumdepth{\l_@@_saved_secnumdepth_tl}
1904 }
1905 %
1906 \@nobreakfalse
1907 \global\@noskipsectrue
1908 \everypar{%
1909 \if@noskipsec
1910 \global\@noskipsecfalse
1911 {\setbox\z@\lastbox}
1912 \clubpenalty\@M
1913 %FMi group in headformat
1914 % \begingroup
1915 \@svsechd
1916 % \endgroup
1917 \unskip
1918 % \end{macrocode}
1919 % This tagging socket starts the \enquote{paragraph} after the run-in heading
1920 % \begin{macrocode}
1921 \UseTaggingSocket{sec/title/split}
1922 \skip_horizontal:N \l_@@_after_skip
1923 \else
1924 \clubpenalty \@clubpenalty
1925 \everypar{}%
1926 \fi
1927 }
1928 % \end{macrocode}
1929 % --- prepare next paragraph (does nothing by default)
1930 % \begin{macrocode}
1931 \l_@@_final_code_tl
1932 %
1933 \ignorespaces
1934 }
1935 % \end{macrocode}
1936 % \end{template}
1937 %
1938 % \subsubsection{headformat templates code}
1939 %
1940 % \begin{template}{headformat display}
1941 % This template creates a headformat where the number is on a line on its own.
1942 % \begin{macrocode}
1943 \DeclareTemplateCode{headformat}{display}{5} % args: key,number,title,subtitle,quotation
1944 {
1945 , heading-indent = \l_@@_heading_indent_dim
1946 , title-format = \@@_title_format:n
1947 , number-title-sep = \l_@@_number_title_sep_tl
1948 }
1949 {
1950 \@@_show_arguments:nnnnn {#1}{#2}{#3}{#4}{#5}
1951 % \end{macrocode}
1952 % First evaluate any key setting done by the user (normally
1953 % supplied from the main heading instance.
1954 % \begin{macrocode}
1955 \tl_if_empty:oF {#1} { \SetTemplateKeys{headformat}{display}{#1} }
1956 % \end{macrocode}
1957 %
1958 % \begin{macrocode}
1959 \group_begin:
1960 \UseTaggingSocket{sec/title/begin}{{\int_use:N\l_@@_level_int}{#3}}
1961 % \end{macrocode}
1962 %
1963 % \begin{macrocode}
1964 \normalfont \normalcolor
1965 \interlinepenalty \@M
1966 \l_@@_heading_decls_tl{}
1967 % \end{macrocode}
1968 % If there is a number and so a prefix, the link target should be before the number.
1969 % We use for now the same place in the unnumbered case.
1970 % TODO: If we put the target here we do not know the height of the line and the
1971 % target is perhaps not high enough. And for unnumbered chapter it is perhaps too high.
1972 % Check!
1973 % \begin{macrocode}
1974 \bool_if:NTF \l_@@_unnumbered_bool
1975 {
1976 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
1977 {
1978 \skip_horizontal:N \l_@@_heading_indent_dim
1979 \MakeLinkTarget[\l_@@_name_tl]{}
1980 }
1981 {
1982 \MakeLinkTarget[\l_@@_name_tl]{}\skip_horizontal:N \l_@@_heading_indent_dim
1983 }
1984 }
1985 {
1986 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
1987 {
1988 \skip_horizontal:N \l_@@_heading_indent_dim
1989 \MakeLinkTarget{\l_@@_name_tl}
1990 }
1991 {
1992 \MakeLinkTarget{\l_@@_name_tl}\skip_horizontal:N \l_@@_heading_indent_dim
1993 }
1994 \l_@@_number_decls_tl
1995 #2
1996 \par\nobreak
1997 \skip_vertical:n { \l_@@_number_title_sep_tl }
1998 }
1999 \l_@@_title_decls_tl
2000 % \end{macrocode}
2001 % \fmi{probably better to use a format:n and drop these two code
2002 % variables even though they are used by titlesec}
2003 % \begin{macrocode}
2004 \@@_title_format:n
2005 {#3}
2006 \par
2007 \UseTaggingSocket{sec/title/end}
2008 \group_end:
2009 }
2010 % \end{macrocode}
2011 % \end{template}
2012 %
2013 % \begin{template}{headformat hang}
2014 % This template creates a heading with a hanging number.
2015 % \begin{macrocode}
2016 \DeclareTemplateCode{headformat}{hang}{5}
2017 {
2018 , heading-indent = \l_@@_heading_indent_dim
2019 , title-format = \@@_title_format:n
2020 , number-title-sep = \l_@@_number_title_sep_tl
2021 }
2022 {
2023 \@@_show_arguments:nnnnn {#1}{#2}{#3}{#4}{#5}
2024 % \end{macrocode}
2025 % First evaluate any key setting done by the user (normally
2026 % supplied from the main heading isntance.
2027 % \begin{macrocode}
2028 \tl_if_empty:oF {#1} { \SetTemplateKeys{headformat}{hang}{#1} }
2029 % \end{macrocode}
2030 %
2031 % \begin{macrocode}
2032 \group_begin:
2033 \UseTaggingSocket{sec/title/init}{\int_use:N\l_@@_level_int}
2034 % \end{macrocode}
2035 %
2036 % \begin{macrocode}
2037 \normalfont \normalcolor
2038 \interlinepenalty \@M
2039 \l_@@_heading_decls_tl{}
2040 % \end{macrocode}
2041 % The link target should be at the left text margin, or, if the section
2042 % is moved into the margin, at the left of the number.
2043 % \begin{macrocode}
2044 \bool_if:NTF \l_@@_unnumbered_bool
2045 {
2046 \tl_set:Nn\l_@@_tmpa_tl
2047 {
2048 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
2049 {
2050 \skip_horizontal:N \l_@@_heading_indent_dim \MakeLinkTarget[\l_@@_name_tl]{}
2051 }
2052 {
2053 \MakeLinkTarget[\l_@@_name_tl]{}
2054 \skip_horizontal:N \l_@@_heading_indent_dim
2055 }
2056 }
2057 }
2058 {
2059 \tl_set:Nn \l_@@_tmpa_tl
2060 {
2061 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
2062 {
2063 \skip_horizontal:N \l_@@_heading_indent_dim \MakeLinkTarget{\l_@@_name_tl}
2064 }
2065 {
2066 \MakeLinkTarget{\l_@@_name_tl}\skip_horizontal:N \l_@@_heading_indent_dim
2067 }
2068 { \l_@@_number_decls_tl #2 }
2069 \skip_horizontal:n { \l_@@_number_title_sep_tl }
2070 }
2071 }
2072 \UseTaggingSocket{sec/title/hang}
2073 {{\int_use:N\l_@@_level_int}\l_@@_unnumbered_bool{\l_@@_tmpa_tl}{#3}}
2074 {
2075 \@hangfrom
2076 {
2077 \l_@@_tmpa_tl
2078 }
2079 }
2080 \l_@@_title_decls_tl
2081 % \end{macrocode}
2082 % \fmi{probably better to use a format:n and drop these two code
2083 % variables even though they are used by titlesec}
2084 % \begin{macrocode}
2085 \@@_title_format:n
2086 {#3}
2087 \par
2088 \group_end:
2089 }
2090 % \end{macrocode}
2091 % \end{template}
2092 %
2093 %
2094 % \begin{template}{headformat runin}
2095 % This template creates a heading with a run-in title.
2096 % Arguments are key-value, number, title, subtitle, quotation.
2097 % \begin{macrocode}
2098 \DeclareTemplateCode{headformat}{runin}{5}
2099 {
2100 , heading-indent = \l_@@_heading_indent_dim
2101 , title-format = \@@_title_format:n
2102 , number-title-sep = \l_@@_number_title_sep_tl
2103 }
2104 {
2105 \@@_show_arguments:nnnnn {#1}{#2}{#3}{#4}{#5}
2106 % \end{macrocode}
2107 % First evaluate any key setting done by the user (normally
2108 % supplied from the main heading isntance.
2109 % \begin{macrocode}
2110 \tl_if_empty:oF {#1} { \SetTemplateKeys{headformat}{hang}{#1} }
2111 % \end{macrocode}
2112 %
2113 % \begin{macrocode}
2114 \group_begin:
2115 % \end{macrocode}
2116 %
2117 %
2118 % \begin{macrocode}
2119 \normalfont \normalcolor
2120 % \end{macrocode}
2121 % \fmi{Setting \cs{interlinepenalty} makes little sense I think
2122 % (but that's the way it was in \LaTeXe)}
2123 % \begin{macrocode}
2124 \interlinepenalty \@M
2125 \l_@@_heading_decls_tl{}
2126 % \end{macrocode}
2127 % We must avoid that the sep between number and title is used in the unnumbered case.
2128 % So we test with the boolean.
2129 % \begin{macrocode}
2130 \bool_if:NTF \l_@@_unnumbered_bool
2131 {
2132 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
2133 {
2134 \skip_horizontal:N \l_@@_heading_indent_dim
2135 \MakeLinkTarget[\l_@@_name_tl]{}
2136 }
2137 {
2138 \MakeLinkTarget[\l_@@_name_tl]{}\skip_horizontal:N \l_@@_heading_indent_dim
2139 }
2140 }
2141 {
2142 \dim_compare:nNnTF \l_@@_heading_indent_dim < \c_zero_skip
2143 {
2144 \skip_horizontal:N \l_@@_heading_indent_dim \MakeLinkTarget{\l_@@_name_tl}
2145 }
2146 {
2147 \MakeLinkTarget{\l_@@_name_tl}\skip_horizontal:N \l_@@_heading_indent_dim
2148 }
2149 {
2150 \l_@@_number_decls_tl
2151 \UseTaggingSocket{sec/title/number}{\int_use:N\l_@@_level_int}{#2}
2152 }
2153 \skip_horizontal:n { \l_@@_number_title_sep_tl }
2154 }
2155 \l_@@_title_decls_tl
2156 % \end{macrocode}
2157 %
2158 % \begin{macrocode}
2159 \@@_title_format:n
2160 {#3}
2161 \group_end:
2162 }
2163 % \end{macrocode}
2164 % \end{template}
2165 %
2166 % \subsubsection{Internal commands used by the template code}
2167 %
2168 % \begin{macro}{\@@_determine_penalty:}
2169 %
2170 % \begin{macrocode}
2171 \cs_new:Npn \@@_determine_penalty: {
2172 % \end{macrocode}
2173 % If a penalty was specified use it, otherwise use \cs{@secpenalty}.
2174 % \begin{macrocode}
2175 \int_compare:nNnT \l_@@_penalty_int = \c_max_int
2176 { \int_set:Nn \l_@@_penalty_int \@secpenalty }
2177 }
2178 % \end{macrocode}
2179 % \end{macro}
2180
2181 % \begin{macro}{\@@_determine_number_typesetting:N}
2182 %
2183 % \begin{macrocode}
2184 \cs_new:Npn \@@_determine_number_typesetting:N #1 {
2185 % \end{macrocode}
2186 %
2187 % Using or suppressing a heading number depends on the heading level
2188 % compared to the document value of \cs{c@secnumdepth}. If that
2189 % doesn't suppress the number then an explicit key or a star form
2190 % might still have suppressed it.
2191 % \begin{macrocode}
2192 \bool_set:Nn \l_@@_unnumbered_bool
2193 { \bool_lazy_or_p:nn
2194 { \int_compare_p:nNn \l_@@_level_int > \c@secnumdepth }
2195 { \bool_if_p:N #1 }
2196 }
2197 % \end{macrocode}
2198 % If we aren't producing a heading with a number we set
2199 % \cs{c@secnumdepth} to a low number (it is reset at the end of the template
2200 % code)
2201 % \begin{macrocode}
2202 \bool_if:NTF \l_@@_unnumbered_bool
2203 {
2204 \int_gset:Nn\c@secnumdepth{-99}
2205 % \end{macrocode}
2206 % and we set \cs{l_@@_typeset_number_tl} to do nothing.
2207 % \begin{macrocode}
2208 \tl_clear:N \l_@@_typeset_number_tl
2209 }
2210 % \end{macrocode}
2211 % Otherwise the heading counter is incremented and a formatted
2212 % version of the number plus any following (or preceding) space is
2213 % stored in \cs{l_@@_typeset_number_tl}.
2214 % We use the kernel version of \cs{refstepcounter} as anchors are handled elsewhere.
2215 % \begin{macrocode}
2216 {
2217 \@kernel@refstepcounter{ \l_@@_name_tl }
2218 \protected@edef \l_@@_typeset_number_tl
2219 {
2220 \@@_number_format:n { \l_@@_name_tl }
2221 }
2222 }
2223 }
2224 % \end{macrocode}
2225 % \end{macro}
2226
2227
2228
2229 % \begin{macro}{\@@_vertical_before_spacing:}
2230 %
2231 % \begin{macrocode}
2232 \cs_new:Npn \@@_vertical_before_spacing: {
2233 \tl_if_blank:VTF \l_@@_start_code_tl
2234 {
2235 \if@noskipsec \leavevmode \fi
2236 \par
2237 \if@nobreak
2238 \everypar{}
2239 \else
2240 \addpenalty \l_@@_penalty_int
2241 \addvspace \l_@@_before_skip
2242 % \end{macrocode}
2243 % The \cs{vspace*} inserts a rule, we insert therefore the
2244 % skip only if it is different to zero.
2245 % \begin{macrocode}
2246 \dim_compare:nNnF{\l_@@_after_penalty_skip}={0pt}
2247 { \vspace* \l_@@_after_penalty_skip }
2248 \fi
2249 }
2250 {
2251 \l_@@_start_code_tl
2252 % \end{macrocode}
2253 % If \cs{l_@@_start_code_tl} holds code, we assume that it handles
2254 % pagination, e.g., a \cs{clearpage}, etc. We therefore only add
2255 % the skip that would follow the penalty.
2256 % \begin{macrocode}
2257 \dim_compare:nNnF{\l_@@_after_penalty_skip}={0pt}
2258 { \vspace* \l_@@_after_penalty_skip }
2259 }
2260 }
2261 % \end{macrocode}
2262 % \end{macro}
2263 %
2264 % \begin{macro}{\addcontentslinebookmark,\addcontentslinebookmarkOff,
2265 % \addcontentslinebookmarkOn}
2266 % We want to be able to control bookmarks independently from the toc entries,
2267 % and also want to disable a bookmark by setting it to empty. For this we need
2268 % some command to handle the bookmark command.
2269 % \begin{macrocode}
2270 \newcommand\addcontentslinebookmark[3]{}
2271 \newcommand\addcontentslinebookmarkOff{}
2272 \newcommand\addcontentslinebookmarkReset{}
2273 \providecommand\texorpdfstring[2]{#1}
2274 % \end{macrocode}
2275 % This can go once hyperref is updated (ufi,2026-04-21):
2276 % \begin{macrocode}
2277 \AddToHook{package/hyperref/after}
2278 {
2279 \RenewCommandCopy\addcontentslinebookmark\Hy@addcontentsline@addbookmark
2280 \renewcommand\addcontentslinebookmarkOff
2281 {
2282 \ifHy@bookmarks
2283 \let\addcontentslinebookmarkReset\Hy@bookmarkstrue
2284 \else
2285 \let\addcontentslinebookmarkReset\relax
2286 \fi
2287 \Hy@bookmarksfalse
2288 }
2289 }
2290 % \end{macrocode}
2291 % \end{macro}
2292 %
2293 % \begin{macro}{\@@_handle_marks_etc:nnnnn}
2294 % Arg 1: toc entry, arg 2: mark, arg 3: bookmark, arg 4: nameref, arg 5: label code
2295 % \begin{macrocode}
2296 \cs_new:Npn \@@_handle_marks_etc:nnnnn #1#2#3#4#5 {
2297 %
2298 \tl_set:Nn\@currentlabelname{#4}
2299 \IfBlankF {#2}
2300 { \@@_mark_cmd:n { #2 } }
2301 \IfBlankTF {#1}
2302 % \end{macrocode}
2303 % If the toc entry is empty the bookmarks must be set without \cs{addcontentsline}
2304 % \begin{macrocode}
2305 {
2306 \IfBlankF{#3}
2307 {
2308 \addcontentslinebookmark{toc}{\l__head_name_tl}
2309 {
2310 \bool_if:NF \l_@@_unnumbered_bool
2311 {
2312 \protect\numberline{ \use:c{ the \l_@@_name_tl } }
2313 }
2314 #3
2315 }
2316 }
2317 }
2318 {
2319 % \end{macrocode}
2320 % If the bookmark entry is empty we must disable the bookmarks locally
2321 % before the \cs{addcontentsline} and reenable afterwards. We do not
2322 % check if they are already disabled, perhaps later.
2323 % \begin{macrocode}
2324 \IfBlankT{#3}{\addcontentslinebookmarkOff}
2325 \addcontentsline{toc}{ \l_@@_name_tl }
2326 {
2327 \bool_if:NF \l_@@_unnumbered_bool
2328 {
2329 \protect\numberline{ \use:c{ the \l_@@_name_tl } }
2330 }
2331 % \end{macrocode}
2332 % We use \cs{texorpdfstring} to separate toc and bookmark text.
2333 % \begin{macrocode}
2334 \texorpdfstring{#1}{#3}
2335 }
2336 \addcontentslinebookmarkReset
2337 }
2338 % \end{macrocode}
2339 % Some headings (like \cs{chapter}) also want to write stuff to
2340 % other contents files like \texttt{.lot} or
2341 % \texttt{.lof}.\fmi{perhaps this should have a hook for packages}
2342 % \begin{macrocode}
2343 \l_@@_contents_extra_tl
2344 % \end{macrocode}
2345 % We can always run the label code (it might be empty)
2346 % \begin{macrocode}
2347 \@@_debug_typeout:n{--->~label(s):~ \exp_not:n{#5}}
2348 #5
2349 }
2350 % \end{macrocode}
2351 % \end{macro}
2352 %
2353 %
2354 %
2355 %
2356 % \subsection{Support for legacy classes and packages}
2357 %
2358 % If we define heading commands in the kernel (or in the tagging
2359 % code) using
2360 % \begin{verbatim}
2361 % \DeclareDocumentCommand \section {s ={shorttitle}o m}
2362 % { \ParseLaTeXeHeading {section} {#1} {#2} {#3} }
2363 % \end{verbatim}
2364 % then this gets overwritten by every class right now.
2365 %
2366 % If we redeclare them after the class was loaded then
2367 % we overwrite the layout of legacy classes (done, for example, with
2368 % \cs{@startsection}). New classes that use the above interface
2369 % would be fine though, as long as we have declared the instances
2370 % before the class is loaded.
2371 %
2372 % So to make legacy classes work with their existing layout, we
2373 % should not (ever) overwrite \cs{section} but let the class
2374 % definition call
2375 % \cs{@startsection} which would then set up suitable instances and
2376 % only after that call \cs{ParseLaTeXeHeading}.
2377 %
2378 % However, that would mean a \enquote{new} class like ltx-article
2379 % would need to add the above definition and declare or edit the
2380 % corresponding instances, instead of just declaring or adding the
2381 % instances.
2382 %
2383 % A perhaps better alternative could be to delay the definition of
2384 % \cs{section} and friends until after the class got loaded and
2385 % then take a peak at \cs{section} as defined by the class and if
2386 % it contains a \cs{@startsection} call, leave it alone and
2387 % otherwise overwrite it. And if the class hasn't defined
2388 % \cs{section} (because it is a new class) declare it. Of course
2389 % that means \cs{section} would then be available with every class
2390 % even if it was never meant to contain headings.
2391 %
2392 % But while writing this up, I start to think it is best if a new
2393 % class defines both the document interface (i.e., the above
2394 % command) as well as the layout (declare the instances) and the
2395 % kernel does neither. It has been this way before and it is
2396 % consistent, so why change.
2397 %
2398 % The situation with headings defined via \cs{secdef} is worse: we
2399 % don't have a nice handle as with \cs{@startsection} so there is
2400 % no real way other than through static analysis to set up such
2401 % a heading in the new template style. So all that is possible (I
2402 % think) is that after the class has been loaded, we look if the
2403 % usual candidates (\cs{part} and \cs{chapter}) have been defined
2404 % and overwrite them with a standard layout. That would make the
2405 % class tagging aware but, of course, would likely change the
2406 % layout.
2407 %
2408 % The current implementation
2409 % \begin{itemize}
2410 % \item provides instance for the heading commands of the standard classes;
2411 % \item declares the heading commands for the standard classes with
2412 % the new interfaces through class hooks;
2413 % \item other classes call the adapted \cs{@startsection}; other headings
2414 % command like \cs{part} or \cs{chapter} are not handled and must be setup
2415 % individually.
2416 % \end{itemize}
2417 %
2418 % \subsubsection{Instances (sample/default definitions)}
2419 %
2420 % \begin{instance}{heading part}
2421 % An instance suitable for \cls{book} and \cls{report}. An instance suitable
2422 % for \cls{article} is above in the hook.
2423 % \begin{macrocode}
2424 \DeclareInstance{heading}{part}{display}
2425 {
2426 , name = part
2427 , level = -1
2428 , placement = page
2429 , after-penalty-vspace = 0pt
2430 , number-format = \partname\nobreakspace\thepart
2431 , heading-decls = \centering\bfseries
2432 , number-decls = \huge
2433 , title-decls = \Huge
2434 , headformat-instance = display
2435 , mark-cmd = \partmark {#1}
2436 }
2437 % \end{macrocode}
2438 % \end{instance}
2439 %
2440 % \begin{instance}{heading chapter}
2441 %
2442 % \begin{macrocode}
2443 \DeclareInstance{heading}{chapter}{display}
2444 {
2445 , name = chapter
2446 , level = 0
2447 , placement = top
2448 , after-penalty-vspace = 50pt
2449 % , number-title-sep = 20pt % currently in headformat
2450 , after-vspace = 40pt
2451 , number-format = \@chapapp\space \thechapter
2452 , heading-decls = \raggedright \parindent0pt \bfseries
2453 , number-decls = \huge
2454 , title-decls = \Huge
2455 , headformat-instance = display
2456 , mark-cmd = \chaptermark {#1}
2457 , contents-extra = \addtocontents{lof}{\protect\addvspace{10\p@}}
2458 \addtocontents{lot}{\protect\addvspace{10\p@}}
2459 }
2460 % \end{macrocode}
2461 % \end{instance}
2462 %
2463 %
2464 % \begin{instance}{heading section}
2465 %
2466 % \begin{macrocode}
2467 \DeclareInstance{heading}{section}{display}
2468 {
2469 , name = section
2470 , level = 1
2471 , mark-cmd = \sectionmark {#1}
2472 , before-vspace = 3.5ex plus 1ex minus .2ex
2473 , after-vspace = 2.3ex plus .2ex
2474 , heading-decls = \normalfont\Large\bfseries
2475 }
2476 % \end{macrocode}
2477 % \end{instance}
2478 %
2479 %
2480 % \begin{instance}{heading subsection}
2481 %
2482 % \begin{macrocode}
2483 \DeclareInstance{heading}{subsection}{display}
2484 {
2485 , name = subsection
2486 , parent-name = section
2487 , level = 2
2488 , mark-cmd = \subsectionmark {#1}
2489 , before-vspace = 3.25ex plus 1ex minus .2ex
2490 , after-vspace = 1.5ex plus .2ex
2491 , heading-decls = \normalfont\large\bfseries
2492 }
2493 % \end{macrocode}
2494 % \end{instance}
2495 %
2496 %
2497 % \begin{instance}{heading subsubsection}
2498 %
2499 % \begin{macrocode}
2500 \DeclareInstance{heading}{subsubsection}{display}
2501 {
2502 , name = subsubsection
2503 , parent-name = subsection
2504 , level = 3
2505 , before-vspace = 3.25ex plus 1ex minus .2ex
2506 , after-vspace = 1.5ex plus .2ex
2507 , heading-decls = \normalfont\normalsize\bfseries
2508 }
2509 % \end{macrocode}
2510 % \end{instance}
2511 %
2512 % \begin{instance}{heading paragraph}
2513 %
2514 % \begin{macrocode}
2515 \DeclareInstance{heading}{paragraph}{runin}
2516 {
2517 , name = paragraph
2518 , parent-name = subsubsection
2519 , level = 4
2520 , before-vspace = 3.25ex \@plus1ex \@minus.2ex
2521 , after-vspace = 1em
2522 , heading-decls = \normalfont\normalsize\bfseries
2523 , mark-cmd = \paragraphmark {#1}
2524 , headformat-instance = paragraph
2525
2526 }
2527 % \end{macrocode}
2528 % \end{instance}
2529 %
2530 % \begin{instance}{heading subparagraph}
2531 %
2532 % \begin{macrocode}
2533 \DeclareInstance{heading}{subparagraph}{runin}
2534 {
2535 , name = subparagraph
2536 , parent-name = paragraph
2537 , level = 5
2538 , before-vspace = 3.25ex \@plus1ex \@minus .2ex
2539 , after-vspace = 1em
2540 , heading-decls = \normalfont\normalsize\bfseries
2541 , mark-cmd = \subparagraphmark {#1}
2542 , headformat-instance = subparagraph
2543
2544 }
2545 % \end{macrocode}
2546 % \end{instance}
2547 %
2548 % \begin{instance}{headformat display}
2549 %
2550 % \begin{macrocode}
2551 \DeclareInstance{headformat}{display}{display}
2552 {
2553 , heading-indent = 0pt
2554 }
2555 % \end{macrocode}
2556 % \end{instance}
2557 %
2558 % \begin{instance}{headformat hang}
2559 %
2560 % \begin{macrocode}
2561 \DeclareInstance{headformat}{hang}{hang}
2562 {
2563 , heading-indent = 0pt
2564 }
2565 % \end{macrocode}
2566 % \end{instance}
2567 %
2568 % \begin{instance}{headformat paragraph}
2569 %
2570 % \begin{macrocode}
2571 \DeclareInstance{headformat}{paragraph}{runin}
2572 {
2573 , heading-indent = 0pt
2574 }
2575 % \end{macrocode}
2576 % \end{instance}
2577 %
2578 % \begin{instance}{headformat subparagraph}
2579 % \begin{macrocode}
2580 \DeclareInstance{headformat}{subparagraph}{runin}
2581 {
2582 , heading-indent = \parindent
2583 }
2584 % \end{macrocode}
2585
2586 % \end{instance}
2587 %
2588 % \begin{instance}{headformat section-special}
2589 % A special headformat instance for testing. It can be used with
2590 % \verb+\section[headformat-instance=section-special]{bla}+
2591 % \begin{macrocode}
2592 \DeclareInstance{headformat}{section-special}{hang}
2593 {
2594 , heading-indent = 4em
2595 , title-format = {#1!}
2596 }
2597 % \end{macrocode}
2598 % \end{instance}
2599 %
2600
2601 % \subsubsection{New \cs{@startsection}}
2602 %
2603 % \begin{macro}{\@startsection}
2604 % To support legacy classes that implement headings through a call
2605 % to \cs{@startsection} we redefine this command to generate a
2606 % \text{heading} instance from the arguments of \cs{@startsection} if
2607 % it doesn't yet exist and then use this instance to typeset the
2608 % heading.
2609 %
2610 % NOTE: To handle legacy definition like
2611 % \verb+{\normalfont\Large\bfseries\MakeUppercase}+ in the last argument
2612 % it copies this argument both to
2613 % \texttt{heading-decls} and to \texttt{title-format}.
2614 %
2615 % \begin{macrocode}
2616 \DeclareDocumentCommand \@startsection {mmmmmm s ={shorttitle}o m}{
2617 % \end{macrocode}
2618 % If there already exists an instance \texttt{\#1-@startsection} then arguments
2619 % 2--6 are ignored and we simply call that instance. Otherwise we
2620 % go through the process to set it up. This allows classes, packages
2621 % and authors to create and overwrite definitions with \cs{@startsection}.
2622 % But after a call to a command using the \cs{@startsection}
2623 % the instances are created. It is therefore not possible to redefine the command
2624 % after a first use or to create two commands using the same level, e.g. \cs{section}
2625 % and \cs{specialsection}. Such setups should use the new interfaces to declare
2626 % heading commands.
2627 % \begin{macrocode}
2628 \IfInstanceExistsF{heading}{#1-@startsection}{
2629 % \end{macrocode}
2630 %
2631 % \begin{macrocode}
2632 \@@_debug_typeout:n{Info:~ setting~ up~ instances~ for~
2633 legacy~ \string\@startsection}
2634 % \end{macrocode}
2635 % In the \LaTeXe{} logic a negative \#4 means we do not indent the
2636 % following paragraph \ldots
2637 %
2638 % It is okay to change any \texttt{em} or \texttt{ex}
2639 % specifications to real \texttt{pt} values here, because this code
2640 % is executed in the same place where \cs{@startsection} is
2641 % normally executed and inside the original definitions such
2642 % assignments happen as well, before there is any font change
2643 % happening for \#4 and \#5.
2644 % \begin{macrocode}
2645 \@tempskipa #4\relax
2646 \@afterindenttrue
2647 \ifdim \@tempskipa <\z@
2648 \@tempskipa -\@tempskipa
2649 \@afterindentfalse
2650 \fi
2651 % \end{macrocode}
2652 % \ldots\ and a negative \#5 means we should produce a runin heading.
2653 % \begin{macrocode}
2654 \@tempskipb #5\relax
2655 \ifdim \@tempskipb>\z@
2656 \use:e {
2657 \DeclareInstance{heading}{#1-@startsection}{display}{
2658 , name = #1
2659 , level = #2
2660 , mark-cmd = \exp_not:c {#1mark} {##1}
2661 , before-vspace = \the\@tempskipa
2662 , after-vspace = \the\@tempskipb
2663 , para-indent = \if@afterindent true \else false\fi
2664 , heading-decls = \exp_not:n {#6}
2665 % \end{macrocode}
2666 % we also do the declarations in \texttt{title-format} to handle
2667 % settings which ends, e.g., with \cs{MakeUppercase},
2668 % \begin{macrocode}
2669 , headformat-instance = #1-@startsection
2670 }}
2671 % \end{macrocode}
2672 % We can expect that the instance \texttt{\#1-@startsection} is not
2673 % set up by the user if \texttt{\#1-@startsection} isn't, so no check.
2674 % \begin{macrocode}
2675 \DeclareInstance{headformat}{#1-@startsection}{hang}{heading-indent = #3,title-format={#6{##1}}}
2676 \else
2677 \@tempskipb=-\@tempskipb
2678 \use:e {
2679 \DeclareInstance{heading}{#1-@startsection}{runin}{
2680 , name = #1
2681 , level = #2
2682 , mark-cmd = \exp_not:c {#1mark} {##1}
2683 , before-vspace = \the\@tempskipa
2684 , after-vspace = \the\@tempskipb
2685 , heading-decls = \exp_not:n{#6}
2686 , headformat-instance = #1-@startsection
2687 }}
2688 \DeclareInstance{headformat}{#1-@startsection}{runin}{heading-indent = #3,title-format={#6{##1}}}
2689 \fi
2690 }
2691 \ParseLaTeXeHeading {#1-@startsection}{#7}{#8}{#9}
2692 }
2693 % \end{macrocode}
2694 % \end{macro}
2695 %
2696 % Some classes redefine \cs{@startsection} and then our redefinition is lost. So we
2697 % reinstate it
2698 % \begin{macrocode}
2699 \NewCommandCopy\kernel@startsection\@startsection
2700 \AddToHook{class/after}[head/@startsection]
2701 {\RenewCommandCopy\@startsection\kernel@startsection}
2702 % \end{macrocode}
2703 %
2704 %
2705 % \subsubsection{New \cs{secdef}}
2706 % The command maps standard \cs{secdef} definition of \cs{part} and \cs{chapter} to
2707 % the new interface. Unknown heading commands warn (or error if tagging is active) and
2708 % then call the old commands.
2709 % \begin{macro}{\secdef}
2710 % \begin{macrocode}
2711 \RenewDocumentCommand\secdef{mmsO{#5}m}
2712 {
2713 \str_case:enF {\cs_to_str:N#1}
2714 {
2715 {@part}
2716 {
2717 \IfNoValueTF{#4}
2718 {\ParseLaTeXeHeading{part}{#3} {start-code=\relax} {#5}}
2719 {
2720 \@@_process_shorttitle:nN{#4}\l_@@_tmpa_tl
2721 \ExpandArgs{nne}\ParseLaTeXeHeading{part}{#3}{start-code=\relax,\exp_not:o{\l_@@_tmpa_tl}} {#5}
2722 }
2723 \@latex@warning{\noexpand\secdef~detected~in~\noexpand\part~command.\MessageBreak
2724 \noexpand\part~will~be~redefined~to~use~templates.\MessageBreak
2725 This~may~change~the~layout!}
2726 \DeclareDocumentCommand \part {s ={shorttitle}o m}
2727 { \ParseLaTeXeHeading {part} {##1} {##2} {##3} }
2728 }
2729 {@chapter}
2730 {
2731 \IfNoValueTF{#4}
2732 { \ParseLaTeXeHeading{chapter}{#3} {#4} {#5} }
2733 {
2734 \@@_process_shorttitle:nN{#4}\l_@@_tmpa_tl
2735 \ExpandArgs{nno}\ParseLaTeXeHeading{chapter}{#3}{\l_@@_tmpa_tl} {#5}
2736 }
2737 \@latex@warning{\noexpand\secdef~detected~in~\noexpand\chapter~command.\MessageBreak
2738 \noexpand\chapter~will~be~redefined~to~use~templates.\MessageBreak
2739 This~may~change~the~layout!}
2740 \DeclareDocumentCommand \chapter {s ={shorttitle}o m}
2741 { \ParseLaTeXeHeading {chapter} {##1} {##2} {##3} }
2742 }
2743 }
2744 {
2745 \tag_if_active:TF\@latex@error\@latex@warning
2746 {\noexpand\secdef~with~unknown~argument~\noexpand#1 found.\MessageBreak
2747 The~command~can~not~be~adapted~on~the~fly~to~support~tagging.\MessageBreak
2748 The~heading~command~using~this~\noexpand\secdef~should~be~
2749 reimplemented\MessageBreak with~templates}{}
2750 \IfBooleanTF{#3}{#2{#5}}{#1[#4]{#5}}
2751 }
2752 }
2753 % \end{macrocode}
2754 % A helper command to reprocess the argument as key-val
2755 % \begin{macrocode}
2756 \NewDocumentCommand\@@_process_shorttitle:nN{={shorttitle}mm}
2757 { \tl_set:Nn#2{#1} }
2758 % \end{macrocode}
2759 % \end{macro}
2760 % \subsubsection{Core \LaTeX{}}
2761 % We define here as an example the heading commands with the new
2762 % interfaces for the three standard classes.
2763 %
2764 % \begin{macrocode}
2765 \AddToHook{class/article/after}[head/example]
2766 {
2767 \DeclareInstance{heading}{part}{display}
2768 {
2769 , name = part
2770 , level = -1
2771 , before-vspace = 4ex
2772 , after-vspace = 3ex
2773 , mark-cmd = \partmark {#1}
2774 , number-format = \partname\nobreakspace\thepart
2775 , heading-decls = \raggedright\bfseries
2776 , number-decls = \Large
2777 , title-decls = \huge
2778 , headformat-instance = part
2779 }
2780 \DeclareInstance{headformat}{part}{display}
2781 {
2782 , heading-indent = 0pt
2783 , number-title-sep = 0pt
2784 }
2785 \DeclareDocumentCommand \part {s ={shorttitle}o m}
2786 { \ParseLaTeXeHeading {part} {#1} {#2} {#3} }
2787 \@@_setup_default_heading:
2788 }
2789 % \end{macrocode}
2790 % \changes{0.9b}{2026-01-26}{Correct chapter definition, tagging/1984}
2791 % \begin{macrocode}
2792 \AddToHook{class/report/after}[head/example]
2793 {
2794 \DeclareDocumentCommand \part {s ={shorttitle}o m}
2795 { \ParseLaTeXeHeading {part} {#1} {#2} {#3} }
2796 \DeclareDocumentCommand \chapter {s ={shorttitle}o m}
2797 {
2798 \ParseLaTeXeHeading {chapter}{#1} {#2} {#3}
2799 }
2800 \@@_setup_default_heading:
2801 }
2802 \AddToHook{class/book/after}[head/example]
2803 {
2804 \DeclareDocumentCommand \part {s ={shorttitle}o m}
2805 { \ParseLaTeXeHeading {part} {#1} {#2} {#3} }
2806 \DeclareDocumentCommand \chapter {s ={shorttitle}o m}
2807 {
2808 \if@mainmatter
2809 \ParseLaTeXeHeading {chapter}{#1} {#2} {#3}
2810 \else
2811 \IfBooleanTF{#1}
2812 {% starred:
2813 \ParseLaTeXeHeading {chapter}{\BooleanTrue} {#2} {#3}
2814 }
2815 {\IfNoValueTF{#2}
2816 {\ParseLaTeXeHeading {chapter}{\BooleanTrue} {shorttitle={#3}} {#3}}
2817 {\ParseLaTeXeHeading {chapter}{\BooleanTrue} {#2} {#3}}
2818 }
2819 \fi
2820 }
2821 \@@_setup_default_heading:
2822 }
2823 % \end{macrocode}
2824 %
2825 %
2826 %
2827 % \begin{macrocode}
2828 \cs_new_protected:Npn \@@_setup_default_heading:
2829 {
2830 % \end{macrocode}
2831 % \begin{macro}{\section}
2832 %
2833 % \begin{macrocode}
2834 \DeclareDocumentCommand \section {s ={shorttitle}o m}
2835 { \ParseLaTeXeHeading {section} {##1} {##2} {##3} }
2836 % \end{macrocode}
2837 % \end{macro}
2838 %
2839 % \begin{macro}{\subsection}
2840 %
2841 % \begin{macrocode}
2842 \DeclareDocumentCommand \subsection {s ={shorttitle}o m}
2843 { \ParseLaTeXeHeading {subsection} {##1} {##2} {##3} }
2844 % \end{macrocode}
2845 % \end{macro}
2846 %
2847 % \begin{macro}{\subsubsection}
2848 %
2849 % \begin{macrocode}
2850 \DeclareDocumentCommand \subsubsection {s ={shorttitle}o m}
2851 { \ParseLaTeXeHeading {subsubsection} {##1} {##2} {##3} }
2852 % \end{macrocode}
2853 % \end{macro}
2854 %
2855 % \begin{macro}{\paragraph}
2856 %
2857 % \begin{macrocode}
2858 \DeclareDocumentCommand \paragraph {s ={shorttitle}o m}
2859 { \ParseLaTeXeHeading {paragraph} {##1} {##2} {##3} }
2860 % \end{macrocode}
2861 % \end{macro}
2862 %
2863 % \begin{macro}{\subparagraph}
2864 %
2865 % \begin{macrocode}
2866 \DeclareDocumentCommand \subparagraph {s ={shorttitle}o m}
2867 { \ParseLaTeXeHeading {subparagraph} {##1} {##2} {##3} }
2868 } %end of command
2869 % \end{macrocode}
2870 % \end{macro}
2871
2872 % \begin{macrocode}
2873 %</package>
2874 % \end{macrocode}
2875 %
2876 % \section{Issues and problems noticed along the way}
2877 %
2878 % \subsection{Local \cs{DeclareInstance}}
2879 %
2880 % \cs{DeclareInstance} does its declaration locally. That might be the
2881 % right decision (or not) but we need to make sure that it in that
2882 % case all of the declaration is local.
2883 %
2884 % One potential problem with that is that it might allocate \TeX{}
2885 % registers.
2886 %
2887 % The problem showed up in the redefinition of \cs{@startsection},
2888 % because in the current document some headings are local to an
2889 % environment, so things get redeclared over and over again.
2890 %
2891 % \subsection{\cs{SetCurrentTemplateKeys}}
2892 %
2893 % Furthermore, I think I would like to have some
2894 % \cs{SetCurrentTemplateKeys} where one does not have to specify
2895 % the type nor the template name, because that is how it is always
2896 % used (by me).
2897 %
2898 % \subsection{O-expansion of \cs{UseInstance} arguments}
2899 %
2900 % Whenever a template code calls a sub-instance and that
2901 % sub-instance takes arguments, then the values for these arguments
2902 % are typically inside tokenlists or registers so that for
2903 % efficiency (and sometimes as a total must) one has to o-expand
2904 % all arguments. While that can be coded somehow, e.g.,
2905 % \begin{verbatim}
2906 % \use:e {
2907 % \UseInstance{headformat} { \l_@@_headformat_instance_tl }
2908 % { \exp_not:o \UnusedTemplateKeys }
2909 % { \exp_not:o { \l_@@_typeset_number_tl } }
2910 % { \exp_not:n { #3 } }
2911 % { \exp_not:o { \use_i:nn #9 } }
2912 % { \exp_not:o { \use_ii:nn #9 } }
2913 % }
2914 % \end{verbatim}
2915 %
2916 % it would be better to have a version of \cs{UseInstance} that
2917 % does this automatically. No suggestion for a name.
2918 %
2919 %
2920 %
2921 % \subsection{Switch \cs{openright} is not defined by core}
2922 %
2923 % I think this should change and it might be enough to just define
2924 % it in the kernel (I think \cs{newif} no longer complains if it
2925 % acts on an existing \cs{if...}
2926 %
2927 % \subsection{Indexheading at least for l3doc is wrong now}
2928 %
2929 % Needs checking.
2930 %
2931 % \end{implementation}
2932 %
2933 %
2934 %
2935 % \Finale
2936 %
2937 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2938 \endinput