summaryrefslogtreecommitdiff
path: root/macros/latex-dev/required/latex-lab/latex-lab-toc-kernel-changes.dtx
blob: bb88ffdcb0377a305095e7ac90127ac1adf262a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
% \iffalse meta-comment
%
%% File: latex-lab-toc-kernel-changes.dtx (C) Copyright 2022-2024 LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
%
% The development version of the bundle can be found below
%
%    https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
% dates for latex-lab-kernel-changes.sty (pulled from various sources, see ins)
\def\ltlabkerneldate{2024-02-12}
\def\ltlabkernelversion{0.85c}
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-toc-kernel-changes.dtx}
\end{document}
%</driver>
%
% \fi
%
% \title{The \textsf{latex-lab-toc-kernel-changes} package\\
% Changes and additions to the kernel related to tagging and links in toc entries}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{Version 0.83 2023-06-07}
%
% \maketitle
%
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
% \newcommand{\docclass}{document class \marginpar{\raggedright document class
% customizations}}
%
% \providecommand\hook[1]{\texttt{#1}}
%
% \begin{abstract}
% \end{abstract}
%
% \section{Introduction}
%
% The followings contains various changes to kernel commands
% needed for the tagging of table of contents and similar lists.
% \section{General kernel and class changes}
% 
% Some of the changes can probably could go into the kernel. 
% This are marked with [kernel?],
% the other are marked with [latex-lab]. A few changes must be done in the classes.
% They are marked as [class]. They are inserted with hooks here: this will break
% if a non-standard class is used.
%
% \subsection{Package declaration}
%    \begin{macrocode}
%<*kernelchange>
\ProvidesPackage{latex-lab-kernel-changes}
        [\ltlabkerneldate\space v\ltlabkernelversion\space 
         General kernel and class changes]
%    \end{macrocode}
%
% \subsection{Providing the counter representation \cs{theHxx} generally}
% [kernel?]
%
% The \cs{theHxx} representation allows to create a unique representation of a counter
% that is for example used to create destination names. It will also be needed
% to add |/Ref| keys to various tagging structures. It makes therefore sense
% to provide it by default as soon as a counter is created. The |\@addtoreset| change
% (taken also from hyperref) tries to ensure the counter stays unique if it is reset.
%
% At first we suppress the patches from hyperref:
%    \begin{macrocode}
\def\hyper@nopatch@counter{}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
     \setcounter{#1}\z@
     \global\expandafter\let\csname cl@#1\endcsname\@empty
     \@addtoreset{#1}{@ckpt}%
     \global\expandafter\let\csname p@#1\endcsname\@empty
     \expandafter\gdef\csname theH#1\endcsname{\the\value{#1}}%new
     \expandafter
     \gdef\csname the#1\expandafter\endcsname\expandafter
          {\expandafter\@arabic\csname c@#1\endcsname}}
\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}%
  \expandafter\gdef\csname theH#1\endcsname{\csname theH#2\endcsname.\the\value{#1}}%
}
%    \end{macrocode}
% The following counters are defined in the kernel
%    \begin{macrocode}
\gdef\theHenumi{\the\value{enumi}}
\gdef\theHenumii{\the\value{enumii}}
\gdef\theHenumiii{\the\value{enumiii}}
\gdef\theHenumiv{\the\value{enumiv}}
\gdef\theHequation{\the\value{equation}}
\gdef\theHfootnote{\the\value{footnote}}
\gdef\theHmpfootnote{\the\value{mpfootnote}}
%    \end{macrocode}
%
% \subsection{Providing and updating \cs{@currentHref}}
% [kernel?]
%
%  \cs{@currentHref} contains the current unique representation of a counter.
%  It is useful also without hyperref as it allows to connect with a |/Ref|
%  key a toc entry with the heading it refers too. So \cs{refstepcounter}
%  is extended to update the command. This is done at the begin so that the
%  hyperref code can still adapt it later (using the cmd/refstepcounter/after hook
%  would be too late, and could also break with cleverref.)
%
%  We also provide a kernel version that hyperref doesn't touch and so will never
%  set a target.
%    \begin{macrocode}
\def\@currentHref{}
\def\refstepcounter#1{\stepcounter{#1}%
    \edef\@currentcounter{#1}%
    \xdef\@currentHref {#1.\csname theH#1\endcsname}%
    \protected@edef\@currentlabel
       {\csname p@#1\expandafter\endcsname\csname the#1\endcsname}%
}
\let\@kernel@refstepcounter\refstepcounter
%    \end{macrocode}
%
% \subsection{Assigning the headings level numbers}
% [kernel?]
%
% The code for the toc must be able to compare the level of |subsection| against
% |section|. So some numbers are needed. It uses for now the commands used by
% |hyperref| for the bookmark levels. While headings are more a class thing, it
% could make sense to provide a standard set already in the kernel.
% The numbers are a bit different to the one in hyperref, but imho sensible.
% 
% Note for the future: 
% once that heading stuff is turned into template 
% instances the template should set that number.
%    \begin{macrocode}
\def\toclevel@document{-10}
\def\toclevel@part{-1}
\def\toclevel@chapter{0}
\def\toclevel@section{1}
\def\toclevel@subsection{2}
\def\toclevel@subsubsection{3}
\def\toclevel@paragraph{4}
\def\toclevel@subparagraph{5}
\def\toclevel@figure{1}
\def\toclevel@table{1}
%    \end{macrocode}
%
%\subsection{Storing the relation between target names and structure numbers}
% To be able to add a /Ref key to structures the tagging makes use of target names
% and stores the relationship in a property. We add a hook to \cs{MakeLinkTarget} to 
% catch as much as possible, see also tagging issue \#20. This should work also
% without hyperref. The property is defined in tagpdf-base, so the code would 
% work also without tagging but we add a test anyway, this is probably faster.
%    \begin{macrocode}
\ExplSyntaxOn
\AddToHookWithArguments{cmd/MakeLinkTarget/after}
  {
    \tag_if_active:T
      {    
        \tl_if_blank:VF \@currentHref
          {
            \prop_gput:Nee \g__tag_struct_dest_num_prop {\@currentHref}{\tag_get:n{struct_num}}
          }
      }    
  }
\ExplSyntaxOff  
%    \end{macrocode}
%    \begin{macrocode}
%</kernelchange>
%    \end{macrocode}
%
% \subsection{load kernel changes}
%    \begin{macrocode}
%<*package>
\RequirePackage{latex-lab-kernel-changes}
%    \end{macrocode}
%
% \subsection{\cs{contentsline} extension}
% [latex-lab]
% The tagging code must add code at the begin and end of \cs{contentsline}.
% This code must have access to the arguments. We set it up as config point
% for now.
%    \begin{macrocode}
\def\@contentsline@cfgpoint@before#1#2#3#4{}
\def\@contentsline@cfgpoint@after#1#2#3#4{}
%    \end{macrocode}
% for the hooks we need that the \cs{l@XX} pass also the destination and
% that \cs{l@XX} takes three arguments. 
% TODO: test that, if there are two many problems we should pass the 
% destination through a command name ...
%    \begin{macrocode}
\def\contentsline#1#2#3#4%
  {%
    \@contentsline@cfgpoint@before{#1}{#2}{#3}{#4}%
    \gdef\@contentsline@destination{#4}%
    \csname l@#1\endcsname{#2}{#3}%
    \@contentsline@cfgpoint@after{#1}{#2}{#3}{#4}%
  }
%    \end{macrocode}
%
% \subsection{\cs{addcontentsline} changes}
% [latex-lab]
%
% \cs{addcontentsline} should always write the unique counter representation
% \cs{@currentHref} so that we can create the |/Ref| key. At the same time
% we need a hook, so that hyperref can add its bookmarks code, the generic hook
% with arguments is ok. 
%
%    \begin{macrocode}
\def\addcontentsline#1#2#3{% #1=toc extension, #2= heading type, tag
    \addtocontents{#1}{%
      \protect\contentsline{#2}{#3}{\thepage}{\@currentHref}\protected@file@percent
    }}
%    \end{macrocode}
%
% \subsection{\cs{@starttoc}}
% [kernel?]
%
% We add a configuration point before and after reading the file
% for the tagging.
%    \begin{macrocode}
\def\@starttoc@cfgpoint@before#1{}
\def\@starttoc@cfgpoint@after#1{}
\def\@starttoc#1{%
  \begingroup
    \makeatletter
    \@starttoc@cfgpoint@before{#1}%
    \@input{\jobname.#1}%
    \@starttoc@cfgpoint@after{#1}%
    \if@filesw
      \expandafter\newwrite\csname tf@#1\endcsname
      \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
  \endgroup}
%    \end{macrocode}
%
% \subsection{Formatting of the toc entries}
%
% \subsubsection{Kernel commands}
% [latex-lab] or [kernel?]?
%
% We need hooks before and after the content and before and after the page number.
% The number needs some thing so that it can be tagged as label.
% The text and the page
% hooks are used in manual \cs{l@xx} commands and in \cs{l@xx} defined by
% \cs{@dottedtocline} or by other means.
% The arguments of the following hooks are:
%  \begin{itemize}
%  \item[\#1] level as a \emph{number} (integer expression), see above for the standard
%   numbering. The standard first argument of \cs{contentsline} is not used, as
%   \cs{@dottedtocline} knows only a number.
%  \item[\#2] content (the second argument of a \cs{contentsline}
%  \item[\#3] page    (the third argument of a \cs{contentsline}
%  \item[\#3] target  (the fourth argument of a \cs{contentsline}
%  \end{itemize}
%    \begin{macrocode}
\NewMirroredHookPairWithArguments{contentsline/number/before}{contentsline/number/after}{1}%
\NewMirroredHookPairWithArguments{contentsline/text/before}{contentsline/text/after}{4}%
\NewMirroredHookPairWithArguments{contentsline/page/before}{contentsline/page/after}{4}%
%    \end{macrocode}
%
% The dot in the leader must be set as artifact.
% But luatex requires that the mc are set outside the leaders.
% So we need a config point.
%
% \cs{@dottedtocline} needs hooks and formatting commands.
% We add a sixth argument so that it can grab the destination too.
%    \begin{macrocode}
\def\@dottedtocline@cfgpoint@leaders#1{#1}

\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     \UseHookWithArguments{contentsline/text/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     \csname contentsline@text@#1@format\endcsname{#4}%
     \UseHookWithArguments{contentsline/text/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     \nobreak
     \@dottedtocline@cfgpoint@leaders{%
       \leaders\hbox{$\m@th
         \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
          mu$}\hfill}%
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor
     \UseHookWithArguments{contentsline/page/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     #5%
     \UseHookWithArguments{contentsline/page/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
                        \kern-\p@\kern\p@}%
     \par}%
  \fi}
%    \end{macrocode}
%
% \subsubsection{\cs{numberline}}
% [kernel?]
% Inside or outside the box?
%    \begin{macrocode}
\def\numberline#1{\hb@xt@\@tempdima{%
  \UseHookWithArguments{contentsline/number/before}{1}{#1}%
  #1\hfil
  \UseHookWithArguments{contentsline/number/after}{1}{#1}%
  }}
%    \end{macrocode}
%
% \subsubsection{\cs{l@xx} in the classes}
% Depending on the class we have to redefine also more commands
% For other classes it should be documented what is needed.
% Let the commands grab also the third arguments. If a class redefines them again
% it will break tagging anyway.
%    \begin{macrocode}
\AddToHook{class/article/after}
 {
  \renewcommand*\l@part[2]{% as in contentsline: content, page
  \ifnum \c@tocdepth >-2\relax
    \addpenalty\@secpenalty
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries
       \UseHookWithArguments{contentsline/text/before}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       \csname contentsline@text@-1@format\endcsname{#1}%
       \UseHookWithArguments{contentsline/text/after}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       \hfil
       \hb@xt@\@pnumwidth{\hss
       \UseHookWithArguments{contentsline/page/before}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       #2%
       \UseHookWithArguments{contentsline/page/after}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
                          \kern-\p@\kern\p@}}\par
       \nobreak
    \endgroup
  \fi}
\renewcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
       \UseHookWithArguments{contentsline/text/before}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       \csname contentsline@text@1@format\endcsname{#1}%
       \UseHookWithArguments{contentsline/text/after}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       \nobreak\hfil
      \nobreak\hb@xt@\@pnumwidth{\hss
      \UseHookWithArguments{contentsline/page/before}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       #2%
      \UseHookWithArguments{contentsline/page/after}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
      \kern-\p@\kern\p@}\par
    \endgroup
  \fi}
 }
\AddToHook{class/report/after}
 {
    \renewcommand*\l@part[2]{%
     \ifnum \c@tocdepth >-2\relax
       \addpenalty{-\@highpenalty}%
       \addvspace{2.25em \@plus\p@}%
       \setlength\@tempdima{3em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         {\leavevmode
          \large \bfseries
          \UseHookWithArguments{contentsline/text/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          \csname contentsline@text@-1@format\endcsname{#1}%
          \UseHookWithArguments{contentsline/text/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          \hfil
          \hb@xt@\@pnumwidth{\hss          
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
                             \kern-\p@\kern\p@}}\par
          \nobreak
       \endgroup
     \fi}
   \renewcommand*\l@chapter[2]{%
     \ifnum \c@tocdepth >\m@ne
       \addpenalty{-\@highpenalty}%
       \vskip 1.0em \@plus\p@
       \setlength\@tempdima{1.5em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         \leavevmode \bfseries
         \advance\leftskip\@tempdima
         \hskip -\leftskip
         \UseHookWithArguments{contentsline/text/before}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
         \csname contentsline@text@0@format\endcsname
           {#1}%
         \UseHookWithArguments{contentsline/text/after}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          \nobreak\hfil
         \nobreak\hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
                                    \kern-\p@\kern\p@}\par
         \penalty\@highpenalty
       \endgroup
     \fi}
 }
\AddToHook{class/book/after}
 {
    \renewcommand*\l@part[2]{%
     \ifnum \c@tocdepth >-2\relax
       \addpenalty{-\@highpenalty}%
       \addvspace{2.25em \@plus\p@}%
       \setlength\@tempdima{3em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         {\leavevmode
          \large \bfseries
          \UseHookWithArguments{contentsline/text/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          \csname contentsline@text@-1@format\endcsname{#1}%
          \UseHookWithArguments{contentsline/text/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          \hfil
          \hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
                             \kern-\p@\kern\p@}}\par
          \nobreak
       \endgroup
     \fi}
   \renewcommand*\l@chapter[2]{%
     \ifnum \c@tocdepth >\m@ne
       \addpenalty{-\@highpenalty}%
       \vskip 1.0em \@plus\p@
       \setlength\@tempdima{1.5em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         \leavevmode \bfseries
         \advance\leftskip\@tempdima
         \hskip -\leftskip
         \UseHookWithArguments{contentsline/text/before}{4}
          {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
         \csname contentsline@text@0@format\endcsname
           {#1}%
         \UseHookWithArguments{contentsline/text/after}{4}
          {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          \nobreak\hfil
         \nobreak\hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
                                    \kern-\p@\kern\p@}\par
         \penalty\@highpenalty
       \endgroup
     \fi}
 }
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}