Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-sec.dtx (C) Copyright 2022-2025 LaTeX Project
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 %
13 % The development version of the bundle can be found below
14 %
15 % https://github.com/latex3/latex2e/required/latex-lab
16 %
17 % for those people who are interested or want to report an issue.
18 %
19 \def\ltlabsecdate{2025-10-20}
20 \def\ltlabsecversion{0.84k}
21 % \end{macrocode}
22 %<*driver>
23 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
24 \documentclass[kernel]{l3doc}
25 \usepackage{latex-lab-testphase-l3doc}
26 \EnableCrossrefs
27 \CodelineIndex
28 \begin{document}
29 \DocInput{latex-lab-sec.dtx}
30 \end{document}
31 %</driver>
32 %
33 % \fi
34 %
35 %
36 % \title{The \textsf{latex-lab-sec} package\\
37 % Changes related to the tagging of sectioning commands}
38 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
39 % \date{v\ltlabsecversion\ \ltlabsecdate}
40 %
41 % \maketitle
42 %
43 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
44 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
45 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
46 % customizations}}
47 %
48 % \providecommand\hook[1]{\texttt{#1}}
49 %
50 % \begin{abstract}
51 % The following code implements a first draft for the tagging of sectioning commands.
52 % \end{abstract}
53 %
54 % \section{Limitations}
55 %
56 % Sectioning commands are in general not defined by the format but by the classes.
57 % Their implementation vary: some are defined with the help of \cs{@startsection},
58 % some are like \cs{chapter} handcrafted,
59 % some build with the help of extension packages or as in the KOMA classes
60 % with class code that extends the \cs{@startsection} functionality.
61 %
62 % The following code can therefore currently be used \emph{only} with the standard classes
63 % or with classes which do not overwrite the changed definitions.
64 %
65 %
66 %
67 % \section{Introduction}
68 %
69 % Tagging of sectioning commands consist of two parts:
70 %
71 % \begin{itemize}
72 % \item The heading/title text of the section should be surrounded by a
73 % heading tag, typically \texttt{Hn} with some value of \texttt{n}.
74 % In theory, one could put the number of the section command in an \texttt{Lbl}.
75 % However, current AT doesn't handle this well, so
76 % we use a tag \texttt{section-number} that is mapped to \texttt{Span}.
77 % The number of the \texttt{Hn} tag should reflect the \enquote{natural} level.
78 % So in an article \cs{section} will use \texttt{H1}, in a book \cs{chapter} will use
79 % \texttt{H1} and \cs{section} \texttt{H2}.
80 % Titles of \cs{part} are a bit out of this system as they are normally
81 % not part of the hierarchy: often only some chapters are grouped under a part.
82 % Their title is therefore tagged as \texttt{Title}.
83 % \item
84 % The whole section should normally be surrounded by
85 % a \texttt{Sect} tag. Parts should be surrounded by \texttt{Part}.
86 % It is a bit unclear if the headings should be inside or outside of these
87 % structures---the best practice guide puts them outside---but on the whole
88 % it sounds more logical to group the heading with the text inside the \texttt{Sect}.
89 % For the part this is actually required, as there can be only one \texttt{Title}
90 % in a structure, so the part title can't be at the same level as the
91 % document \texttt{Title}.
92 %
93 % Starting such an enclosing \texttt{Sect} structure is rather easy,
94 % but closing it requires code in various place,
95 % for example the commands \cs{mainmatter}, \cs{backmatter},
96 % \cs{frontmatter} and \cs{appendix} should typically close everything.
97 % Following sectioning commands should close all previous structures
98 % with a level equal or higher than their own level.
99 % \end{itemize}
100 %
101 % \section{Technical details and problems}
102 %
103 % The implementation has to take care of various details.
104 %
105 % \begin{itemize}
106 %
107 % \item As sections in \LaTeX{} are not environments, the
108 % \texttt{<Sect>} structures can be wrongly nested with other structures. For example
109 % if a document puts a sectioning command into a list or a trivlist or
110 % a minipage then it can no longer close previous \texttt{<Sect>} structures correctly.
111 % The problem can be detected by checking the structure stack
112 % and a warning can be issued, but the author then has to close the structures
113 % manually before the list or minipage.
114 %
115 % Thus there have to be user interfaces to handle such cases.
116 % It should also be possible not to create all the \texttt{<Sect>} structures
117 % automatically but to tag only the headings so that the author can handle special
118 % cases manually.
119 %
120 % \item If hyperref is used, targets for links should be inserted, either with
121 % \cs{refstepcounter} or manually with \cs{MakeLinkTarget}. These targets must be
122 % in the correct structure for the structure destinations. They replace some
123 % of the current patches in hyperref.
124 %
125 % \end{itemize}
126 %
127 % \subsection{Functions and keys}
128 %
129 % \begin{function}{\tag_tool:n,\tagtool}
130 % \emph{deprecated}, use tagging sockets instead.
131 % \end{function}
132 %
133 % \subsection{TODO}
134 %
135 % \begin{itemize}
136 % \item A dedicated command to close a sectioning unit should be provided.
137 %
138 % \item A dedicated command to open a sectioning unit should be provided too.
139 %
140 % \item It should also be possible to suppress the sectioning unit in sectioning commands
141 % to allow e.g. to put an epigraph or similar in front.
142 %
143 % \item The number in \cs{part} and \cs{chapter} is currently not correctly
144 % tagged as a \texttt{section-number} as this requires to redefine the internal (class dependent)
145 % commands too.
146 %
147 % \end{itemize}
148 %
149 % \begin{macrocode}
150 %<*package>
151 % \end{macrocode}
152 %
153 % \section{Implementation}
154 % \begin{macrocode}
155 \ProvidesExplPackage {latex-lab-testphase-sec} {\ltlabsecdate} {\ltlabsecversion}
156 {Code related to the tagging of sectioning commands}
157 % \end{macrocode}
158 % \changes{0.84k}{2025-10-20}{Switched to tagging sockets and various corrections}.
159 % \subsection{Temporary fix}
160 % Until tagpdf correctly sets the symbolic name (2025-10-06)
161 % \begin{macrocode}
162 \tl_set:Nn \l__tag_para_tag_default_tl { \UseStructureName {para/textblock} }
163 \tl_set:Nn \l__tag_para_main_tag_tl { \UseStructureName {para/semantic} }
164 % \end{macrocode}
165 %
166 % \subsection{Surrounding by \texttt{Sect} structures}
167 % We use a stack to record the levels of the open \texttt{Sect}. The first item
168 % has level -100. A sectioning command will take a record from the stack. If its level is
169 % greater or equal it closes this structure and takes the next record from the stack.
170 % If the record has a smaller level then it puts it back and stops.
171 % The stack is compared with the main structure stack, if they don't match
172 % it means we can't safely close the \texttt{Sect} and so we issue a warning
173 % and do nothing.
174 %
175 % \begin{macrocode}
176 %</package>
177 % \end{macrocode}
178 % \subsubsection{Glyphtounicode improvements}
179 %
180 % As lualatex runs with legacy encodings in the test files, we enable and
181 % load glyphtounicode. For the math we load additional definitions.
182 %
183 % \begin{macrocode}
184 %<*kernelchange>
185 \ifdefined\directlua
186 \ifnum\outputmode > 0
187 \pdfvariable gentounicode =1
188 \protected\def\pdfglyphtounicode {\pdfextension glyphtounicode }
189 \protected\edef\pdfgentounicode {\pdfvariable gentounicode}
190 \input{glyphtounicode}
191 \fi
192 \fi
193 \ifdefined\pdfglyphtounicode
194 \input{glyphtounicode-cmex}
195 \fi
196 %</kernelchange>
197 % \end{macrocode}
198 %
199 % \begin{macrocode}
200 %<*package>
201 %<@@=tag>
202 % \end{macrocode}
203 %
204 % \subsubsection{Tagging commands}
205 %
206 %
207 % \begin{variable}{\g_@@_sec_stack_seq}
208 % The stack holds the tag, the level and the structure number.
209 % \begin{macrocode}
210 \seq_new:N \g_@@_sec_stack_seq
211 \seq_gpush:Nn\g_@@_sec_stack_seq {{Document}{-100}{2}}
212 % \end{macrocode}
213 % \end{variable}
214 %
215 % \begin{macro}{\@@_get_data_current_Sect:}
216 % This allows to retrieve the number of the current Sect structure (or
217 % Document if we are outside any Sect) with |\tag_get:n{current_Sect}|
218 % \begin{macrocode}
219 \cs_new:Npn \@@_get_data_current_Sect:
220 {
221 \exp_last_unbraced:Ne\use_iii:nnn{\seq_item:Nn\g_@@_sec_stack_seq{1}}
222 }
223 % \end{macrocode}
224 % \end{macro}
225 % \begin{variable}{\l_@@_sec_Sect_bool}
226 % This boolean controls if a Sect structure is opened.
227 % \begin{macrocode}
228 \bool_new:N \l_@@_sec_Sect_bool
229 \bool_set_true:N\l_@@_sec_Sect_bool
230 % \end{macrocode}
231 % \end{variable}
232 %
233 % \begin{macro}{\@@_sec_begin:nn}
234 % This starts a sectioning structure (the „Sect environment“).
235 % Currently the default tag is either Sect or Part, depending on the level,
236 % but this can be changed by adapting the symbolic structure names which are
237 % built from the level.
238 % The second argument allows to add more options but is currently unused.
239 % \begin{macrocode}
240 \cs_new_protected:Npn\@@_sec_begin:nn #1 #2 %#1 level #2 keyval
241 {
242 \tag_struct_begin:n
243 {
244 tag= \UseStructureName{sec/#1}
245 ,#2
246 }
247 \seq_gpush:Ne \g_@@_sec_stack_seq
248 {{\g_@@_struct_tag_tl}{\int_eval:n{#1}}{\g_@@_struct_stack_current_tl}}
249 }
250 \cs_generate_variant:Nn \@@_sec_begin:nn {en}
251 % \end{macrocode}
252 % \end{macro}
253 %
254 % \begin{macro}{\@@_sec_end:n}
255 % \begin{macrocode}
256 \msg_new:nnn { tag } {wrong-sect-nesting}
257 {
258 The~structure~#1~can~not~be~closed.\\
259 It~is~not~equal~to~the~current~structure~#2~on~the~main~stack
260 }
261
262 \cs_new_protected:Npn\@@_sec_end:n #1 % #1 level
263 {
264 \seq_get:NN \g_@@_sec_stack_seq \l_@@_tmpa_tl
265 \int_compare:nNnT {#1}<{\exp_last_unbraced:NV\use_ii:nnn\l_@@_tmpa_tl+1}
266 {
267 \seq_get:NN\g_@@_struct_tag_stack_seq \l_@@_tmpb_tl
268 \exp_args:Nee
269 \tl_if_eq:nnTF
270 {\exp_last_unbraced:NV\use_i:nnn\l_@@_tmpa_tl}
271 {\exp_last_unbraced:NV\use_i:nn\l_@@_tmpb_tl}
272 {
273 \seq_gpop:NN \g_@@_sec_stack_seq \l_@@_tmpa_tl
274 \tag_struct_end:
275 \@@_sec_end:n {#1}
276 }
277 {
278 \msg_warning:nnee {tag}{wrong-sect-nesting}
279 { \exp_last_unbraced:NV\use_i:nnn \l_@@_tmpa_tl }
280 { \exp_last_unbraced:NV\use_i:nn \l_@@_tmpb_tl }
281 }
282 }
283 }
284 % \end{macrocode}
285 % \end{macro}
286
287 % \begin{macro}{\@@_sec_title_split:,\@@_sec_restore_para:}
288 % Runin-sectioning command must separate the heading from the following text.
289 % The code is in an \cs{everypar} which is perhaps executed in a group (e.g. when
290 % a list follows), we have to ensure that the restoring of the para can escape.
291 % \begin{macrocode}
292 \cs_new_protected:Npn\@@_sec_restore_para:
293 {
294 \UseTaggingSocket {para/restore}
295 \if_int_compare:w \tex_currentgrouptype:D =14 % semi-simple group
296 \group_insert_after:N \@@_sec_restore_para:
297 \else:
298 \if_int_compare:w \tex_currentgrouptype:D =\c_one_int % simple group
299 \group_insert_after:N \@@_sec_restore_para:
300 \fi:
301 \fi:
302 }
303 \cs_new_protected:Npn \@@_sec_title_split:
304 {
305 % \end{macrocode}
306 % This ends the title structure. As the begin is from the
307 % automatic (flattened) para-tagging we have to increase the counter.
308 % \begin{macrocode}
309 \tag_mc_end:
310 \tag_struct_end:
311 \@@_gincr_para_end_int:
312 % \end{macrocode}
313 % In case something (e.g. a list) did reset the boolean we need to close also a semantic
314 % paragraph.
315 % \begin{macrocode}
316 \bool_if:NF\l__tag_para_flattened_bool
317 {\UseTaggingSocket{para/semantic/end}{}}
318 % \end{macrocode}
319 % Now restore the para-tagging and start a normal paragraph:
320 % \begin{macrocode}
321 \@@_sec_restore_para:
322 \UseTaggingSocket{para/begin}
323 }
324 % \end{macrocode}
325 % \end{macro}
326 %
327 % \begin{macro}{\@@_sec_title_begin:nn}
328 % This command is used in the socket at the begin of display sectioning commands
329 % like part and chapter. It takes two arguments: the level and the title.
330 % \begin{macrocode}
331 \cs_new_protected:Npn \@@_sec_title_begin:nn #1 #2 %level, title
332 {
333 \tag_struct_begin:n{tag=\UseStructureName{sec/#1/title},title={#2}}
334 \bool_set_true:N\l_@@_para_flattened_bool
335 \tl_set:Nn\l_@@_para_tag_tl {\UseStructureName{sec/#1/titleline}}
336 }
337 % \end{macrocode}
338 % \end{macro}
339
340 % \begin{macro}{\@@_sec_title_end:}
341 % \begin{macrocode}
342 \cs_new_protected:Npn \@@_sec_title_end:
343 {
344 \tag_struct_end: %P = Hn
345 \UseTaggingSocket{para/restore}
346 }
347 % \end{macrocode}
348 % \end{macro}
349 %
350 % \begin{macro}{\@@_set_title_hang:nnn}
351 % To be able to correctly tag the number and insert the link target in the
352 % title of a sectioning command created with \cs{@startsection}
353 % we need a special\cs{@hangfrom} variant. This is a bit tricky:
354 % The argument contains the link target and for a correct structure
355 % destination it should be typeset \emph{after} the structure has been opened.
356 % But to measure the hangindent it must be typeset \emph{before} the paragraph is started.
357 % This means that we have to open the title structure manually
358 % and then have to suppress the para-tagging.
359 % Additionally there is an engine difference: with pdftex the literals for the mc are inserted
360 % with the box after the paragraph has started but luatex sets the attributes before
361 % and we have to reset them. Hiding all this in a tagging socket is non-trivial.
362 % The code assumes that we are in vmode!
363 % Attention: The code opens a structure that it doesn't close (it is closed by the \cs{par}).
364 % It therefore does not handle the full tagging of the title. In a new implementation
365 % of the sectioning command this will perhaps have to change.
366 %
367 % \begin{macrocode}
368 \cs_new_protected:Npn \@@_set_title_hang:nNnn #1 #2 #3 #4
369 %#1 level,
370 %#2 boolean: nonumber? (will be later \l__head_nonumber_bool)
371 %#3 formated number /hang space
372 %#4 title
373 % \end{macrocode}
374 % The handling of the title is not perfect. It would be better to pass it through
375 % something like \cs{GetTitleString}. TODO.
376 % \begin{macrocode}
377 {
378 \tagstructbegin{tag=\UseStructureName{sec/#1/title},title-o={#4}}
379 \cs_if_exist_use:N \@@_gincr_para_begin_int:
380 \bool_if:NF #2
381 { \tagstructbegin{tag=\UseStructureName{sec/#1/number}} }
382 \setbox\@tempboxa\hbox{{#3}}
383 % \end{macrocode}
384 % We stop paratagging now, to avoid that the \cs{noindent} creates a structure.
385 % \begin{macrocode}
386 \bool_set_false:N \l_@@_para_bool
387 \hangindent \wd\@tempboxa\noindent
388 % \end{macrocode}
389 % Restart paratagging and insert the box. If the box has a real content (if there is a
390 % number) we have to add mc-chunks and reset the attribute of the box.
391 % \begin{macrocode}
392 \bool_set_true:N \l_@@_para_bool
393 \bool_if:NTF #2
394 {
395 \box\@tempboxa
396 }
397 {
398 \tagmcbegin{}
399 % \end{macrocode}
400 % In lua mode we have to reset the attributes inside the box!
401 % \begin{macrocode}
402 \tag_mc_reset_box:N\@tempboxa
403 \box\@tempboxa
404 \tagmcend
405 \tagstructend
406 }
407 \tagmcbegin{}
408 }
409 % \end{macrocode}
410 % \end{macro}
411 %
412 %
413 % \begin{macro}{\@@_sec_title_runin_number:nn}
414 % \begin{macrocode}
415 \cs_new_protected:Npn \@@_sec_title_runin_number:nNn #1 #2 #3 %#1 level, #2 boolean no number, #3 content
416 {
417 \bool_if:NTF #2
418 { #3 }
419 {
420 \tag_mc_end_push:
421 \tag_struct_begin:n{tag=\UseStructureName{sec/#1/number}}
422 \tag_mc_begin:n{}
423 #3
424 \tag_mc_end:
425 \tag_struct_end:
426 \tag_mc_begin_pop:n{}
427 }
428 }
429 % \end{macrocode}
430 % \end{macro}
431 % Open sec structures should be closed at the end of the document. This should
432 % be done before tagpdf closes the Document structure.
433 % \begin{macrocode}
434 \hook_gput_code:nnn
435 {tagpdf/finish/before}
436 {tagpdf/sec}
437 {\AssignTaggingSocketPlug{sec/end}{kernel}\UseTaggingSocket{sec/end}{-10}}
438 \hook_gset_rule:nnnn {tagpdf/finish/before}{tagpdf/sec}{before}{tagpdf}
439 % \end{macrocode}
440 %
441 % The commands \cs{mainmatter}, \cs{backmatter}, \cs{frontmatter} and
442 % \cs{appendix} close all \texttt{Sect} and \texttt{Part} structures.
443 % \changes{v0.84g}{2025/01/05}{ensure that paragraph is ended (tagging/777)}
444 % \begin{macrocode}
445 \AddToHook{cmd/frontmatter/before}{\par\UseTaggingSocket{sec/end}{-10}}
446 \AddToHook{cmd/mainmatter/before} {\par\UseTaggingSocket{sec/end}{-10}}
447 \AddToHook{cmd/backmatter/before} {\par\UseTaggingSocket{sec/end}{-10}}
448 \AddToHook{cmd/appendix/before} {\par\UseTaggingSocket{sec/end}{-10}}
449 % \end{macrocode}
450 %
451 % \subsection{Tagging Sockets}
452 % First the sockets that handle the Sect structures.
453 %
454 % The argument of the begin socket consists of two brace groups,
455 % in the first brace is the level, in the second the keys for the structure.
456 % \begin{macrocode}
457 \NewTaggingSocketPlug{sec/begin}{kernel}
458 {
459 \@@_sec_begin:en #1
460 }
461 \AssignTaggingSocketPlug{sec/begin}{kernel}
462 % \end{macrocode}
463 % The end socket takes as argument only the level to close.
464 % \begin{macrocode}
465 \NewTaggingSocketPlug{sec/end}{kernel}
466 {
467 \@@_sec_end:n {#1}
468 }
469 \AssignTaggingSocketPlug{sec/end}{kernel}
470 % \end{macrocode}
471 %
472 % These two sockets handle the tagging of headings with special formatting
473 % like part and chapter.
474 % The argument of the begin socket is two brace groups containing the level and
475 % the title.
476 % \begin{macrocode}
477 \NewTaggingSocketPlug{sec/title/begin}{kernel}
478 {
479 \@@_sec_title_begin:nn #1
480 }
481 \AssignTaggingSocketPlug{sec/title/begin}{kernel}
482 % \end{macrocode}
483 % The end socket does not take an argument. It only closes the structures
484 % and restores the para settings.
485 % \begin{macrocode}
486
487 \NewTaggingSocketPlug{sec/title/end}{kernel}
488 {
489 \@@_sec_title_end:
490 }
491 \AssignTaggingSocketPlug{sec/title/end}{kernel}
492 % \end{macrocode}
493 %
494 %
495 % The \texttt{sec/title/hang} socket is used to typeset the heading of a title using \cs{@hangfrom}.
496 % It is the most tricky one. It takes two argument.
497 % The second argument of this socket will pass the normal \cs{@hangfrom} command
498 % if tagging is not active. It is not used with tagging.
499 % The first argument passes four brace groups:
500 % the level, a boolean for \enquote{nonumber},
501 % the actual content of the number and the title.
502 %
503 % Attention: The socket opens a structure that it doesn't close (it is closed by the \cs{par}).
504 % It therefore does not handle the full tagging of the title. In a new implementation
505 % of the sectioning command this will perhaps have to change.
506 % \begin{macrocode}
507 \NewTaggingSocketPlug{sec/title/hang}{kernel}
508 {
509 \@@_set_title_hang:nNnn #1
510 }
511 \AssignTaggingSocketPlug{sec/title/hang}{kernel}
512 % \end{macrocode}
513 %
514 % The \texttt{sec/title/init} socket is used to do some initialization
515 % for sectioning commands that are setup with \cs{@startsection}. It sets
516 % the tag name and flattens the para-tagging. It is mostly needed for run-in
517 % headings which set the heading inside \cs{everypar}.
518 % It takes 1 argument, the level.
519 % \begin{macrocode}
520 \NewTaggingSocketPlug{sec/title/init}{kernel}
521 {
522 \tl_set:Ne\l_@@_para_tag_tl{\UseStructureName{sec/#1/title}}
523 \bool_set_true:N \l_@@_para_flattened_bool
524 }
525 \AssignTaggingSocketPlug{sec/title/init}{kernel}
526 % \end{macrocode}
527 %
528 % This socket handles the tagging between a run-in heading and the following text.
529 % It takes no argument.
530 % \begin{macrocode}
531 \NewTaggingSocketPlug{sec/title/split}{kernel}
532 {
533 \@@_sec_title_split:
534 }
535 \AssignTaggingSocketPlug{sec/title/split}{kernel}
536 % \end{macrocode}
537 %
538 % The following tagging socket command is used to handle the tagging
539 % of the number in the title of run-in headings. Similar to the hang variant it does not
540 % handle the full structure but relies in part on the paragraph
541 % tagging. This again can change if sectioning commands are reimplemented.
542 % It takes as first argument the level and a boolean for the numbering.
543 % The second argument contains the formatted number (if numbered) and the destination.
544 % \begin{macrocode}
545 \NewTaggingSocketPlug{sec/title/runin/number}{kernel}
546 {
547 \@@_sec_title_runin_number:nNn #1 {#2}
548 }
549 \AssignTaggingSocketPlug{sec/title/runin/number}{kernel}
550 % \end{macrocode}
551 %
552 % The following tagging socket command simply tags a number.
553 % It is not used here, as the legacy code needs a more complicated setup.
554 %
555 % It takes the level as first argument.
556 % The second argument contains the formatted number.
557 % \begin{macrocode}
558 \NewTaggingSocketPlug{sec/title/number}{kernel}
559 {
560 \tag_mc_end_push:
561 \tag_struct_begin:n{tag=\UseStructureName{sec/#1/number}}
562 \tag_mc_begin:n{}
563 #2
564 \tag_mc_end:
565 \tag_struct_end:
566 \tag_mc_begin_pop:n{}
567 }
568 \AssignTaggingSocketPlug{sec/title/number}{kernel}
569 % \end{macrocode}
570 %
571 % \section{Sectioning commands}
572 %
573 % \subsection{\cs{part} and \cs{chapter}}
574 %
575 % \cs{part} and \cs{chapter} are defined by the classes.
576 % To tag them we redefine the user commands.
577 % This will probably break with various classes and with titlesec.
578 % The tagging inside relies on the para tagging.
579 % We do not yet use keyval in the optional argument, as this requires latex-dev
580 % and the naming of the keys and their key family is unclear.
581 % \changes{v0.84f}{2024/10/04}{Added braces around optional arg (tagging/725)}
582 % \begin{macrocode}
583 \AddToHook{class/after}
584 {
585 \@ifundefined{chapter}
586 {
587 % \end{macrocode}
588 % This redefines \cs{part} in article class.
589 % \begin{macrocode}
590 \@ifundefined{part}{}
591 {
592 \RenewDocumentCommand\part{ s O{#3} m }
593 {
594 \if@noskipsec \leavevmode \fi
595 \par
596 \addvspace{4ex}%
597 \@afterindentfalse
598 % \end{macrocode}
599 % This are the tagging commands needed at the begin. They open a Part structure
600 % and the structure for the title of the heading.
601 % \begin{macrocode}
602 % tagging start commands
603 \UseTaggingSocket{sec/end}{-1}
604 \UseTaggingSocket{sec/begin}{{-1}{tag=\UseStructureName{sec/-1}}}
605 \UseTaggingSocket{sec/title/begin}{{-1}{#2}}
606 % end tagging start commands
607 % \end{macrocode}
608 % This adds a manual target if the part is unnumbered or starred.
609 % It replaces the hyperref patches.
610 % \begin{macrocode}
611 \bool_lazy_any:nT
612 {
613 { #1 }
614 {
615 \int_compare_p:nNn {\c@secnumdepth}<{-1}
616 }
617 }
618 {
619 \MakeLinkTarget[part]{}
620 }
621 % \end{macrocode}
622 % The main call to the underlying commands.
623 % \begin{macrocode}
624 \IfBooleanTF
625 {#1}
626 { \@spart {#3} }
627 { \@part [{#2}]{#3} }
628 % \end{macrocode}
629 % and now the closing command for the tagging of the title.
630 % \begin{macrocode}
631 \UseTaggingSocket{sec/title/end}
632 }
633 }
634 }
635 % \end{macrocode}
636 % Redefinitions for book and report
637 % \begin{macrocode}
638 {
639 \RenewDocumentCommand\chapter{ s O{#3} m }
640 {
641 \if@openright\cleardoublepage\else\clearpage\fi
642 \thispagestyle{plain}%
643 \global\@topnum\z@
644 \@afterindentfalse
645 % \end{macrocode}
646 % This are the tagging commands needed at the begin. They open a Sect structure
647 % and the structure for the title of the heading.
648 % \begin{macrocode}
649 \UseTaggingSocket{sec/end}{0}
650 \UseTaggingSocket{sec/begin}{{0}{tag=\UseStructureName{sec/0}}}
651 \UseTaggingSocket{sec/title/begin}{{0}{#2}}
652 % \end{macrocode}
653 % This adds a manual target if the chapter is unnumbered or starred.
654 % It replaces the hyperref patches.
655 % \begin{macrocode}
656 \bool_lazy_any:nT
657 {
658 { #1 }
659 {
660 \int_compare_p:nNn {\c@secnumdepth}<{0}
661 }
662 {
663 %in book target also needed in frontmatter
664 \bool_lazy_and_p:nn
665 { \cs_if_exist_p:c { @mainmattertrue } }
666 { ! \legacy_if_p:n { @mainmatter } }
667 }
668 }
669 {
670 % \end{macrocode}
671 % The relation target-struct is stored internally by the MakeLinkTarget commands
672 % \begin{macrocode}
673 \MakeLinkTarget[chapter]{}
674 }
675 % \end{macrocode}
676 % The main call to the underlying commands.
677 % \begin{macrocode}
678 \IfBooleanTF
679 {#1}
680 { \@schapter {#3} }
681 { \@chapter [{#2}]{#3} }
682 % \end{macrocode}
683 % and now the closing command for the tagging of the title.
684 % \begin{macrocode}
685 \UseTaggingSocket{sec/title/end}
686 }
687 % \end{macrocode}
688 % and similar for \cs{part}
689 % \begin{macrocode}
690 \RenewDocumentCommand\part{ s O{#3} m }
691 {
692 \if@openright
693 \cleardoublepage
694 \else
695 \clearpage
696 \fi
697 \thispagestyle{plain}%
698 \if@twocolumn
699 \onecolumn
700 \@tempswatrue
701 \else
702 \@tempswafalse
703 \fi
704 \null\vfil
705 % \end{macrocode}
706 % These are the tagging commands needed at the begin. They open a Part structure
707 % and the structure for the title of the heading.
708 % \begin{macrocode}
709 \UseTaggingSocket{sec/end}{-1}
710 \UseTaggingSocket{sec/begin}{{-1}{tag=\UseStructureName{sec/-1}}}
711 \UseTaggingSocket{sec/title/begin}{{-1}{#2}}
712 % \end{macrocode}
713 % This adds a manual target if the part is unnumbered or starred.
714 % It replaces the hyperref patches.
715 % \begin{macrocode}
716 \bool_lazy_any:nT
717 {
718 { #1 }
719 {
720 \int_compare_p:nNn {\c@secnumdepth}<{-1}
721 }
722 {
723 %in book target also needed in frontmatter
724 \bool_lazy_and_p:nn
725 { \cs_if_exist_p:c { @mainmattertrue } }
726 { ! \legacy_if_p:n { @mainmatter } }
727 }
728 }
729 {
730 \MakeLinkTarget[part]{}
731 }
732 % \end{macrocode}
733 % The main call to the underlying commands.
734 % \begin{macrocode}
735 \IfBooleanTF
736 {#1}
737 { \@spart {#3} }
738 { \@part [{#2}]{#3} }
739 % \end{macrocode}
740 % and now the closing command for the tagging of the title.
741 % \begin{macrocode}
742 \UseTaggingSocket{sec/title/end}
743 }
744 }
745 }
746 % \end{macrocode}
747 %
748 % \subsection{Sectioning commands based on \cs{@startsection}}
749 %
750 % The tagging relies again on the para tagging:
751 % we simply exchange the tag name by the one given as \#1.
752 % This assumes that a tag with the name of the sectioning type is defined.
753 % We don't try to pass the title, this will be done together with
754 % the new keyval handling in the user command.
755 %
756 % \subsubsection{Hyperref code}
757 % hyperref has to insert anchors. If the sectioning is numbered this is done by
758 % \cs{refstepcounter} (and so in vmode). For unnumbered section hyperref
759 % injects the anchor in hmode before the text, it also inserts a
760 % kern to compensate the indent.
761 %
762 % This means that the target of numbered and unnumbered sectioning commands
763 % differ, both regarding the location and in relation to the
764 % tagging structure: The anchor from the \cs{refstepcounter} is outside of
765 % the structure created by the heading title if the para tags are used,
766 % while the other anchors are inside and so the structure destinations are different.
767 %
768 % We unify this by suppressing the anchor from the refstepcounter.
769 % Also we only go back if the indent is positive.
770 %
771 % At first suppress all hyperref patches related to sectioning:
772 % \begin{macrocode}
773 \def\hyper@nopatch@sectioning{}
774 % \end{macrocode}
775 %
776 % \begin{variable}{\l__kernel_sec_nonumber_bool}
777 % A boolean to keep track if a sectioning command should be numbered or not.
778 % \begin{macrocode}
779 \bool_new:N\l__kernel_sec_nonumber_bool
780 % \end{macrocode}
781 % \end{variable}
782 %
783 % \begin{macro}[no-user-doc]{\@hyp@section@target@nnn}
784 % A simple internal command. There is no need for something public,
785 % as packages defining their own version of \cs{@startsection} will
786 % probably need something slightly different based on \cs{MakeLinkTarget}.
787 % \changes{v0.84i}{2025/02/13}{Wrapped all use inside \cs{NoCaseChange},
788 % tagging-project issue \#787}
789 % \begin{macrocode}
790 \cs_new_protected:Npn \@hyp@section@target@nnn #1 #2 #3 %#1 optarg #2 name/counter, #3 indent
791 {
792 \makebox[0pt][l]
793 {
794 \skip_set:Nn \@tempskipa {#3}
795 \dim_compare:nNnF {\@tempskipa}<{0pt}{\kern-\@tempskipa}
796 \MakeLinkTarget#1{#2}
797 }
798 }
799 % \end{macrocode}
800 % \end{macro}
801 %
802 % \begin{macro}[no-user-doc]{\@@_makecurrentHref:n}
803 % In run-in heading we have to set the name of the anchor before it is actually
804 % created in a everypar. If hyperref is loaded we could use \cs{Hy@MakeCurrentHrefAuto}
805 % but without it we need a similar command.
806 % \begin{macrocode}
807 \cs_new_protected:Npn \@kernel@makecurrentHref #1 %#1 prefix
808 {
809 \int_gincr:N\g__kernel_target_int
810 \tl_gset:Ne \@currentHref {#1.\int_use:N\g__kernel_target_int}
811 }
812 % \end{macrocode}
813 % hyperref uses a different counter so we need to use a different command,
814 % TODO: merge that.
815 %
816 % \begin{macrocode}
817 \AddToHook{package/hyperref/after}
818 {
819 \cs_set_eq:NN \@kernel@makecurrentHref \Hy@MakeCurrentHrefAuto
820 }
821 % \end{macrocode}
822 % \end{macro}
823
824 % \subsection{Adaption of the heading commands}
825 % We add to \cs{@startsection} the commands to open the \texttt{Sect}
826 % structure and to change the para tag.
827 %
828 % We save the current level so that unnumbered sections can use it too.
829 % \begin{macro}{\@currentseclevel}
830 % \begin{macrocode}
831 \newcommand\@currentseclevel{-2}
832 % \end{macrocode}
833 % \end{macro}
834 % \begin{macrocode}
835 \def\@startsection#1#2#3#4#5#6{%
836 \def\@currentseclevel{#2}
837 \if@noskipsec \leavevmode \fi
838 \par
839 \@tempskipa #4\relax
840 \@afterindenttrue
841 \ifdim \@tempskipa <\z@
842 \@tempskipa -\@tempskipa \@afterindentfalse
843 \fi
844 \if@nobreak
845 \everypar{}%
846 \else
847 \addpenalty\@secpenalty\addvspace\@tempskipa
848 \fi
849 \UseTaggingSocket{sec/end}
850 {#2}
851 \UseTaggingSocket{sec/begin}
852 {
853 {#2}
854 {tag=\cs_if_exist_use:cF{g_tag_role_#1_tl}{Sect}}
855 }
856 % \end{macrocode}
857 % \changes{0.84j}{2025-10-07}{Moved para-flattened setting}
858 % This tagging sockets changes the para-tagging: it is flattened and
859 % the tag is changed. This is actually used only by runin headings,
860 % but nevertheless this looks like the best place to use it as we can
861 % not make the changes inside some \cs{everypar} command.
862 % \begin{macrocode}
863 \UseTaggingSocket{sec/title/init}{#2}
864 \@ifstar
865 {\@ssect{#3}{#4}{#5}{#6}}%
866 {\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}
867 % \end{macrocode}
868 %
869 % \cs{@sect} is only changed to replace the hyperref patches
870 % and to use the new \cs{@kernel@tag@hangfrom}.
871 % \begin{macrocode}
872 %<@@=>
873 \def\@sect#1#2#3#4#5#6[#7]#8{%
874 % #1= name, #2= level, #3= indent #4 unused #5 after vspace #6 formatting #7=short title, #8=title
875 \ifnum #2>\c@secnumdepth
876 \bool_set_true:N\l__kernel_sec_nonumber_bool
877 \@kernel@makecurrentHref {#1*}
878 \def\@svsec{\NoCaseChange{\@hyp@section@target@nnn{*}{\@currentHref}{#3}}}
879 \else
880 \bool_set_false:N\l__kernel_sec_nonumber_bool
881 \LinkTargetOff
882 \refstepcounter{#1}%
883 \tl_gset:Ne\@currentHref{#1.\use:c{theH#1}}
884 \LinkTargetOn
885 \protected@edef\@svsec{\NoCaseChange{\@hyp@section@target@nnn{}{#1}{#3}}\@seccntformat{#1}\relax}%
886 \fi
887 \@tempskipa #5\relax
888 \ifdim \@tempskipa>\z@
889 \begingroup
890 #6{%
891 % \end{macrocode}
892 % The formatting can contain a \cs{MakeUppercase}
893 % so we must protect the name of the socket:
894 % \begin{macrocode}
895 \NoCaseChange
896 {\UseTaggingSocket{sec/title/hang}
897 {{#2}\l__kernel_sec_nonumber_bool{\hskip #3\relax\@svsec}{#7}}
898 {\@hangfrom {\hskip #3\relax\@svsec}}}
899 \interlinepenalty \@M #8\@@par}%
900 \endgroup
901 \csname #1mark\endcsname{#7}%
902 \addcontentsline{toc}{#1}{%
903 \ifnum #2>\c@secnumdepth \else
904 \protect\numberline{\csname the#1\endcsname}%
905 \fi
906 #7}%
907 \else
908 \def\@svsechd{%
909 #6{\hskip #3\relax
910 \NoCaseChange{
911 \UseTaggingSocket{sec/title/runin/number}{{#2}\l__kernel_sec_nonumber_bool}{\@svsec}}
912 #8}%
913 \csname #1mark\endcsname{#7}%
914 \addcontentsline{toc}{#1}{%
915 \ifnum #2>\c@secnumdepth \else
916 \protect\numberline{\csname the#1\endcsname}%
917 \fi
918 #7}}%
919 \fi
920 \@xsect{#5}}
921 % \end{macrocode}
922 % similar for \cs{@ssect}
923 % \begin{macrocode}
924 \def\@ssect#1#2#3#4#5{%
925 \@tempskipa #3\relax
926 \ifdim \@tempskipa>\z@
927 \begingroup
928 #4{
929 \NoCaseChange
930 {
931 \UseTaggingSocket{sec/title/hang}
932 {
933 {\@currentseclevel}
934 \c_true_bool
935 {\hskip #1\relax\NoCaseChange{\@hyp@section@target@nnn{[section]}{}{#1}}}
936 {#5}
937 }
938 {\@hangfrom{\hskip #1\relax\NoCaseChange{\@hyp@section@target@nnn{[section]}{}{#1}}}}
939 }
940 \interlinepenalty \@M #5\@@par}%
941 \endgroup
942 \else
943 \@kernel@makecurrentHref{section*}
944 \def\@svsechd{#4{\hskip #1\relax\NoCaseChange{\@hyp@section@target@nnn{*}{\@currentHref}{#3}}\relax #5}}%
945 \fi
946 \@xsect{#3}}
947 % \end{macrocode}
948 % At last \cs{@xsect} needs code in two places. For display headings it has to
949 % restore the default para code, for run in headings it has to separated the
950 % heading from the following text.
951 % \begin{macrocode}
952 \def\@xsect#1{%
953 \@tempskipa #1\relax
954 \ifdim \@tempskipa>\z@
955 \par \nobreak
956 \vskip \@tempskipa
957 \UseTaggingSocket {para/restore}
958 \@afterheading
959 \else
960 \@nobreakfalse
961 \global\@noskipsectrue
962 \everypar{%
963 \if@noskipsec
964 \global\@noskipsecfalse
965 {\setbox\z@\lastbox}%
966 \clubpenalty\@M
967 \begingroup \@svsechd \endgroup
968 \unskip
969 \UseTaggingSocket{sec/title/split}
970 \@tempskipa #1\relax
971 \hskip -\@tempskipa
972 \else
973 \clubpenalty \@clubpenalty
974 \everypar{}%
975 \fi}%
976 \fi
977 \ignorespaces}
978 % \end{macrocode}
979 %
980 % \subsection{Keys for \cs{tagpdfsetup}}
981 % We need to provide user and package level commands
982 % \changes{0.84k}{2025/04/09}{Add braces around key values, tagging issue \#830}
983 % \changes{0.84j}{2025-10-11}{add \cs{tagpdfsetup} keys for two user facing keys}
984 %
985 % \begin{macrocode}
986 \keys_define:nn{__tag / setup}
987 {
988 ,sec/end .code:n =
989 {
990 \par
991 \UseTaggingSocket{sec/end}{\int_eval:n{\cs_if_exist_use:c{toclevel@#1}+0}}
992 }
993 ,sec/end .value_required:n = true
994 ,sec/grouping .choice:,
995 ,sec/grouping / true .code:n =
996 {
997 \AssignTaggingSocketPlug{sec/begin}{kernel}
998 \AssignTaggingSocketPlug{sec/end}{kernel}
999 }
1000 ,sec/grouping / false .code:n =
1001 {
1002 \AssignTaggingSocketPlug{sec/begin}{noop}
1003 \AssignTaggingSocketPlug{sec/end}{noop}
1004 }
1005 ,sec/grouping .default:n = true
1006 }
1007 % \end{macrocode}
1008 %
1009 % \subsubsection{Tagging tools (deprecated)}
1010 % \cs{tag_tool:n} is deprecated.
1011 % \begin{macrocode}
1012 \cs_if_free:NT \tag_tool:n
1013 {
1014 \cs_new_protected:Npn \tag_tool:n #1
1015 {
1016 \tag_if_active:T { \keys_set:nn {tag / tool}{#1} }
1017 }
1018 \cs_set_eq:NN\tagtool\tag_tool:n
1019 }
1020 \keys_define:nn { tag / tool}
1021 {
1022 ,sec-start-part .code:n =
1023 {
1024 \UseTaggingSocket{sec/end}{-1}
1025 \UseTaggingSocket{sec/begin}{{-1}{tag=\UseStructureName{sec/-1}}}
1026 \UseTaggingSocket{sec/title/begin}{{-1}{#1}}
1027 % \end{macrocode}
1028 % We remap here the text-unit from the paragraph to NonStruct.
1029 % It would be better to suppress it completely as with the other
1030 % sectioning commands, but this would require to redefine \cs{@spart}
1031 % and \cs{@part}, as there is the grouping, and these commands are
1032 % all slightly different in the standard classes. So this is delayed
1033 % to the time when sectioning commands are redefined with templates.
1034 % \begin{macrocode}
1035 }
1036 ,sec-stop-part .code:n = {\UseTaggingSocket{sec/title/end}}
1037 ,sec-start-chapter .code:n =
1038 {
1039 \UseTaggingSocket{sec/end}{0}
1040 \UseTaggingSocket{sec/begin}{{0}{tag=\UseStructureName{sec/0}}}
1041 \UseTaggingSocket{sec/title/begin}{{0}{#1}}
1042 }
1043 ,sec-stop-chapter .meta:n = { sec-stop-part}
1044 ,sec-start .code:n = % #1 is a name like "section"
1045 {
1046 \UseTaggingSocket{sec/end} {\int_eval:n{\cs_if_exist_use:c{toclevel@#1}+0}}
1047 \UseTaggingSocket{sec/begin}
1048 {
1049 {\int_eval:n{\cs_if_exist_use:c{toclevel@#1}+0}}
1050 {tag=\cs_if_exist_use:cF{g_tag_role_#1_tl}{Sect}}
1051 }
1052 \tl_set:Nn\l_@@_para_tag_tl{#1}
1053 }
1054 ,sec-start .value_required:n = true
1055 ,sec-split-para .code:n = {\UseTaggingsocket{sec/title/split}}
1056 ,restore-para .code:n = {\UseTaggingSocket{para/restore}}%
1057 ,sec-stop .code:n =
1058 {
1059 \par
1060 \UseTaggingSocket{sec/end}{\int_eval:n{\cs_if_exist_use:c{toclevel@#1}+0}}
1061 }
1062 ,sec-stop .value_required:n = true
1063 ,sec-add-grouping .choice:,
1064 ,sec-add-grouping / true .code:n =
1065 {
1066 \AssignTaggingSocketPlug{sec/begin}{kernel}
1067 \AssignTaggingSocketPlug{sec/end}{kernel}
1068 }
1069 ,sec-add-grouping / false .code:n =
1070 {
1071 \AssignTaggingSocketPlug{sec/begin}{noop}
1072 \AssignTaggingSocketPlug{sec/end}{noop}
1073 }
1074 ,sec-add-grouping .default:n = true
1075 }
1076 %</package>
1077 % \end{macrocode}
1078
1079 % \begin{macrocode}
1080 %<*latex-lab>
1081 \ProvidesFile{sec-latex-lab-testphase.ltx}
1082 [\ltlabsecdate\space v\ltlabsecversion\space latex-lab wrapper sec]
1083 \RequirePackage{latex-lab-testphase-sec}
1084 %</latex-lab>
1085 % \end{macrocode}