Debian Codegraph @

texmf-dist/source/latex-dev/latex-lab/latex-lab-toc.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-toc.dtx (C) Copyright 2022-2026 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 latex-lab bundle is developed in the LaTeX2e GitHub.
14 % Issues may be reported at
15 %
16 % https://github.com/latex3/latex2e/issues
17 %
18 \def\ltlabtocdate{2026-04-28}
19 \def\ltlabtocversion{0.85k}
20 %<*driver>
21 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
22 \documentclass[kernel]{l3in2edoc}
23
24 \EnableCrossrefs
25 \CodelineIndex
26 \begin{document}
27 \DocInput{latex-lab-toc.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 %
34 % \title{The \textsf{latex-lab-toc} package\\
35 % Changes related to the tagging of toc and similar lists}
36 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
37 % \date{v\ltlabtocdate\ \ltlabtocversion}
38 %
39 % \maketitle
40 %
41 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
42 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
43 % \newcommand{\docclass}{document class \marginpar{\raggedright document class
44 % customizations}}
45 %
46 %
47 %
48 % \begin{abstract}
49 % \end{abstract}
50 %
51 % Header for the testphase package
52 % \begin{macrocode}
53 %<*header>
54 \ProvidesExplPackage {latex-lab-testphase-toc} {\ltlabtocdate} {\ltlabtocversion}
55 { Code related to the tagging of toc-like lists}
56 %</header>
57 % \end{macrocode}
58 % \begin{documentation}
59 % \section{Introduction}
60 %
61 % The followings contains various functions related to the tagging of the
62 % table of contents and similar list.
63 %
64 % The structure of tocs consist of nested TOC and TOCI structures.
65 % The code uses the first argument of the \cs{contentsline} command to detect the
66 % level and to decided if a structure should be closed. The structure that
67 % should be used in \texttt{/Ref} key to link to the heading is detected from the
68 % target name in the fourth argument -- because of this with this code such a target
69 % name is created and stored also if hyperref is not loaded.
70 %
71 % \subsection{Manual toc additions}
72 % As the \texttt{/Ref} key relies on the target name,
73 % manual \cs{addcontentsline} commands must ensure that they reference the right
74 % structure. If an unnumbered heading command is used before this is normally the case,
75 % so the following should work fine:
76 %
77 % \begin{verbatim}
78 % \chapter*{Unnumbered}
79 % \addcontentsline{toc}{chapter}{Unnumbered}
80 % \end{verbatim}
81 %
82 % If there is no heading command a target name must be created manually \emph{inside the
83 % right structure} with \cs{MakeLinkTarget}:
84 %
85 % \begin{verbatim}
86 % \noindent % start e.g. P-structure
87 % \MakeLinkTarget*{unnumbered}% target inside the P-structure
88 % Unnumbered
89 % \addcontentsline{toc}{chapter}{Unnumbered}
90 % \end{verbatim}
91 %
92 % \end{documentation}
93 % \begin{implementation}
94 % \begin{macrocode}
95 %<*package>
96 %<@@=tag>
97 % \end{macrocode}
98 % \section{Temporary variables}
99 % \begin{macro}{\l_@@_toc_tmpa_tl}
100 % \begin{macrocode}
101 \tl_new:N \l_@@_toc_tmpa_tl
102 % \end{macrocode}
103 % \end{macro}
104 %
105 % \section{General struct commands}
106 % The following variables and commands are not restricted to toc, but
107 % probably will be need in other places too.
108 % \begin{variable}{\g_@@_struct_dest_num_prop}
109 % This variable records for (some or all, not clear yet)
110 % destination names the related structure number to allow
111 % to reference them in a Ref. The key is the destination.
112 % Defined by tagpdf.
113 % \end{variable}
114 %
115 % \changes{0.85h}{2025-07-02}{Removed patches for hyperref to record targets, is now in the kernel.}
116 %
117 % \section{Toc code}
118 % \begin{variable}{\g_@@_toc_level_int,\g_@@_toc_stack_seq}
119 % |\g_@@_toc_level_int| records in a toc the current absolute level.
120 % We must close open structures at the end of the toc, for this
121 % we maintain a stack |\g_@@_toc_stack_seq|.
122 % \begin{macrocode}
123 \int_new:N \g_@@_toc_level_int
124 \seq_new:N \g_@@_toc_stack_seq
125 % \end{macrocode}
126 % \end{variable}
127
128 % \begin{macro}[no-user-doc]{\_tag_toc_starttoc_init:n}
129 % The init code clears the stack, and set the level to -100
130 % and start to TOC structure. We also disable paratagging.
131 % The |/Title| is currently simply the type, but this could be done nicer.
132 % The argument is the argument of \cs{@starttoc}, so, e.g., \texttt{toc}.
133 % \changes{0.85i}{2026-04-27}{Use symbolic names}
134 % \begin{macrocode}
135 \cs_new_protected:Npn \@@_toc_starttoc_init:n #1
136 {
137 \bool_set_false:N \l_@@_para_bool
138 \seq_gclear:N \g_@@_toc_stack_seq
139 \int_gset:Nn \g_@@_toc_level_int {-100}
140 \tag_struct_begin:n{tag=\UseStructureName{toc},title=#1}
141 }
142 % \end{macrocode}
143 % \end{macro}
144 % Now define the tagging plug and assign it to the socket.
145 % \begin{plugdecl}{default}
146 % \begin{macrocode}
147 \NewTaggingSocketPlug{toc/starttoc/before}{default}
148 {
149 \@@_toc_starttoc_init:n{#1}
150 }
151 \AssignTaggingSocketPlug{toc/starttoc/before}{default}
152 % \end{macrocode}
153 % \end{plugdecl}
154 % \begin{macro}{\@@_toc_starttoc_finalize:}
155 % \begin{macrocode}
156 \cs_new_protected:Npn \@@_toc_starttoc_finalize:
157 {
158 \int_step_inline:nn
159 {\seq_count:N \g_@@_toc_stack_seq }
160 {\tag_struct_end:}
161 \tag_struct_end:
162 \seq_gclear:N \g_@@_toc_stack_seq
163 }
164 % \end{macrocode}
165 % \end{macro}
166 % Now define the tagging plug and assign it to the socket.
167 % \begin{plugdecl}{default}
168 % \begin{macrocode}
169 \NewTaggingSocketPlug{toc/starttoc/after}{default}
170 {
171 \@@_toc_starttoc_finalize:
172 }
173 \AssignTaggingSocketPlug{toc/starttoc/after}{default}
174 % \end{macrocode}
175 % \end{plugdecl}
176 %
177 % \begin{macro}{\@@_toc_end:n}
178 % This commands ends all TOC on the stack with a level higher than the argument
179 % \begin{macrocode}
180 \cs_new_protected:Npn \@@_toc_end:n #1
181 {
182 \seq_get:NNT\g_@@_toc_stack_seq \l_@@_toc_tmpa_tl
183 {
184 \bool_lazy_and:nnT
185 {
186 \str_if_eq_p:ee{\tl_head:N\l_@@_toc_tmpa_tl}{TOC}
187 }
188 {
189 \int_compare_p:nNn {#1}<{\tl_tail:N \l_@@_toc_tmpa_tl}
190 }
191 {
192 \seq_gpop:NN\g_@@_toc_stack_seq \l_@@_toc_tmpa_tl
193 \tag_struct_end:
194 \@@_toc_end:n{#1}
195 }
196 }
197 }
198 \cs_generate_variant:Nn \@@_toc_end:n {e}
199 % \end{macrocode}
200 % \end{macro}
201 %
202 % \begin{macro}{\@@_toc_contentsline_begin:nnnn}
203 % This is main command executed at the begin of a |\contentsline|.
204 % \changes{0.85h}{2025-07-02}{Removed unneeded \cs{tag_if_active:T}, command is used in tagging socket.}
205 % \begin{macrocode}
206 \cs_new_protected:Npn \@@_toc_contentsline_begin:nnnn #1 #2 #3 #4
207 %#1 level, #2 content, #3 page number (unused) #4 destination
208 {
209 % \end{macrocode}
210 % We detect the intended level by checking the value of |toclevel@...|
211 % (currently only provided by hyperref, but should be there always).
212 % To be on the safe side we set it to 1 if not defined.
213 % \begin{macrocode}
214 \ExpandArgs{c}\providecommand { toclevel@#1 }{ 1 } % just in case ...
215 \int_compare:nNnF { \use:c{toclevel@#1} } > {\use:c{c@tocdepth}}
216 {
217 % \end{macrocode}
218 % if level goes up, start new sub TOC unless we are at the begin
219 % \begin{macrocode}
220 \bool_lazy_and:nnT
221 { \int_compare_p:nNn { \g_@@_toc_level_int } > {-100} }
222 { \int_compare_p:nNn { \use:c{toclevel@#1} } > { \g_@@_toc_level_int } }
223 {
224 \seq_gpush:Ne \g_@@_toc_stack_seq {{TOC}\use:c{toclevel@#1}}
225 \tag_struct_begin:n{tag=\UseStructureName{toc}}
226 }
227 % \end{macrocode}
228 % if level goes down close all TOC's with a higher level
229 % \begin{macrocode}
230 \int_compare:nNnT
231 { \use:c{toclevel@#1} } < { \g_@@_toc_level_int }
232 {
233 \@@_toc_end:e { \use:c{toclevel@#1} }
234 }
235 % \end{macrocode}
236 %if same level do nothing
237 % update toclevel to the current level.
238 % \begin{macrocode}
239 \int_gset:Nn \g_@@_toc_level_int { \use:c{toclevel@#1} }
240 % \end{macrocode}
241 % now open the TOCI, the tagging of the
242 % inner structure is left to the |\l@xxx| commands.
243 % setting the title is not strictly necessary but looks nicer
244 % but we have to remove the |\numberline|
245 % \changes{v0.85k}{2026-04-28}{Use \cs{pdf_purify:nN} for the title of the structure.}
246 % \begin{NOTE}{UF}
247 % perhaps keep the number? How to insert a space then
248 % \end{NOTE}
249 % \begin{macrocode}
250 \group_begin:
251 \text_declare_expand_equivalent:Nn \numberline \use_none:n
252 \pdf_purify:nN {#2}\l_@@_sec_tmpa_tl
253 \tag_struct_begin:n{tag=\UseStructureName{toc/item},title-o=\l_@@_sec_tmpa_tl}
254 % \end{macrocode}
255 % The TOCI structure should get a /Ref, we use a destination
256 % to retrieve it.
257 % \begin{NOTE}{UF}
258 % This only works with hyperref currently. Without hyperref we
259 % need to store fake names.
260 % \end{NOTE}
261 % \begin{macrocode}
262 \tag_struct_gput:nnn { \tag_get:n {struct_num} }{ref_dest}{#4}
263 \seq_gpush:Ne \g_@@_toc_stack_seq {{\UseStructureName{toc/item}}\use:c{toclevel@#1}}
264 \group_end:
265 }
266 }
267 % \end{macrocode}
268 % \end{macro}
269 % Now define the tagging plug and assign it to the socket.
270 % \begin{plugdecl}{default}
271 % \begin{macrocode}
272 \NewTaggingSocketPlug{toc/contentsline/before}{default}
273 {
274 \@@_toc_contentsline_begin:nnnn #1
275 }
276 \AssignTaggingSocketPlug{toc/contentsline/before}{default}
277 % \end{macrocode}
278 % \end{plugdecl}
279 %
280 % \begin{macro}{\@@_toc_contentsline_end:nnnn}
281 % This is the closing code of a |\contentsline|.
282 % If the contentsline was actually printed, the code has to
283 % close the TOCI structure and to update the stack.
284 % \begin{macrocode}
285 \msg_new:nnn {tag}{toc-no-TOCI}{Missing~TOCI~structure~on~toc~stack}
286
287 \cs_new_protected:Npn \@@_toc_contentsline_end:nnnn #1 #2 #3 #4
288 % #1 level, #2 content (unused), #3 page number (unused) #4 destination (unused)
289 {
290 \int_compare:nNnF { \use:c{toclevel@#1} } > {\use:c{c@tocdepth}}
291 {
292 \seq_gpop:NNT \g_@@_toc_stack_seq\l_@@_tmpa_tl
293 {
294 \str_if_eq:eeTF{\tl_head:N\l_@@_tmpa_tl}{\UseStructureName{toc/item}}
295 {
296 \tag_struct_end:
297 }
298 {
299 \msg_warning:nn{tag}{toc-no-TOCI}
300 }
301 }
302 }
303 }
304 % \end{macrocode}
305 % \end{macro}
306 % Now define the tagging plug and assign it to the socket.
307 % \begin{plugdecl}{default}
308 % \begin{macrocode}
309 \NewTaggingSocketPlug{toc/contentsline/after}{default}
310 {
311 \@@_toc_contentsline_end:nnnn #1
312 }
313 \AssignTaggingSocketPlug{toc/contentsline/after}{default}
314 % \end{macrocode}
315 % \end{plugdecl}
316 %
317 % \subsection{Tagging of the content}
318 % This needs discussion.
319 %
320 % \begin{macrocode}
321 \AddToHook{contentsline/text/before}[tagpdf]{%
322 \tag_struct_begin:n{tag=\UseStructureName{toc/item/text}}%
323 \tag_mc_begin:n{}}
324 \AddToHook{contentsline/text/after}[tagpdf]{%
325 \tag_mc_end:}
326 \AddToHook{contentsline/page/before}[tagpdf]{%
327 \tag_mc_begin:n{}}
328 \AddToHook{contentsline/page/after}[tagpdf]{%
329 \tag_mc_end:
330 \tag_struct_end:} %Reference
331 \AddToHook{contentsline/number/before}[tagpdf]{%
332 \tag_mc_end:
333 \tag_struct_begin:n{tag=\UseStructureName{toc/item/label}}%
334 \tag_mc_begin:n{}}
335 \AddToHook{contentsline/number/after}[tagpdf]{%
336 \tag_mc_end:
337 \tag_struct_end:
338 \tag_mc_begin:n{}}
339 % \end{macrocode}
340 % \begin{plugdecl}{artifact}
341 % \begin{macrocode}
342 \NewTaggingSocketPlug{toc/leaders/before}{artifact}
343 {\tag_mc_begin:n{artifact}\nobreak}
344 \NewTaggingSocketPlug{toc/leaders/after}{artifact}
345 {\nobreak\tag_mc_end:}
346 \AssignTaggingSocketPlug{toc/leaders/before}{artifact}
347 \AssignTaggingSocketPlug{toc/leaders/after}{artifact}
348 % \end{macrocode}
349 % \end{plugdecl}
350 %
351 % \begin{macrocode}
352 %</package>
353 % \end{macrocode}
354 % \end{implementation}