Debian Codegraph @

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

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