Debian Codegraph @

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