Debian Codegraph @

texmf-dist/source/latex/latex-lab/latex-lab-amsmath.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-amsmath.dtx
4 %
5 % Copyright (C) 2022-2025 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 %
15 % The development version of the bundle can be found below
16 %
17 % https://github.com/latex3/latex2e/required/latex-lab
18 %
19 % for those people who are interested or want to report an issue.
20 %
21 %<*driver>
22 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
23 \documentclass{l3doc}
24 \usepackage{latex-lab-testphase-l3doc}
25 \EnableCrossrefs
26 \CodelineIndex
27 \begin{document}
28 \DocInput{latex-lab-amsmath.dtx}
29 \end{document}
30 %</driver>
31 %
32 % \fi
33 %
34 %
35 % \title{The \texttt{latex-lab-amsmath} code\thanks{}}
36 % \author{\LaTeX{} Project}
37 %
38 % \maketitle
39 %
40 % \newcommand\fmi[1]{\begin{quote} TODO: \itshape #1\end{quote}}
41 % \newcommand\NEW[1]{\marginpar{\mbox{}\hfill\fbox{New: #1}}}
42 % \providecommand\class[1]{\texttt{#1.cls}}
43 % \providecommand\pkg[1]{\texttt{#1}}
44 % \providecommand\hook[1]{\texttt{#1}}
45 %
46 % \begin{abstract}
47 % \end{abstract}
48 %
49 % \tableofcontents
50 %
51 % \section{Introduction}
52 %
53 % This file implements adaptations to the \pkg{amsmath} package needed for
54 % the tagging project.
55 %
56 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
57 %
58 % \section{The Implementation}
59 %
60 % Better no @@ expansion here \ldots ...
61 % \begin{macrocode}
62 %<@@=>
63 % \end{macrocode}
64 %
65 % \begin{macrocode}
66 %<*kernel>
67 % \end{macrocode}
68 %
69 % \subsection{File declaration}
70 % \begin{macrocode}
71 \ProvidesFile{latex-lab-amsmath.ltx}
72 [2025-09-29 v0.1i amsmath adaptions]
73 % \end{macrocode}
74 % \subsection{Tagpdf support}
75 % To make the code independent from tagging being loaded and active
76 % we load the \pkg{tagpdf-base} package:
77 %
78 % \begin{macrocode}
79 \RequirePackage{tagpdf-base}
80 % \end{macrocode}
81 %
82 % \begin{macrocode}
83 \ExplSyntaxOn
84 % \end{macrocode}
85 %
86 % \subsection{Measuring}
87 % When measuring we neither want tagging nor the luamml processing.
88 % \changes{0.1h}{2025-06-20}{moved redefinition to begin document, see tagging-project issue \#920}
89 % \begin{macro}{\measuring@true}
90 % \begin{macrocode}
91 \AddToHook{begindocument}
92 {
93 \def\measuring@true{\let\ifmeasuring@\iftrue\tag_suspend:n{\measuring}\luamml_ignore:}
94 }
95 % \end{macrocode}
96 % \end{macro}
97 %
98 % \subsection{Display environments}
99 % \changes{0.1e}{2025-01-31}{moved redefinitions from latex-lab-math and luamml into this file}
100 %
101 % \subsubsection{Tag}
102 % The tag/label must be saved, so that it can be reinserted later.
103 %
104 % TODO: \cs{maketag@@@} is perhaps used in places where tagging/luamml handling
105 % is not wanted. This must be checked and handled.
106 % \begin{macro}{\maketag@@@}
107 % \begin{macrocode}
108 \def\maketag@@@#1
109 {%
110 \ifmeasuring@
111 \hbox{\m@th\normalfont#1}%
112 \else
113 \UseTaggingSocket{math/display/tag/begin}
114 \hbox{\m@th\normalfont#1
115 \UseTaggingSocket{math/luamml/mtable/tag/save}
116 }%
117 \UseTaggingSocket{math/display/tag/end}
118 \fi
119 }
120 % \end{macrocode}
121 % \end{macro}
122 % \cs{eqref} uses \cs{tagform@} and so \cs{maketag@@@} but we do not want this tagging
123 % there.
124 %
125 % \begin{macro}{\eqref}
126 % \begin{macro}{\maketag@@@notog}
127 % \begin{macrocode}
128 \def\maketag@@@notag#1{\hbox{\m@th\normalfont#1}}
129 \DeclareRobustCommand{\eqref}[1]
130 {\textup{\let\maketag@@@\maketag@@@notag\tagform@{\ref{#1}}}}
131 % \end{macrocode}
132 % \end{macro}
133 % \end{macro}
134 %
135 % \subsubsection{align \& friends}
136 %
137 % Most display environment uses a common command for the end which contains
138 % the luamml socket to finalize the mtable.
139 % \begin{macro}{\common@align@ending}
140 % \begin{macrocode}
141 \def\common@align@ending {
142 \math@cr \black@\totwidth@
143 \UseExpandableTaggingSocket {math/luamml/mtable/finalize} {\@currenvir}
144 \egroup
145 \ifingather@
146 \restorealignstate@
147 \egroup
148 \nonumber
149 \ifnum0=`{\fi\iffalse}\fi
150 \else
151 \dollardollar@end
152 \fi
153 \ignorespacesafterend
154 }
155 % \end{macrocode}
156 % \end{macro}
157 % Now we redefine the display alignments to use these ending.
158 % \begin{macrocode}
159 \renewenvironment{align}{%
160 \start@align\@ne\st@rredfalse\m@ne
161 }{%
162 \common@align@ending
163 }
164 \renewenvironment{align*}{%
165 \start@align\@ne\st@rredtrue\m@ne
166 }{%
167 \common@align@ending
168 }
169 \renewenvironment{alignat}{%
170 \start@align\z@\st@rredfalse
171 }{%
172 \common@align@ending
173 }
174 \renewenvironment{alignat*}{%
175 \start@align\z@\st@rredtrue
176 }{%
177 \common@align@ending
178 }
179 \renewenvironment{flalign}{%
180 \start@align\tw@\st@rredfalse\m@ne
181 }{%
182 \common@align@ending
183 }
184 \renewenvironment{flalign*}{%
185 \start@align\tw@\st@rredtrue\m@ne
186 }{%
187 \common@align@ending
188 }
189 \renewenvironment{xalignat}{%
190 \start@align\@ne\st@rredfalse
191 }{%
192 \common@align@ending
193 }
194 \renewenvironment{xalignat*}{%
195 \start@align\@ne\st@rredtrue
196 }{%
197 \common@align@ending
198 }
199 \renewenvironment{xxalignat}{%
200 \start@align\tw@\st@rredtrue
201 }{%
202 \common@align@ending
203 }
204 % \end{macrocode}
205 %
206 % And register these environments for the math grabbing.
207 % \begin{macrocode}
208 \math_register_halign_env:nn {align}{}
209 \math_register_halign_env:nn {align*}{}
210 \math_register_halign_env:nn {alignat}{}
211 \math_register_halign_env:nn {alignat*}{}
212 \math_register_halign_env:nn {flalign}{}
213 \math_register_halign_env:nn {flalign*}{}
214 \math_register_halign_env:nn {xalignat}{}
215 \math_register_halign_env:nn {xalignat*}{}
216 \math_register_halign_env:nn {xxalignat}{}
217 % \end{macrocode}
218 %
219 % The align preamble (used in \cs{align@}) needs code for luamml
220 % to save the cells.
221 % \begin{macro}{\align@preamble}
222 % \begin{macrocode}
223 \def\align@preamble{%
224 &\hfil
225 \strut@
226 \setboxz@h
227 {
228 \@lign
229 $
230 \m@th\displaystyle{##}
231 \ifmeasuring@
232 \luamml_ignore:
233 \else
234 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
235 \fi
236 $
237 }%
238 \ifmeasuring@
239 \savefieldlength@
240 \else
241 \UseTaggingSocket{math/luamml/mtable/finalizecol}{box}
242 \fi
243 \set@field
244 \tabskip\z@skip
245 &\setboxz@h
246 {
247 \@lign
248 $
249 \m@th\displaystyle{{}##}
250 \ifmeasuring@
251 \luamml_ignore:
252 \else
253 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
254 \fi
255 $
256 }%
257 \ifmeasuring@
258 \savefieldlength@
259 \else
260 \UseTaggingSocket{math/luamml/mtable/finalizecol}{box}
261 \fi
262 \set@field
263 \hfil
264 \tabskip\alignsep@
265 }
266 % \end{macrocode}
267 % \end{macro}
268 %
269 % \begin{macro}{\math@cr@@@align}
270 % \begin{macrocode}
271 \def\math@cr@@@align{%
272 \ifst@rred\nonumber\fi
273 \if@eqnsw \global\tag@true \fi
274 \global\advance\row@\@ne
275 \add@amps\maxfields@
276 \omit
277 \kern-\alignsep@
278 \iftag@
279 \setboxz@h{\@lign\strut@{\make@display@tag}}%
280 \place@tag
281 \fi
282 \UseTaggingSocket{math/luamml/mtable/tag/set}
283 \ifst@rred\else\global\@eqnswtrue\fi
284 \global\lineht@\z@
285 \cr
286 }
287 % \end{macrocode}
288 % \end{macro}
289 %
290 % \subsubsection{\texttt{gather} and \texttt{gather*}}
291 %
292 % \begin{macro}{\gather@}
293 % \begin{macrocode}
294 \def\gather@#1{%
295 \ingather@true \let\split\insplit@
296 \let\tag\tag@in@align \let\label\label@in@display
297 \chardef\dspbrk@context\z@
298 \intertext@ \displ@y@ \Let@
299 \let\math@cr@@@\math@cr@@@gather
300 \gmeasure@{#1}%
301 \global\shifttag@false
302 \tabskip\z@skip
303 \global\row@\@ne
304 \halign to\displaywidth\bgroup
305 \strut@
306 \setboxz@h
307 {
308 $\m@th\displaystyle{##}
309 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
310 $
311 }%
312 \UseTaggingSocket{math/luamml/mtable/finalizecol}{box}
313 \calc@shift@gather
314 \set@gather@field
315 \tabskip\@centering
316 &\setboxz@h{\strut@{##}}%
317 \place@tag@gather
318 \UseTaggingSocket{math/luamml/mtable/tag/set}
319 \tabskip \iftagsleft@ \gdisplaywidth@ \else \z@skip \span\fi
320 \crcr
321 #1%
322 }
323 % \end{macrocode}
324 % \end{macro}
325 %
326 % \begin{macro}{\endgather}
327 % \begin{macrocode}
328 \def\endgather{
329 \math@cr
330 \black@ \totwidth@
331 \UseExpandableTaggingSocket{math/luamml/mtable/finalize} {gather}
332 \egroup
333 \dollardollar@end
334 \ignorespacesafterend
335 }
336 % \end{macrocode}
337 % \end{macro}
338 %
339 % The original definition of \texttt{gather*} uses \cs{endgather}
340 % but this redirection doesn't work if we alter \texttt{gather} so we use
341 % the real meaning and add the socket.
342 % \begin{environment}{gather*}
343 % \begin{macrocode}
344 \renewenvironment{gather*}
345 {
346 \start@gather\st@rredtrue
347 }
348 {
349 \math@cr
350 \black@\totwidth@
351 \UseExpandableTaggingSocket{math/luamml/mtable/finalize} {gather}
352 \egroup
353 \dollardollar@end
354 \ignorespacesafterend
355 }
356 % \end{macrocode}
357 % \end{environment}
358 %
359 % Register both environments
360 % \begin{macrocode}
361 \math_register_halign_env:nn {gather}{}
362 \math_register_halign_env:nn {gather*}{}
363 % \end{macrocode}
364 %
365 % \subsubsection{gathered, aligned and alignedat}
366 % These environments are not grabbed as they are inside other display
367 % environments but they need various sockets for luamml support.
368 % \begin{macro}{\start@aligned}
369 % \begin{macrocode}
370 \renewcommand{\start@aligned}[2]{
371 \RIfM@
372 \else
373 \nonmatherr@ {\begin{\@currenvir}}
374 \fi
375 \savecolumn@ % Assumption: called inside a group
376 \UseTaggingSocket{math/luamml/annotate/false}{}{ \alignedspace@left }
377 \ams@start@box {#1} \bgroup
378 \maxfields@ #2 \relax
379 \ifnum \maxfields@ > \m@ne
380 \multiply \maxfields@ \tw@
381 \let \math@cr@@@ \math@cr@@@alignedat
382 \alignsep@ \z@skip
383 \else
384 \let \math@cr@@@ \math@cr@@@aligned
385 \alignsep@ \minalignsep
386 \fi
387 \Let@ \chardef \dspbrk@context \@ne
388 \default@tag
389 \spread@equation % no-op if already called
390 \global \column@ \z@
391 \ialign \bgroup
392 & \column@plus
393 \hfil
394 \strut@
395 $
396 \m@th
397 \displaystyle
398 {##}
399 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
400 $
401 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
402 \tabskip \z@skip
403 & \column@plus
404 $
405 \m@th
406 \displaystyle
407 {
408 {}
409 ##
410 }
411 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
412 $
413 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
414 \hfil
415 \tabskip\alignsep@
416 \crcr
417 \ams@return@opt@arg
418 }
419 % \end{macrocode}
420 % \end{macro}
421 %
422 % \begin{environment}{gathered}
423 % \begin{macrocode}
424 \renewenvironment{gathered}[1][c]{%
425 \RIfM@\else
426 \nonmatherr@{\begin{gathered}}%
427 \fi
428 % \end{macrocode}
429 % This annotates the space
430 % \begin{macrocode}
431 \UseTaggingSocket{ math/luamml/annotate/false } {}{ \alignedspace@left }
432 \ams@start@box{#1}\bgroup
433 \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
434 \spread@equation
435 \ialign\bgroup
436 \hfil\strut@$\m@th\displaystyle##
437 % \end{macrocode}
438 % This save the cell and then finalize it.
439 % \begin{macrocode}
440 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
441 $
442 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
443 \hfil
444 \crcr
445 \ams@return@opt@arg
446 }{%
447 \endaligned
448 }
449 % \end{macrocode}
450 % \end{environment}
451 %
452 % \begin{macro}{\endaligned}
453 % \begin{macrocode}
454 \def\endaligned
455 {
456 \crcr
457 \UseExpandableTaggingSocket{math/luamml/mtable/innertable/save}
458 \egroup
459 \restorecolumn@
460 \egroup
461 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}
462 }
463 % \end{macrocode}
464 % \end{macro}
465 %
466 % \subsubsection{multline and multline*}
467 %
468 % \texttt{multline*} needs a redefinition before it is registered as it uses
469 % \cs{endmultline}, this must be replaced by the true code.
470 %
471 % \begin{environment}{multline*}
472 % \begin{macrocode}
473 \renewenvironment{multline*}{\start@multline\st@rredtrue}
474 {%
475 \iftagsleft@ \@xp\lendmultline@ \else \@xp\rendmultline@ \fi
476 \ignorespacesafterend
477 }
478 % \end{macrocode}
479 % \end{environment}
480 %
481 % And now we register both
482 % \begin{macrocode}
483 \math_register_halign_env:nn {multline}{}
484 \math_register_halign_env:nn {multline*}{}
485 % \end{macrocode}
486 %
487 % In the internal commands we have to add sockets for alignment attributes
488 % \begin{macro}{\multline@}
489 % \begin{macrocode}
490 \def\multline@#1{%
491 \Let@
492 \@display@init{\global\advance\row@\@ne \global\dspbrk@lvl\m@ne}%
493 \chardef\dspbrk@context\z@
494 \restore@math@cr
495 \let\tag\tag@in@align
496 \global\tag@false \global\let\raise@tag\@empty
497 \mmeasure@{#1}%
498 \let\tag\gobble@tag \let\label\@gobble
499 \tabskip \if@fleqn \@mathmargin \else \z@skip \fi
500 \totwidth@\displaywidth
501 \if@fleqn
502 \advance\totwidth@-\@mathmargin
503 \fi
504 \halign\bgroup
505 \hbox to\totwidth@{%
506 \if@fleqn
507 \hskip \@centering \relax
508 \else
509 \hfil
510 \fi
511 \strut@
512 $\m@th\displaystyle{}##\endmultline@math
513 \hfil
514 }%
515 \crcr
516 \if@fleqn
517 \hskip-\@mathmargin
518 \def\multline@indent{\hskip\@mathmargin}% put it back
519 \else
520 \hfilneg
521 \def\multline@indent{\hskip\multlinegap}%
522 \fi
523 \iftagsleft@
524 \iftag@
525 \begingroup
526 \ifshifttag@
527 \rlap{\vbox{%
528 \normalbaselines
529 \hbox{%
530 \strut@
531 \make@display@tag
532 }%
533 \vbox to\lineht@{}%
534 \raise@tag
535 }}%
536 \multline@indent
537 \else
538 \setbox\z@\hbox{\make@display@tag}%
539 \dimen@\@mathmargin \advance\dimen@-\wd\z@
540 \ifdim\dimen@<\multlinetaggap
541 \dimen@\multlinetaggap
542 \fi
543 \box\z@ \hskip\dimen@\relax
544 \fi
545 \endgroup
546 \else
547 \multline@indent
548 \fi
549 \else
550 \multline@indent
551 \fi
552 \ifmeasuring@ \else
553 \UseTaggingSocket{math/luamml/mtable/aligncol} {left}
554 \fi
555 #1%
556 \ifmeasuring@ \else
557 \UseTaggingSocket{math/luamml/mtable/aligncol} {right}
558 \fi
559 }
560 % \end{macrocode}
561 % \end{macro}
562 %
563 % Luckily, \texttt{multline} uses \cs{endmultline@math} in exactly
564 % the spot where we have to set the flag.
565 % Less luckily, \cs{endmultline@math} sometimes get overwritten for the last line.
566 % But that isn't much of a problem since we want special behavior there anyway.
567 % \begin{macro}{\endmultline@math}
568 % \begin{macrocode}
569 \def\endmultline@math
570 {
571 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
572 $
573 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
574 }
575 % \end{macrocode}
576 % \end{macro}
577 %
578 % \begin{macro}{\rendmultline@}
579 % \begin{macrocode}
580 \def\rendmultline@{%
581 \iftag@
582 % \end{macrocode}
583 % we need to surround the math token with tagging sockets.
584 % \begin{macrocode}
585 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }
586 $
587 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
588 \let\endmultline@math\relax
589 \ifshifttag@
590 \hskip\multlinegap
591 \llap{\vtop{%
592 \raise@tag
593 \normalbaselines
594 \setbox\@ne\null
595 \dp\@ne\lineht@
596 \box\@ne
597 \hbox{\strut@\make@display@tag}%
598 }}%
599 \else
600 \hskip\multlinetaggap
601 \make@display@tag
602 \fi
603 % \end{macrocode}
604 % Here we set the tag TODO: is that sensible with multline? Where is the tag saved?
605 % \begin{macrocode}
606 \UseTaggingSocket{math/luamml/mtable/tag/set}
607 \else
608 \hskip\multlinegap
609 \fi
610 \hfilneg
611 \math@cr
612 % \end{macrocode}
613 % Now we finalize the mtable.
614 % \begin{macrocode}
615 \UseExpandableTaggingSocket {math/luamml/mtable/finalize} {multline}
616 \egroup
617 \dollardollar@end
618 }
619 % \end{macrocode}
620 % \end{macro}
621 % And something similar for the left version
622 % \begin{macro}{\lendmultline@}
623 % \begin{macrocode}
624 \def\lendmultline@{%
625 \hfilneg
626 \hskip\multlinegap
627 \math@cr
628 \UseExpandableTaggingSocket {math/luamml/mtable/finalize} {multline}
629 \egroup
630 \dollardollar@end
631 }
632 % \end{macrocode}
633 % \end{macro}
634 %
635 % \subsection{Cases}
636 %
637 % \begin{macro}{env@cases}
638 % \begin{macrocode}
639 \def\env@cases{%
640 \let\@ifnextchar\new@ifnextchar
641 \left\lbrace
642 \def\arraystretch{1.2}%
643 \array{@{}l@{\quad}l@{\luamml_ignore:}}%
644 }
645 % \end{macrocode}
646 % \end{macro}
647 %
648 % \subsubsection{smallmatrix}
649
650 % \begin{environment}{smallmatrix}
651 % \begin{macrocode}
652 \renewenvironment {smallmatrix} {
653 \UseTaggingSocket{ math/luamml/annotate/false } {} { \null\, }
654 \vcenter \bgroup
655 \Let@
656 \restore@math@cr
657 \default@tag
658 \baselineskip 6 \ex@
659 \lineskip 1.5 \ex@
660 \lineskiplimit \lineskip
661 \ialign \bgroup
662 \hfil
663 $
664 \m@th
665 \scriptstyle
666 ##
667 % No \scriptsize here since we want to add the mstyle nodes
668 \UseTaggingSocket{math/luamml/save/nn}{ {} {mtd} }
669 $
670 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
671 \hfil
672 &&
673 \thickspace
674 \hfil
675 $
676 \m@th
677 \scriptstyle
678 ##
679 % No \scriptsize here since we want to add the mstyle nodes
680 \UseTaggingSocket{math/luamml/save/nn}{ {} {mtd} }
681 $
682 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}
683 \hfil
684 \crcr
685 }{%
686 \crcr
687 \UseExpandableTaggingSocket{math/luamml/mtable/smallmatrix/save}
688 \egroup
689 \egroup
690 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}
691 \UseTaggingSocket{ math/luamml/annotate/false } {}{ \, }
692 }
693 % \end{macrocode}
694 % \end{environment}
695 %
696 % \subsection{The split environment}
697 %
698 % The split environment is not trivial to handle as it has a rather convoluted
699 % implementation in amsmath: depending on in which display environment it is embedded
700 % it takes different branches, which makes it difficult to finalize the mtable.
701 %
702 % The following patches work with leqno and reqno if the (default) \texttt{centertags}
703 % are used.
704 % The currently fail (the structure is wrong) if the option \texttt{tbtags} is used.
705 % The alignment of the cells is currently not handled.
706 % A simple debug command until everything is sorted.
707 % \begin{macro}{\__math_split_debug_typeout:n}
708 % \begin{macrocode}
709 \cs_new:Npn\__math_split_debug_typeout:n#1{\use_none:n{#1}}
710 % \end{macrocode}
711 % \end{macro}
712 % We need to detect if \cs{gather@split} has been used or not
713 % \begin{macro}{\l__math_gathersplit_bool}
714 % \begin{macrocode}
715 \bool_new:N\l__math_gathersplit_bool
716 % \end{macrocode}
717 % \end{macro}
718 % At first a redefinition of the main environment. Here we only have to add
719 % the saving command for the inner table:
720 % \begin{environment}{split}
721 % \begin{macrocode}
722 \renewenvironment{split}{%
723 \__math_split_debug_typeout:n {begin~split}
724 \if@display
725 \ifinner
726 \@xp\@xp\@xp\split@aligned
727 \else
728 \ifst@rred \else \global\@eqnswtrue \fi
729 \fi
730 \else
731 \let\endsplit\@empty \@xp\collect@body\@xp\split@err
732 \fi
733 \collect@body\gather@split
734 }{\__math_split_debug_typeout:n{end~split}%
735 \crcr
736 \UseExpandableTaggingSocket{math/luamml/mtable/innertable/save}%
737 \egroup
738 \egroup
739 \iftagsleft@ \@xp\lendsplit@ \else \@xp\rendsplit@ \fi
740 }
741 % \end{macrocode}
742 % \end{environment}
743 %
744 % In \cs{gather@split} we have to add the finalization socket.
745 % We also set the boolean to true so that we can detect if
746 % the finalization has already happened. Perhaps this
747 % could be done in the luamml code instead?
748 % \begin{macro}{\gather@split}
749 % \begin{macrocode}
750 \def\gather@split#1#2#3{
751 \__math_split_debug_typeout:n{gather@split}%
752 \@xp\endgroup \reset@equation % math@cr will handle equation numbering
753 \iftag@
754 \toks@\@xp{\df@tag}%
755 \edef\split@tag{%
756 \gdef\@nx\df@tag{\the\toks@}%
757 \global\@nx\tag@true \@nx\nonumber
758 }%
759 \else \let\split@tag\@empty
760 \fi
761 \bool_set_true:N\l__math_gathersplit_bool
762 \spread@equation
763 \vcenter\bgroup
764 \gather@{\split@tag
765 \begin{split}#1\end{split}}%
766 \def\endmathdisplay@a{%
767 \__math_split_debug_typeout:n{endmathdisplay@a}
768 \__math_split_debug_typeout:n{finalize~innertable~endmathdisplay@a}
769 \math@cr
770 \black@ \totwidth@
771 \egroup
772 \egroup
773 \UseExpandableTaggingSocket{math/luamml/mtable/innertable/finalize}%
774 }%
775 }
776 % \end{macrocode}
777 % \end{macro}
778 %
779 % \begin{macro}{\insplit@}
780 % In \cs{insplit@} we have to add the sockets which store the cell content.
781 % \begin{macrocode}
782 \def\insplit@{\__math_split_debug_typeout:n{insplit@}%
783 \global\setbox\z@\vbox\bgroup
784 \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
785 \default@tag % disallow use of \tag here
786 \ialign\bgroup
787 \hfil
788 \strut@
789 $\m@th\displaystyle {##}%
790 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }%
791 $%
792 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
793 &$\m@th\displaystyle {{}##}%
794 \UseTaggingSocket{math/luamml/save/nNn}{ {} \displaystyle {mtd} }%
795 $%
796 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
797 \hfill % Why not \hfil?---dmj, 1994/12/28
798 \crcr
799 }
800 % \end{macrocode}
801 % \end{macro}
802 % And now the difficult part. Depending on the options leqno/reqno \cs{lendsplit@}
803 % or \cs{rendsplit@} are used for the typesetting and the inner table must
804 % be finalized here in case this hasn't happen yet. This must be tested with the
805 % boolean from \cs{gather@split}
806 % \begin{macrocode}
807 \def\lendsplit@{%
808 \global\setbox9\vtop{\unvcopy\z@}%
809 \ifinalign@
810 \setbox\@ne\vbox{%
811 \unvcopy\z@
812 \global\setbox8\lastbox
813 }%
814 \setbox\@ne\hbox{%
815 \unhcopy8%
816 \unskip
817 \setbox\tw@\lastbox
818 \unskip
819 \global\setbox\thr@@\lastbox
820 }%
821 \__math_split_debug_typeout:n{lendsplit@/aligncase}
822 \ifctagsplit@
823 \__math_split_debug_typeout:n{lendsplit@/aligncase/centertags}
824 \gdef\split@{%
825 \hbox to\wd\thr@@{}%
826 &\vcenter{\vbox{\moveleft\wd\thr@@\box9}}%
827 \__math_split_debug_typeout:n{finalize~innertable~aligncase}
828 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}%
829 }%
830 \else
831 %TODO, not correct yet.
832 \__math_split_debug_typeout:n{lendsplit@/aligncase/tbtags}
833 \gdef\split@{%
834 \hbox to\wd\thr@@{}%
835 &\vbox{\moveleft\wd\thr@@\box9}%
836 \__math_split_debug_typeout:n{finalize~innertable~aligncase}
837 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}%
838 }%
839 \fi
840 \else
841 % \end{macrocode}
842 % If not in align we need to test for \cs{gather@split}
843 % \begin{macrocode}
844 \ifctagsplit@
845 \bool_if:NTF \l__math_gathersplit_bool
846 {
847 \__math_split_debug_typeout:n{lendsplit/equationcase/centertags}
848 \gdef\split@%
849 {\UseTaggingSocket{math/luamml/annotate/false}{}{\vcenter{\box9}}}
850 }
851 {
852 \__math_split_debug_typeout:n {lendsplit/gathercase/centertags}
853 \gdef\split@{\vcenter{\box9}%
854 \__math_split_debug_typeout:n {finalize~innertable~gathercase}
855 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}}
856 }
857 \else
858 % TODO tbtags not correct yet.
859 \bool_if:NTF \l__math_gathersplit_bool
860 {
861 \__math_split_debug_typeout:n {lendsplit/equationcase/tbtags}
862 \gdef\split@%
863 {\UseTaggingSocket{math/luamml/annotate/false}{}{\box9}}
864 }
865 {
866 \__math_split_debug_typeout:n {lendsplit/gathercase/tbtags}
867 \gdef\split@{
868 \box9%
869 \__math_split_debug_typeout:n {finalize~innertable~gathercase}
870 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}}
871 }
872 \fi
873 \fi
874 \aftergroup\split@
875 }
876 % \end{macrocode}
877 %
878 % \begin{macro}{\rendsplit@}
879 % And more or less the same for the \cs{rendsplit@} environment.
880 % \begin{macrocode}
881 \def\rendsplit@{%
882 \ifinalign@
883 \global\setbox9 \vtop{%
884 \unvcopy\z@
885 \global\setbox8 \lastbox
886 \unskip
887 }%
888 \setbox\@ne\hbox{%
889 \unhcopy8
890 \unskip
891 \global\setbox\tw@\lastbox
892 \unskip
893 \global\setbox\thr@@\lastbox
894 }%
895 \ifctagsplit@
896 \gdef\split@{%
897 \hbox to\wd\thr@@{}
898 &\vcenter{\vbox{\moveleft\wd\thr@@\boxz@}}%
899 \__math_split_debug_typeout:n {rendsplit/aligncase/centertags}
900 \__math_split_debug_typeout:n {finalize~innertable~aligncase}
901 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}
902 }%
903 \else
904 \__math_split_debug_typeout:n{rendsplit@/aligncase/tbtags}
905 %TODO tbtags is not correct yet
906 \global\setbox7 \hbox{\unhbox\tw@\unskip}%
907 \gdef\split@{%
908 \global\@tempcnta\column@
909 &\setboxz@h{}%
910 \savetaglength@
911 \global\advance\row@\@ne
912 \vbox{\moveleft\wd\thr@@\box9}%
913 \crcr
914 \noalign{\global\lineht@\z@}%
915 \add@amps\@tempcnta
916 \UseTaggingSocket{math/luamml/annotate/false}{}{\box\thr@@}
917 &\box7
918 \__math_split_debug_typeout:n {finalize~innertable~aligncase}
919 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}%
920 }%
921 \fi
922 % \end{macrocode}
923 % and again the if we are not in align we need to test for \cs{gathersplit}
924 % \begin{macrocode}
925 \else
926 \ifctagsplit@
927 \bool_if:NTF \l__math_gathersplit_bool
928 {
929 \__math_split_debug_typeout:n {rendsplit/equationcase/centertags}
930 \gdef\split@%
931 {\UseTaggingSocket{math/luamml/annotate/false}{}{\vcenter{\boxz@}}}
932 }
933 {
934 \__math_split_debug_typeout:n {rendsplit/gathercase/centertags}
935 \gdef\split@{\vcenter{\boxz@}%
936 \__math_split_debug_typeout:n {finalize~innertable~gathercase}
937 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}}
938 }
939 \else
940 \bool_if:NTF \l__math_gathersplit_bool
941 {
942 \__math_split_debug_typeout:n {rendsplit/equationcase/tbtags}
943 \gdef\split@{%
944 \UseTaggingSocket{math/luamml/annotate/false}{}{\boxz@}}
945 }
946 {
947 \__math_split_debug_typeout:n {rendsplit/gathercase/tbtags}
948 \gdef\split@{%
949 \boxz@
950 \UseTaggingSocket{math/luamml/mtable/innertable/finalize}}
951 }
952 \fi
953 \fi
954 \aftergroup\split@
955 }
956 % \end{macrocode}
957 % \end{macro}
958 % \subsection{\cs{intertext}}
959 % The \cs{intertext} command errors with active tagging as it is processed twice
960 % which leads to duplicated structures.
961 %
962 % \begin{macro}{\intertext@}
963 % TODO: review and add sockets!
964 % \begin{macrocode}
965 \def\intertext@{%
966 \def\intertext##1{%
967 \ifvmode\else\\\@empty\fi
968 \noalign{%
969 \penalty\postdisplaypenalty\vskip\belowdisplayskip
970 \vbox{
971 % \end{macrocode}
972 % Stop tagging when measuring:
973 % \begin{macrocode}
974 \ifmeasuring@\tag_suspend:n{\measuring}\fi
975 \normalbaselines
976 \ifdim\linewidth=\columnwidth
977 \else \parshape\@ne \@totalleftmargin \linewidth
978 \fi
979 % \end{macrocode}
980 % End the previous mc:
981 % \begin{macrocode}
982 \tag_mc_end_push:
983 % \end{macrocode}
984 % We are already in a par so we change now to text:
985 % \begin{macrocode}
986 \tagpdfsetup{para/tag=P}%
987 % \end{macrocode}
988 % TODO why \cs{tagpdfparaOn} needed?
989 % \begin{macrocode}
990 \tagpdfparaOn
991 \noindent\ignorespaces##1\par
992 % \end{macrocode}
993 % Restart the MC
994 % \begin{macrocode}
995 \tag_mc_begin_pop:n{}}%
996 \penalty\predisplaypenalty\vskip\abovedisplayskip%
997 }%
998 }}
999 % \end{macrocode}
1000 % \end{macro}
1001 %
1002 % \subsection{\cs{text}}
1003 %
1004 % The \cs{text} command uses \cs{mathchoice} which \enquote{typesets} the argument
1005 % four times. This makes it quite problematic for tagging. Without precautions
1006 % structure objects would be created four times and would get MC-chunks as kids
1007 % that don't really exist. amsmath contains a switch that allows to execute code
1008 % only in the first (displaymath) branch, but that isn't usable here. At first because
1009 % we don't know if the first branch creates the same structure as the one that is
1010 % actually used. At second because the engines executes some commands like \cs{label}
1011 % and \cs{pdfannot} only at shipout from the branch that really was used. So we would
1012 % get structure data from one \cs{mathchoice}-branch and MC-labels and links from another
1013 % one and that gets very messy.
1014 %
1015 % We therefore have to avoid that tagging is active in unused branches. In pdflatex it
1016 % is not possible to detect the mathstyle before, so we use a label. With lualatex is
1017 % is possible to redefine \cs{text} not to use \cs{mathchoice}
1018 %
1019 % \begin{macro}{\text@}
1020 % \begin{macrocode}
1021 \AddToHook{package/amstext/after}
1022 {
1023 % \end{macrocode}
1024 % currently amsmath is loaded in a begindocument hook, so this
1025 % test is fine. If amstext is loaded earlier (in the kernel), this needs perhaps a change.
1026 % \changes{v0.1d}{2024/12/03}{Add tagging socket for luamml support}
1027 % \changes{v0.1i}{2025-09-29}{Additional braces to allow use in subscripts, tagging issue 995}
1028 % \begin{macrocode}
1029 \tag_if_active:T
1030 {
1031 \sys_if_engine_luatex:TF
1032 {
1033 \def\text@#1{{
1034 \tag_socket_use:nnn {math/luamml/hbox}{}
1035 {{%
1036 \ifcase\mathstyle
1037 \hbox{{#1}}\or
1038 \hbox{{#1}}\or
1039 \hbox{{#1}}\or
1040 \hbox{{#1}}\or
1041 \hbox{{\let\f@size\sf@size\selectfont#1}}\or
1042 \hbox{{\let\f@size\sf@size\selectfont#1}}\or
1043 \hbox{{\let\f@size\ssf@size\selectfont#1}}\or
1044 \hbox{{\let\f@size\ssf@size\selectfont#1}}\or
1045 \ERROR
1046 \fi
1047 \check@mathfonts
1048 }}}}
1049 }
1050 {
1051 \def\text@#1
1052 {{
1053 \int_gincr:N\g__math_mathchoice_int
1054 \tag_suspend:n{\text@}
1055 \mathchoice
1056 {
1057 \__math_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{0}
1058 \textdef@\displaystyle\f@size{#1}
1059 }
1060 {
1061 \__math_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{2}
1062 \textdef@\textstyle\f@size{\firstchoice@false #1}
1063 }
1064 {
1065 \__math_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{4}
1066 \textdef@\textstyle\sf@size{\firstchoice@false #1}
1067 }
1068 {
1069 \__math_tag_if_mathstyle:en{mathchoice-\int_use:N\g__math_mathchoice_int}{6}
1070 \textdef@\textstyle \ssf@size{\firstchoice@false #1}
1071 }
1072 \check@mathfonts
1073 }}
1074 }
1075 }
1076 }
1077 % \end{macrocode}
1078 % \end{macro}
1079 %
1080 % \subsection{\cs{pmb}}
1081 % \cs{pmb} prints its argument three times. For tagging we must mark
1082 % two of occurrences as artifact.
1083 % For luatex the attributes in the box must be reset, for this
1084 % we switch to expl3-boxes.
1085 % \begin{macro}{\pbm@@,\pmb@}
1086 % \changes{0.1g}{2025-05-23}{correct box names, tagging issue #842}
1087 % \begin{macrocode}
1088 \AddToHook{package/amsbsy/after}
1089 {
1090 \def\pmb@@#1#2#3{\leavevmode\hbox_set:Nn\l__math_tmpa_box{xxx#3}
1091 \dimen@-\box_wd:N\l__math_tmpa_box
1092 \kern-.5\ex@\box_use:N\l__math_tmpa_box
1093 \tag_mc_end:\tag_mc_begin:n{artifact}
1094 \tag_mc_reset_box:N\l__math_tmpa_box
1095 \kern\dimen@\kern.25\ex@\raise.4\ex@\box_use:N\l__math_tmpa_box
1096 \kern\dimen@\kern.25\ex@\box_use_drop:N\l__math_tmpa_box
1097 \tag_mc_end:\tag_mc_begin:n{}
1098 }
1099 \def\pmb@#1#2{\hbox_set:Nn\l__math_tmpa_box{$\m@th#1{#2}$}
1100 \setboxz@h{$\m@th#1\mkern.5mu$}\pmbraise@\wdz@
1101 \binrel@{#2}
1102 \dimen@-\box_wd:N\l__math_tmpa_box
1103 \binrel@@{
1104 \mkern-.8mu\box_use:N\l__math_tmpa_box
1105 \tag_mc_end:\tag_mc_begin:n{artifact}
1106 \tag_mc_reset_box:N\l__math_tmpa_box
1107 \kern\dimen@\mkern.4mu\raise\pmbraise@\box_use:N\l__math_tmpa_box
1108 \kern\dimen@\mkern.4mu\box_use_drop:N\l__math_tmpa_box
1109 \tag_mc_end:\tag_mc_begin:n{}
1110 }
1111 }
1112 }
1113 % \end{macrocode}
1114 % \end{macro}
1115 %
1116 %
1117 % \begin{macrocode}
1118 \ExplSyntaxOff
1119 % \end{macrocode}
1120 %
1121 %
1122 % \begin{macrocode}
1123 %</kernel>
1124 % \end{macrocode}
1125 %
1126 % \Finale
1127 %