Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-float.dtx (C) Copyright 2023-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\ltlabfloatdate{2026-04-24}
19 \def\ltlabfloatversion{0.81n}
20 %<*driver>
21 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
22 \documentclass{l3in2edoc}
23
24 \EnableCrossrefs
25 \CodelineIndex
26 \begin{document}
27 \DocInput{latex-lab-float.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 %
34 % \title{The \textsf{latex-lab-floats} package\\
35 % Tagging of floats }
36 % \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
37 % \date{v\ltlabfloatversion\ \ltlabfloatdate}
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{documentation}
48 % \begin{abstract}
49 % The following code implements a first draft for the tagging of float
50 % environments
51 % \end{abstract}
52 %
53 %
54 % \section{Introduction}
55 %
56 % The code here handles the tagging of float environments.
57 %
58 % Figures (and tables) are in \LaTeX{} typically typeset in float environments.
59 % These are boxes which can \enquote{float} away to special float areas on the pages,
60 % e.g., to the top or the bottom of a page or to special float pages.
61 % If the rules allow it they can also be placed in the main text stream (\enquote{here}).
62 % Floats can also be collected at the end of the document.
63 % In either case the order within each type of floats
64 % (e.g., figures, tables, algorithms, etc.) is preserved.
65 %
66 % A special type, called a H-float, (provided by the float package)
67 % is always placed in the main text stream and does not
68 % necessarily preserve the order with normal floats of the same type: It is basically
69 % a minipage with a caption.
70 %
71 % Floats typically contain a figure (or a table, etc.) and a caption,
72 % but more complex constructions with subfigures,
73 % copyright statements, sources or additional description are possible too.
74 %
75 % In the \LaTeX{} source a float is normally more or less at the place of
76 % the first call-out, but when preparing a document for print the code
77 % is sometimes moved around to place floats in a more visually pleasing way.
78 %
79 % \section{Tagging}
80 %
81 % Floats (with the exception of H-floats) do not belong into the text stream, they
82 % are \enquote{consultation objects}: Readers must be able to choose if and when they read the float.
83 % Floats can have captions: the PDF rules require that if a \texttt{Caption} structure element is used,
84 % it is the first or last structure in its parent structure.
85 % This poses some challenges on a good tagging.
86 %
87 % In PDF~2.0 there is the suitable \texttt{Aside} tag which hopefully will
88 % be handled correctly regarding the reading order once processor actually support
89 % PDF~2.0. But in PDF~1.7 we rolemap it to \texttt{Note} and this doesn't lead to
90 % a good reading order. The code therefore defers the output of float:
91 % it collects the float structures and moves
92 % them to a \texttt{Sect} structure which is flushed out at the end of the document
93 % or earlier if the author requests it. (H-floats once they are handled will not be moved).
94 %
95 % To fulfill the requirement that a \texttt{Caption} should be at the begin or end, we always
96 % move it to the begin of the structure. If a float has two captions the author
97 % has to insert a command which splits the float in two.
98 %
99 % Subfigures and subcaptions are currently not handled, but will be implemented
100 % as simple \texttt{Part} with their own \texttt{Caption}.
101 %
102 % Unknown float types are put into a generic structure when they are encountered in the
103 % source. To add support for a new float type the \texttt{float/new} key described below can
104 % be used.
105 %
106 % \section{Links}
107 % \pkg{hyperref} doesn't patch the caption commands anymore if \cs{DocumentMetadata} is used.
108 % The code here has to add a target for links. To improve the linking behavior,
109 % it add this target at the begin of the float (and at the begin of split if the float is splitted)
110 % and changes \cs{caption} so that a link to a caption label will go to this target.
111 %
112 % \section{Keys}
113 %
114 % The code adds the following keys for the \cs{tagpdfsetup} command:
115 %
116 % \begin{function}{float/new,float/flush,float/split,float/container}
117 % \begin{description}
118 % \item[float/new] This sets up the needed code for a new float type. The value
119 % is the \meta{captype} which should be the same name as
120 % the name stored in \cs{\@captype}. The code defines symbolic
121 % structure names \texttt{float/\meta{captype}s} (for the container),
122 % \texttt{float/\meta{captype}} (for a single float), \texttt{float/\meta{captype}/caption}
123 % and \texttt{float/\meta{captype}/label} (for the caption and its label) and
124 % assigns standard roles to them. The roles can be changed with \cs{AssignStructureRole}.
125 %
126 % \item[float/flush] This will flush out all so far deferred floats (currently
127 % table and figure) into a container. The value is a sectioning level for which
128 % \cs{toclevel@}\meta{name} exists, e.g., \texttt{section} or \texttt{chapter},
129 % or an integer number describing the level, like $1$ for
130 % the section level. The container will then behave like a sectioning command
131 % of this level and create a \texttt{Sect} structure element containing the floats.
132 % The default value is the current sectioning level.
133 % The command should typically be used directly before a sectioning of the same
134 % or higher level. E.g. this here is wrong as following text \enquote{some text}
135 % would no longer be inside subsection A:
136 % \begin{verbatim}
137 % \DocumentMetadata{tagging=on}
138 % \documentclass{article}
139 %
140 % \begin{document}
141 % \section{section A}
142 % \subsection{subsection A}
143 % \begin{figure}
144 % \caption{hallo}
145 % \end{figure}
146 % \tagpdfsetup{float/flush}
147 % some text
148 % \section{section B}
149 % \end{document}
150 % \end{verbatim}
151 %
152 % Correct would be to put it directly before \verb+\subsection{section B}+.
153 % The key issues a \cs{par} to end the current paragraph.
154 %
155 % The floats will then be inserted as a \texttt{Sect} of this level (all \texttt{Sect} of smaller or equal
156 % level are closed). The key then creates a new container for following floats.
157 % At the end of the document all remaining floats will be flushed automatically.
158 %
159 % \item[float/split] This can be used inside a float if there are two captions.
160 % It will only work reasonably well if the content of the float parts are in a sensible order
161 % and can be separated by this command. More complex setups with tabulars will need more
162 % thoughts.
163 %
164 % \item[float/defer] The value \texttt{false} disables
165 % the deferring of floats in a container: the float structure is then inserted directly
166 % where it appears in the source. The key sets a global boolean. It can be used
167 % in the document to switch the collection on and off. The default value is \texttt{true}.
168 %
169 % \item[float/here] This is the inverse of the \texttt{defer} key and so a shorter
170 % way to disable the deferring. The default value is \texttt{true}.
171 % \end{description}
172 % \end{function}
173 %
174 % \section{Kernel commands}
175 % \begin{function}{\@current@float@struct}
176 % This variable holds the number of the current float structure. With tagging
177 % this is the structure number, without tagging a unique counter. A float
178 % can contain more than one float structure (e.g., if there is more than one
179 % caption).
180 % \end{function}
181 %
182 % \begin{function}{\@makecaption}
183 % \cs{@makecaption} is defined by the classes so we overwrite it for now
184 % at begin document.
185 % \end{function}
186 %
187 %
188 % \begin{macrocode}
189 %<@@=tag>
190 %<*package>
191 % \end{macrocode}
192 % \end{documentation}
193 % \begin{implementation}
194 % \section{Implementation}
195 % \begin{macrocode}
196 \ProvidesExplPackage {latex-lab-testphase-float} {\ltlabfloatdate} {\ltlabfloatversion}
197 {Code related to the tagging of floats}
198 % \end{macrocode}
199 % \changes{0.81m}{2025-12-04}{Use symbolic names}
200 % \changes{0.81m}{2025-12-04}{Deprecate \cs{tagtool}}
201 % \changes{0.81m}{2025-12-04}{Interface for new float types}
202 % \changes{0.81m}{2025-12-04}{Interface to disable deferring of float into a container}
203 %
204 % \subsection{Variables}
205 % We rolemap floats to Aside, and float sections to Sect.
206 %
207 % \begin{variable}{
208 % \g_@@_float_sect_prop,
209 % \g_@@_float_types_seq,
210 % \@current@float@struct,
211 % \g_@@_float_int
212 % }
213 % These variables will hold the structure number for the float container
214 % and the list of float types. Currently only figure and table are supported
215 % TODO: interface to declare new float types.
216 % To set the target for links we need also a unique counter.
217 % With tagging we could use the structure number, but
218 % the structure commands now are hidden inside tagging sockets
219 % so we use a dedicated counter.
220 % \begin{macrocode}
221 \prop_new:N \g_@@_float_sect_prop
222 \seq_new:N \g_@@_float_types_seq
223 \seq_gput_right:Nn \g_@@_float_types_seq {figure}
224 \seq_gput_right:Nn \g_@@_float_types_seq {table}
225 \tl_new:N\@current@float@struct
226 \int_new:N\g_@@_float_int
227 % \end{macrocode}
228 % \end{variable}
229 %
230 % \begin{variable}{\g_@@_float_sect_bool}
231 % With this boolean float collection is switched on and off.
232 % Currently it is always on and set globally.
233 % TODO: think if an interface is needed.
234 % TODO: would a local variable make more sense?
235 % \begin{macrocode}
236 \bool_new:N \g_@@_float_sect_bool
237 \bool_gset_true:N \g_@@_float_sect_bool
238 % \end{macrocode}
239 % \end{variable}
240 % \begin{variable}{\l_@@_float_tmpa_tl,\l_@@_float_tmpb_tl}
241 % \begin{macrocode}
242 \tl_new:N\l_@@_float_tmpa_tl
243 \tl_new:N\l_@@_float_tmpb_tl
244 % \end{macrocode}
245 % \end{variable}
246 % \begin{macro}{\@@_float_init:}
247 % To be able to set unique targets for links, we
248 % need a counter outside the tagging sockets.
249 % TODO: check if this command should be public or
250 % a socket or a hook.
251 % \begin{macrocode}
252 \cs_new_protected:Npn \@@_float_init:
253 {
254 \int_gincr:N \g_@@_float_int
255 \tl_set:Ne \@current@float@struct { \int_use:N \g_@@_float_int}
256 }
257 % \end{macrocode}
258 % \end{macro}
259 %
260 % \subsection{Setup for a new float type}
261 % A new float type needs
262 % \begin{itemize}
263 % \item symbolic names for the float, the caption and the label
264 % \item symbolic name for the container
265 % \item and it must be recorded in the sequence.
266 % \end{itemize}
267 %
268 % This is all done with the following key. The value is the captype.
269 %
270 % \begin{macrocode}
271 \keys_define:nn {__tag/setup}
272 {
273 float/new .code:n =
274 {
275 \NewStructureName{float/#1s}
276 \AssignStructureRole{float/#1s}{Sect}
277 \NewStructureName{float/#1}
278 \AssignStructureRole{float/#1}{float}
279 \NewStructureName{float/#1/caption}
280 \AssignStructureRole{float/#1/caption}{Caption}
281 \NewStructureName{float/#1/label}
282 \AssignStructureRole{float/#1/label}{Lbl}
283 \seq_gput_right:Nn \g_@@_float_types_seq {#1}
284 }
285 }
286 % \end{macrocode}
287 %
288 %\subsection{Moving float structures}
289 %
290 % Currently it is for all float types or none.
291 % Probably we will need some more options here to select some float types.
292 %
293 % \begin{macro}{float/defer,float/here}
294 % \begin{macrocode}
295 \keys_define:nn{__tag/setup}
296 {
297 float/defer .bool_gset:N = \g_@@_float_sect_bool
298 ,float/here .bool_gset_inverse:N = \g_@@_float_sect_bool
299 }
300 % \end{macrocode}
301 % \end{macro}
302 %
303 % \begin{macro}{\@@_float_container_new:n}
304 % This creates a new container and then stashes it.
305 % \begin{macrocode}
306 \cs_new_protected:Npn \@@_float_container_new:nN #1 #2 % #1 captype #2 tl-var to return number
307 {
308 \tag_struct_begin:n{tag=\UseStructureName{float/#1s},stash}
309 \tl_set:Ne #2 {\int_use:N\c@g__tag_struct_abs_int}
310 \prop_gput:Nne\g_@@_float_sect_prop {#1-struct}{\int_use:N\c@g__tag_struct_abs_int}
311 \tag_struct_end:
312 }
313 \cs_generate_variant:Nn \@@_float_container_new:nN {eN}
314 % \end{macrocode}
315 % \end{macro}
316 % \begin{macro}{\@@_float_init_collect:}
317 % This initializes a container structure for every float type.
318 % It can be used more than once in a document, this allows to have e.g.
319 % chapter wise containers.
320 % \begin{macrocode}
321 \cs_new_protected:Npn\@@_float_init_collect:
322 {
323 \bool_if:NT\g_@@_float_sect_bool
324 {
325 \seq_map_inline:Nn\g_@@_float_types_seq
326 {
327 \@@_float_container_new:nN {##1}\l_@@_tmpa_tl
328 }
329 }
330 }
331 % \end{macrocode}
332 % \end{macro}
333 %
334 %
335 % \begin{macro}{\@@_float_stop_sect:}
336 % This pushes out the floats. For every type is checks if there is actually
337 % a float of this type and then writes out the container structure.
338 % \begin{macrocode}
339 \cs_new_protected:Npn \@@_float_stop_sect:
340 {
341 \seq_map_inline:Nn\g_@@_float_types_seq
342 {
343 \prop_get:NnNT\g_@@_float_sect_prop{##1-used}\l_@@_float_tmpa_tl
344 {
345 \prop_get:NnNF\g_@@_float_sect_prop{##1-struct}\l_@@_float_tmpb_tl
346 {
347 \@@_float_container_new:eN {##1}\l_@@_float_tmpb_tl
348 }
349 \exp_args:Ne
350 \tag_struct_use_num:n{\l_@@_float_tmpb_tl}
351 \prop_gremove:Nn \g_@@_float_sect_prop{##1-used}
352 }
353 }
354 }
355 % \end{macrocode}
356 % \end{macro}
357 % \begin{macro}{flush/float}
358 % This is a key for \cs{tagpdfsetup} to flush out the collected floats.
359 % The value allows to set to which level the create Sect belongs.
360 % So \texttt{section} will close all previous Sect until the section level
361 % and create a new section structure.
362 % \begin{macrocode}
363 \keys_define:nn { __tag / setup }
364 {
365 float/flush .code:n =
366 {
367 \par
368 \cs_if_exist:cTF{toclevel@#1}
369 { \UseTaggingSocket{sec/end}{\int_eval:n{\use:c{toclevel@#1}}}}
370 { \UseTaggingSocket{sec/end}{\int_eval:n{#1}}}
371 \@@_float_stop_sect:
372 \@@_float_init_collect:
373 },
374 float/flush .default:n = \@currentseclevel
375 }
376 % \end{macrocode}
377 % \end{macro}
378
379 % \begin{macro}{flush-floats (deprecated)}
380 % This is the same key for the deprecated command
381 % \cs{tagtool} to flush out the collected floats.
382 % Kept for compatibility for now but will be removed at some time.
383 % The value allows to set to which level the create Sect contains.
384 % So \texttt{section} will close all previous Sect until the section level
385 % and create a new section.
386 % \begin{macrocode}
387 \keys_define:nn { tag / tool}
388 {
389 flush-floats .meta:nn = {__tag/setup}{float/flush=#1},
390 flush-floats .default:n = \@currentseclevel
391 }
392 % \end{macrocode}
393 % \end{macro}
394 %
395 % We need at least one pair
396 % \begin{macrocode}
397 \AddToHook{begindocument/end}[latex-lab/float]
398 {\@@_float_init_collect:}
399 \AddToHook{tagpdf/finish/before}[latex-lab/float]
400 {\par\__tag_sec_end:n{-10}\@@_float_stop_sect:}
401 \DeclareHookRule{tagpdf/finish/before}{latex-lab/float}{before}{tagpdf}
402 % \end{macrocode}
403 %
404 % \subsection{Splitting floats}
405 % \begin{macro}{float/split}
406 % TODO: check if the target affect spacing!!
407 % \begin{macrocode}
408 \keys_define:nn { __tag / setup}
409 {
410 float/split .code:n =
411 {
412 \UseTaggingSocket{float/end}
413 \@@_float_init:
414 \UseTaggingSocket{float/begin}
415 \MakeLinkTarget*{floatstructure.\@current@float@struct}
416 }
417 }
418 % \end{macrocode}
419 % \end{macro}
420 % \begin{macro}{split-float (deprecated)}
421 % This is the same key for the deprecated command
422 % \cs{tagtool} to split a float.
423 % Kept for compatibility for now but will be removed at some time.
424 % \begin{macrocode}
425 \keys_define:nn { tag / tool}
426 {
427 split-float .meta:nn = {__tag/setup}{float/split}
428 }
429 % \end{macrocode}
430 % \end{macro}
431 %
432 % \subsection{Tagging sockets}
433 % The tagging sockets are declared in lttagging.
434 % Currently we have the tagging sockets \texttt{float/hmode/begin}, \texttt{float/hmode/end},
435 % \texttt{float/begin} and \texttt{float/end}. All sockets have no argument.
436 %
437 % \begin{plugdecl}{default (tagsupport/float/hmode/begin)}
438 % This plug should be used if a float is called in hmode.
439 % It then closes the MC-chunks and starts the structure.
440 % \begin{macrocode}
441 \NewTaggingSocketPlug{float/hmode/begin}{kernel}
442 {
443 \@@_float_stop_par:
444 }
445 \AssignTaggingSocketPlug{float/hmode/begin}{kernel}
446 % \end{macrocode}
447 % \end{plugdecl}
448 %
449 % \begin{plugdecl}{default (tagsupport/float/hmode/end)}
450 % This plug should be used if a float is called in hmode;
451 % at the end of the float it then restarts the MC.
452 % \begin{macrocode}
453 \NewTaggingSocketPlug{float/hmode/end}{kernel}
454 {
455 \@@_float_start_par:
456 }
457 \AssignTaggingSocketPlug{float/hmode/end}{kernel}
458 % \end{macrocode}
459 % \end{plugdecl}
460 %
461 % \begin{plugdecl}{default (tagsupport/float/begin)}
462 % \begin{macrocode}
463 \NewTaggingSocketPlug{float/begin}{kernel}
464 {
465 \@@_float_begin:
466 }
467 \AssignTaggingSocketPlug{float/begin}{kernel}
468 % \end{macrocode}
469 % \end{plugdecl}
470 %
471 % \begin{plugdecl}{default (tagsupport/float/end)}
472 % \begin{macrocode}
473 \NewTaggingSocketPlug{float/end}{kernel}
474 {
475 \@@_float_end:
476 }
477 \AssignTaggingSocketPlug{float/end}{kernel}
478 % \end{macrocode}
479 % \end{plugdecl}
480 %
481 % \begin{macro}{\@@_float_stop_par:,\@@_float_start_par:}
482 % if a float is in a par, we need commands to stop and restart the P-mc
483 % \begin{macrocode}
484 \cs_new_protected:Npn \@@_float_stop_par:
485 {
486 \tag_mc_end:
487 \bool_if:NF \g_@@_float_sect_bool
488 {
489 \tag_struct_end:
490 }
491 }
492 \cs_new_protected:Npn \@@_float_start_par:
493 {
494 \bool_if:NF \g_@@_float_sect_bool
495 {
496 \tag_struct_begin:n{tag=\UseStructureName{para/textblock}}%
497 }
498 \tag_mc_begin:n{tag=P}
499 }
500 % \end{macrocode}
501 % \end{macro}
502 % These commands are the main commands to start and end the float tagging.
503 %
504 % \begin{macrocode}
505 \cs_new_protected:Npn \@@_float_begin:
506 {%
507 % \end{macrocode}
508 % We test if the float structure should be included directly or move to a dedicated section.
509 % \begin{macrocode}
510 \seq_if_in:NoTF\g_@@_float_types_seq{\@captype}
511 {
512 \bool_if:NTF\g_@@_float_sect_bool
513 {
514 \prop_get:NoNF \g_@@_float_sect_prop{\@captype-struct}\l_@@_float_tmpa_tl
515 {
516 \@@_float_container_new:eN {\@captype}\l_@@_float_tmpa_tl
517 }
518 \tag_struct_begin:e
519 {
520 tag=\UseStructureName{float/\@captype},
521 parent=\l_@@_float_tmpa_tl
522 }%
523 \prop_gput:Nee \g_@@_float_sect_prop {\@captype-used}{true}
524 }
525 {
526 \tag_struct_begin:n{tag=\UseStructureName{float/\@captype}}
527 }
528 }
529 {
530 \tag_struct_begin:n{tag=\UseStructureName{float/generic}}
531 }
532 \tl_set:Ne\@current@float@struct{\tag_get:n{struct_num}}%
533 \typeout{Float structure: \@current@float@struct}
534 }
535
536 \cs_new_protected:Npn\@@_float_end:{\tag_struct_end:} %end Aside
537
538 % \end{macrocode}
539 % \subsection{Patching}
540 % This patches the main command \cs{@xfloat}.
541 % There is a : in the code, so we disable expl3 syntax
542 % \begin{macrocode}
543 \ExplSyntaxOff
544 \def\@xfloat #1[#2]{%
545 \@nodocument
546 \def \@captype {#1}%
547 \def \@fps {#2}%
548 \@onelevel@sanitize \@fps
549 \def \reserved@b {!}%
550 \ifx \reserved@b \@fps
551 \@fpsadddefault
552 \else
553 \ifx \@fps \@empty
554 \@fpsadddefault
555 \fi
556 \fi
557 \ifhmode
558 \@bsphack
559 % \end{macrocode}
560 % If the float is in hmode we have to interrupt the P
561 % \begin{macrocode}
562 \UseTaggingSocket{float/hmode/begin}%
563 \@floatpenalty -\@Mii
564 \else
565 \@floatpenalty-\@Miii
566 \fi
567 \ifinner
568 \@parmoderr\@floatpenalty\z@
569 \else
570 \@next\@currbox\@freelist
571 {%
572 \@tempcnta \sixt@@@@n
573 \expandafter \@tfor \expandafter \reserved@a
574 \expandafter :\expandafter =\@fps
575 \do
576 {%
577 \if \reserved@a h%
578 \ifodd \@tempcnta
579 \else
580 \advance \@tempcnta \@ne
581 \fi
582 \else\if \reserved@a t%
583 \@setfpsbit \tw@
584 \else\if \reserved@a b%
585 \@setfpsbit 4%
586 \else\if \reserved@a p%
587 \@setfpsbit 8%
588 \else\if \reserved@a !%
589 \ifnum \@tempcnta>15
590 \advance\@tempcnta -\sixt@@@@n\relax
591 \fi
592 \else
593 \@latex@error{Unknown float option `\reserved@a'}%
594 {Option `\reserved@a' ignored and `p' used.}%
595 \@setfpsbit 8%
596 \fi\fi\fi\fi\fi
597 }%
598 \@tempcntb \csname ftype@\@captype \endcsname
599 \multiply \@tempcntb \@xxxii
600 \advance \@tempcnta \@tempcntb
601 \global \count\@currbox \@tempcnta
602 }%
603 \@fltovf
604 \fi
605 % \end{macrocode}
606 % This starts the structure for the float.
607 % \begin{macrocode}
608 \csname @@_float_init:\endcsname
609 \UseTaggingSocket{float/begin}%
610 \global \setbox\@currbox
611 \color@vbox
612 \normalcolor
613 \vbox \bgroup
614 \hsize\columnwidth
615 \@parboxrestore
616 \@floatboxreset
617 % \end{macrocode}
618 % We add a target for links. TODO: check that it doesn't affect spacing!!
619 % \begin{macrocode}
620 \MakeLinkTarget*{\@captype.struct.\@current@float@struct}%
621 }%
622 % \end{macrocode}
623 % The end code of the float ...
624 % \begin{macrocode}
625 \def\end@float{%
626 \@endfloatbox
627 \UseTaggingSocket{float/end}%
628 \ifnum\@floatpenalty <\z@
629 \@largefloatcheck
630 \@cons\@currlist\@currbox
631 \ifnum\@floatpenalty <-\@Mii
632 \penalty -\@Miv
633 \@tempdima\prevdepth
634 \vbox{}%
635 \prevdepth\@tempdima
636 \penalty\@floatpenalty
637 \else
638 \vadjust{\penalty -\@Miv \vbox{}\penalty\@floatpenalty}\@Esphack
639 \UseTaggingSocket{float/hmode/end}%
640 \fi
641 \fi
642 }
643 % \end{macrocode}
644 % and similar for double floats:
645 % \begin{macrocode}
646 \def\end@dblfloat{%
647 \if@twocolumn
648 \@endfloatbox
649 \UseTaggingSocket{float/end}%
650 \ifnum\@floatpenalty <\z@
651 \@largefloatcheck
652 \global\dp\@currbox1sp %
653 \@cons\@currlist\@currbox
654 \ifnum\@floatpenalty <-\@Mii
655 \penalty -\@Miv
656 \@tempdima\prevdepth
657 \vbox{}%
658 \prevdepth\@tempdima
659 \penalty\@floatpenalty
660 \else
661 \vadjust{\penalty -\@Miv \vbox{}\penalty\@floatpenalty}\@Esphack
662 \UseTaggingSocket{float/hmode/end}%
663 \fi
664 \fi
665 \else
666 \end@float
667 \fi
668 }%
669 \ExplSyntaxOn
670 % \end{macrocode}
671 %
672 % \subsection{Handling captions}
673 %
674 % \subsubsection{(Tagging) sockets}
675 %
676 % These sockets are in lttagging: \texttt{caption/begin} (1 argument),
677 % \texttt{caption/end}, \texttt{caption/label/begin}, \texttt{caption/label/end}.
678 %
679 % \begin{socketdecl}{caption/label}
680 % This socket is a lightweight start for
681 % some interface to format the label or add a font command.
682 % The argument is the label text.
683 % The default plug \texttt{kernel} adds a colon
684 % and a space.
685 % TODO: revisit after checking float and caption packages
686 % to identify which sockets and hooks are needed.
687 % \begin{macrocode}
688 \NewSocket{caption/label}{1}
689 % \end{macrocode}
690 % \end{socketdecl}
691 %
692 % \begin{plugdecl}{kernel (caption/label)}
693 % The standard label formatting from the kernel.
694 % \begin{macrocode}
695 \NewSocketPlug{caption/label}{kernel}
696 {
697 #1:~
698 }
699 \AssignSocketPlug{caption/label}{kernel}
700 % \end{macrocode}
701 % \end{plugdecl}
702 % \begin{plugdecl}{default}
703 % The caption begin socket takes an argument: the structure number of
704 % the parent float. If the argument is empty, the current structure is used.
705 % TODO: a tagpdf key that moves a structure to the begin of the parent.
706 % The caption is moved to the first position with the firstkid option.
707 % \begin{macrocode}
708 \NewTaggingSocketPlug{caption/begin}{default}
709 {
710 \tl_if_empty:eTF {#1}
711 {
712 \tag_struct_begin:n{tag=\UseStructureName{float/\@captype/caption},firstkid}
713 }
714 {
715 \tag_struct_begin:n{tag=\UseStructureName{float/\@captype/caption},parent=#1,firstkid}
716 }
717 \bool_set_true:N \l__tag_para_flattened_bool
718 }
719 \AssignTaggingSocketPlug{caption/begin}{default}
720 % \end{macrocode}
721 % \end{plugdecl}
722 % \begin{plugdecl}{default}
723 % \begin{macrocode}
724 \NewTaggingSocketPlug{caption/end}{default}
725 {
726 \tag_struct_end:
727 }
728 \AssignTaggingSocketPlug{caption/end}{default}
729 % \end{macrocode}
730 % \end{plugdecl}
731 %
732 % \begin{plugdecl}{default (tagsupport/caption/label/begin)}
733 % \begin{macrocode}
734 \NewTaggingSocketPlug{caption/label/begin}{default}
735 {
736 % \end{macrocode}
737 % suppress para tagging at the begin.
738 % \begin{macrocode}
739 \tagpdfparaOff
740 \tag_struct_begin:n{tag=\UseStructureName{float/\@captype/label}}
741 \tag_mc_begin:n{}
742 }
743 \AssignTaggingSocketPlug{caption/label/begin}{default}
744 % \end{macrocode}
745 % \end{plugdecl}
746 %
747 % \begin{plugdecl}{default (tagsupport/caption/label/end)}
748 % \begin{macrocode}
749 \NewTaggingSocketPlug{caption/label/end}{default}
750 {
751 \tag_mc_end:
752 \tag_struct_end:
753 \tagpdfparaOn
754 }
755 \AssignTaggingSocketPlug{caption/label/end}{default}
756 % \end{macrocode}
757 % \end{plugdecl}
758 %
759 % \subsubsection{Redefinitions}
760 %
761 % With hyperref that means that the \cs{refstepcounter} now can affect spacing so we
762 % change that to the kernel refstepcounter:
763 % \begin{macrocode}
764 \def\caption{%
765 \ifx\@captype\@undefined
766 \@latex@error{\noexpand\caption\c_space_tl outside~float}\@ehd
767 \expandafter\@gobble
768 \else
769 % \end{macrocode}
770 % if a caption is used outside a float no
771 % target has been set and \cs{@current@float@struct} is empty
772 % \begin{macrocode}
773 \tl_if_empty:NTF\@current@float@struct
774 {
775 \refstepcounter\@captype
776 }
777 {
778 \@kernel@refstepcounter\@captype
779 % \end{macrocode}
780 % we need to reset the target for \cs{addcontentsline}.
781 % We use \cs{@captype} to support autoref.
782 % \begin{macrocode}
783 \xdef\@currentHref{\@captype.struct.\@current@float@struct}%
784 }
785 \expandafter\@firstofone
786 \fi
787 {\@dblarg{\@caption\@captype}}%
788 }
789 % \end{macrocode}
790 %
791 % \begin{macro}{\@makecaption}
792 % \cs{@makecaption} is defined by the classes so we overwrite it for now
793 % at begin document.
794 % \changes{0.81k}{2025-10-11}{Add command hook explicitly to avoid patching error, tagging issue 998}
795 % \begin{macrocode}
796 \NewHookWithArguments{cmd/@makecaption/before}{2}
797 \AddToHook{begindocument}
798 {
799 \long\def\@makecaption#1#2{%
800 \UseHookWithArguments{cmd/@makecaption/before}{2}{#1}{#2}%
801 \vskip\abovecaptionskip
802 % \end{macrocode}
803 % We don't want tagging when storing the caption for the singleline check.
804 % \changes{0.81l}{2025-10-17}{Measure \texttt{caption/label} socket instead of hard-coded colon and space, tagging issue 935}
805 % \begin{macrocode}
806 \SuspendTagging{\@makecaption}
807 \sbox\@tempboxa{\UseSocket{caption/label}{#1}#2}%
808 \ResumeTagging{\@makecaption}
809 % \end{macrocode}
810 % We pass \cs{@current@float@struct} as parent structure
811 % number. If that is empty the socket will use the parent structure and hope ...
812 % \begin{macrocode}
813 \UseTaggingSocket{caption/begin}{\@current@float@struct}
814 \ifdim \wd\@tempboxa >\hsize
815 \UseTaggingSocket{caption/label/begin}
816 \UseSocket{caption/label}{#1}
817 \UseTaggingSocket{caption/label/end}
818 \UseTaggingSocket{para/begin}
819 #2
820 \par
821 \else
822 % \end{macrocode}
823 % we don't reuse the box as it doesn't contain tagging, but set the text explicitly.
824 % \begin{macrocode}
825 \global \@minipagefalse
826 \hb@xt@\hsize{\hfil
827 \UseTaggingSocket{caption/label/begin}
828 \UseSocket{caption/label}{#1}
829 \UseTaggingSocket{caption/label/end}
830 \UseTaggingSocket{para/begin}
831 #2
832 \UseTaggingSocket{para/end}
833 \hfil}%
834 \fi
835 \UseTaggingSocket{caption/end}
836 \vskip\belowcaptionskip}
837 }
838 % \end{macrocode}
839 % \end{macro}
840 %
841 % \begin{macrocode}
842 %</package>
843 % \end{macrocode}
844 % \end{implementation}