Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-tikz.dtx (C) Copyright 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 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\ltlabtikzdate{2026-04-28}
19 \def\ltlabtikzversion{0.80h}
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-tikz.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 % \providecommand\tikzname{Ti\textit{k}Z}
34 % \title{The \textsf{latex-lab-tikz} package\\
35 % Support for the tagging of \tikzname\ pictures }
36 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
37 % \date{v\ltlabtikzversion\ \ltlabtikzdate}
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 % \begin{abstract}
48 % \end{abstract}
49 %
50 % \section{Introduction}
51 %
52 % This implements the tagging of \tikzname\ picture along the ideas
53 % described in latex-lab-graphic, please check its documentation for the
54 % background!
55 %
56 % Resulting structures and contents should be checked!
57 %
58 % The code doesn't use the generic sockets documented in \texttt{latex-lab-graphic}
59 % but instead sets up sockets and keys that are \tikzname-specific. One reason for
60 % this approach is to avoid that the tagging \tikzname\ pictures has side effect on pictures
61 % but more importantly because as this implementation patches into
62 % tikz internals special code in the plugs is required.
63 %
64 % The module is automatically loaded if \texttt{tagging=on} or \texttt{testphase=latest}
65 % is used. The patches of \tikzname\ commands can be suppressed by removing the
66 % code with the label \texttt{latex-lab-testphase-tikz} from the hook
67 % \texttt{package/tikz/after} before loading \tikzname.
68 %
69 % The module implements a number of keys that can be used both in \cs{tikzset} and in the
70 % optional argument of \texttt{tikzpicture} and \cs{tikz}.
71 %
72 % \begin{description}
73 % \item[\texttt{alt}] This key switches to the illustrative mode. Its value is the
74 % alternative text. If no value is given the alternative text variable is not changed,
75 % \texttt{alt=} will empty the text variable.
76 % \item[\texttt{actualtext}] This switches to the actualtext mode.
77 % This is useful for small graphics that represent single chars or a short word like a
78 % logo. If \texttt{actualtext} is used, the graphics is not enclosed in a \texttt{Figure}
79 % structure but in a \texttt{Span} structure and no \texttt{/BBox} attribute is added.%
80 % \footnote{This is in accordance with PDF/UA-2 but violates perhaps PDF/UA-1.}
81 % \item[\texttt{artifact}] This tags the graphic as an artifact.
82 % \item[\texttt{tagging-setup}] This key takes as argument a key-list,
83 % which can contain the following keys:
84 % \begin{description}
85 % \item[\texttt{alt=\meta{text}}] This is a second way to tag the graphic as a figure with
86 % alternative text. \texttt{alt} without value will tag as a figure but not change
87 % the text variable, \texttt{alt=} will empty the text variable.
88 % \item[\texttt{actualtext=\meta{text}}] This is a second way to tag the graphic as a
89 % symbol with actualtext.
90 % \item[\texttt{artifact}] This is a second way to tag the graphic as artifact.
91 % \item[\texttt{text}] This switches to text mode.
92 % \item[\texttt{off}] When used tagging will be stopped completely. It is then the
93 % responsibility of the surrounding code to add appropriate tagging commands.
94 % \item[\texttt{tag=\meta{name}}] This switches to the illustrative mode but uses
95 % \meta{name} as tag name in the structure instead of the default \texttt{Figure}.
96 % This can for example be used to tag an image of a formula with \texttt{Formula}.
97 % The key typically should be combined with a suitable alt key:
98 % \begin{verbatim}
99 % \begin{tikzpicture}[tagging-setup={alt=math,tag=Formula}]
100 % \end{verbatim}
101 % \end{description}
102 % \end{description}
103 %
104 % It is possible to define new keys to change the tagging setup.
105 % As they should be processed earlier than normal tikz keys,
106 % they must be part of a special family:
107 %
108 % \begin{verbatim}
109 % \tikzset
110 % {
111 % my alt/.style 2 args={alt=`#1' is for #2,},
112 % my alt/.belongs to family=/tikz/tagging-setup,
113 % }
114 % \begin{tikzpicture}[my alt={A}{ardvaark}]
115 % ...
116 % \end{tikzpicture}
117 % \end{verbatim}
118 %
119 % \changes{v0.80f}{2026-02-27}{Use \texttt{pgfkeys} for setup.}
120 % \changes{v0.80f}{2026-02-27}{Use symbolic names.}
121 % \begin{implementation}
122 % \section{Implementation}
123 % \begin{macrocode}
124 %<*package>
125 %<@@=tag>
126 % \end{macrocode}
127 % \begin{macrocode}
128 \ProvidesExplPackage {latex-lab-testphase-tikz} {\ltlabtikzdate} {\ltlabtikzversion}
129 {Code related to the tagging of tikz pictures}
130 % \end{macrocode}
131 %
132 % \subsection{Sockets}
133 %
134 % \begin{socketdecl}{
135 % tagsupport/tikz/picture/init,
136 % tagsupport/tikz/picture/begin,
137 % tagsupport/tikz/picture/end}
138 % Sockets at the begin and the end of a tikzpicture.
139 % The argument in the \texttt{init}
140 % should process the keys of the picture and switch
141 % the plugs if needed. Unlike the generic sockets the
142 % begin and end sockets here do not need arguments.
143 % \begin{macrocode}
144 \NewTaggingSocket{tikz/picture/init}{1}
145 \NewTaggingSocket{tikz/picture/begin}{0}
146 \NewTaggingSocket{tikz/picture/end}{0}
147 % \end{macrocode}
148 % \end{socketdecl}
149 %
150 % \begin{socketdecl}{tagsupport/tikz/picture/text/begin,tagsupport/tikz/picture/text/end}
151 % Sockets at the end and begin of text parts.
152 % \begin{macrocode}
153 \NewSocket{tagsupport/tikz/picture/text/begin}{0}
154 \NewSocket{tagsupport/tikz/picture/text/end}{0}
155 % \end{macrocode}
156 % \end{socketdecl}
157 %%
158 %
159 % \subsection{Plugs}
160 %
161 % \begin{plugdecl}{default (tagsupport/tikz/picture/init)}
162 % The init socket takes a list of keys, processes the known keys to setup tagging options
163 % and then assigns the plugs.
164 %
165 % \begin{macrocode}
166 \NewTaggingSocketPlug{tikz/picture/init}{default}
167 {
168 \pgfkeyssavekeyfilterstateto\tikz@temp@tagging@saved@filterstate
169 \pgfkeysinstallkeyfilter{/pgf/key~filters/active~families}{}
170 \pgfqkeysactivatesinglefamilyandfilteroptions{/tikz/tagging-setup}{/tikz}{#1}
171 \tikz@temp@tagging@saved@filterstate
172 }
173 \AssignTaggingSocketPlug{tikz/picture/init}{default}
174 % \end{macrocode}
175 % \end{plugdecl}
176 %
177 % \begin{plugdecl}{text (tagsupport/tikz/picture/begin),text (tagsupport/tikz/picture/end)}
178 % This plug handles the \tikzname\ picture as a text object. So the graphical parts
179 % are tagged as artifact, but when we encounter a node we activate tagging there.
180 % There is no BBox. A \tikzname\ picture does not necessarly starts with a \cs{leavevmode}
181 % so we test for vmode.
182 % \begin{macrocode}
183 \NewTaggingSocketPlug{tikz/picture/begin}{text}
184 {
185 \mode_if_vertical:T
186 {
187 \legacy_if:nTF {@inlabel} {\leavevmode} {\tag_socket_use:n{para/begin}}
188 }
189 \tag_mc_end_push:
190 \tagmcbegin{artifact}
191 % \end{macrocode}
192 % We hook into two pgf commands to add the tagging code.
193 % They are only used for postscript and svg so it should be
194 % safe inside a tagging socket for now.
195 % TODO: ask for an interface.
196 % \begin{macrocode}
197 \def\pgfsys@begin@text
198 {
199 \tag_resume:n{\pgfpicture}
200 \tag_socket_use:n{tikz/picture/text/begin}
201 }
202 \def\pgfsys@end@text
203 {
204 \tag_socket_use:n{tikz/picture/text/end}
205 \tag_suspend:n{\pgfpicture}
206 }
207 }
208 \NewTaggingSocketPlug{tikz/picture/end}{text}
209 {
210 \tagmcend
211 \tag_mc_begin_pop:n{}
212 }
213 % \end{macrocode}
214 % \end{plugdecl}
215 %
216 % \begin{plugdecl}{figure (tagsupport/tikz/picture/begin),figure (tagsupport/tikz/picture/end)}
217 % This plug handles the \tikzname\ picture as a figure.
218 % Around the graphic is a \texttt{Figure} environment which will
219 % use an alt text given in the optional argument and internally tagging is suspended.
220 % The Bbox will be set (after the second compilation) to the size of the bounding box.
221 % \begin{macrocode}
222 \NewTaggingSocketPlug{tikz/picture/begin}{alt}
223 {
224 \mode_if_vertical:T
225 {
226 \legacy_if:nTF {@inlabel} {\leavevmode} {\tag_socket_use:n{para/begin}}
227 }
228 \tag_mc_end_push:
229 \tag_struct_begin:n
230 {
231 tag=\UseStructureName{graphic},
232 alt=\l__tikz_tagging_alt_tl
233 }
234 \pgfrememberpicturepositiononpagetrue
235 \tag_mc_begin:n{tag=Figure}
236 }
237
238 \NewTaggingSocketPlug{tikz/picture/end}{alt}
239 {
240 \tag_mc_end:
241 % \end{macrocode}
242 % this is the code that calculates and sets the BBox attribute
243 % \changes{0.80d}{2025-09-27}{Support also pgfplots. Tagging issue 924}
244 % \begin{macrocode}
245 \cs_set:Npn\pgfqpoint##1##2
246 {
247 \tl_set:Ne\l_tmpa_tl
248 {
249 \dim_to_decimal_in_bp:n {##1+ \pgf@picminx}
250 \c_space_tl
251 \dim_to_decimal_in_bp:n {##2+ \pgf@picminy}
252 \c_space_tl
253 \dim_to_decimal_in_bp:n {##1+ \pgf@picmaxx}
254 \c_space_tl
255 \dim_to_decimal_in_bp:n {##2+ \pgf@picmaxx}
256 }
257 }
258 % \end{macrocode}
259 % The following command executes a \cs{pgfqpoint} command with the origin coordinates,
260 % and so set the \cs{l_tmpa_tl} used then in the BBox command. With pgfplots
261 % the command is not expandable. So we execute it before the structure command.
262 % \begin{macrocode}
263 \cs_if_exist:cT { pgf@sys@pdf@mark@pos@pgfid\the\pgf@picture@serial@count }
264 {
265 \use:c { pgf@sys@pdf@mark@pos@pgfid\the\pgf@picture@serial@count }
266 \tag_struct_gput:ene
267 {\tag_get:n{struct_num}}
268 {attribute}
269 {
270 /O /Layout /BBox~
271 [
272 \l_tmpa_tl
273 ]
274 }
275 }
276 \tag_struct_end:
277 \tag_mc_begin_pop:n{}
278 }
279 % \end{macrocode}
280 % \end{plugdecl}
281 %
282 % \begin{plugdecl}{actualtext (tagsupport/tikz/picture/begin),actualtext (tagsupport/tikz/picture/end)}
283 % This plug handles the \tikzname\ picture as a symbol with an actualtext.
284 % It tags the content as a Span and expects an actualtext.
285 % Internally tagging is suspended.
286 % \begin{macrocode}
287 \NewTaggingSocketPlug{tikz/picture/begin}{actualtext}
288 {
289 \mode_if_vertical:T
290 {
291 \legacy_if:nTF {@inlabel} {\leavevmode} {\tag_socket_use:n{para/begin}}
292 }
293 \tag_mc_end_push:
294 \tag_struct_begin:n
295 {tag=\UseStructureName{graphic/symbol},
296 actualtext=\l__tikz_tagging_actualtext_tl}
297 \tag_mc_begin:n{}
298 }
299
300 \NewTaggingSocketPlug{tikz/picture/end}{actualtext}
301 {
302 \tag_mc_end:
303 \tag_struct_end:
304 \tag_mc_begin_pop:n{}
305 }
306 % \end{macrocode}
307 % \end{plugdecl}
308 %
309 % \begin{plugdecl}{artifact (tagsupport/tikz/picture/begin),artifact (tagsupport/tikz/picture/end)}
310 % This plug handles the \tikzname\ picture as an artifact, as decoration.
311 % So it is surrounded by an artifact MC and internal text does not restart tagging.
312 % \begin{macrocode}
313 \NewTaggingSocketPlug{tikz/picture/begin}{artifact}
314 {
315 \mode_if_vertical:T
316 {
317 \legacy_if:nTF {@inlabel} {\leavevmode} {\tag_socket_use:n{para/begin}}
318 }
319 \tag_mc_end_push:
320 \tag_mc_begin:n {artifact}
321 }
322
323 \NewTaggingSocketPlug{tikz/picture/end}{artifact}
324 {
325 \tag_mc_end:
326 \tag_mc_begin_pop:n{}
327 }
328 % \end{macrocode}
329 % \end{plugdecl}
330 %
331 % By default we use the text plugs. This allow packages like todonotes to
332 % get a sensible tagging without changes.
333 % \begin{macrocode}
334 \AssignTaggingSocketPlug{tikz/picture/begin}{text}
335 \AssignTaggingSocketPlug{tikz/picture/end}{text}
336 % \end{macrocode}
337 %
338 % \begin{plugdecl}{default (tagsupport/tikz/picture/text/begin),
339 % default (tagsupport/tikz/picture/text/end)}
340 % These sockets are used inside the text
341 % plugs and ends the previous mc and restarts it after the text.
342 % \begin{macrocode}
343 \NewTaggingSocketPlug{tikz/picture/text/begin}{default}
344 {
345 \tag_mc_end:
346 \tag_mc_begin:n{}
347 }
348 \NewTaggingSocketPlug{tikz/picture/text/end}{default}
349 {
350 \tag_mc_end:
351 \tag_mc_begin:n {artifact}
352 }
353 \AssignTaggingSocketPlug{tikz/picture/text/begin}{default}
354 \AssignTaggingSocketPlug{tikz/picture/text/end}{default}
355 % \end{macrocode}
356 % \end{plugdecl}
357 %
358 % \subsection{Patching tagging into tikz}
359 % We add the begin socket to the \cs{tikz@picture} command (it is also used
360 % by \cs{tikz}).
361 % This allows us to process the keys of the picture, assign the plugs
362 % and then to suspend tagging.
363 % \begin{macrocode}
364 \AddToHook{package/tikz/after}
365 {
366 \AddToHookWithArguments{cmd/tikz@picture/before}
367 {
368 \tag_socket_use:nn {tikz/picture/init} {#1}
369 \tag_socket_use:n {tikz/picture/begin}
370 }
371 % \end{macrocode}
372 % The suspend command is in the \texttt{cmd/pgfpicture/before} hook so that it works
373 % also if pgfpicture is used internally, e.g. by the forest package.
374 % \changes{0.80e}{2026-01-19}{Moved suspend command into another hook for symmetry}
375 % \begin{macrocode}
376 \AddToHookWithArguments{cmd/pgfpicture/before}
377 {
378 \tag_suspend:n {\pgfpicture}
379 }
380 % \end{macrocode}
381 % The end socket is in the \cs{endpgfpicture} command.
382 % \begin{macrocode}
383 \AddToHook{cmd/endpgfpicture/after}
384 {
385 \tag_resume:n {\pgfpicture}
386 \tag_socket_use:n {tikz/picture/end}
387 }
388 }
389 % \end{macrocode}
390 %
391 %
392 % \subsection{User interface: Keys to change the tagging behaviour}
393 % These keys will be processed directly at the begin of the picture commands
394 % to change the tagging behaviour.
395 % They are specific to tikz and so are implemented with pgfkeys.
396 % \begin{variable}{\l__tikz_tagging_alt_tl,\l__tikz_tagging_actualtext_tl}
397 % The variables that hold the text alternatives.
398 % \begin{macrocode}
399 \tl_new:N \l__tikz_tagging_alt_tl
400 \tl_set:Nn \l__tikz_tagging_alt_tl {Alternative~text~missing!}
401 \tl_new:N \l__tikz_tagging_actualtext_tl
402 % \end{macrocode}
403 % \end{variable}
404 %
405 % \begin{macrocode}
406 \AddToHook{package/tikz/after}
407 {
408 % \end{macrocode}
409 % The tagging keys must be set earlier than the normal processing
410 % of \tikzname\ keys. We implement that with a family and filter the
411 % relevant keys in the init socket, this means that the keys will be processed
412 % again when tikz does its normal processing but there is no good way to avoid that
413 % without changing various tikz internals.
414 % \changes{v0.80h}{2026-04-28}{Use \cs{pdf_purify:nN} for the alt and actualtext.}
415 % \begin{macrocode}
416 \pgfqkeys{/tikz}
417 {
418 tagging-setup/.is~family,
419 % \end{macrocode}
420 % The short versions are forwarded to the longer one
421 % \begin{macrocode}
422 alt/.forward~to=/tikz/tagging-setup/alt,
423 alt/.belongs~to~family=/tikz/tagging-setup,
424 actualtext/.forward~to=/tikz/tagging-setup/actualtext,
425 actualtext/.belongs~to~family=/tikz/tagging-setup,
426 artifact/.forward~to=/tikz/tagging-setup/artifact,
427 artifact/.belongs~to~family=/tikz/tagging-setup,
428 % \end{macrocode}
429 % The tagging-setup key.
430 % \begin{macrocode}
431 tagging-setup/.code=\pgfqkeys{/tikz/tagging-setup}{#1},
432 tagging-setup/.belongs~to~family=/tikz/tagging-setup,
433 tagging-setup/text/.code =
434 {
435 \AssignTaggingSocketPlug{tikz/picture/begin}{text}
436 \AssignTaggingSocketPlug{tikz/picture/end}{text}
437 },
438 tagging-setup/text/.belongs~to~family=/tikz/tagging-setup,
439 tagging-setup/alt/.code =
440 {
441 \tl_if_eq:nnF{#1}{\pgfkeysnovalue}
442 { \pdf_purify:nN {#1}\l__tikz_tagging_alt_tl }
443 \AssignTaggingSocketPlug{tikz/picture/begin}{alt}
444 \AssignTaggingSocketPlug{tikz/picture/end}{alt}
445 \def\pgfsys@begin@text{}
446 \def\pgfsys@end@text{}
447 },
448 tagging-setup/alt/.belongs~to~family=/tikz/tagging-setup,
449 tagging-setup/tag/.code =
450 {
451 \AssignStructureRole{graphic}{#1}
452 \AssignTaggingSocketPlug{tikz/picture/begin}{alt}
453 \AssignTaggingSocketPlug{tikz/picture/end}{alt}
454 \def\pgfsys@begin@text{}
455 \def\pgfsys@end@text{}
456 },
457 tagging-setup/tag/.belongs~to~family=/tikz/tagging-setup,
458 tagging-setup/actualtext/.code =
459 {
460 \tl_if_eq:nnF{#1}{\pgfkeysnovalue}
461 { \pdf_purify:nN {#1} \l__tikz_tagging_actualtext_tl }
462 \AssignTaggingSocketPlug{tikz/picture/begin}{actualtext}
463 \AssignTaggingSocketPlug{tikz/picture/end}{actualtext}
464 \def\pgfsys@begin@text{}
465 \def\pgfsys@end@text{}
466 },
467 tagging-setup/actualtext/.belongs~to~family=/tikz/tagging-setup,
468 tagging-setup/artifact/.code =
469 {
470 \AssignTaggingSocketPlug{tikz/picture/begin}{artifact}
471 \AssignTaggingSocketPlug{tikz/picture/end}{artifact}
472 \def\pgfsys@begin@text{}
473 \def\pgfsys@end@text{}
474 },
475 tagging-setup/artifact/.belongs~to~family=/tikz/tagging-setup,
476 tagging-setup/off/.code =
477 {
478 \AssignTaggingSocketPlug{tikz/picture/begin}{noop}
479 \AssignTaggingSocketPlug{tikz/picture/end}{noop}
480 \def\pgfsys@begin@text{}
481 \def\pgfsys@end@text{}
482 },
483 tagging-setup/off/.belongs~to~family=/tikz/tagging-setup,
484 }
485 }
486 % \end{macrocode}
487 %
488 % Todonotes works with the default text recipe quite ok, details can
489 % be handled later.
490 % \begin{macrocode}
491 %</package>
492 % \end{macrocode}
493 % \end{implementation}