Debian Codegraph @

texmf-dist/source/latex/latex-lab/latex-lab-toc-kernel-changes.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-toc-kernel-changes.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 % dates for latex-lab-kernel-changes.sty (pulled from various sources, see ins)
20 \def\ltlabkerneldate{2025-07-09}
21 \def\ltlabkernelversion{0.85i}
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-toc-kernel-changes.dtx}
30 \end{document}
31 %</driver>
32 %
33 % \fi
34 %
35 % \title{The \textsf{latex-lab-toc-kernel-changes} package\\
36 % Changes and additions to the kernel related to tagging and links in toc entries}
37 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
38 % \date{Version 0.83 2023-06-07}
39 %
40 % \maketitle
41 %
42 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
43 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
44 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
45 % customizations}}
46 %
47 % \providecommand\hook[1]{\texttt{#1}}
48 %
49 %
50 % \ProvideDocElement[printtype=\textit{socket},idxtype=socket,idxgroup=Sockets]{Socket}{socketdecl}
51 % \ProvideDocElement[printtype=\textit{hook},idxtype=hook,idxgroup=Hooks]{Hook}{hookdecl}
52 % \ProvideDocElement[printtype=\textit{plug},idxtype=plug,idxgroup=Plugs]{Plug}{plugdecl}
53 %
54 % \begin{abstract}
55 % \end{abstract}
56 %
57 % \section{Introduction}
58 %
59 % The followings contains various changes to kernel commands
60 % needed for the tagging of table of contents and similar lists.
61 % \section{General kernel and class changes}
62 %
63 % Some of the changes can probably could go into the kernel.
64 % This are marked with [kernel?],
65 % the other are marked with [latex-lab]. A few changes must be done in the classes.
66 % They are marked as [class]. They are inserted with hooks here: this will break
67 % if a non-standard class is used.
68 %
69 % \subsection{Package declaration}
70 % \begin{macrocode}
71 %<*kernelchange>
72 \ProvidesPackage{latex-lab-kernel-changes}
73 [\ltlabkerneldate\space v\ltlabkernelversion\space
74 General kernel and class changes]
75 % \end{macrocode}
76 %
77 % \changes{v0.85i}{2025/07/09}{removed now unneeded hyperref definition, socket declaration}
78
79 % \subsection{Assigning the headings level numbers}
80 % [kernel?]
81 %
82 % The code for the toc must be able to compare the level of |subsection| against
83 % |section|. So some numbers are needed. It uses for now the commands used by
84 % |hyperref| for the bookmark levels. While headings are more a class thing, it
85 % could make sense to provide a standard set already in the kernel.
86 % The numbers are a bit different to the one in hyperref, but imho sensible.
87 %
88 % Note for the future:
89 % once that heading stuff is turned into template
90 % instances the template should set that number.
91 % \begin{macrocode}
92 \def\toclevel@document{-10}
93 \def\toclevel@part{-1}
94 \def\toclevel@chapter{0}
95 \def\toclevel@section{1}
96 \def\toclevel@subsection{2}
97 \def\toclevel@subsubsection{3}
98 \def\toclevel@paragraph{4}
99 \def\toclevel@subparagraph{5}
100 \def\toclevel@figure{1}
101 \def\toclevel@table{1}
102 % \end{macrocode}
103 %
104 % \subsection{Storing relationship between target and structure}
105 % Most of this is now in the kernel, but the definition of \cs{MakeLinkTarget} in hyperref misses
106 % the socket. Until it is there, we add it through a hook:
107 % \begin{macrocode}
108 \ExplSyntaxOn
109 \AddToHook{package/hyperref/after}
110 {
111 \RenewDocumentCommand\MakeLinkTarget {s O{} m}
112 {
113 \bool_if:NT \l__hyp_target_create_bool
114 {
115 \group_begin:
116 \hook_use:n { makelinktarget }
117 \IfBooleanTF {#1}
118 {
119 \__hyp_target_manual:nn {#2}{#3}
120 }
121 {
122 \__hyp_target_counter:nn {#2}{#3}
123 }
124 \group_end:
125 \UseTaggingSocket{recordtarget}
126 }
127 }
128 }
129 \ExplSyntaxOff
130 % \end{macrocode}
131 % \begin{macrocode}
132 %</kernelchange>
133 % \end{macrocode}
134 %
135 % \subsection{load kernel changes}
136 % \begin{macrocode}
137 %<*package>
138 \RequirePackage{latex-lab-kernel-changes}
139 % \end{macrocode}
140 %
141 % \subsection{\cs{contentsline} extension}
142 % [latex-lab]
143 % The tagging code must add code at the begin and end of \cs{contentsline}.
144 % This code must have access to the arguments. They are passed
145 % together in tagging socket which are declared in lttagging.
146 % For the hooks we need that the \cs{l@XX} pass also the destination and
147 % that \cs{l@XX} takes three arguments.
148 % TODO: test that, if there are two many problems we should pass the
149 % destination through a command name ...
150 % \begin{macrocode}
151 \def\contentsline#1#2#3#4%
152 {%
153 \UseTaggingSocket{toc/contentsline/before}{{#1}{#2}{#3}{#4}}%
154 \gdef\@contentsline@destination{#4}%
155 \csname l@#1\endcsname{#2}{#3}%
156 \UseTaggingSocket{toc/contentsline/after}{{#1}{#2}{#3}{#4}}%
157 }
158 % \end{macrocode}
159 %
160 % \subsection{\cs{addcontentsline} changes}
161 % [latex-lab]
162 %
163 % \cs{addcontentsline} should always write the unique counter representation
164 % \cs{@currentHref} so that we can create the |/Ref| key. At the same time
165 % we need a hook, so that hyperref can add its bookmarks code, the generic hook
166 % with arguments is ok.
167 %
168 % \begin{macrocode}
169 \def\addcontentsline#1#2#3{% #1=toc extension, #2= heading type, tag
170 \addtocontents{#1}{%
171 \protect\contentsline{#2}{#3}{\thepage}{\@currentHref}\protected@file@percent
172 }}
173 % \end{macrocode}
174 %
175 % \subsection{\cs{@starttoc}}
176 % [kernel?]
177 %
178 %
179 % \begin{macrocode}
180 \def\@starttoc#1{%
181 \begingroup
182 \makeatletter
183 \UseTaggingSocket{toc/starttoc/before}{#1}%
184 \@input{\jobname.#1}%
185 \UseTaggingSocket{toc/starttoc/after}{#1}%
186 \if@filesw
187 \expandafter\newwrite\csname tf@#1\endcsname
188 \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
189 \fi
190 \@nobreakfalse
191 \endgroup}
192 % \end{macrocode}
193 %
194 % \subsection{Formatting of the toc entries}
195 %
196 % \subsubsection{Kernel commands}
197 % [latex-lab] or [kernel?]?
198 %
199 % We need hooks before and after the content and before and after the page number.
200 % The number needs some thing so that it can be tagged as label.
201 % The text and the page
202 % hooks are used in manual \cs{l@xx} commands and in \cs{l@xx} defined by
203 % \cs{@dottedtocline} or by other means.
204 % The arguments of the following hooks are:
205 % \begin{itemize}
206 % \item[\#1] level as a \emph{number} (integer expression), see above for the standard
207 % numbering. The standard first argument of \cs{contentsline} is not used, as
208 % \cs{@dottedtocline} knows only a number.
209 % \item[\#2] content (the second argument of a \cs{contentsline}
210 % \item[\#3] page (the third argument of a \cs{contentsline}
211 % \item[\#3] target (the fourth argument of a \cs{contentsline}
212 % \end{itemize}
213 % \begin{macrocode}
214 \NewMirroredHookPairWithArguments{contentsline/number/before}{contentsline/number/after}{1}%
215 \NewMirroredHookPairWithArguments{contentsline/text/before}{contentsline/text/after}{4}%
216 \NewMirroredHookPairWithArguments{contentsline/page/before}{contentsline/page/after}{4}%
217 % \end{macrocode}
218 %
219 % The dots in the leader must be set as artifact.
220 % But luatex requires that the mc are set outside the leaders.
221 % So we need sockets.
222 %
223 % \cs{@dottedtocline} needs hooks and formatting commands.
224 % To make the leaders into artifact we use tagging sockets.
225 %
226 % The destination is accessed through \cs{@contentsline@destination}.
227 % \begin{macrocode}
228 \def\@dottedtocline#1#2#3#4#5{%
229 \ifnum #1>\c@tocdepth \else
230 \vskip \z@ \@plus.2\p@
231 {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
232 \parindent #2\relax\@afterindenttrue
233 \interlinepenalty\@M
234 \leavevmode
235 \@tempdima #3\relax
236 \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
237 \UseHookWithArguments{contentsline/text/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
238 \csname contentsline@text@#1@format\endcsname{#4}%
239 \UseHookWithArguments{contentsline/text/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
240 \nobreak
241 \UseTaggingSocket{toc/leaders/before}\SuspendTagging{toc/leaders}%
242 \leaders\hbox{$\m@th
243 \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
244 mu$}\hfill
245 \ResumeTagging{toc/leaders}\UseTaggingSocket{toc/leaders/after}%
246 \nobreak
247 \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor
248 \UseHookWithArguments{contentsline/page/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
249 #5%
250 \UseHookWithArguments{contentsline/page/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
251 \kern-\p@\kern\p@}%
252 \par}%
253 \fi}
254 % \end{macrocode}
255 %
256 % \subsubsection{\cs{numberline}}
257 % [kernel?]
258 % Inside or outside the box?
259 % \begin{macrocode}
260 \def\numberline#1{\hb@xt@\@tempdima{%
261 \UseHookWithArguments{contentsline/number/before}{1}{#1}%
262 #1\hfil
263 \UseHookWithArguments{contentsline/number/after}{1}{#1}%
264 }}
265 % \end{macrocode}
266 %
267 % \subsubsection{\cs{l@xx} in the classes}
268 % Depending on the class we have to redefine also more commands
269 % For other classes it should be documented what is needed.
270 % Let the commands grab also the third arguments. If a class redefines them again
271 % it will break tagging anyway.
272 % \begin{macrocode}
273 \AddToHook{class/article/after}
274 {
275 \renewcommand*\l@part[2]{% as in contentsline: content, page
276 \ifnum \c@tocdepth >-2\relax
277 \addpenalty\@secpenalty
278 \addvspace{2.25em \@plus\p@}%
279 \setlength\@tempdima{3em}%
280 \begingroup
281 \parindent \z@ \rightskip \@pnumwidth
282 \parfillskip -\@pnumwidth
283 {\leavevmode
284 \large \bfseries
285 \UseHookWithArguments{contentsline/text/before}{4}
286 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
287 \csname contentsline@text@-1@format\endcsname{#1}%
288 \UseHookWithArguments{contentsline/text/after}{4}
289 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
290 \hfil
291 \hb@xt@\@pnumwidth{\hss
292 \UseHookWithArguments{contentsline/page/before}{4}
293 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
294 #2%
295 \UseHookWithArguments{contentsline/page/after}{4}
296 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
297 \kern-\p@\kern\p@}}\par
298 \nobreak
299 \endgroup
300 \fi}
301 \renewcommand*\l@section[2]{%
302 \ifnum \c@tocdepth >\z@
303 \addpenalty\@secpenalty
304 \addvspace{1.0em \@plus\p@}%
305 \setlength\@tempdima{1.5em}%
306 \begingroup
307 \parindent \z@ \rightskip \@pnumwidth
308 \parfillskip -\@pnumwidth
309 \leavevmode \bfseries
310 \advance\leftskip\@tempdima
311 \hskip -\leftskip
312 \UseHookWithArguments{contentsline/text/before}{4}
313 {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
314 \csname contentsline@text@1@format\endcsname{#1}%
315 \UseHookWithArguments{contentsline/text/after}{4}
316 {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
317 \nobreak\hfil
318 \nobreak\hb@xt@\@pnumwidth{\hss
319 \UseHookWithArguments{contentsline/page/before}{4}
320 {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
321 #2%
322 \UseHookWithArguments{contentsline/page/after}{4}
323 {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
324 \kern-\p@\kern\p@}\par
325 \endgroup
326 \fi}
327 }
328 \AddToHook{class/report/after}
329 {
330 \renewcommand*\l@part[2]{%
331 \ifnum \c@tocdepth >-2\relax
332 \addpenalty{-\@highpenalty}%
333 \addvspace{2.25em \@plus\p@}%
334 \setlength\@tempdima{3em}%
335 \begingroup
336 \parindent \z@ \rightskip \@pnumwidth
337 \parfillskip -\@pnumwidth
338 {\leavevmode
339 \large \bfseries
340 \UseHookWithArguments{contentsline/text/before}{4}
341 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
342 \csname contentsline@text@-1@format\endcsname{#1}%
343 \UseHookWithArguments{contentsline/text/after}{4}
344 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
345 \hfil
346 \hb@xt@\@pnumwidth{\hss
347 \UseHookWithArguments{contentsline/page/before}{4}
348 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
349 #2%
350 \UseHookWithArguments{contentsline/page/after}{4}
351 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
352 \kern-\p@\kern\p@}}\par
353 \nobreak
354 \endgroup
355 \fi}
356 \renewcommand*\l@chapter[2]{%
357 \ifnum \c@tocdepth >\m@ne
358 \addpenalty{-\@highpenalty}%
359 \vskip 1.0em \@plus\p@
360 \setlength\@tempdima{1.5em}%
361 \begingroup
362 \parindent \z@ \rightskip \@pnumwidth
363 \parfillskip -\@pnumwidth
364 \leavevmode \bfseries
365 \advance\leftskip\@tempdima
366 \hskip -\leftskip
367 \UseHookWithArguments{contentsline/text/before}{4}
368 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
369 \csname contentsline@text@0@format\endcsname
370 {#1}%
371 \UseHookWithArguments{contentsline/text/after}{4}
372 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
373 \nobreak\hfil
374 \nobreak\hb@xt@\@pnumwidth{\hss
375 \UseHookWithArguments{contentsline/page/before}{4}
376 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
377 #2%
378 \UseHookWithArguments{contentsline/page/after}{4}
379 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
380 \kern-\p@\kern\p@}\par
381 \penalty\@highpenalty
382 \endgroup
383 \fi}
384 }
385 \AddToHook{class/book/after}
386 {
387 \renewcommand*\l@part[2]{%
388 \ifnum \c@tocdepth >-2\relax
389 \addpenalty{-\@highpenalty}%
390 \addvspace{2.25em \@plus\p@}%
391 \setlength\@tempdima{3em}%
392 \begingroup
393 \parindent \z@ \rightskip \@pnumwidth
394 \parfillskip -\@pnumwidth
395 {\leavevmode
396 \large \bfseries
397 \UseHookWithArguments{contentsline/text/before}{4}
398 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
399 \csname contentsline@text@-1@format\endcsname{#1}%
400 \UseHookWithArguments{contentsline/text/after}{4}
401 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
402 \hfil
403 \hb@xt@\@pnumwidth{\hss
404 \UseHookWithArguments{contentsline/page/before}{4}
405 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
406 #2%
407 \UseHookWithArguments{contentsline/page/after}{4}
408 {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
409 \kern-\p@\kern\p@}}\par
410 \nobreak
411 \endgroup
412 \fi}
413 \renewcommand*\l@chapter[2]{%
414 \ifnum \c@tocdepth >\m@ne
415 \addpenalty{-\@highpenalty}%
416 \vskip 1.0em \@plus\p@
417 \setlength\@tempdima{1.5em}%
418 \begingroup
419 \parindent \z@ \rightskip \@pnumwidth
420 \parfillskip -\@pnumwidth
421 \leavevmode \bfseries
422 \advance\leftskip\@tempdima
423 \hskip -\leftskip
424 \UseHookWithArguments{contentsline/text/before}{4}
425 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
426 \csname contentsline@text@0@format\endcsname
427 {#1}%
428 \UseHookWithArguments{contentsline/text/after}{4}
429 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
430 \nobreak\hfil
431 \nobreak\hb@xt@\@pnumwidth{\hss
432 \UseHookWithArguments{contentsline/page/before}{4}
433 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
434 #2%
435 \UseHookWithArguments{contentsline/page/after}{4}
436 {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
437 \kern-\p@\kern\p@}\par
438 \penalty\@highpenalty
439 \endgroup
440 \fi}
441 }
442 % \end{macrocode}
443 % \begin{macrocode}
444 %</package>
445 % \end{macrocode}