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