Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-math.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 %
21
22 \def\ltlabmathdate{2026-05-07}
23 \def\ltlabmathversion{0.7c}
24 %
25 %<*driver>
26 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
27 \documentclass[kernel]{l3in2edoc}
28
29 \EnableCrossrefs
30 \CodelineIndex
31
32 \usepackage{todonotes}
33
34 \begin{document}
35 \DocInput{latex-lab-math.dtx}
36 \end{document}
37 %</driver>
38 %
39 % \fi
40 %
41 %
42 % \title{The \texttt{latex-lab-math} code\thanks{}}
43 % \author{Frank Mittelbach, Joseph Wright, \LaTeX{} Project}
44 % \date{v\ltlabmathversion\ \ltlabmathdate}
45 %
46 % \maketitle
47 %
48 % \newcommand\NEW[1]{\marginpar{\mbox{}\hfill\fbox{New: #1}}}
49 % \providecommand\class[1]{\texttt{#1.cls}}
50 % \providecommand\pkg[1]{\texttt{#1}}
51 %
52 %
53 %
54 % ^^A \car {...} for marginal comments
55 % ^^A \car*{...} for longer inline comments
56 %
57 % \NewDocumentCommand\car{sO{}m}
58 % {\IfBooleanTF{#1}{\todo[inline,color=blue!10,#2]{#3}}^^A
59 % {\todo[color=blue!10,#2]{#3}}}
60 %
61 % \NewDocumentCommand\fmi{sO{}m}
62 % {\IfBooleanTF{#1}{\todo[inline,#2]{#3}}^^A
63 % {\todo[#2]{#3}}}
64 %
65 % \begin{abstract}
66 % This module handles the tagging of math. It adapts the math command and switches to
67 % capture math material and then tag this material.
68 % \end{abstract}
69 %
70 % \tableofcontents
71 %
72 % \section{Introduction}
73 %
74 % Tagging math involves a variety of tasks that require that
75 % math is captured before the typesetting:
76 % \begin{itemize}
77 % \item When typesetting the math MC-tags and structure commands must
78 % be inserted at the begin and the end, and perhaps also around lines
79 % or other subparts of the equation.
80 % \item The source and/or a MathML-representation of the source must be available
81 % so that it can be (perhaps after some preprocessing) be used in an associated file
82 % or in an alternate text.
83 % \item It should be possible to measure the math for, e.g., a bbox setting.
84 % \end{itemize}
85 %
86 % This file implements capture of all math mode material at the outer
87 % level, i.e., a formula is captured in its entirety with inner text
88 % blocks (possibly containing further math) absorbed as part of the
89 % formula. For example,
90 %\begin{verbatim}
91 % \[ a \in A \text{ for all $a<5$} \]
92 %\end{verbatim}
93 % would only result in a single capture of the tokens
94 % ``\verb*/a \in A \text{ for all $a<5$}/''.
95 %
96 %
97 % \section{Math capture} \label{sec:mathcapture}
98 % In the current setup
99 % \begin{itemize}
100 % \item |$|, |\(...\)|, |\[...\]| and |$$| grab (eventually through a command in \cs{everymath}/cs{everydisplay})
101 % if the boolean \cs{l_@@_collected_bool} is false.
102 % If the boolean is true they behave normally and can for example contain verbatim.
103 %
104 % \item All (registered) environments grab their body
105 % regardless of the state of the boolean. For
106 % |equation|, |equation*| and |math| this is a change as they no longer can
107 % contain verbatim.
108 % \end{itemize}
109 %
110 % \section{Avoiding math capture}\label{sec:nocapture}
111 % In most cases when an environment or command switches into math,
112 % the semantic meaning of the content \emph{is} math and
113 % grabbing and then tagging that as a Formula is adequate.
114 %
115 % But there are exceptions, most prominently with the math shift token |$|.
116 % \begin{itemize}
117 % \item |$| can be used to center a box with |\vcenter|,
118 % for example, in the tabular code\footnote{This will change in \LaTeX\ 2026-06-01.
119 % The tabular code will then no longer use \cs{vcenter} and math}.
120 % The opening |$| is then often in a
121 % different command than the closing |$| and the content of the box
122 % should be tagged normally, including all math it contains.
123 % This means one wants to avoid that the opening |$| triggers the math
124 % grabbing and creates a Formula structure, and after the |$| one
125 % wants to switch back to normal text and math capture/tagging.
126 %
127 % \item |$| is used to place superscripts and subscripts, see the definition of
128 % |\textsuperscript| which uses |\ensuremath| internally\footnote{This will change in \LaTeX\ 2026-06-01.
129 % \cs{textsuperscript} and \cs{textsubscript} will no longer use math mode.}.
130 % Inside the superscript
131 % in special cases you may want more tagging (including nested math tagging) but
132 % typically it is simple text.
133 %
134 % \item |$| is used to access a char in a math font.
135 %
136 % For example the |\meta| command uses internally the math commands
137 % |\langle| and |\rangle| around its argument:\\
138 % |$\langle$\textit{argument}$\rangle$|
139 % which gives $\langle$\textit{argument}$\rangle$.
140 %
141 % The symbols are then clearly not math.
142 % (Beside disabling math tagging one could also use text commands like
143 % |\textlangle| and |\textrangle|: \textlangle\textit{argument}\textrangle{}.
144 % Depending on the font that could even look better,
145 % but it requires that the font supports the chars, which is not the case
146 % for various OpenType fonts.)
147 % Additional tagging inside the math should be not needed.
148 % If the symbols need an actualtext then a Span can be added around the whole material.
149 %
150 % \end{itemize}
151 %
152 % \subsection{Options to suppressing math capture and tagging}
153 % We are therefore providing a number of options to suppress the collection/grabbing
154 % of the math and with it the tagging.
155 % \DescribeMacro\MathCollectTrue
156 % \DescribeMacro\MathCollectFalse
157 % \DescribeMacro\m@th
158 % \DescribeMacro\SuspendTagging
159 % These commands can be used to control locally the tagging of math.
160 % The first two are new commands. |\m@th| is a standard \LaTeX\ command
161 % used in a number of places to set |\mathsurround| to zero;
162 % with active tagging it is also used to identify math
163 % that should not be tagged, because it has traditionally be used in exactly
164 % the places where math mode is used for its layout charactistics and not for
165 % representing a formula.\footnote{This way even code that is not adjusted up
166 % for tagging is handled correctly if it uses \cs{m@th}.}.
167 % Details are described below.
168 % (|\SuspendTagging| is documented in source2e.pdf.)
169 %
170 % To set |\mathsurround| without disabling math tagging, use |\mathsurround\z@| directly.
171 %
172 %
173 % \subsubsection{Using the trigger token \cs{m@th} \emph{inside} the math}
174 %
175 % If the grabbed math contains the token |\m@th| the tagging/processing
176 % of the math is suppressed. As the math is nevertheless grabbed, this requires
177 % that the end math shift token is not hidden in some other command.
178 % |\m@th| sets also |\mathsurround| to zero, which is often
179 % wanted in untagged math anyway.
180 %
181 % Text tagging is \emph{not} suppressed inside the math, e.g.,
182 % |\mbox{\emph{text}}| will still create an Em-structure.
183 % In contrast nested math is not tagged.
184 %
185 % To suppress the text tagging |\SuspendTagging{}| can be used:
186 % \begin{itemize}
187 % \item no math tagging, but |\emph| is tagged:\\
188 % |$\m@th\langle\mbox{\emph{if and only if} $x=y$}\rangle$|
189 %
190 % \item no internal tagging:
191 % \\
192 % |$\m@th\SuspendTagging{}\langle\mbox{\emph{if and only if}} $x=y$}\rangle$|
193 % \end{itemize}
194 %
195 % The method is quite suitable for symbols and also for sub- and superscripts
196 % (as long as they don't contain nested math that should be tagged).
197 %
198 %
199 % \subsubsection{Using \cs{m@th} \emph{before} the opening \texttt{\$}}
200 %
201 % |\m@th| (as defined in the latex-lab-math code) sets also the internal boolean
202 % which controls the grabbing to true and so when it is used \emph{before}
203 % some math it disables math grabbing and tagging for all following math
204 % in the current group (including nested math).
205 % Text tagging inside the math is still active, it can be disabled as above
206 % with |\SuspendTagging{}|.
207 %
208 % When |\m@th| is used like this it is possible to reenable the math
209 % tagging of nested math, by adding |\MathCollectTrue| after the opening |$|.
210 %
211 % \begin{itemize}
212 % \item no math tagging, but |\emph| is tagged:\\
213 % |{\m@th$\langle\mbox{\emph{if and only if} $x=y$}\rangle$}|
214 %
215 % \item both nested math and |\emph| is tagged:\\
216 % |{\m@th$\MathCollectTrue\langle\mbox{\emph{if and only if} $x=y$}\rangle$}|
217 % \end{itemize}
218 %
219 % The method is suitable for symbols and sub- and superscripts too
220 % (it is actually how superscripts avoid math tagging before \LaTeX~2026-06-01).
221 % It can also be used in cases where the end math shift token is
222 % hidden in some other command. But it is not so useful for
223 % larger boxes as it sets |\mathsurround| to zero.
224 % Grouping should be used to avoid side-effects on following math.
225 %
226 %
227 %\subsubsection{Disabling math tagging with \cs{MathCollectFalse}}
228 %
229 % Without a side-effect on |\mathsurround| the math grabbing can be
230 % suppressed and reenabled by setting the boolean that controls the collecting.
231 % So in the following example the math in the |\mbox| is properly tagged,
232 % but the angled brackets are simple text.
233 %
234 % \begin{verbatim}
235 % %stop math grabbing
236 % \MathCollectFalse
237 % $
238 % %(optional) restart math grabbing for nested math
239 % \MathCollectTrue
240 % \langle\mbox{\emph{if and only if}} $x=y$}\rangle
241 % $
242 % %(optional) restart math grabbing for following math
243 % % if there is no grouping
244 % \MathCollectTrue
245 % \quad $x=1$
246 % \end{verbatim}
247 % The method is suitable if a box should be centered with
248 % |\vcenter| (and before \LaTeX~2026-06-01 was used in array.sty for the tabular code).
249 %
250 % \section{Math capture interfaces}
251 %
252 % \subsection{Code level interfaces}
253 %
254 % \begin{function}{\math_register_env:n, \math_register_env:nn}
255 % \begin{syntax}
256 % \cs{math_register_env:n} \Arg{env}
257 % \cs{math_register_env:nn} \Arg{env} \Arg{options}
258 % \end{syntax}
259 % Registers the \meta{env} as a math environment which should be captured
260 % and made available. This is necessary for all top-level math mode
261 % environments: low-level errors may result if these are not correct
262 % set up. One or more key--value \meta{options} may also be given:
263 % \begin{itemize}
264 % \item[\texttt{arg-spec}] The argument specification taken by the
265 % beginning of the environment; this is used to remove non-mathematical
266 % material.
267 % \end{itemize}
268 % \end{function}
269 %
270 % \begin{function}{\math_processor:n}
271 % \begin{syntax}
272 % \cs{math_processor:n} \Arg{tokens}
273 % \end{syntax}
274 % Declares that the captured math content should be passed to the
275 % \meta{tokens}, which will receive the environment type as |#1| and
276 % the content as |#2|. The processing is done before the typesetting. It is not
277 % applied if \cs{ifmeasuring@} is true.
278 % \end{function}
279 %
280 % \subsection{Document level interfaces}
281 %
282 % \begin{function}{\RegisterMathEnvironment}
283 % \begin{syntax}
284 % \cs{RegisterMathEnvironment} \oarg{options} \Arg{env}
285 % \end{syntax}
286 % Registers the \meta{env} as a math environment which should be captured
287 % and made available. This is necessary for all top-level math mode
288 % environments: low-level errors may result if these are not correct
289 % set up. One or more key--value \meta{options} may also be given:
290 % \begin{itemize}
291 % \item[\texttt{arg-spec}] The argument specification taken by the
292 % beginning of the environment; this is used to remove non-mathematical
293 % material.
294 % \end{itemize}
295 % \end{function}
296 %
297 % \begin{function}{\MathCollectTrue,\MathCollectFalse}
298 % \begin{syntax}
299 % \cs{MathCollectTrue}\\
300 % \cs{MathCollectFalse}
301 % \end{syntax}
302 % This activates/deactivates the math collection of the math shift token.
303 % See above for cases when this can be usefull.
304 % \end{function}
305 %
306 % \section{Math tagging}
307 %
308 % \subsection{Code requirements}
309 % The tagging code has to handle
310 % \begin{itemize}
311 % \item the embedding into the surrounding. This means
312 % \begin{itemize}
313 % \item closing and reopening MC-chunks
314 % \item closing and reopening text/P-structures
315 % \item handling interferences of the tagging code with penalties and spacing.
316 % \end{itemize}
317 % \item the actual tagging which means to do some or all of the following tasks:
318 % \begin{itemize}
319 % \item setup content for an associated source file
320 % \item setup content for an associated MathML file
321 % \item setup content for the /Alt key
322 % \item setup content for the /ActualText key
323 % \item setup attributes
324 % \item add associated files
325 % \item add a Formula structure
326 % \item surround elements of the equation with MathML structure elements
327 % (currently only luatex with luamml)
328 % \end{itemize}
329 % \end{itemize}
330 %
331 % \subsection{Inline math}
332 %
333 % The embedding code is added through
334 % the tagging sockets
335 % \begin{itemize}
336 % \item |math/inline/begin|
337 % \item |math/inline/end|
338 % \end{itemize}
339 % The sockets simply push and pop the MC currently. Without
340 % tagging they use the noop-plug.
341 %
342 % The actual tagging is in done through the tagging sockets
343 % \begin{itemize}
344 % \item |math/inline/formula/begin|
345 % This socket takes the math as second argument and its code
346 % should output it for typesetting.
347 % The |default| plug of the socket calls these three internal sockets
348 % for the tagging support:
349 % \begin{itemize}
350 % \item |math/content| This should set up the various
351 % content variables (empty variables are ignored by the structure code
352 % and so can be used to suppress a setting).
353 % \item |math/struct/begin| This calls \cs{tag_struct_begin:n}.
354 % It should also write the associated files if needed.
355 % \end{itemize}
356 % \item |math/inline/formula/end|
357 % This socket ends the formula structure(s). The |default|
358 % plug calls this internal socket:
359 % \begin{itemize}
360 % \item |tagsupport/math/struct/end|
361 % \end{itemize}
362 % \end{itemize}
363 %
364 % \subsection{Display math}
365 %
366 % \textit{to be written}
367 %
368 % \subsection{Associated Files}
369 %
370 % The current code allows the attachment of two types of associated file to the
371 % Formula structure:
372 % the \LaTeX\ source and a MathML representation.
373 % Technically both can be attached---AF is an array
374 % of file references-----in practice there can be problems with PDF consumers:
375 % e.g., ngpdf used both and so showed the equation twice
376 % (this has been corrected in the newest version) and
377 % Foxit seems to see only the first AF in the array (so we attach the
378 % MathML as the first file).
379 %
380 % The \LaTeX\ source can be (and is) attached automatically.
381 % It can be suppressed by an option with
382 % \texttt{math/tex/AF=false}, see below.
383 %
384 % The MathML is attached if the files |\jobname-mathml.html| and/or
385 % |\jobname-luamml-mathml.html| are found
386 % and if they contains a suitable MathML snippet for the current formula.
387 % If the files contain more than one suitable snippet (as identified by the hash)
388 % the first one is used.
389 % |\jobname-luamml-mathml.html| is automatically generated (see below section~\ref{sec:luamml})
390 % and read after |\jobname-mathml.html|. This means that |\jobname-mathml.html| can contain
391 % improved versions of a formula.
392 %
393 % The MathML processing can be suppressed globally by emptying the list of
394 % mathml files with |math/mathml/sources=|. Locally for a formula |math/mathml/AF=false|
395 % can be used.
396 %
397 % For a MathML representation a file with such representations must be provided.
398 % If the equation is numbered the numbering should be part of the MathML as
399 % the |Lbl| substructure is ignored if an MathML is used (see https://github.com/foxitsoftware/PDF_UA-2).
400 %
401 % The MathML representation is given in a special format.
402 % It is meant to be a valid html file
403 % that can be viewed in a browser.
404 % For this it can start with |<!DOCTYPE html><html>| and end with |</html>|
405 % It should have the extension \texttt{.html}. The \meta{mathml} content
406 % is read with special catcodes, so can contain ambersands, hashes, comment chars
407 % and unmatched braces such as |<mo>{</mo>|
408 %
409 % The file should contain a number of representations in this format:
410 % \begin{quote}
411 % |<div>| \\
412 % | <h2>\mml| \meta{key}|</h2>|\\
413 % | <p>|\meta{source}|</p>| \\
414 % | <p>|\meta{hash}|</p>| \\
415 % | <math | \meta{attributes} |>|\\
416 % \meta{mathml}\\
417 % | </math>|\\
418 % |</div>|
419 % \end{quote}
420 % The keywords |<div>|, |<h2>\mml|, |<p>|, |<math|, |</math>| |</div>| are required as
421 % they are used to delimit the arguments by the \LaTeX{} code.
422 %
423 % \meta{key} and \meta{source} are only used for debugging, they help to identify
424 % the equation referred by this representation. The source should be used correctly escaped
425 % |&| and |<| so that if gives valid html!
426 %
427 % \meta{attributes} is not required either, but can, e.g., contain attributes
428 % to improve the display in a browser:
429 % \begin{verbatim}
430 % <math alttext="\mathbf{G}" class="ltx_Math" display="inline">
431 % \end{verbatim}
432 % It can also contain the name space declaration: \\
433 % |xmlns="http://www.w3.org/1998/Math/MathML"|\footnote{But it is probably not needed and only blows up the PDF.}
434 %
435 %
436 % By default the code tries at the begin of the document
437 % to read a file |\jobname-mathml.html| in the |html|-format.
438 % The file name can be changed with
439 % \begin{verbatim}
440 % mathml/setfiles={filename1,filename2}
441 % \end{verbatim}
442 % (without extension, |html| is added automatically).
443 % If there is a list, all files are loaded.
444 % If a file doesn't exist it is ignored, only an info is written to the log.
445 %
446 % Currently every MathML-snippet from a file is embedded into the PDF,
447 % it is not checked first if it is actually used (simply writing everything to the PDF
448 % is a bit easier than keeping everything in memory and also means that
449 % the snippets are one after the other in the PDF).
450 %
451 % As mentioned above the MathML-AF can be suppressed for the equations in a group with
452 % |math/mathml/AF=false|, or
453 % completely by setting |math/mathml/sources=| in the preamble.
454 %
455 % Files embedded in a PDF can be listed in the attachments panel of a PDF viewer.
456 % This is probably not so useful for lots of small files (but one could create
457 % collections), but as long as PDF editors or viewers don't offer
458 % proper support to access the AF it can help so have them there. The MathML are
459 % added by default, but the \LaTeX{} source not. This can be changed with
460 % |viewer/pane/mathsource=true| (anywhere in the document) and |viewer/pane/mathml=false| (in the
461 % preamble, before the external file is read).
462 %
463 % \subsection{MathML in an attribute}
464 % Microsoft Word puts the MathML into a proprietary attribute. For testing
465 % purpose this is implemented here too. The writing of such an attribute can
466 % be activated globally with |tagging-setup={math/setup=mathml-MS}|.
467 % Locally it can be disabled with |math/MS/use=false|. The code makes use of the exported
468 % MathML file, so it needs two compilation and will not work if embedding of MathML is disabled.
469 %
470 %
471 % \subsection{Automatic mathml creation with luamml}\label{sec:luamml}
472 %
473 % If lualatex and one of the packages \pkg{unicode-math} or \pkg{lua-unicode-math}
474 % is used,
475 % the package \texttt{luamml} is loaded and this package
476 % will then automatically generate the file |\jobname-luamml-mathml.html|
477 % with MathML representations of all math formulas.
478 % This file is then used in subsequent compilations and works also with
479 % pdflatex.
480 %
481 % The generation of the file can be suppressed (in the preamble)
482 % with\\
483 % |math/mathml/luamml/write=false|.
484 %
485 % If neither \pkg{unicode-math} or \pkg{lua-unicode-math} are used,
486 % the loading of \pkg{luamml} and with it the generation of the file can be forced
487 % with |math/mathml/luamml/load=true| or |math/mathml/luamml/write=true|
488 % but be aware that it is then possible that various symbols
489 % are mapped to the wrong Unicode code points.
490 %
491 % The package \pkg{luamml} is still quite experimental and the output should be checked.
492 % The |\jobname-luamml-mathml.html| file may be previewed in a browser although
493 % you may need to add additional css or javascript declarations
494 % to enable browser support for all MathML constructs.
495 %
496 %
497 % \subsection{Summary of math options}
498 % The following options exist to make math more accessible:
499 % \begin{description}
500 % \item[ActualText] An \texttt{ActualText} can be placed on structure elements,
501 % but can also be added in the stream on a \texttt{BDC} marker with a \texttt{Span}
502 % tag (normally an independent marker without an MCID number, it is not clear yet
503 % if it can be used on an MC-chunk).
504 % The content is a text string, typically one or a few Unicode characters.
505 % \texttt{ActualText} is meant to replaces the content
506 % and should only be used on small entities,
507 % e.g., to define the semantic or the Unicode code point of a symbol.
508 % \texttt{ActualText} is not supported by all PDF reader.
509 % It is also unknown where it should be used at best (in a structure element,
510 % or on an independent Span-BDC) and what happens if it is used in more than
511 % one place.
512 % \begin{description}
513 % \item[enabled by default?] False
514 % \item[how to enable/disable] No interface yet.
515 % \texttt{ActualText} can only be added on the Formula structure element by
516 % changing the \texttt{tagsupport/math/content} or some other socket.
517 % For a BDC marker one can, e.g., use
518 % \begin{verbatim}
519 % \pdf_string_from_unicode:nnN{utf16/hex}{€}\l_tmpa_tl
520 % \pdf_bdc:ee{Span}{/ActualText\l_tmpa_tl}content\pdf_emc:
521 % \end{verbatim}
522 % There should be no page break in the \meta{content} and the BDC should be correctly
523 % nested into tagging, so, e.g., a \cs{leavevmode} should be issued before the bdc command.
524 % \item[Consumer support] in part and in part buggy, needs tests \ldots
525 % \end{description}
526 %
527 % \item[Alt] Like \texttt{ActualText} the \texttt{Alt} key can be used on
528 % structure elements and on \texttt{Span} in the stream. It should contain a description
529 % of the content and is mainly meant for images. PDF/UA-1,
530 % which views math formulas as illustrations, mandates the key
531 % also for \texttt{Formula} structure elements.
532 % \begin{description}
533 % \item[enabled by default?] false unless PDF/UA-1 is detected,
534 % then it is enabled in the begindocument/end hook
535 % (this will reconsidered when it is clear, that
536 % the use of \texttt{Alt} does not shadow MathML). It can be enabled for
537 % all engines and PDF versions.
538 % \item[enable/disable] \verb+\tagpdfsetup{math/alt/use}+ (local boolean,
539 % so can be used on individual equations)
540 % \item[default value] A template text (stored in \cs{l_@@_content_template_tl})
541 % starting with \texttt{LaTeX formula starts}.
542 % \item[user value] No interface currently provided. This needs optional arguments
543 % or an external setup command.
544 % See \url{https://github.com/latex3/tagging-project/discussions/717}.
545 %
546 % \end{description}
547 %
548 % \item[source-AF] The \LaTeX{}-source of the equation can
549 % be attached as an associated file with mime-type
550 % application/Fx-tex. The \texttt{AFRelationship} is \texttt{Source}.
551 % The source is embedded without expansion. This means that targets of
552 % references and macros are not resolved.
553 % The files are by default not shown in the EmbeddedFiles pane,
554 % this can be enabled with |viewer/pane/mathsource=true|.
555 % If an A-standard is used, it must be one that allows embedded files, e.g., A-4f.
556 %
557 % \begin{description}
558 % \item[enabled by default?] true for all engines and PDF versions
559 % \item[enable/disable] \verb+\tagpdfsetup{math/tex/AF}+ (local boolean, so can
560 % be used on individual equations)
561 % \item[default value] source code including dollars or environment name.
562 % \item[consumer support] Currently only ngpdf makes
563 % use of it: if there is no MathML it passes the source to mathjax.
564 % \end{description}
565 %
566 % \item[luamml] The following options make (with lualatex) use
567 % of the \pkg{luamml} package. \pkg{luamml} is currently automatically
568 % loaded (at the end of the preamble) if \pkg{unicode-math} or \pkg{lua-unicode-math}
569 % has been detected.
570 % The loading can be forced or suppressed
571 % with \verb+\tagpdfsetup{math/mathml/luamml/load=true/false}+.
572 %
573 % \pkg{luamml} affects all math, locally it can be stopped with |math/mathml/ignore|,
574 % or by using the commands described in the package.
575 %
576 % \item[mathml-AF] A MathML representation of the equation can be attached
577 % to the structure. The configuration possibilities are rather complex as the
578 % keys have to control three different aspects:
579 % The \emph{generation} of the file with the MathML fragments,
580 % the \emph{reading} and \emph{embedding} of the MathML fragments,
581 % and the \emph{association} of a MathML fragment to a specific equation.
582 %
583 % \begin{description}
584 % \item[generation]
585 % With pdf\LaTeX{} MathML fragments can not be generated automatically,
586 % but a file with dummy fragments for every equation will be written if
587 % \verb+\tagpdfsetup{math/mathml/write-dummy}+ is issued in the preamble.
588 %
589 % With lua\LaTeX{} a file with MathML fragments will be created automatically
590 % if the package \pkg{luamml} has been loaded (see above).
591 %
592 % \item[reading and embedding]
593 % By default the code will read and embed
594 % MathML from |\jobname-mathml.html| and |\jobname-luamml-mathml.html| in this order and
595 % the first fragment with a new hash value will be inserted.
596 % The list of sources and their order can be changed with the key
597 % |math/mathml/sources|, setting that to an empty value suppresses
598 % the loading MathML associated files completely. For efficiency reasons
599 % it embeds math fragments directly, there is no check yet if the fragment is
600 % actually used.
601 %
602 % The files are by default shown in the EmbeddedFiles pane,
603 % this can be disabled with |viewer/pane/mathml=false|.
604 %
605 % \item[attaching] A MathML fragment is currently
606 % attached as an associated file to a Formula if the hash of
607 % the source matches the hash of the fragment. This is not a perfect test:
608 % equations with the same source and so the same hash
609 % can have different MathML representation, e.g.,
610 % if there are references or commands or counters in the equation. This
611 % will change in a future version.
612 % The attachment can be suppressed locally with |math/mathml/AF=false|.
613 % The MathML fragment will still be embedded in the PDF!
614 %
615 % % TODO: adapt test
616 % \end{description}
617 %
618 % \item[mathml structure elements]
619 % MathML structure elements can be used in PDF~2.0 directly.
620 % In PDF~1.7. one could theoretically
621 % use them if one declares a role mapping first,
622 % (this can be done with \verb+\tagpdfsetup{role/mathml-tags}+)
623 % which maps all to \texttt{Span}. But such a role mapping breaks the reading and
624 % with it the accessibility of the math, as only \texttt{Span} is passed to
625 % the screen readers and so it is not recommended.
626 %
627 % Automatic generation of structure elements is only possible with
628 % lualatex. It requires that the package \pkg{luamml} has been loaded.
629 % \begin{description}
630 % \item[enabled by default?] false
631 % \item[enable/disable] \verb+\tagpdfsetup{math/mathml/structelem}+
632 % (local setting, so can be used with grouping on individual equations).
633 % \item[consumer support] Needs more tests.
634 % \end{description}
635 %
636 % \end{description}
637 %
638 % \section{Debugging}
639 %
640 % \begin{function}{\DebugMathOn,\DebugMathOff, \math_debug_on:, \math_debug_off:}
641 %
642 % These commands enable/disable debugging messages.
643 %
644 % \end{function}
645 %
646 %
647 % \section{Known current bugs, etc.}
648 %
649 % \subsection{Capture/grabbing problems}
650 %
651 % \begin{enumerate}
652 % \item Incorrect grabbing of |$|-math when there is also
653 % explicit |$|-math within a \textit{text environment}
654 % that is itself within the math that should all be grabbed.
655 % For example,
656 % \begin{verbatim}
657 % $a\begin{minipage}{1cm}$b$\end{minipage}$
658 % \end{verbatim}
659 % would only result in the capture of the tokens
660 % ``\verb*/a\begin {minipage}{1cm}/''.
661 % This can be avoided by an additional brace group:
662 % \begin{verbatim}
663 % $a{\begin{minipage}{1cm}$b$\end{minipage}}$
664 % \end{verbatim}
665 %
666 % \item Similar incorrect grabbing with |$$| also.
667 %
668 % \item The grabbing, for all the display environments (and |\) \]|), needs
669 % to deal with nesting: \pkg{amsmath} contains code for this.
670 %
671 % \item The math can't contain verbatim and verbatim-like commands. This is
672 % nothing new for the \pkg{amsmath} environments but changes |$| and |\[\]|
673 % and |equation*| (see, e.g., tagging-project issue \#30).
674 %
675 % \item Begin and end of the math or math environment can not be hidden in commands.
676 % For example \verb+>{$}l<{$}+ in a tabular would lead to errors. Therefore
677 % in a tabular a slower token-by-token grabbing is used.
678 % \end{enumerate}
679 %
680 % \subsection{Fake math}
681 % For the current state see \ref{sec:nocapture} above. The text here is mainly
682 % kept for history.
683 %
684 % In a number of places in \LaTeX{} math commands (mainly |$|) is used
685 % only for technical reason, e.g., to access a math font, to setup a symbol
686 % or to use \cs{vcenter}.
687 %
688 % The code identifies such fake math mostly by making use of the \cs{m@th} command
689 % where two methods are used for the automatic detection:
690 %
691 % \begin{itemize}
692 % \item After grabbing math content the code checks if the content contains the token
693 % \cs{m@th} and if yes it doesn't call the processor before reinserting
694 % the content and perhaps adding tagging code.
695 % This method requires that the math can be grabbed (e.g. that the end dollar is visible)
696 % and that the \cs{m@th} is visible. It applies for example in \cs{@iiiparbox} where the
697 % code from |$\vcenter| to |\m@th$| is grabbed an put back. It does not work for
698 % example for |tabular| where the dollars and the \cs{m@th} token are spread around
699 % over three commands. |tabular| needs therefore manual intervention.
700 %
701 % A look in the list of usages (in \texttt{[email protected]}) justifies this approach.
702 % All usages are either not math at all, or related to small elements that probably
703 % shouldn't be grabbed and processed on their own.
704 %
705 % \item \cs{m@th} is redefined so that it sets the boolean \cs{l_@@_collected_bool}
706 % to true. If \cs{m@th} is used inside math that has been grabbed
707 % this doesn't change much as the boolean is set by the grabbing anyway. For usages
708 % outside math the benefit is not so clear: The setting avoids that in \cs{LaTeXe}
709 % the epsilon is processed as math, but it also prevents that the content of the amsmath
710 % command \cs{boxed} is processed as math.
711 % It means that if one wants to reenable math processing inside some (fake) math
712 % one has to do it after \cs{m@th} calls.
713 % \end{itemize}
714 %
715 % \subsubsection{Open problems}
716 %
717 % \begin{enumerate}
718 % \item The grabbing code doesn't pass the info that it detected a \cs{m@th} token.
719 % This means that the tagging code has to do the same check (and doesn't do this
720 % in all cases yet).
721 %
722 % \item Commands are missing to locally disable the grabbing and processing, e.g.,
723 % to handle |tabular|.
724 %
725 % \item It must be checked if setting the boolean in \cs{m@th} really makes sense
726 % or if commands like \cs{LaTeXe} should be handled manually.
727 %
728 % \end{enumerate}
729 %
730 % \subsection{Processor}
731 %
732 % The grabbed math is at first passed to the processor. The processor is not called
733 % in a measuring phase (from the amsmath \cs{ifmeasuring@}) and if the \cs{m@th}
734 % token is detected.
735 % It is not quite clear what purpose the processor has. As it is a public interface
736 % it can't be used for internal code. And typesetting happens later and the processor
737 % can't really change this. Currently it is mostly used for debugging and messages.
738 % If the \cs{m@th} is found the \cs{l_@@_fakemath_bool} is set, so if the code
739 % is changed this must be preserved.
740 %
741 % \subsection{Other problems}
742 %
743 % \begin{enumerate}
744 % \item
745 % The presence of \cs{m@th} in association with \cs{ensuremath}
746 % does not necessarily indicate fake math. This is because
747 % wanting mathsurround to be zero is very reasonable and common,
748 % \emph{even when the math is genuine} (and hence needs to be collected).
749 %
750 % TODO: this claim needs some examples.
751 %
752 % \item User-defined environments can create problems; but this area, of
753 % new, copied and changed environments, has not yet been developed.
754 %
755 % \car*{Joseph wrote, inter alia:\\
756 % My thinking [regarding] \cs{RegisterMathEnvironment}\\
757 % - (New) Math environments should not be created-then-patched, but only
758 % generated by a [(future)] dedicated command (\cs{DeclareMathEnvironment},
759 % presumably)\\
760 % - Math environments created with \pkg{ltcmd} [commands] should not be copied, . . .\\
761 % - Package authors should be able to manually set up math environments with a public boolean.}
762 % \end{enumerate}
763 %
764 %
765 % \subsection{Other ToDos}
766 %
767 % \begin{enumerate}
768 % \item Add (some of) the math display commands that were \enquote{lifted from
769 % plain}, e.g., \cs{displaylines} \cs{eqalign}(??).
770 % \item The breqn packages changes catcodes and that isn't yet covered
771 % by our mechanism.
772 % \item \cs{intertext} is not correctly taken into account by the
773 % code splitting multiline math into subformulas.
774 % \end{enumerate}
775 %
776 %
777 % \car*{\cs{MaybeStop} (temporarily) not executed, as it is unknown on Chris' system.}
778 % \iffalse
779 % \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex }
780 % \else
781 % \StopEventually{\setlength\IndexMin{200pt} \PrintIndex }
782 % \fi
783 %
784 % \section{The Implementation}
785 %
786 % \begin{macrocode}
787 %<*kernel>
788 % \end{macrocode}
789 %
790 % \subsection{File declaration}
791 %
792 % \begin{macrocode}
793 \ProvidesFile{latex-lab-math.ltx}
794 [\ltlabmathdate\space
795 v\ltlabmathversion\space
796 Grab all the math(s) and tag it]
797 % \end{macrocode}
798 %
799 %
800 % \begin{macrocode}
801 %<@@=math>
802 % \end{macrocode}
803 %
804 %
805 % \begin{macrocode}
806 \ExplSyntaxOn
807 % \end{macrocode}
808 %
809 % \subsection{Setup}
810 %
811 % Loading \pkg{amsmath} is an absolute requirement: this avoids needing to
812 % have conditional definitions and deals with how to define \cs{[}/\cs{]}
813 % neatly. The package is loaded before \pkg{lua-unicode-math} or
814 % at begin document to allow user to load it with options.
815 % \begin{macrocode}
816 \AddToHook{package/lua-unicode-math/before}{\RequirePackage { amsmath }}
817 \AddToHook{begindocument/before}{ \RequirePackage { amsmath } }
818 % \end{macrocode}
819 %
820 % \subsection{Debugging}
821 %
822 %
823 % \begin{variable}{\g_@@_debug_bool}
824 %
825 % \begin{macrocode}
826 \bool_new:N \g_@@_debug_bool
827 % \end{macrocode}
828 % \end{variable}
829 %
830 %
831 % \begin{macro}{\@@_debug:n,\@@_debug_typeout:n}
832 %
833 % \begin{macrocode}
834 \cs_new_eq:NN \@@_debug:n \use_none:n
835 \cs_new_eq:NN \@@_debug_typeout:n \use_none:n
836 % \end{macrocode}
837 % \end{macro}
838 %
839 % \begin{macro}{\math_debug_on:,\math_debug_off:,
840 % \@@_debug_gset:}
841 % \begin{macrocode}
842 \cs_new_protected:Npn \math_debug_on:
843 {
844 \bool_gset_true:N \g_@@_debug_bool
845 \@@_debug_gset:
846 }
847 % \end{macrocode}
848 %
849 % \begin{macrocode}
850 \cs_new_protected:Npn \math_debug_off:
851 {
852 \bool_gset_false:N \g_@@_debug_bool
853 \@@_debug_gset:
854 }
855 % \end{macrocode}
856 %
857 % \begin{macrocode}
858 \cs_new_protected:Npn \@@_debug_gset:
859 {
860 \cs_gset_protected:Npe \@@_debug:n ##1
861 { \bool_if:NT \g_@@_debug_bool {##1} }
862 \cs_gset_protected:Npe \@@_debug_typeout:n ##1
863 { \bool_if:NT \g_@@_debug_bool { \typeout{[Math]~ ==>~ ##1} } }
864 }
865 % \end{macrocode}
866 % \end{macro}
867 %
868 %
869 % \begin{macro}{\DebugMathOn,\DebugMathOff}
870 % If we are debugging blocks we also want to know about template
871 % instances, so we turn the debugging for templates as well (for now).
872 % \begin{macrocode}
873 \cs_new_protected:Npn \DebugMathOn { \math_debug_on: }
874 \cs_new_protected:Npn \DebugMathOff { \math_debug_off: }
875 % \end{macrocode}
876 %
877 % \begin{macrocode}
878 \DebugMathOff
879 % \end{macrocode}
880 % \end{macro}
881 %
882 % \subsection{Data structures}
883 %
884 % \begin{variable}{\l_@@_collected_bool}
885 % Tracks whether math mode material has been collected, which happens inside
886 % \pkg{amsmath} environments as well as those handled directly here.
887 % If true following math will not grab and/or process.
888 % See \ref{sec:mathcapture} for details.
889 % \begin{macrocode}
890 \bool_new:N \l_@@_collected_bool
891 % \end{macrocode}
892 % \end{variable}
893 %
894 %
895 % \begin{variable}{\l_@@_fakemath_bool}
896 % Tracks whether math mode material has been identified as fake math during
897 % the grabbing phase, which happens currently if the
898 % grabbed contents contains the \cs{m@th} token.
899 %
900 % \begin{macrocode}
901 \bool_new:N \l_@@_fakemath_bool
902 % \end{macrocode}
903 % \end{variable}
904 %
905 %
906 % \car{Change first tl name below: `env' $=>$ `info'?\\
907 % Or do we need an extra storage tl?}
908 %
909 % \begin{variable}{\g_@@_grabbed_env_tl, \g_@@_grabbed_math_tl}
910 % \cs{g_@@_grabbed_env_tl} contains the name of the math environment
911 % (\texttt{math} in the case of inline math,
912 % \cs{g_@@_grabbed_math_tl} the math content.
913 % \begin{macrocode}
914 \tl_new:N \g_@@_grabbed_env_tl
915 \tl_new:N \g_@@_grabbed_math_tl
916 % \end{macrocode}
917 % \end{variable}
918 %
919 % \begin{variable}{\l_@@_tmpa_tl,\l_@@_tmpa_skip,\l_@@_tmpa_str}
920 % Temporary variables
921 % \begin{macrocode}
922 \tl_new:N \l_@@_tmpa_tl
923 \skip_new:N \l_@@_tmpa_skip
924 \str_new:N \l_@@_tmpa_str
925 % \end{macrocode}
926 % \end{variable}
927 %
928 % \begin{variable}{\l_@@_content_alt_tl,
929 % \l_@@_content_actual_tl,
930 % \l_@@_content_AF_tl}
931 % Temporary variables to hold math content that should
932 % be used in actual or alt text and stored as AF.
933 % \begin{macrocode}
934 \tl_new:N \l_@@_content_alt_tl
935 \tl_new:N \l_@@_content_actual_tl
936 \tl_new:N \l_@@_content_AF_source_tl
937 \tl_new:N \l_@@_content_AF_source_tmpa_tl
938 \tl_new:N \l_@@_content_AF_mathml_tl
939 % \end{macrocode}
940 % \end{variable}
941 %
942 % \subsection{Math attributes}
943 % The attributes \texttt{inline} and \texttt{display} are declared in latex-lab-namespace.
944 % The variable will hold the attribute class.
945 % \changes{0.7b}{2026-04-27}{Moved attributes to latex-lab-namespace.}
946 % \begin{variable}{\l_@@_attribute_class_tl}
947 % \begin{macrocode}
948 \tl_new:N\l_@@_attribute_class_tl
949 % \end{macrocode}
950 % \end{variable}
951 %
952 % \subsection{Code related to AF}
953 % Booleans to handle the options.
954 % \begin{variable}{
955 % \l__tag_math_texsource_AF_bool,
956 % \l__tag_math_texsource_pane_bool,
957 % \l__tag_math_mathml_AF_bool,
958 % \g__tag_math_mathml_AF_bool,
959 % \l__tag_math_mathml_pane_bool,
960 % \l__tag_math_alt_bool,
961 % \g__tag_math_luamml_tl,
962 % \l__tag_math_MSFT_bool
963 % }
964 % The variable \cs{g__tag_math_luamml_tl} is initially 0 and
965 % the user key can set it to -1 or 1. This allows to distinguish
966 % the unset case from a value set by the user.
967 % \begin{macrocode}
968 \bool_new:N\l__tag_math_texsource_AF_bool
969 \bool_new:N\l__tag_math_texsource_pane_bool
970 \bool_new:N\l__tag_math_mathml_AF_bool
971 \bool_new:N\g__tag_math_mathml_AF_bool
972 \bool_new:N\l__tag_math_mathml_pane_bool
973 \bool_new:N\l__tag_math_alt_bool
974 \bool_new:N\l__tag_math_MSFT_bool
975 \tl_new:N\g__tag_math_luamml_tl
976 \tl_gset:Nn\g__tag_math_luamml_tl {0}
977 % \end{macrocode}
978 % \end{variable}
979 %
980 % \begin{variable}{
981 % \g_@@_mathml_total_int,
982 % \g_@@_mathml_int,
983 % \g_@@_math_total_int,
984 % \g_@@_mathml_AF_found_int,
985 % \g_@@_mathml_AF_attached_int,
986 % }
987 % \cs{g_@@_mml_total_int} records the MathML fragments read in.
988 % \cs{g_@@_mml_int} records the MathML fragments read in with a different hash.
989 % \cs{g_@@_AF_total_int} records the number of math structures that try to
990 % attach a mathml AF.
991 % \cs{g_@@_AF_found_int} records the number of math structures for which a fitting
992 % mathml is found.
993 % \cs{g_@@_AF_attached_int} records the number of math structures which got a MathML fragment
994 % (if mathml-AF are not disabled locally this should be the equal to the previous number.
995 %
996 % \begin{macrocode}
997 \int_new:N\g_@@_mathml_total_int
998 \int_new:N\g_@@_mathml_int
999 \int_new:N\g_@@_math_total_int
1000 \int_new:N\g_@@_mathml_AF_found_int
1001 \int_new:N\g_@@_mathml_AF_attached_int
1002 % \end{macrocode}
1003 % \end{variable}
1004 %
1005 % \begin{variable}{\l__tag_math_mathml_files_clist}
1006 % A sequence to store the file list for the MathML.
1007 % We also check the luamml file.
1008 % \begin{macrocode}
1009 \clist_new:N\l__tag_math_mathml_files_clist
1010 \clist_put_right:Ne\l__tag_math_mathml_files_clist
1011 {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
1012 % \end{macrocode}
1013 % \end{variable}
1014 %
1015 % This is the internal variant of the \cs{mml} command.
1016 % \begin{macro}{\@@_AF_mml:nnnn}
1017 % \begin{macrocode}
1018 \cs_new_protected:Npn \@@_AF_mml:nnnn #1 #2 #3 #4
1019 %#1 number, #2 tex source for debugging, #3 hash, #4 mathml
1020 {
1021 \int_gincr:N \g_@@_mathml_total_int
1022 % \end{macrocode}
1023 % MathML with the same hash should be included only once:
1024 % \begin{macrocode}
1025 \tl_if_exist:cF { g_@@_mathml_#3_tl }
1026 {
1027 \int_gincr:N \g_@@_mathml_int
1028 % \end{macrocode}
1029 % a simple Desc key, take care that it is a valid string!
1030 % \begin{macrocode}
1031 \pdfdict_put:nne {l_pdffile/Filespec} {Desc}{(mathml-#1)}
1032 \pdffile_embed_stream:nnN {#4}{mathml-#1.xml}\l_@@_tmpa_tl
1033 % \end{macrocode}
1034 % not strictly necessary but makes the files visible in the file attachment
1035 % page
1036 % \begin{macrocode}
1037 \bool_if:NT \l__tag_math_mathml_pane_bool
1038 {\pdfmanagement_add:nne {Catalog/Names}{EmbeddedFiles}{\l_@@_tmpa_tl}}
1039 \tl_new:c{g_@@_mathml_#3_tl}
1040 \tl_gset_eq:cN{g_@@_mathml_#3_tl}\l_@@_tmpa_tl
1041 }
1042 }
1043 % \end{macrocode}
1044 % \end{macro}
1045 %
1046 % This is a version of the previous command which writes MSFT-attributes
1047 % \changes{0.6x}{2025-11-22}{Add support for the MSFT attribute}
1048 % \begin{macro}{\@@_MSFT_mml:nn,\@@_MSFT_mml_aux:nn}
1049 % \begin{macrocode}
1050 \cs_new_eq:NN \@@_MSFT_mml:nn \use_none:nn
1051 \cs_new_protected:Npn \@@_MSFT_mml_aux:nn #1 #2
1052 %#1 hash, #2 mathml
1053 {
1054 % \end{macrocode}
1055 % mathml with the same hash should be included only once:
1056 % \begin{macrocode}
1057 \tl_if_exist:cF { g_@@_mathml_MSFT_#1_tl }
1058 {
1059 \pdf_string_from_unicode:nnN{utf16/hex}{#2}\l_@@_tmpa_tl
1060 \__tag_attr_new_entry:ee
1061 {MSFT-#1}
1062 {/O /MSFT_Office /MSFT_MathML \l_@@_tmpa_tl }
1063 \tl_new:c {g__math_mathml_MSFT_#1_tl}
1064 \tl_gset:cn {g__math_mathml_MSFT_#1_tl} {MSFT-#1}
1065 }
1066 }
1067 % \end{macrocode}
1068 % \end{macro}
1069 %
1070 % The html reader.
1071 % \changes{0.6o}{2025-05-01}{set catcode of hat to other too, tagging issue 836}
1072 % \begin{macrocode}
1073 \cs_new_protected:Npn \@@_AF_html_reader:w#1</h2>#2<p>#3</p>#4<p>#5</p>#6<math{
1074 \begingroup
1075 \char_set_catcode_other:N\{
1076 \char_set_catcode_other:N\}
1077 \char_set_catcode_other:N\#
1078 \char_set_catcode_other:N\%
1079 \char_set_catcode_other:N\^
1080 \@@_AF_html_reader_verb:w{#1}{#3}{#5}<math
1081 }
1082 % \end{macrocode}
1083 % \begin{macrocode}
1084 \cs_new_protected:Npn\@@_AF_html_reader_verb:w#1#2#3#4~</div>{
1085 \endgroup
1086 \@@_AF_mml:nnnn{#1}{#2}{#3}{#4}
1087 \@@_MSFT_mml:nn {#3}{#4}
1088 }
1089 % \end{macrocode}
1090 %
1091 % As with luatex we write two files we define a few constants for
1092 % the shared texts.
1093 %
1094 % \begin{macro}
1095 % {\c_@@_mathml_write_init_tl,\l_@@_mathml_write_before_tl,
1096 % \c_@@_mathml_write_after_tl,\c_@@_mathml_write_final_tl}
1097 % \begin{macrocode}
1098 \tl_const:Nn \c_@@_mathml_write_init_tl
1099 {
1100 <!DOCTYPE~html>
1101 \iow_newline:
1102 <html~ xmlns="http://www.w3.org/1999/xhtml">
1103 \iow_newline:
1104 }
1105 \tl_new:N \l_@@_mathml_write_before_tl
1106 \tl_const:Nn \c_@@_mathml_write_after_tl
1107 {
1108 \iow_newline:
1109 </div>
1110 \iow_newline:
1111 }
1112 \tl_const:Nn \c_@@_mathml_write_final_tl
1113 {
1114 </html>
1115 }
1116 % \end{macrocode}
1117 % \end{macro}
1118
1119 % \begin{taggingsocketdecl}{math/mathml/write/prepare}
1120 % To prepare the hash and the starting command we use a socket, so
1121 % that both the dummy and luamml can make use of it.
1122 % \begin{macrocode}
1123 \NewTaggingSocket{math/mathml/write/prepare}{0}
1124 % \end{macrocode}
1125 % \end{taggingsocketdecl}
1126 %
1127 % \begin{plugdecl}{On}
1128 % \begin{macrocode}
1129 \NewTaggingSocketPlug{math/mathml/write/prepare}{On}
1130 {
1131 \str_set:NV\l_@@_tmpa_str\l_@@_content_AF_source_tl
1132 \str_replace_all:Nnn\l_@@_tmpa_str{&}{&amp;}
1133 \str_replace_all:Nnn\l_@@_tmpa_str{<}{&lt;}
1134 \tl_set:Nn \l_@@_mathml_write_before_tl
1135 {
1136 <div>
1137 \iow_newline:
1138 <h2>\c_backslash_str mml\c_space_tl \int_use:N \g_@@_math_total_int </h2>
1139 \iow_newline:
1140 <p>\l_@@_tmpa_str</p>
1141 \iow_newline:
1142 <p>\l_@@_content_hash_tl </p>
1143 \iow_newline:
1144 }
1145 }
1146 % \end{macrocode}
1147 % \end{plugdecl}
1148 %
1149 % With luatex we automatically generate MathML with \pkg{luamml} if the package
1150 % can be loaded and \pkg{unicode-math} or \pkg{lua-unicode-math} are detected.
1151 % We start the process in the begindocument/end hook
1152 % so that the reading from a previous compilation can happen before!
1153 %
1154 % For other engines, for future name changes
1155 % and in case luamml is not loaded we provide
1156 % some commands
1157 % \begin{macrocode}
1158 \cs_new_protected:Npn\@@_provide_luamml_commands:
1159 {
1160 \providecommand\luamml_flag_structelem:{}
1161 \cs_if_free:NT \luamml_structelem:
1162 {
1163 \cs_set_eq:NN\luamml_structelem:\luamml_flag_structelem:
1164 }
1165 \providecommand\luamml_flag_process:{}
1166 \cs_if_free:NT \luamml_process:
1167 {
1168 \cs_set_eq:NN\luamml_process:\luamml_flag_process:
1169 }
1170 \providecommand\luamml_flag_ignore:{}
1171 \cs_if_free:NT \luamml_ignore:
1172 {
1173 \cs_set_eq:NN\luamml_ignore:\luamml_flag_ignore:
1174 }
1175 }
1176 % \end{macrocode}
1177 % \changes{v0.6y}{2025/12/16}{Support lua-unicode-math}
1178 % \begin{macrocode}
1179 \sys_if_engine_luatex:TF
1180 {
1181 \AddToHook{begindocument/before}
1182 {
1183 \str_case:on \g_@@_luamml_load_tl
1184 {
1185 { 1 } {
1186 \RequirePackage { luamml }
1187 \AddToHook{begindocument/end}
1188 {
1189 \@@_luamml_activate_write:
1190 }
1191 }
1192 {-1 } {
1193 \AddToHook{begindocument/end}
1194 {
1195 \msg_note:nnnn { tag }
1196 { luamml-status }{ disabled }{ not~create }
1197 }
1198 }
1199 { 0 }
1200 {
1201 \bool_lazy_or:nnTF
1202 {\cs_if_exist_p:c{[email protected]}}
1203 {\cs_if_exist_p:c{[email protected]}}
1204 {
1205 \RequirePackage { luamml }
1206 \AddToHook{begindocument/end}
1207 {
1208 \@@_luamml_activate_write:
1209 }
1210 }
1211 { \msg_warning:nn { tag }{ unicode-math-missing } }
1212 }
1213 }
1214 \@@_provide_luamml_commands:
1215 }
1216 }
1217 {
1218 \AddToHook{begindocument/before}
1219 {
1220 \@@_provide_luamml_commands:
1221 }
1222 }
1223 \msg_new:nnn { tag }{ luamml-status }
1224 {
1225 luamml~has~been~#1~and~will~#2~a~MathML~file.
1226 }
1227
1228 \msg_new:nnn { tag }{ unicode-math-missing }
1229 {
1230 Neither~unicode-math~nor~lua-unicode-math~has~been~loaded.\\
1231 luamml~will~not~create~a~MathML~file.\\
1232 To~avoid~this~warning~load~unicode-math~\\
1233 or~lua-unicode-math~or~disable~luamml~with~\\
1234 \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=false}}\\
1235 or~force~luamml~with~\\
1236 \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=true}}
1237 }
1238 \cs_new_protected:Npn \@@_luamml_activate_write:
1239 {
1240 \bool_if:NT \g_@@_luamml_write_bool
1241 {
1242 % \end{macrocode}
1243 % to avoid that nothing is written in the first run, we must activate the sockets:
1244 % \begin{macrocode}
1245 \bool_gset_true:N\g__tag_math_mathml_AF_bool
1246 \AssignTaggingSocketPlug{math/struct/begin}{mathml-AF}
1247 \AssignTaggingSocketPlug{math/struct/end}{mathml-AF}
1248 \int_set:Nn \l__luamml_pretty_int { 7 }
1249 \RegisterFamilyMapping\symsymbols{oms}
1250 \RegisterFamilyMapping\symletters{oml}
1251 \AssignTaggingSocketPlug{math/mathml/write/prepare}{On}
1252 \iow_new:N \g_@@_luamml_iow
1253 \iow_open:Nn \g_@@_luamml_iow {\c_sys_jobname_str-luamml-mathml.html}
1254 \iow_now:Ne \g_@@_luamml_iow { \c_@@_mathml_write_init_tl }
1255 \cs_new:Npn \@@_luamml_output_hook:n ##1
1256 {
1257 \tl_if_empty:NF \l_@@_mathml_write_before_tl
1258 {
1259 % \end{macrocode}
1260 % We check here if the current group level is equal to the one stored for the
1261 % outer math. We only write output if that is the case.
1262 %
1263 % Currently in \LaTeX{}, the \cs{math@level} is increased for every
1264 % nested math mode (via \cs{frozen@everymath}. However, to make the
1265 % code below work correctly we undo that in the case of
1266 % \enquote{fake math}, i.e., in math mode that
1267 % is only entered to make use of super or subscript positioning of
1268 % text or for \cs{vcenter}ing text, i.e., if it is not really a
1269 % \enquote{math formula}. We therefore provide a special declaration,
1270 % \cs{UseMathForPositioningText}, to indicate that the directly following
1271 % \texttt{\$} represents such \enquote{fake math}.
1272 %
1273 % \changes{0.6k}{2024-12-04}{Test for current group level}
1274 % \changes{0.6q}{2025-06-29}{Suppress output for inner mathematics}
1275 % \begin{macrocode}
1276 \int_compare:nNnT
1277 { \@math@level } = { 1 }
1278 {
1279 \iow_now:Ne \g_@@_luamml_iow
1280 {
1281 \l_@@_mathml_write_before_tl
1282 ##1
1283 \c_@@_mathml_write_after_tl
1284 }
1285 }
1286 }
1287 }
1288 \__luamml_register_output_hook:N \@@_luamml_output_hook:n
1289 % \end{macrocode}
1290 % At the end of the document we must finish and close the file:
1291 % \begin{macrocode}
1292 \AddToHook{enddocument/afterlastpage}
1293 {
1294 \iow_now:Ne \g_@@_luamml_iow
1295 { \c_@@_mathml_write_final_tl }
1296 \iow_close:N \g_@@_luamml_iow
1297 }
1298 \msg_note:nnnn { tag }
1299 { luamml-status }{ enabled }{ create }
1300 }
1301 }
1302 % \end{macrocode}
1303 %
1304 %
1305 %
1306 %
1307 % \begin{macro}{\UseMathForPositioningText}
1308 %
1309 % \changes{0.6u}{2025-09-10}{Macro added}
1310 %
1311 % The \cs{UseMathForPositioningText} command indicates that a
1312 % directly following \texttt{\$} is not a real math formula, but
1313 % that the math mode is only entered to position ordinary text with
1314 % the help of built in \TeX{} algorithms normally used for math,
1315 % e.g., \cs{vcenter}, super and subscripts.
1316 %
1317 % Signalling that special usage is necessary in the case tagged PDF
1318 % is produced, because then such math mode should not generate a
1319 % \enquote{formula} structure.
1320 %
1321 % The command requires that it is immediately followed by
1322 % \texttt{\$}; anything else will result in a low-level \TeX{}
1323 % error. As it is not a user but a developer command, this seems
1324 % acceptable for the sake of fast runtime execution.
1325 %
1326 % The way it is implemented it can be used in legacy code in front
1327 % of any \texttt{\$} that switches to math mode for non-math
1328 % purposes. If tagging is active it will ensure that this
1329 % particular math mode content is not treated as math with respect
1330 % to tagging (though nested math still is). If tagging is inactive
1331 % the command is simply ignored.
1332 %
1333 % Starting with \LaTeX~2026-06-01 it is not longer used by the kernel as
1334 % math is not longer used in tabular and super and subscripts.
1335 %
1336 % \begin{macrocode}
1337 \cs_set_protected:Npn \UseMathForPositioningText $ {
1338 % \end{macrocode}
1339 % Before the math mode is started we ensure that its content is not
1340 % collected by the grabber.
1341 % \begin{macrocode}
1342 \bool_if:NTF \l__math_collected_bool
1343 { $ }
1344 {
1345 \bool_set_true:N \l__math_collected_bool
1346 $
1347 % \end{macrocode}
1348 % Once inside math mode we reenable grabbing, so that any nested
1349 % math (within text) is grabbed.
1350 % \begin{macrocode}
1351 \bool_set_false:N \l__math_collected_bool
1352 % \end{macrocode}
1353 % In addition we decrement the math level (which was automatically
1354 % incremented when entering math mode) so that this math mode is
1355 % transparent in \cs{@@_luamml_activate_write:}.
1356 % \begin{macrocode}
1357 \int_decr:N \@math@level
1358 }
1359 }
1360 % \end{macrocode}
1361 % \end{macro}
1362 %
1363 %
1364 % And now keys to activate/deactivate luamml feature
1365 % \begin{variable}{\g_@@_luamml_load_tl}
1366 % This variable will be used to suppress the loading of luamml
1367 % altogether.
1368 % \begin{macrocode}
1369 \tl_new:N \g_@@_luamml_load_tl
1370 \tl_gset:Nn \g_@@_luamml_load_tl {0}
1371 % \end{macrocode}
1372 % \end{variable}
1373 % \begin{variable}{\g_@@_luamml_write_bool}
1374 % This variable decides if luamml writes a mathml
1375 % altogether.
1376 % \begin{macrocode}
1377 \bool_new:N \g_@@_luamml_write_bool
1378 \bool_gset_true:N \g_@@_luamml_write_bool
1379 % \end{macrocode}
1380 % \end{variable}
1381 % \begin{macro}{\@@_luamml_ignore:,\@@_luamml_structelem:}
1382 % Internal variants of the luamml commands, that can be remapped if needed.
1383 % \begin{macrocode}
1384 \cs_new:Npn\@@_luamml_structelem:{}
1385 \cs_new:Npn\@@_luamml_ignore:{}
1386 % \end{macrocode}
1387 % \end{macro}
1388 %
1389 % \begin{macrocode}
1390 \msg_new:nnn { tag }{ PDF-2.0-recommended }
1391 {
1392 The~key~#1~will~not~work~properly~with~PDF~#2.\\
1393 Switching~to~PDF~2.0~is~recommended.
1394 }
1395 \keys_define:nn { __tag / setup }
1396 {
1397 % \end{macrocode}
1398 % At first a key to suppress the loading altogether
1399 % \begin{macrocode}
1400 math/mathml/luamml/load .choice: ,
1401 math/mathml/luamml/load/true .code:n = {\tl_gset:Nn \g_@@_luamml_load_tl{1}},
1402 math/mathml/luamml/load/false .code:n = {\tl_gset:Nn \g_@@_luamml_load_tl{-1}},
1403 math/mathml/luamml/load .default:n = true,
1404 math/mathml/luamml/load .usage:n=preamble,
1405 % \end{macrocode}
1406 % A key to activate math structure elements.
1407 % \changes{v0.6j}{2024-11-19}{no longer enable globally for better fake math handling, issue \#764}
1408 % \begin{macrocode}
1409 math/mathml/structelem .choice:,
1410 math/mathml/structelem/true .code:n =
1411 {
1412 \pdf_version_compare:NnT < {2.0}
1413 {
1414 \msg_warning:nnne { tag }{ PDF-2.0-recommended }
1415 { math/mathml/structelem }{ \pdf_version: }
1416 }
1417 \cs_set:Npn\@@_luamml_structelem:{\luamml_structelem:}
1418 \cs_set:Npn\@@_luamml_ignore:{\luamml_ignore:}
1419 },
1420 math/mathml/structelem/false .code:n =
1421 {
1422 \cs_set_eq:NN\@@_luamml_structelem:\prg_do_nothing:
1423 \cs_set_eq:NN\@@_luamml_ignore:\prg_do_nothing:
1424 },
1425 math/mathml/structelem .default:n = true,
1426 % \end{macrocode}
1427 % and a key to call the ignore flag. This should only be used locally.
1428 % \begin{macrocode}
1429 math/mathml/ignore .code:n = {\luamml_ignore:},
1430 % \end{macrocode}
1431 % \begin{macrocode}
1432 math/mathml/luamml/write .choice:,
1433 math/mathml/luamml/write/true .code:n =
1434 {
1435 \tl_gset:Nn \g_@@_luamml_load_tl{1}
1436 \bool_gset_true:N \g_@@_luamml_write_bool
1437 },
1438 math/mathml/luamml/write/false .code:n =
1439 {
1440 \bool_gset_false:N \g_@@_luamml_write_bool
1441 },
1442 math/mathml/luamml/write .default:n = true,
1443 math/mathml/luamml/write .usage:n=preamble,
1444 % \end{macrocode}
1445 % alias keys for compatibility
1446 % \begin{macrocode}
1447 math/mathml/luamml .bool_gset:N = \g_@@_luamml_write_bool,
1448 math/mathml/luamml .usage:n=preamble
1449 }
1450 % \end{macrocode}
1451 % \begin{taggingsocketdecl}{math/mathml/write}
1452 % This writes a html-dummy with the hash and the math content.
1453 % This should be optional, so it uses a socket that can be disabled
1454 %
1455 % \begin{macrocode}
1456 \NewTaggingSocket{math/mathml/write}{0}
1457 % \end{macrocode}
1458 % \end{taggingsocketdecl}
1459 %
1460 % \begin{plugdecl}{On}
1461 % \begin{macrocode}
1462 \NewTaggingSocketPlug{math/mathml/write}{On}
1463 {
1464 \iow_now:Ne \g_@@_writedummy_iow
1465 {
1466 \l_@@_mathml_write_before_tl
1467 <math~ xmlns="http://www.w3.org/1998/Math/MathML"></math>
1468 \c_@@_mathml_write_after_tl
1469 }
1470 }
1471 % \end{macrocode}
1472 % \end{plugdecl}
1473 % And now a key to activate the socket.
1474 % \begin{macrocode}
1475
1476 \keys_define:nn { __tag / setup }
1477 {
1478 math/mathml/write-dummy .code:n =
1479 {
1480 \bool_gset_true:N \g__tag_math_mathml_AF_bool
1481 \tl_if_exist:NF\g_@@_writedummy_iow
1482 {
1483 \iow_new:N \g_@@_writedummy_iow
1484 \iow_open:Nn \g_@@_writedummy_iow
1485 {
1486 \c_sys_jobname_str-mathml-dummy.html
1487 }
1488 \iow_now:Ne \g_@@_writedummy_iow
1489 {
1490 \c_@@_mathml_write_init_tl
1491 }
1492 \AssignTaggingSocketPlug{math/mathml/write/prepare}{On}
1493 \AssignTaggingSocketPlug{math/mathml/write}{On}
1494 \AddToHook{enddocument/afterlastpage}
1495 {
1496 \iow_now:Ne \g_@@_writedummy_iow
1497 { \c_@@_mathml_write_final_tl }
1498 \iow_close:N \g_@@_writedummy_iow
1499 }
1500 }
1501 },
1502 math/mathml/write-dummy .usage:n=preamble
1503 }
1504 % \end{macrocode}
1505 %
1506 % \begin{macro}{\@@_AF_process_mathml_files:}
1507 % \changes{0.6s}{2025-07-30}{Clear the box to avoid glyph node leakage, tagging issue 963}
1508 % \begin{macrocode}
1509 \box_new:N\l_@@_tmpa_box
1510 \cs_new_protected:Npn \@@_AF_process_mathml_files:
1511 {
1512 \hbox_set:Nn \l_@@_tmpa_box
1513 {
1514 \pdfdict_put:nnn { l_pdffile/Filespec }{AFRelationship} { /Supplement }
1515 \pdfdict_put:nne
1516 { l_pdffile }{Subtype}
1517 { \pdf_name_from_unicode_e:n{application/mathml+xml} }
1518 \char_set_catcode_other:N \#
1519 \cs_set_eq:NN\mml \@@_AF_html_reader:w
1520 \clist_map_inline:Nn \l__tag_math_mathml_files_clist
1521 {
1522 \file_if_exist:nTF {##1.html}
1523 {
1524 \typeout{Info:~reading~mathml~file~##1}
1525 \file_input:n {##1.html}
1526 \bool_gset_true:N\g__tag_math_mathml_AF_bool
1527 }
1528 {
1529 \typeout{Info:~mathml~file~##1~does~not~exist}%info message
1530 }
1531 }
1532 }
1533 \box_clear:N \l_@@_tmpa_box
1534 \bool_if:NT\g__tag_math_mathml_AF_bool
1535 {
1536 \typeout{Info:~Activating~mathml~support}
1537 \AssignTaggingSocketPlug{math/struct/begin}{mathml-AF}
1538 \AssignTaggingSocketPlug{math/struct/end}{mathml-AF}
1539 \AddToHook{enddocument/info}
1540 {
1541 \iow_term:n{MathML~statistic}
1542 \iow_term:n{================}
1543 \iow_term:e{==>~\int_use:N\g_@@_mathml_total_int\c_space_tl
1544 MathML~fragments~read}
1545 \iow_term:e{==>~\int_use:N\g_@@_mathml_int\c_space_tl
1546 different~MathML~fragments}
1547 \iow_term:e{==>~\int_use:N\g_@@_math_total_int\c_space_tl
1548 math~fragments~found}
1549 \iow_term:e{==>~\int_use:N\g_@@_mathml_AF_found_int\c_space_tl
1550 fitting~MathML~AF~found}
1551 \iow_term:e{==>~\int_use:N\g_@@_mathml_AF_attached_int\c_space_tl
1552 MathML~AF~attached}
1553 }
1554 }
1555 }
1556 \AddToHook{begindocument}{\@@_AF_process_mathml_files:}
1557 % \end{macrocode}
1558 % \end{macro}
1559 %
1560 % \subsection{Mathstyle detection}
1561 % In some cases we need to detect the mathstyle used in a \cs{mathchoice}
1562 % command and to disable/enable tagging in the unused branches.
1563 % This is currently only used in the amstext command \cs{text}
1564 % but is perhaps also needed in other cases, so we create a general command.
1565 %
1566 %\begin{macro}[no-user-doc]{\l_@@_mathstyle_int,\g_@@_mathchoice_int,mathstyle}
1567 % \begin{macrocode}
1568 \int_new:N \l_@@_mathstyle_int
1569 \int_new:N \g_@@_mathchoice_int
1570 \property_new:nnnn{mathstyle}{now}{-1}{\int_use:N \l_@@_mathstyle_int }
1571 % \end{macrocode}
1572 %\end{macro}
1573 % For now internal, but perhaps will need a public version.
1574 % The command should be used in every branch of a \cs{mathchoice}
1575 % (with the correct mathstyle number) and with an unique label (which should
1576 % be the same in every branch).
1577 % \cs{g_@@_mathchoice_int} can be, e.g., increased before the mathchoice and
1578 % then used.
1579 % \begin{macro}{\@@_tag_if_mathstyle:nn}
1580 % \begin{macrocode}
1581 \cs_new_protected:Npn \@@_tag_if_mathstyle:nn #1 #2
1582 %#1 refers to label
1583 %#2 is a number for the mathstyle (typically 0,2,4,6)
1584 {
1585 \int_set:Nn \l_@@_mathstyle_int {#2}
1586 \property_record:nn {#1} { mathstyle }
1587 \int_compare:nNnTF { \property_ref:nn {#1}{ mathstyle} } = { #2 }
1588 { \tag_resume:n{\mathchoice} }{ \tag_suspend:n{\mathchoice} }
1589 }
1590 \cs_generate_variant:Nn \@@_tag_if_mathstyle:nn {en}
1591 % \end{macrocode}
1592 % \end{macro}
1593 %
1594 % \subsection{Tagging options}
1595 % \begin{macrocode}
1596 \keys_define:nn { __tag / setup }
1597 {
1598 math/mathml/sources .clist_set:N = \l__tag_math_mathml_files_clist,
1599 math/alt/use .bool_set:N = \l__tag_math_alt_bool,
1600 math/MS/use .bool_set:N = \l__tag_math_MSFT_bool,
1601 viewer/pane/mathml .bool_set:N = \l__tag_math_mathml_pane_bool,
1602 viewer/pane/mathml .initial:n = true,
1603 viewer/pane/mathsource .bool_set:N = \l__tag_math_texsource_pane_bool,
1604 math/mathml/AF .bool_set:N = \l__tag_math_mathml_AF_bool,
1605 math/mathml/AF .initial:n = true,
1606 math/tex/AF .bool_set:N = \l__tag_math_texsource_AF_bool,
1607 math/tex/AF .initial:n = true
1608 }
1609 % \end{macrocode}
1610 % alt is required for pdf/UA-1.
1611 % TODO: l3pdfmeta should support this test.
1612 % \begin{macrocode}
1613 \AddToHook{begindocument/end}
1614 {
1615 \str_if_eq:eeT
1616 {1}
1617 {
1618 \exp_last_unbraced:Ne\use_i:nn
1619 {\GetDocumentProperties{document/pdfstandard-UA}}
1620 \c_empty_tl\c_empty_tl
1621 }
1622 {
1623 \bool_if:NF \l__tag_math_alt_bool
1624 {
1625 \typeout{PDF/UA-1~detected.~Enabling~alt~text~on~Formula}
1626 }
1627 \bool_set_true:N\l__tag_math_alt_bool
1628 }
1629 }
1630 % \end{macrocode}
1631 %
1632 % \subsubsection{Meta keys}
1633 % The |math/setup| key accepts a list with the values |mathml-SE|, |mathml-AF| and |tex-AF|.
1634 % It is a fast way to set the main option. It at first disables them all, to get a clean state.
1635 % \begin{macrocode}
1636 \keys_define:nn {__tag / setup}
1637 {
1638 math/setup .code:n =
1639 {
1640 %deactivate loading of luamml
1641 \tl_gset:Nn \g_@@_luamml_load_tl{-1}
1642 \keys_set:nn {__tag / setup}
1643 {
1644 %deactivate tex source AF
1645 math/tex/AF = false,
1646 %deactivate reading of mathml-AF
1647 math/mathml/sources=,
1648 math/mathml/AF=false,
1649 %deactivate structelem
1650 math/mathml/structelem=false,
1651 %handle value
1652 }
1653 \clist_map_inline:nn { #1}
1654 {
1655 \keys_set:nn {__tag/ setup}{math/__setup/##1}
1656 }
1657 },
1658 math/__setup / mathml-SE .code:n =
1659 {
1660 \tl_gset:Nn \g_@@_luamml_load_tl{1}
1661 \keys_set:nn {__tag / setup}
1662 {
1663 math/mathml/structelem=true
1664 }
1665 },
1666 math/__setup / mathml-AF .code:n =
1667 {
1668 \tl_gset:Nn \g_@@_luamml_load_tl{1}
1669 \clist_put_right:Ne\l__tag_math_mathml_files_clist
1670 {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
1671 \keys_set:nn {__tag / setup}
1672 {
1673 math/mathml/AF=true
1674 }
1675 },
1676 math/__setup / mathml-MS .code:n =
1677 {
1678 \tl_gset:Nn \g_@@_luamml_load_tl{1}
1679 \cs_set_eq:NN\@@_MSFT_mml:nn\@@_MSFT_mml_aux:nn
1680 \bool_set_true:N\l__tag_math_MSFT_bool
1681 \clist_put_right:Ne\l__tag_math_mathml_files_clist
1682 {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
1683 },
1684 math/__setup / tex-AF .code:n =
1685 {
1686 \keys_set:nn {__tag / setup}
1687 {
1688 math/tex/AF =true
1689 }
1690 },
1691 }
1692 % \end{macrocode}
1693 % \subsection{Sockets}
1694 % \subsubsection{Main inline math sockets}
1695 %
1696 % \begin{taggingsocketdecl}
1697 % {
1698 % math/inline/begin,
1699 % math/inline/end,
1700 % math/inline/formula/begin,
1701 % math/inline/formula/end,
1702 % }
1703 % These sockets are already declared in lttagging and only documented here.
1704 % The first two sockets are meant to embed inline
1705 % math into the surrounding (so to close/reopen, e.g., MC-chunks).
1706 % The other two implement the actual formula structure.
1707 % The formula sockets are despite their naming not symmetric:
1708 % the begin socket is issued after the math has started, while
1709 % the end socket is after the math!
1710 % \changes{v0.6j}{2024-11-19}{change the socket to two arguments so that it can
1711 % be used as tagging socket}
1712 % \begin{macrocode}
1713 %\NewTaggingSocket{math/inline/begin}{0}
1714 %\NewTaggingSocket{math/inline/end}{0}
1715 %\NewTaggingSocket{math/inline/formula/begin}{2} %
1716 %\NewTaggingSocket{math/inline/formula/end}{0}
1717 % \end{macrocode}
1718 %\end{taggingsocketdecl}
1719 %
1720 %
1721 % \begin{plugdecl}{MC}
1722 % \begin{macrocode}
1723 \NewTaggingSocketPlug
1724 {math/inline/begin}
1725 {MC}
1726 {\tag_mc_end_push:}
1727 \NewTaggingSocketPlug
1728 {math/inline/end}
1729 {MC}
1730 {\tag_mc_begin_pop:n{}}
1731 % \end{macrocode}
1732 % \end{plugdecl}
1733 %
1734 % We probably will want to test different tagging recipes.
1735 % \begin{plugdecl}{default}
1736 % \begin{macrocode}
1737 \NewTaggingSocketPlug
1738 {math/inline/formula/begin}
1739 {default}
1740 % \end{macrocode}
1741 % \changes{v0.6g}{2024-10-02}{disable paratagging, issue \#711}
1742 % \changes{v0.6j}{2024-11-19}{activate structelem locally issue \#764}
1743 % \changes{v0.6j}{2024-11-19}{change to two arguments}
1744 % \begin{macrocode}
1745 { \tagpdfparaOff
1746 \@@_luamml_structelem:
1747 \tag_socket_use:n{math/content}
1748 \tag_socket_use:n{math/struct/begin}
1749 #2
1750 \tag_socket_use:n{math/end}
1751 }
1752 \NewTaggingSocketPlug
1753 {math/inline/formula/end}
1754 {default}
1755 {
1756 \tag_socket_use:n{math/struct/end}
1757 }
1758 % \end{macrocode}
1759 % \end{plugdecl}
1760 %
1761 % \subsubsection{Main display math sockets}
1762 %
1763 % \begin{taggingsocketdecl}
1764 % {
1765 % math/display/begin,
1766 % math/display/end,
1767 % math/display/formula/begin,
1768 % math/display/formula/end,
1769 % }
1770 % These sockets are already declared in lttagging and only documented here.
1771 % The first two sockets are meant to embed display
1772 % math into the surrounding (so to close/reopen, e.g., MC-chunks and
1773 % P-structure).
1774 % The other two implement the actual formula structure.
1775 % The formula sockets are despite their naming not symmetric:
1776 % the begin socket is issued after the math has started, while
1777 % the end socket is after the math!
1778 % \changes{v0.6j}{2024-11-19}{change number of arguments of formula/begin socket}
1779 % \begin{macrocode}
1780 %\NewTaggingSocket{math/display/begin}{0}
1781 %\NewTaggingSocket{math/display/end}{0}
1782 %\NewTaggingSocket{math/display/formula/begin}{2} %
1783 %\NewTaggingSocket{math/display/formula/end}{0}
1784 % \end{macrocode}
1785 %\end{taggingsocketdecl}
1786
1787 % \begin{plugdecl}{default}
1788 % \begin{macrocode}
1789 \NewTaggingSocketPlug
1790 {math/display/begin}
1791 {default}
1792 {
1793 \UseTaggingSocket{para/textblock/end}
1794 }
1795 \NewTaggingSocketPlug
1796 {math/display/end}
1797 {default}
1798 {
1799 }
1800 % \end{macrocode}
1801 % \end{plugdecl}
1802
1803
1804 % \begin{plugdecl}{default}
1805 % \changes{v0.6j}{2024-11-19}{moved \cs{tagpdfparaOff} into the socket, tagging/765}
1806 % \begin{macrocode}
1807 \NewTaggingSocketPlug
1808 {math/display/formula/begin}
1809 {default}
1810 {
1811 \tagpdfparaOff
1812 \@@_luamml_structelem:
1813 \tag_socket_use:n{math/content}
1814 \tag_socket_use:n{math/struct/begin}
1815 #2
1816 \tag_socket_use:n{math/end}
1817 }
1818 \NewTaggingSocketPlug
1819 {math/display/formula/end}
1820 {default}
1821 {
1822 \tag_socket_use:n{math/struct/end}
1823 }
1824 % \end{macrocode}
1825 % \end{plugdecl}
1826 %
1827 % \subsubsection{Sockets plugs for tags (labels)}
1828 % \begin{taggingsocketdecl}
1829 % {
1830 % math/display/tag/begin,
1831 % math/display/tag/end,
1832 % }
1833 % These sockets are already declared in lttagging and only documented here.
1834 % These sockets are used in \cs{maketag@@@} to tag
1835 % labels as Lbl. luamml changes the plug to move
1836 % the Lbl into the math structure with an intent.
1837 % \changes{v0.6l}{2025-02-06}{added sockets for tags/labels}
1838 % \begin{macrocode}
1839 %\NewTaggingSocket{math/display/tag/begin}{0}
1840 %\NewTaggingSocket{math/display/tag/end}{0}
1841 % \end{macrocode}
1842 %\end{taggingsocketdecl}
1843 %
1844 % \begin{plugdecl}{default}
1845 % \changes{v0.6j}{2024-11-19}{moved \cs{tagpdfparaOff} into the socket, tagging/765}
1846 % \begin{macrocode}
1847 \NewTaggingSocketPlug
1848 {math/display/tag/begin}
1849 {default}
1850 {
1851 \tag_mc_end:
1852 \tag_struct_begin:n {tag=\UseStructureName{math/label}}
1853 \tag_mc_begin:n {}
1854 }
1855 \NewTaggingSocketPlug
1856 {math/display/tag/end}
1857 {default}
1858 {
1859 \tag_mc_end:
1860 \tag_struct_end:
1861 \tag_mc_begin:n{}
1862 }
1863 \AssignTaggingSocketPlug{math/display/tag/begin}{default}
1864 \AssignTaggingSocketPlug{math/display/tag/end}{default}
1865 % \end{macrocode}
1866 % \end{plugdecl}
1867 %
1868 % \subsubsection{Internal sockets}
1869 %
1870 % \begin{variable}{\l_@@_content_template_tl}
1871 % The default text used as alt or actual text.
1872 % \begin{macrocode}
1873 \tl_new:N\l_@@_content_template_tl
1874 \tl_set:Nn \l_@@_content_template_tl
1875 {
1876 LaTeX~ formula~ starts~
1877 \exp_not:N\begin{\g_@@_grabbed_env_tl}
1878 \c_space_tl
1879 \exp_not:V\g_@@_grabbed_math_tl
1880 \c_space_tl
1881 \exp_not:N\end{\g_@@_grabbed_env_tl}
1882 \c_space_tl LaTeX~ formula~ ends~
1883 }
1884 % \end{macrocode}
1885 % \end{variable}
1886
1887 % \begin{variable}{\l_@@_texsource_template_tl}
1888 % The default text used as texsource
1889 % \begin{macrocode}
1890 \tl_new:N\l_@@_texsource_template_tl
1891 \tl_const:Nn\c_@@_inline_env_tl {math}
1892 \tl_set:Nn \l_@@_texsource_template_tl
1893 {
1894 \tl_if_eq:NNTF\g_@@_grabbed_env_tl\c_@@_inline_env_tl
1895 {
1896 $
1897 \exp_not:V\g_@@_grabbed_math_tl
1898 $
1899 }
1900 {
1901 \exp_not:N\begin{\g_@@_grabbed_env_tl}
1902 \exp_not:V\g_@@_grabbed_math_tl
1903 \exp_not:N\end{\g_@@_grabbed_env_tl}
1904 }
1905 }
1906 % \end{macrocode}
1907 % \end{variable}
1908
1909 %
1910 % \begin{taggingsocketdecl}{math/content}
1911 % The math content is stored in associated files and used for
1912 % actual and alternative text. As the exact text is still
1913 % unclear we use a socket to be able to test variants.
1914 % The socket should set all four tl vars above, if needed
1915 % to identical values. It can use the two variables
1916 % \cs{g_@@_grabbed_env_tl} and \cs{g_@@_grabbed_math_tl}
1917 % \begin{macrocode}
1918 \NewTaggingSocket{math/content}{0}
1919 % \end{macrocode}
1920 % \end{taggingsocketdecl}
1921 %
1922 % Some default sockets to set the contents.
1923 % TODO: think about naming convention.
1924 % TODO: think how this should organized so that one
1925 % has options to change from the outside and so that
1926 % there are less repetitions.
1927 % \begin{plugdecl}{actual+source}
1928 % \begin{macrocode}
1929 \NewTaggingSocketPlug
1930 {math/content}
1931 {actual+source}
1932 {
1933 \tl_set:Ne\l_@@_content_actual_tl
1934 {
1935 \l_@@_content_template_tl
1936 }
1937 \tl_set:Ne \l_@@_content_AF_source_tl
1938 {
1939 \l_@@_texsource_template_tl
1940 }
1941 \tl_set:Nn \l_@@_content_AF_mathml_tl {}
1942 \tl_set:Nn \l_@@_content_alt_tl {}
1943 }
1944 % \end{macrocode}
1945 % \end{plugdecl}
1946 %
1947 % \begin{plugdecl}{alt+source}
1948 % \begin{macrocode}
1949 \NewTaggingSocketPlug
1950 {math/content}
1951 {alt+source}
1952 {
1953 \tl_set:Ne\l_@@_content_alt_tl
1954 {
1955 \l_@@_content_template_tl
1956 }
1957 \tl_set:Ne \l_@@_content_AF_source_tl
1958 {
1959 \l_@@_texsource_template_tl
1960 }
1961 \tl_set:Nn \l_@@_content_AF_mathml_tl {}
1962 \tl_set:Nn \l_@@_content_actual_tl {}
1963 }
1964 % \end{macrocode}
1965 % \end{plugdecl}
1966 % \begin{macrocode}
1967 \AssignTaggingSocketPlug{math/content}{alt+source}
1968 % \end{macrocode}
1969 %
1970 % \begin{taggingsocketdecl}{math/struct/begin,
1971 % math/struct/end}
1972 % For the main structure we use a socket too.
1973 % This allows, e.g., to create a special one for luamml
1974 % which setups additional objects.
1975 % The begin socket can use the two variables
1976 % \cs{g_@@_grabbed_env_tl} and \cs{g_@@_grabbed_math_tl}
1977 % \begin{macrocode}
1978 \NewTaggingSocket{math/struct/begin}{0}
1979 \NewTaggingSocket{math/struct/end}{0}
1980 % \end{macrocode}
1981 % \end{taggingsocketdecl}
1982 %
1983 % \begin{plugdecl}{default}
1984 % TODO: think about some naming convention ...
1985 % \begin{macrocode}
1986 \NewTaggingSocketPlug
1987 {math/struct/begin}
1988 {default}
1989 {
1990 \bool_if:NTF\l__tag_math_texsource_AF_bool
1991 { \tl_set_eq:NN \l_@@_content_AF_source_tmpa_tl \l_@@_content_AF_source_tl }
1992 { \tl_clear:N \l_@@_content_AF_source_tmpa_tl }
1993 \tl_if_eq:NnTF\g_@@_grabbed_env_tl {math}
1994 {
1995 \tl_set:Nn\l_@@_attribute_class_tl{inline}
1996 }
1997 {
1998 \tl_set:Nn\l_@@_attribute_class_tl{display}
1999 }
2000 \bool_if:NF\l__tag_math_alt_bool
2001 { \tl_set:Nn \l_@@_content_alt_tl{} }
2002 \tag_struct_begin:n
2003 {
2004 tag=\UseStructureName{math/\l_@@_attribute_class_tl},
2005 attribute-class=\l_@@_attribute_class_tl,
2006 texsource = \l_@@_content_AF_source_tmpa_tl,
2007 title-o = \g_@@_grabbed_env_tl,
2008 actualtext = \l_@@_content_actual_tl,
2009 alt = \l_@@_content_alt_tl
2010 }
2011 % \end{macrocode}
2012 % \changes{0.6w}{2025-10-02}{use debug command instead of direct typeout}
2013 % \begin{macrocode}
2014 \@@_debug_typeout:n{grabbed~math=\meaning\g_@@_grabbed_math_tl}
2015 \tag_mc_begin:n{}
2016 }
2017 \NewTaggingSocketPlug
2018 {math/struct/end}
2019 {default}
2020 { \tag_mc_end: \tag_struct_end: }
2021
2022 \AssignTaggingSocketPlug{math/struct/begin}{default}
2023 \AssignTaggingSocketPlug{math/struct/end}{default}
2024 % \end{macrocode}
2025 % \end{plugdecl}
2026 %
2027 % \begin{plugdecl}{mathml-AF}
2028 % This socket tries to add a mathml-AF to formula.
2029 % It is activated if a mathml.html has been found and loaded.
2030 % As it disturbs the reading of the AF
2031 % it currently deactivates the /Alt key,
2032 % unless it has been reenabled with |math/alt/use=true|
2033 % \begin{macrocode}
2034 \cs_generate_variant:Nn \str_mdfive_hash:n {o}
2035 \tl_new:N\l_@@_content_hash_tl
2036 % \end{macrocode}
2037 % we need to save the grabbed math:
2038 % \begin{macrocode}
2039 \tl_new:N\l_@@_grabbed_math_tl
2040 % \end{macrocode}
2041 % the socket definition
2042 % \begin{macrocode}
2043 \NewTaggingSocketPlug
2044 {math/struct/begin}
2045 {mathml-AF}
2046 {
2047 \int_gincr:N\g_@@_math_total_int
2048 \tl_set:Ne\l_@@_content_hash_tl
2049 {\str_mdfive_hash:o { \l_@@_content_AF_source_tl }}
2050 \tl_set_eq:NN\l_@@_grabbed_math_tl\g_@@_grabbed_math_tl
2051 \tl_if_eq:NnTF\g_@@_grabbed_env_tl {math}
2052 {
2053 \tl_set:Nn\l_@@_attribute_class_tl{inline}
2054 }
2055 {
2056 \tl_set:Nn\l_@@_attribute_class_tl{display}
2057 }
2058 \bool_if:NF\l__tag_math_alt_bool
2059 { \tl_set:Nn \l_@@_content_alt_tl{} }
2060 % \end{macrocode}
2061 % debugging option. TODO: hide in debug key.
2062 % \begin{macrocode}
2063 \tl_if_exist:cTF { g_@@_mathml_ \l_@@_content_hash_tl _tl }
2064 {
2065 \int_gincr:N\g_@@_mathml_AF_found_int
2066 \bool_if:NTF \l__tag_math_mathml_AF_bool
2067 {
2068 \int_gincr:N\g_@@_mathml_AF_attached_int
2069 % \end{macrocode}
2070 % \changes{0.6w}{2025-10-02}{use debug command instead of direct typeout}
2071 % \begin{macrocode}
2072 \@@_debug_typeout:n {Inserting~mathml~with~Hash~\l_@@_content_hash_tl}
2073 }
2074 {
2075 \@@_debug_typeout:n {Ignoring~mathml~with~Hash~\l_@@_content_hash_tl}
2076 }
2077 }
2078 {
2079 \bool_if:NT \l__tag_math_mathml_AF_bool
2080 {
2081 \typeout {WARNING:~mathml~missing~for~hash~\l_@@_content_hash_tl}
2082 }
2083 }
2084 \tag_socket_use:n {math/mathml/write/prepare}
2085 \tag_socket_use:n {math/mathml/write} % write hash if request
2086 \bool_if:NTF\l__tag_math_texsource_AF_bool
2087 { \tl_set_eq:NN \l_@@_content_AF_source_tmpa_tl \l_@@_content_AF_source_tl }
2088 { \tl_clear:N \l_@@_content_AF_source_tmpa_tl }
2089 \tag_struct_begin:n
2090 {
2091 tag=\UseStructureName{math/\l_@@_attribute_class_tl},
2092 attribute-class=\l_@@_attribute_class_tl, %
2093 attribute =
2094 \bool_if:NT \l__tag_math_MSFT_bool
2095 {
2096 \cs_if_exist_use:c {g_@@_mathml_MSFT_ \l_@@_content_hash_tl _tl}
2097 },
2098 AFref =
2099 \bool_if:NT\l__tag_math_mathml_AF_bool
2100 {
2101 \cs_if_exist_use:c {g_@@_mathml_ \l_@@_content_hash_tl _tl}
2102 },
2103 texsource = \l_@@_content_AF_source_tmpa_tl, % should be after mathml AF!
2104 title-o = \g_@@_grabbed_env_tl, %
2105 alt = \l_@@_content_alt_tl
2106 }
2107 \@@_debug_typeout:n {grabbed~math=\meaning\g_@@_grabbed_math_tl}
2108 \tag_mc_begin:n{}
2109 }
2110 % \end{macrocode}
2111 % not really needed but looks more symmetric:
2112 % \begin{macrocode}
2113 \NewTaggingSocketPlug
2114 {math/struct/end}
2115 {mathml-AF}
2116 {
2117 \tag_mc_end:
2118 \tag_struct_end:
2119 }
2120 % \end{macrocode}
2121 % \end{plugdecl}
2122 %
2123 % \begin{taggingsocketdecl}{math/end}
2124 % A socket used at the end of the math (before the closing dollar(s))
2125 % which can, e.g., set a flag for luamml.
2126 % \begin{macrocode}
2127 \NewTaggingSocket{math/end}{0}
2128 % \end{macrocode}
2129 % \end{taggingsocketdecl}
2130 %
2131 %
2132 % \changes{v0.6j}{2024-11-19}{removed enable/disable command and assign tagging sockets directly}
2133 % \begin{macrocode}
2134 \AssignTaggingSocketPlug{math/inline/begin}{MC}
2135 \AssignTaggingSocketPlug{math/inline/end}{MC}
2136 \AssignTaggingSocketPlug{math/inline/formula/begin}{default}
2137 \AssignTaggingSocketPlug{math/inline/formula/end}{default}
2138 \AssignTaggingSocketPlug{math/display/begin}{default}
2139 \AssignTaggingSocketPlug{math/display/end}{default}
2140 \AssignTaggingSocketPlug{math/display/formula/begin}{default}
2141 \AssignTaggingSocketPlug{math/display/formula/end}{default}
2142 % \end{macrocode}
2143 %
2144 %
2145 % \subsection{Interface commands}
2146 %
2147 % \begin{macro}
2148 % {\@@_process:nn, \@@_process:Vn, \@@_process_auxi:nn, \@@_process_auxii:nn}
2149 % A no-op place-holder; the internal wrapper means that it does not need to
2150 % be concerned with internals.
2151 % \changes{0.6v}{2025-09-26}{Ensure that \cs{ifmeasuring@} exists, tagging issue 952}
2152 % \begin{macrocode}
2153 \newif\ifmeasuring@
2154 \cs_new_protected:Npn \@@_process:nn #1#2
2155 {
2156 \legacy_if:nF { measuring@ }
2157 {
2158 \tl_if_in:nnTF {#2} { \m@th }
2159 { \bool_set_true:N\l_@@_fakemath_bool }
2160 { \tl_trim_spaces_apply:nN {#2} \@@_process_auxi:nn {#1} }
2161 }
2162 }
2163 \cs_generate_variant:Nn \@@_process:nn { V }
2164 \cs_new_protected:Npn \@@_process_auxi:nn #1#2
2165 {
2166 \tl_gset:Nn \g_@@_grabbed_env_tl {#2}
2167 \tl_gset:Nn \g_@@_grabbed_math_tl {#1}
2168 \@@_process_auxii:nn {#2} {#1}
2169 }
2170 \cs_new_protected:Npn \@@_process_auxii:nn #1#2 { }
2171 % \end{macrocode}
2172 % \end{macro}
2173 %
2174 % \begin{macro}{\math_processor:n}
2175 % A simple installer
2176 % \begin{macrocode}
2177 \cs_new_protected:Npn \math_processor:n #1
2178 { \cs_set_protected:Npn \@@_process_auxii:nn ##1##2 {#1} }
2179 % \end{macrocode}
2180 % \end{macro}
2181 %
2182 % \subsection{Content grabbing}
2183 %
2184 % \begin{macro}{\MathCollectTrue,\MathCollectFalse}
2185 % \begin{macrocode}
2186 \cs_set_protected:Npn\MathCollectTrue{\bool_set_false:N \l__math_collected_bool}
2187 \cs_set_protected:Npn\MathCollectFalse{\bool_set_true:N \l__math_collected_bool}
2188 % \end{macrocode}
2189 % \end{macro}
2190 %
2191 % \begin{macro}{\@@_grab_dollar:w}
2192 % \begin{macro}{\@@_grab:n}
2193 % \changes{v0.6c}{2024-08-22}{Correct handling of empty math segments}
2194 % Top-level function to handle grabbing of inline math mode delimited by
2195 % |$| tokens. We provide two different ways to do that: a token-by-token
2196 % one that can be used everywhere, and a fast delimited one that does not
2197 % work anywhere that the end |$| token may be hidden, most obviously in
2198 % tabulars. The function here is therefore set up as a variable starting
2199 % point.
2200 % \changes{0.6t}{2025-08-27}{Use a loop to scan for \cs{)}}
2201 % \changes{0.6z}{2026-01-28}{Allow for alignment tokens}
2202 % \begin{macrocode}
2203 \cs_new_protected:Npn \@@_grab_dollar:w { \@@_grab_dollar_delim:w }
2204 % \end{macrocode}
2205 % After grabbing inline math material, there is again common processing
2206 % independent of mechanism of collection.
2207 % \begin{macrocode}
2208 \cs_new_protected:Npn \@@_grab:n #1
2209 {
2210 % \end{macrocode}
2211 % We need to do processing first as this picks up \enquote{fake} math mode:
2212 % that information is needed below. The captured material \emph{could} contain
2213 % |&| tokens, so we ensure that these do no cause an issue if we are inside
2214 % an \tn{halign}.
2215 % \begin{macrocode}
2216 \group_align_safe_begin:
2217 \@@_process:nn { math } {#1}
2218 \group_align_safe_end:
2219 % \end{macrocode}
2220 % We do not want math tagging in fakemath or when measuring,
2221 % We also do not want math tagging if tagging has been suspended.
2222 % \begin{macrocode}
2223 \bool_lazy_any:nTF
2224 {
2225 {\legacy_if_p:n { measuring@ }}
2226 { \l_@@_fakemath_bool }
2227 { \tl_if_blank_p:n {#1} }
2228 }
2229 {
2230 \@@_luamml_ignore:
2231 #1 $ % $
2232 }
2233 {
2234 \tag_socket_use:n {math/inline/begin} %end P-MC
2235 % \end{macrocode}
2236 % We do no use a tagging socket here, so that the argument (the
2237 % math) is not lost, tagging-project issue 661.
2238 % \changes{v0.6j}{2024-11-19}{change socket to tagging socket}
2239 % \begin{macrocode}
2240 \tag_socket_use:nnn {math/inline/formula/begin}{}{#1}
2241 $ % $
2242 \tag_socket_use:n {math/inline/formula/end}
2243 \tag_socket_use:n {math/inline/end} % restart P-MC
2244 }
2245 }
2246 % \end{macrocode}
2247 % \end{macro}
2248 % \end{macro}
2249 %
2250 % \begin{macro}{\@@_grab_dollar_delim:w}
2251 % Grab up to a single |$|, for inline math mode, suppressing
2252 % any processing if the token is \tn{m@th} found in the content.
2253 % \begin{macrocode}
2254 \cs_new_protected:Npn \@@_grab_dollar_delim:w #1 $ % $
2255 { \@@_grab:n {#1} }
2256 % \end{macrocode}
2257 % \end{macro}
2258 %
2259 % \begin{macro}{\@@_grab_dollardollar:w}
2260 % And for the classical \TeX{} display structure.
2261 % \changes{0.6n}{2025-03-08}{}
2262 % \begin{macrocode}
2263 \cs_new_protected:Npn \@@_grab_dollardollar:w #1 $$ {
2264 \tl_if_blank:nF {#1}
2265 {
2266 \@@_process:nn { equation* } {#1}
2267 \tag_socket_use:n {math/display/begin}
2268 \tag_socket_use:nn{math/display/formula/begin}{}{#1}
2269 }
2270 % \end{macrocode}
2271 % Prepare to finish the display math formula and let \TeX{} do its job; then
2272 % regain control after the formula has been contributed to the page, in order to add the
2273 % tagging followed by the correct penalty and skip.
2274 % \begin{macrocode}
2275 \@@_prepare_display_end:
2276 $$
2277 }
2278 % \end{macrocode}
2279 % \end{macro}
2280 %
2281 %
2282 %
2283 %
2284 % \begin{macro}{\@@_grab_inline_delim:w}
2285 % Collect inline math content and deal with the need to move to math mode.
2286 % \begin{macrocode}
2287 \cs_new_protected:Npn \@@_grab_inline_delim:w % \(
2288 #1 \)
2289 {
2290 \tl_if_blank:nF {#1}
2291 {
2292 $ #1 $
2293 }
2294 \bool_set_false:N \l_@@_collected_bool
2295 }
2296 % \end{macrocode}
2297 % \end{macro}
2298 %
2299 % \begin{macro}{\@@_grab_inline:w}
2300 % See |\@@_grab_dollar:w|.
2301 % \begin{macrocode}
2302 \cs_new_protected:Npn \@@_grab_inline:w { \@@_grab_inline_delim:w }
2303 % \end{macrocode}
2304 % \end{macro}
2305 %
2306 % \begin{macro}{\@@_grab_eqn:w}
2307 % For the most common use of \cs{[}/\cs{]}: turn into an environment.
2308 % \begin{macrocode}
2309 \cs_new_protected:Npn \@@_grab_eqn:w % \[
2310 #1 \]
2311 {
2312 % \typeout{collected? = \bool_if:NTF \l_@@_collected_bool {true}{false}}
2313 \begin { equation* } #1 \end { equation* }
2314 }
2315 % \end{macrocode}
2316 % \end{macro}
2317 %
2318 % \subsection{Token-by-token inline grabbing}
2319 %
2320 % Grabbing inline math token-by-token is more involved. The mechanism here
2321 % is essentially a simplified version of that originally seen in
2322 % \pkg{collcell} and refined in \pkg{siunitx}. We make use of the fact that
2323 % in math mode spaces are ignored, so we have to deal with only \texttt{N}-type
2324 % tokens and groups. Furthermore, there is no need to look inside groups, so
2325 % the only special cases are a small selection of \texttt{N}-type tokens.
2326 %
2327 % \begin{variable}{\l_@@_grabbed_tl}
2328 % For collection of the material piecewise.
2329 % \begin{macrocode}
2330 \tl_new:N \l_@@_grabbed_tl
2331 % \end{macrocode}
2332 % \end{variable}
2333 %
2334 % \begin{variable}{\l_@@_grab_env_int}
2335 % Needed to count up the number of nested environments encountered.
2336 % \begin{macrocode}
2337 \int_new:N \l_@@_grab_env_int
2338 % \end{macrocode}
2339 % \end{variable}
2340 %
2341 % \begin{macro}{\@@_grab_loop_init:}
2342 % \changes{0.6z}{2026-01-27}{Rename a function}
2343 % \begin{macro}{\@@_grab_loop:}
2344 % The lead-off here establishes a group: we need that as we will have to
2345 % be careful in the way \tn{cr} is handled and ensure this is only
2346 % manipulated whilst grabbing. The main loop is then started.
2347 % \changes{0.6t}{2025-08-27}{Use a loop to scan for \cs{)}}
2348 % \begin{macrocode}
2349 \cs_new_protected:Npn \@@_grab_loop_init:
2350 {
2351 \group_begin:
2352 \tl_clear:N \l_@@_grabbed_tl
2353 \@@_grab_loop:
2354 }
2355 \cs_new_protected:Npn \@@_grab_loop:
2356 {
2357 \peek_remove_spaces:n
2358 {
2359 \peek_meaning:NTF \c_group_begin_token
2360 { \@@_grab_loop_group:n }
2361 { \@@_grab_loop_token:N }
2362 }
2363 }
2364 % \end{macrocode}
2365 % \end{macro}
2366 % \end{macro}
2367 %
2368 % \begin{macro}{\@@_grab_loop_group:n}
2369 % \begin{macro}{\@@_grab_loop_store:n}
2370 % Handling of grabbed groups is pretty easy.
2371 % \begin{macrocode}
2372 \cs_new_protected:Npn \@@_grab_loop_group:n #1
2373 { \@@_grab_loop_store:n { {#1} } }
2374 \cs_new_protected:Npn \@@_grab_loop_store:n #1
2375 {
2376 \tl_put_right:Nn \l_@@_grabbed_tl {#1}
2377 \@@_grab_loop:
2378 }
2379 % \end{macrocode}
2380 % \end{macro}
2381 % \end{macro}
2382 %
2383 % \begin{macro}{\@@_grab_loop_token:N}
2384 % \begin{macro}
2385 % {
2386 % \@@_grab_loop_$: ,
2387 % \@@_grab_loop_\\: ,
2388 % \@@_grab_loop_\begin: ,
2389 % \@@_grab_loop_\end: ,
2390 % \@@_grab_loop_\ignorespaces: ,
2391 % \@@_grab_loop_\unskip: ,
2392 % \@@_grab_loop_\textonly@unskip:
2393 % }
2394 % \changes{0.6t}{2025-08-27}{Use a loop to scan for \cs{)}}
2395 % \changes{0.6z}{2026-01-28}{Allow for alignment tokens}
2396 % \begin{macro}{\@@_grab_loop_\end:n}
2397 % \changes{0.6z}{2026-01-28}{New macro}
2398 % Filter out the special cases: for performance reasons, use a hash table
2399 % approach rather than a loop (\emph{cf.}~\pkg{collcell}). To handle the
2400 % case of a nested array or similar, an alignment safe group is added
2401 % around the first occurrence of a nested environment.
2402 % \begin{macrocode}
2403 \cs_new_protected:Npn \@@_grab_loop_token:N #1
2404 {
2405 \cs_if_exist_use:cF
2406 { @@_grab_loop_ \token_to_str:N #1 : }
2407 { \@@_grab_loop_store:n {#1} }
2408 }
2409 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N $ : }
2410 { \@@_grab_loop_end: }
2411 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N \) : }
2412 { \@@_grab_loop_end: }
2413 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N \\ : }
2414 {
2415 \int_compare:nNnTF \l_@@_grab_env_int = 0
2416 { \@@_grab_loop_newline: }
2417 { \@@_grab_loop_store:n { \\ } }
2418 }
2419 % \end{macrocode}
2420 % In contrast to \pkg{collcell}, nesting is tracked by counting
2421 % \cs{begin}/\cs{end} pairs: this is needed in case there is a tabular-like
2422 % construct containing |\\| inside a cell. As a result, the end-of-tabular
2423 % can be detected without checking the name argument: if \cs{end} is
2424 % encountered at nesting level~0, we've hit the end of a cell. In that case,
2425 % end the row and leave the environment to clean up.
2426 % \begin{macrocode}
2427 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N \begin : }
2428 {
2429 \int_incr:N \l_@@_grab_env_int
2430 \int_compare:nNnT \l_@@_grab_env_int = 1
2431 { \group_align_safe_begin: }
2432 \@@_grab_loop_store:n { \begin }
2433 }
2434 \cs_new_protected:cpe { @@_grab_loop_ \token_to_str:N \end : }
2435 {
2436 \exp_not:N \int_compare:nNnTF \exp_not:N \l_@@_grab_env_int = 0
2437 {
2438 \exp_not:N \@@_grab_loop_newline:
2439 \exp_not:N \end
2440 }
2441 { \exp_not:c { @@_grab_loop_ \token_to_str:N \end :n } }
2442 }
2443 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N \end :n } #1
2444 {
2445 \int_decr:N \l_@@_grab_env_int
2446 \tl_put_right:Nn \l_@@_grabbed_tl { \end {#1} }
2447 \int_compare:nNnT \l_@@_grab_env_int = 0
2448 { \group_align_safe_end: }
2449 \@@_grab_loop:
2450 }
2451 \tl_map_inline:nn { \ignorespaces \unskip \textonly@unskip }
2452 {
2453 \cs_new_protected:cpn { @@_grab_loop_ \token_to_str:N #1 : }
2454 { \@@_grab_loop: }
2455 }
2456 % \end{macrocode}
2457 % \end{macro}
2458 % \end{macro}
2459 % \end{macro}
2460 %
2461 % \begin{macro}{\@@_grab_loop_newline:}
2462 % To allow collection of tokens in the part of the \tn{halign} template after
2463 % |#|, we need \TeX{} to see the primitive with the loop token in the right
2464 % place. That is done by re-defining \tn{cr} at present. Ideally there would
2465 % be a socket in the definition of \texttt{tabular}, etc., to handle this:
2466 % there is also the need to examine in interaction with \pkg{longtable}, which
2467 % also redefines \tn{cr}.
2468 % \begin{macrocode}
2469 \cs_new_protected:Npn \@@_grab_loop_newline:
2470 {
2471 \if_false: { \fi:
2472 \cs_set_protected:Npn \cr
2473 {
2474 \@@_grab_loop:
2475 \tex_cr:D
2476 }
2477 \if_false: } \fi:
2478 \\
2479 }
2480 % \end{macrocode}
2481 % \end{macro}
2482 %
2483 % \begin{macro}{\@@_grab_loop_end:}
2484 % Clean up and pass on.
2485 % \begin{macrocode}
2486 \cs_new_protected:Npn \@@_grab_loop_end:
2487 {
2488 \exp_args:NNV \group_end:
2489 \@@_grab:n \l_@@_grabbed_tl
2490 }
2491 % \end{macrocode}
2492 % \end{macro}
2493 %
2494 % \subsection{Marking math environments}
2495 %
2496 % A general mechanism for math mode environments that do not grab their
2497 % content (\emph{cf.}~most \pkg{amsmath} environments).
2498 %
2499 % \begin{variable}{\l_@@_env_name_tl}
2500 % To allow us to carry out \enquote{special effects}
2501 % \begin{macrocode}
2502 \tl_new:N \l_@@_env_name_tl
2503 % \end{macrocode}
2504 % \end{variable}
2505 %
2506 % Here we set up specialised handling of environments. The idea for the
2507 % \texttt{arg-spec} key is that if an environment takes arguments, we
2508 % don't worry during the main grabbing. Rather, we remove the arguments
2509 % from the grabbed content and forward only the payload. That is done by
2510 % (ab)using \pkg{ltcmd}.
2511 % \begin{macrocode}
2512 \keys_define:nn { @@ }
2513 {
2514 arg-spec .code:n =
2515 {
2516 \ExpandArgs { c } \DeclareDocumentCommand
2517 { @@_env \l_@@_env_name_tl _aux: }
2518 {#1}
2519 { \@@_env_forward:w }
2520 }
2521 }
2522 % \end{macrocode}
2523 %
2524 % \begin{macro}{\math_register_env:nn}
2525 % \begin{macro}{\math_register_env:n}
2526 % \begin{macro}{\RegisterMathEnvironment}
2527 % Set up to capture environment content and make available.
2528 % \changes{0.6k}{2024-12-04}{Store the current grouplevel for luamml }
2529 % \begin{macrocode}
2530 \cs_new_protected:Npn \math_register_env:nn #1#2
2531 {
2532 \tl_set:Nn \l_@@_env_name_tl {#1}
2533 \keys_set:nn { @@ } {#2}
2534 \cs_gset_eq:cc { @@_env_ #1 _begin: } {#1}
2535 \cs_gset_eq:cc { @@_env_ #1 _end: } { end #1 }
2536 %
2537 \ExpandArgs { nne } \RenewDocumentEnvironment {#1} { b }
2538 {
2539 \exp_not:N \bool_if:NTF \exp_not:N \l_@@_collected_bool
2540 {
2541 % \typeout{===>B1}
2542 }
2543 {
2544 % \typeout{===>B2}
2545 \cs_if_exist:cTF { @@_env #1 _aux: }
2546 {
2547 \exp_not:c { @@_env #1 _aux: }
2548 ##1 \exp_not:N \@@_env_end: {#1}
2549 }
2550 { \exp_not:N \@@_process:nn {#1} {##1} }
2551 \exp_not:n { \@kernel@math@registered@begin }
2552 \bool_set_true:N \exp_not:N \l_@@_collected_bool
2553 }
2554 % \exp_not:N \tracingall
2555 \exp_not:c { @@_env_ #1 _begin: }
2556 ##1
2557 \exp_not:c { @@_env_ #1 _end: }
2558 % \exp_not:N \tracingnone
2559 }
2560 {
2561 }
2562 }
2563 % \end{macrocode}
2564 % \changes{v0.6k}{2024/12/04}{Add tagging sockets for luamml support}
2565 % \changes{v0.6k}{2024/12/04}{Store the current grouplevel for luamml }
2566 % \begin{macrocode}
2567 \cs_new_protected:Npn \math_register_halign_env:nn #1#2
2568 {
2569 \tl_set:Nn \l_@@_env_name_tl {#1}
2570 \keys_set:nn { @@ } {#2}
2571 \cs_gset_eq:cc { @@_env_ #1 _begin: } {#1}
2572 \cs_gset_eq:cc { @@_env_ #1 _end: } { end #1 }
2573 %
2574 \ExpandArgs { nnee } \RenewDocumentEnvironment {#1} { b }
2575 {
2576 \exp_not:N \bool_if:NTF \exp_not:N \l_@@_collected_bool
2577 {
2578 % \typeout{===>B1}
2579 }
2580 {
2581 % \typeout{===>B2}
2582 \cs_if_exist:cTF { @@_env #1 _aux: }
2583 {
2584 \exp_not:c { @@_env #1 _aux: }
2585 ##1 \exp_not:N \@@_env_end: {#1}
2586 }
2587 { \exp_not:N \@@_process:nn {#1} {##1} }
2588 \exp_not:n { \@kernel@math@registered@begin }
2589 \bool_set_true:N \exp_not:N \l_@@_collected_bool
2590 }
2591 % \exp_not:N \tracingall
2592 \exp_not:c { @@_env_ #1 _begin: }
2593 ##1
2594 % \exp_not:N \tracingnone
2595 }
2596 {
2597 \exp_not:c { @@_env_ #1 _end: }
2598 }
2599 }
2600
2601
2602
2603 \cs_new:Npn \@kernel@math@registered@begin {
2604 % \ShowTagging{struct-stack}
2605 %\typeout{==>A1}\ShowTagging{struct-stack,mc-current}
2606 \mode_if_vertical:TF
2607 {
2608 % \legacy_if:nTF { @endpe }
2609 % { \legacy_if_set_false:n { @endpe } }
2610 % { \__block_list_beginpar_vmode: }
2611 %
2612 % \typeout{==>~ at:~ \g__tag_struct_tag_tl}
2613 %
2614 \tag_if_active:T
2615 {
2616 \exp_args:Noo\str_if_eq:nnF \g__tag_struct_tag_tl { \l__tag_para_main_tag_tl } % needs correction!
2617 {
2618 % \typeout{==>A2}
2619 \__block_beginpar_vmode:
2620 } % needs correction!
2621 }
2622 }
2623 {
2624 % \typeout{==>A3}
2625 \UseTaggingSocket{para/textblock/end}
2626 }
2627 \tag_socket_use:nn{math/display/formula/begin}{}{}
2628 % \typeout{==>MC1}\ShowTagging{mc-current}
2629 }
2630
2631 \cs_new_protected:Npn \math_register_env:n #1
2632 { \math_register_env:nn {#1} { } }
2633
2634 \NewDocumentCommand \RegisterMathEnvironment { O{} m }
2635 { \math_register_env:nn {#2} {#1} }
2636 % \end{macrocode}
2637 % \end{macro}
2638 % \end{macro}
2639 % \end{macro}
2640 %
2641 % \begin{macro}{\@@_env_forward:w}
2642 % \begin{macrocode}
2643 \cs_new_protected:Npn \@@_env_forward:w #1 \@@_env_end: #2
2644 { \@@_process:nn {#2} {#1} }
2645 % \end{macrocode}
2646 % \end{macro}
2647 %
2648 %
2649 %
2650 %
2651 %
2652 %
2653 % \subsection{Regaining control after a display has finished with \texttt{\$\$}}
2654 %
2655 %
2656 % The tagging structures have to be added after the formula content
2657 % but before \TeX{} is allowed to break a page. But \TeX{} will
2658 % automatically add \cs{postdisplaypenalty} followed by
2659 % \cs{belowdisplayskip} or \cs{belowdisplayshortskip} without
2660 % giving us any chance to take control before these are added.
2661 %
2662 % We therefore implement the following approach:
2663 % \begin{itemize}
2664 % \item Just before we end the formula we save away the current
2665 % value of \cs{postdisplaypenalty} in case it was altered within
2666 % the formula. Then we set it to 10000 so that what is inserted by
2667 % \TeX{} is not allowing for a page break at this point
2668 % \item
2669 % At this point we also normally flip the sign of \cs{belowdisplayskip} and
2670 % \cs{belowdisplayshortskip} so that they become negative and
2671 % record that we have done this, by setting the token list
2672 % \cs{g_@@_skip_sign_tl} to \texttt{-}.
2673 % \item
2674 % However, we only do that if both are non-negative. If either of them
2675 % is negative we assume that this was deliberately done by the
2676 % user to adjust the spacing around this particular display.
2677 % Again, we record in \cs{g_@@_skip_sign_tl} that we haven't
2678 % done the sign flip by setting the variable to empty.
2679 % \item
2680 % Making these two skips negative is essential, because the formula
2681 % will be added using the special \cs{postdisplaypenalty} value that
2682 % doesn't allow a page break even though the real value (that we
2683 % use later on) will probably do that. Thus the below skip added by \TeX{} will add to
2684 % the size of the display and not vanish into the bottom margin and
2685 % if it is positive \TeX{} might decide to move the whole formula
2686 % onto the next page even though it might well fit.
2687 % \item
2688 % Once \TeX{} has finished processing the closing \verb=$$= it has added something
2689 % like
2690 %\begin{verbatim}
2691 % \penalty 100000 % our special value for \postdisplaypenalty
2692 % \glue -10.0 plus -3pt % the \belowdisplayskip (with sign flipped
2693 %\end{verbatim}
2694 % \end{itemize}
2695 % after the formula.
2696 % This means that at this that point we can retrieve this skip by using
2697 % \cs{lastskip} and we just have have to flip the sign again to know how to
2698 % correct it (no need to know whether the normal or the short skip was
2699 % added by \TeX{}) and the right penalty is available to us too as
2700 % we have saved it away in \cs{g_@@_postdisplaypenalty_int}.
2701 %
2702 %
2703 %
2704 % \begin{macro}{\@@_prepare_display_end:}
2705 %
2706 % Prepare to finish the formula and give control to \TeX{}. This is
2707 % normally used directly in front of \verb=$$= (\cs{eqno} or
2708 % \cs{leqno}.
2709 %
2710 % \changes{v0.6n}{2025/03/07}{Flipping the sign of the ``below skips''
2711 % when the formula ends, rather than in \cs{everydisplay} (tagging/809)}
2712 % \begin{macrocode}
2713 \cs_new_protected:Npn \@@_prepare_display_end: {
2714 \bool_lazy_or:nnTF
2715 { \dim_compare_p:nNn \belowdisplayskip < {0pt} }
2716 { \dim_compare_p:nNn \belowdisplayshortskip < {0pt} }
2717 % \end{macrocode}
2718 % No sign flipping if one or both of the skips are already negative.
2719 % \begin{macrocode}
2720 { \tl_gclear:N \g_@@_skip_sign_tl }
2721 % \end{macrocode}
2722 % Otherwise flip the sign of both.
2723 % \begin{macrocode}
2724 {
2725 \tl_gset:Nn \g_@@_skip_sign_tl {-}
2726 \skip_set:Nn \belowdisplayskip {-\belowdisplayskip}
2727 \skip_set:Nn \belowdisplayshortskip {-\belowdisplayshortskip}
2728 }
2729 % \end{macrocode}
2730 % For the skip it is enough to flip the sign, because we get the
2731 % value back through \cs{lastskip} but for the
2732 % \cs{postdisplaypenalty} change we have to save the current value
2733 % somewhere, because it may have been changed within the display
2734 % formula. This has to be done globally, because it is used after
2735 % the formula has ended.
2736 % \begin{macrocode}
2737 \int_gset_eq:NN \g_@@_postdisplaypenalty_int \postdisplaypenalty
2738 \int_set_eq:NN \postdisplaypenalty \@M
2739 }
2740 % \end{macrocode}
2741 % \end{macro}
2742 %
2743 %
2744 %
2745 % \begin{macro}{\@@_tag_dollardollar_display_end:}
2746 %
2747 % The code to be executed after the formula has ended is injected
2748 % using \cs{group_insert_after:N} inside of \cs{tex_everydisplay:D}
2749 % (i.e., when the formula starts but inside the group started by
2750 % the display). As \cs{group_insert_after:N} expects a single
2751 % token we have to store that code in a command.
2752 % \changes{v0.6j}{2024-11-19}{removed unneeded \cs{tagpdfparaOn}}
2753 % \begin{macrocode}
2754 \cs_new_protected:Npn \@@_tag_dollardollar_display_end:
2755 {
2756 % \typeout{== tag dollarldollar display end}
2757 % \ShowTagging{struct-stack}
2758 \para_raw_end:
2759 % \end{macrocode}
2760 % The \cs{postdisplaypenalty} was temporarily set
2761 % to 10000 inside the display and both the \cs{belowdisplayskip} and the
2762 % \cs{belowdisplayshortskip} were negated (with some exceptions, see
2763 % above), so whatever was inserted
2764 % it should have been a negative or possibly zero skip. Whatever
2765 % was added, we pick up the value, so that we can correct
2766 % the spacing after the tagging code has been inserted.
2767 % \begin{macrocode}
2768 \l_@@_tmpa_skip \lastskip
2769 \tag_socket_use:n{math/display/formula/end}
2770 % \end{macrocode}
2771 % Now we add a skip without introducing a page break possibility,
2772 % that should bring the current vertical position back to the point
2773 % where \TeX{} has added the penalty and the \enquote{below skip}.
2774 % \changes{v0.6f}{2024-09-30}{Correct logic for inserting below skips
2775 % after displays (tagging/721)}
2776 % \begin{macrocode}
2777 \nobreak
2778 \skip_vertical:n { -\l_@@_tmpa_skip }
2779 % \end{macrocode}
2780 % Then we finally add the real stuff: the true \cs{postdisplaypenalty}
2781 % (saved in \cs{g_@@_postdisplaypenalty_int} earlier)
2782 % and the negated value of the skip value we saved in \cs{l_@@_tmpa_skip}. It may look
2783 % strange that we have two identical negated skips next to each
2784 % other, but if you think about it, that is correct: the first
2785 % cancels the \enquote{below skip} that \TeX{} has added and the
2786 % second puts the same amount of skip after the penalty (which is where it
2787 % should be).
2788 % \begin{macrocode}
2789 \penalty \g_@@_postdisplaypenalty_int
2790 \skip_vertical:n
2791 % \end{macrocode}
2792 % Actually we do use the skip without flipping the sign when our records show
2793 % that it wasn't flipped earlier i.e., when the negative value was due to the user
2794 % specifying it inside the formula.
2795 % \begin{macrocode}
2796 { \g_@@_skip_sign_tl \l_@@_tmpa_skip }
2797 % \end{macrocode}
2798 % As we are now in vertical mode the situation is different from
2799 % the way \TeX{} would handle things after a display: \TeX{} would
2800 % internally switch to horizontal mode without adding a
2801 % \cs{parskip}. But this is not possible to do on the macro
2802 % level. Therefore we have to neutralize the upcoming \cs{parskip}
2803 % since we can't prevent it from being added.
2804 %
2805 % Actually, we could combine this correction with the previous
2806 % \cs{skip_vertical:n}, which would produce marginally smaller PDFs;
2807 % but that will make \cs{showoutput} tracing somewhat less easy to
2808 % understand, so I haven't done that (yet).
2809 % \changes{v0.6k}{2024/12/01}{Handle \cs{parskip} after \texttt{\$\$}
2810 % display correctly (tagging/762)}
2811 % \begin{macrocode}
2812 % \typeout{------->~ add~ negative~ parskip~ (to~ cancel~ the~
2813 % one~ that~ TeX~ will~ add)}
2814 \skip_vertical:n { -\tex_parskip:D }
2815 % \end{macrocode}
2816 %
2817 % We also set the \texttt{@domathendpetrue} flag to signal that
2818 % paragraph continuation should happen after such a display.
2819 % \begin{macrocode}
2820 \@domathendpetrue
2821 \@doendpe % this has no \end{...} to take care of it
2822 }
2823 % \end{macrocode}
2824 % \end{macro}
2825 %
2826 %
2827 %
2828 %
2829 % \begin{macro}{\g_@@_postdisplaypenalty_int}
2830 % This is the internal variable in which we save the
2831 % \cs{postdisplaypenalty}. It is global because we use it
2832 % immediately after the formula has ended.
2833 % \begin{macrocode}
2834 \int_new:N \g_@@_postdisplaypenalty_int
2835 % \end{macrocode}
2836 % \end{macro}
2837 %
2838 %
2839 %
2840 % \begin{macro}{\g_@@_skip_sign_tl}
2841 % We record whether we have flipped the signs of \cs{belowdisplayskip},
2842 % etc., so that we know what to do after the formula has ended. If
2843 % we have it flipped the signs then variable holds \texttt{-}, whilst otherwise it is
2844 % empty. This means we can flip the signs again by simply prefixing the
2845 % value with this token list variable.
2846 % \begin{macrocode}
2847 \tl_new:N \g_@@_skip_sign_tl
2848 % \end{macrocode}
2849 % \end{macro}
2850 %
2851 %
2852 %
2853 % \begin{macro}[no-user-doc]{\dollardollar@end}
2854 % When we do tagging we augment the kernel definition for
2855 % \cs{dollardollar@end} in order to regain control at the very end
2856 % of the formula (after the user may have altered
2857 % \cs{postdisplaypenalty} or \cs{belowdisplayskip}).
2858 % \begin{macrocode}
2859 \def \dollardollar@end { \@@_prepare_display_end: $$ }
2860 % \end{macrocode}
2861 % \end{macro}
2862 %
2863 %
2864 %
2865 % \begin{macro}[no-user-doc]{\eqno,\leqno}
2866 % However, in case of a formula using the primitives \cs{eqno} or
2867 % \cs{leqno} the \cs{dollardollar@end} comes too late as it is
2868 % executed in the context of the equation number; and the values for
2869 % \cs{postdisplaypenalty}, etc.\ set at this point are not the ones
2870 % used for the formula. We therefore have to execute
2871 % \cs{@@_prepare_display_end:} just in front of the primitive.
2872 % \begin{macrocode}
2873 \protected\def\eqno{
2874 \@@_prepare_display_end:
2875 % \end{macrocode}
2876 % Inside the equation we set it temporarly to do nothing, because
2877 % otherwise it would be executed again when \cs{dollardollar@end}
2878 % is reached (not that this would matter, but it would just take
2879 % unnecessary extra time).
2880 % \begin{macrocode}
2881 \@kernel@eqno
2882 \cs_set_eq:NN \@@_prepare_display_end: \prg_do_nothing:
2883 \aftergroup\ignorespaces
2884 }
2885 % \end{macrocode}
2886 % Same game for \cs{leqno}.
2887 % \begin{macrocode}
2888 \protected\def\leqno{
2889 \@@_prepare_display_end:
2890 \@kernel@leqno
2891 \cs_set_eq:NN \@@_prepare_display_end: \prg_do_nothing:
2892 \aftergroup\ignorespaces
2893 }
2894 % \end{macrocode}
2895 % \end{macro}
2896 %
2897 %
2898 %
2899 %
2900 %
2901 % \subsection{Document commands}
2902 %
2903 % \car*{Add one more here: \texttt{displaymath}, which
2904 % is equivalent to \cs{[} , \cs{]}\\
2905 % and hence to the basic \texttt{equation*}.\\
2906 % Added in more recent branch.}
2907 %
2908 % \begin{macro}[no-user-doc]
2909 % {\equation, \@@_equation_begin:, \equation*, \@@_equation_star_begin:}
2910 % \begin{macro}
2911 % {\endequation, \@@_equation_end:, \endequation*, \@@_equation_star_end:}
2912 % These environments are not set up by \pkg{amsmath} to collect their body,
2913 % so we do that here. This has to be done \emph{after} we can be sure
2914 % \pkg{amsmath} is loaded.
2915 %
2916 % Note that with \pkg{amsmath} loaded, \texttt{equation*} and \texttt{equation}
2917 % are the two basics: they are used to define the other single-row
2918 % display environments, etc.
2919 %
2920 % \begin{macrocode}
2921 \tl_gput_right:Nn \@kernel@before@begindocument
2922 {
2923 \math_register_env:n { equation }
2924 \math_register_env:n { equation* }
2925 % at the moment register_env can only do display math
2926 % \math_register_env:n { math }
2927 \RenewDocumentEnvironment{math} {b}{$#1$}{}
2928 % and this one doesn't work either
2929 % \math_register_env:n { displaymath }
2930 \RenewDocumentEnvironment{displaymath} {b}{\[#1\]}{}
2931 }
2932 % \end{macrocode}
2933 % \end{macro}
2934 % \end{macro}
2935 %
2936 % \begin{macro}[no-user-doc]{\(, \)}
2937 % If math mode has not been collected, we need to do that; otherwise, worry
2938 % about whether we are in math mode or not. The closing command here can only
2939 % occur inside a collected math block: otherwise it will be simply used as
2940 % a delimiter.
2941 % \begin{macrocode}
2942 \cs_gset_protected:Npn \( % \)
2943 {
2944 \bool_if:NTF \l_@@_collected_bool
2945 {
2946 \mode_if_math:TF
2947 { \@badmath }
2948 { $ }
2949 }
2950 {
2951 \@@_grab_inline:w
2952 }
2953 } % \(
2954 \cs_gset_protected:Npn \)
2955 {
2956 \mode_if_math:TF
2957 { $ }
2958 { \@badmath }
2959 }
2960 % \end{macrocode}
2961 % \end{macro}
2962 %
2963 % \begin{macro}[no-user-doc]{\[, \]}
2964 % Again, we need to watch for when \pkg{amsmath} is loaded after this code.
2965 % The flag usage here is to cover the case where \cs{[}/\cs{]} is hidden
2966 % inside another environment. In this case the grabbing happens on
2967 % the outer level and should not be repeated.
2968 % \begin{macrocode}
2969 \tl_gput_right:Nn \@kernel@before@begindocument
2970 {
2971 \cs_gset_protected:Npn \[ % \]
2972 {
2973 \@@_grab_eqn:w
2974 } % \[
2975 \cs_gset_protected:Npn \]
2976 {
2977 \@badmath
2978 }
2979 }
2980 % \end{macrocode}
2981 % \end{macro}
2982 %
2983 % \subsection{\cs{everymath} and \cs{everydisplay}}
2984 %
2985 % The business end for grabbing inline math and \enquote{raw} \TeX{}
2986 % display. Most display math mode is actually handled elsewhere, as we
2987 % have macro control.
2988 % \begin{macrocode}
2989
2990 \exp_args:No \tex_everymath:D
2991 {
2992 \tex_the:D \tex_everymath:D
2993 \bool_if:NF \l_@@_collected_bool
2994 {
2995 \bool_set_true:N \l_@@_collected_bool
2996 \@@_grab_dollar:w
2997 }
2998 }
2999
3000 \exp_args:No \tex_everydisplay:D
3001 {
3002 \tex_the:D \tex_everydisplay:D
3003 % \typeout{==>~ in~ everydisplay}
3004 % \end{macrocode}
3005 % We need to attach tagging after the display math has been processed by
3006 % \TeX{}, and we also need to correct the penalty and spacing that will get added
3007 % there. This is done by the following code through which we regain
3008 % control after the display math group ends.
3009 % \begin{macrocode}
3010 \group_insert_after:N \@@_tag_dollardollar_display_end:
3011 \bool_if:NF \l_@@_collected_bool
3012 {
3013 \bool_set_true:N \l_@@_collected_bool
3014 \@@_grab_dollardollar:w
3015 }
3016 }
3017 % \end{macrocode}
3018 %
3019 % \subsection{Modifying kernel environments}
3020 %
3021 % We need to cover this even though it is, of course, not encouraged.
3022 % Registration is delayed until begindocument to avoid error with redefinition
3023 % in, e.g., fleqn.clo.
3024 % \begin{macrocode}
3025 \tl_gput_right:Nn \@kernel@before@begindocument
3026 {
3027 \math_register_env:n { eqnarray }
3028 \math_register_env:n { eqnarray* }
3029 }
3030 % \end{macrocode}
3031 %
3032 % In tabulars we do need to change the grabbing method to the slow
3033 % loop-method. \cs{@kernel@tabular@init} is provided in array.sty which is loaded
3034 % by the table code.
3035 % \begin{macrocode}
3036 \providecommand\@kernel@tabular@init{}
3037 \tl_put_right:Nn\@kernel@tabular@init
3038 {
3039 \cs_set_protected:Npn \@@_grab_dollar:w { \@@_grab_loop_init: }
3040 \cs_set_protected:Npn \@@_grab_inline:w { $ }
3041 }
3042 % \end{macrocode}
3043 %
3044 % \begin{macro}[no-user-doc]{\@@_m@th:, \m@th}
3045 % Handle non-math use of math mode. At present nesting isn't supported as
3046 % \cs{m@th} pops up in a few places that \emph{are} math mode!
3047 % \begin{macrocode}
3048 \cs_new_eq:NN \@@_m@th: \m@th
3049 \cs_gset_protected:Npn \m@th
3050 {
3051 \bool_set_true:N \l_@@_collected_bool
3052 \@@_m@th:
3053 }
3054 % \end{macrocode}
3055 % \end{macro}
3056 %
3057 % \subsection{Modifying kernel commands}
3058 %
3059 % \begin{macro}[no-user-doc]{\mathpalette}
3060 % The \cs{mathpalette} command is a problem if lualatex is used and math
3061 % structure elements are created as the math is then processed more than once.
3062 % We therefore redefine it to use \cs{mathstyle}.
3063 %
3064 % \begin{macrocode}
3065 \sys_if_engine_luatex:T
3066 {
3067 \def\mathpalette#1#2{%
3068 \ifcase\mathstyle
3069 #1\displaystyle{#2}\or
3070 #1\displaystyle{#2}\or
3071 #1\textstyle{#2}\or
3072 #1\textstyle{#2}\or
3073 #1\scriptstyle{#2}\or
3074 #1\scriptstyle{#2}\or
3075 #1\scriptscriptstyle{#2}\or
3076 #1\scriptscriptstyle{#2}
3077 \fi}
3078 }
3079 % \end{macrocode}
3080 % \end{macro}
3081 %
3082 %
3083 % \begin{macro}[no-user-doc]{\mathsm@sh}
3084 % Similar to the phantom commands in latex-lab-text, \cs{mathsm@sh} must be
3085 % redefined to include luamml sockets.
3086 % \begin{macrocode}
3087 \def\mathsm@sh#1#2{%
3088 \setbox\z@\hbox{$\m@th#1{#2}
3089 \UseTaggingSocket{math/luamml/save/nNn}{{mathsmash} #1 {mpadded}}
3090 $}%
3091 \UseTaggingSocket{math/luamml/finsm@sh}{}{{}\finsm@sh}}
3092 % \end{macrocode}
3093 % \end{macro}
3094 %
3095 % \begin{macro}[no-user-doc]{\eqnarray}
3096 % \begin{macro}[no-user-doc]{\endeqnarray}
3097 % For MathML generation in eqnarray we also need to add some sockets.
3098 % \changes{0.7c}{2026-05-07}{Add luamml tagging sockets to eqnarray}
3099 % \begin{macrocode}
3100 %<@@=>
3101 \def\eqnarray{%
3102 \stepcounter{equation}%
3103 \def\@currentlabel{\p@equation\theequation}%
3104 \def\@currentcounter{equation}%
3105 \global\@eqnswtrue
3106 \m@th
3107 \global\@eqcnt\z@
3108 \tabskip\@centering
3109 \let\\\@eqncr
3110 \dollardollar@begin\everycr{}\halign to\displaywidth\bgroup
3111 \hskip\@centering
3112 $\displaystyle\tabskip\z@skip{##}%
3113 \UseTaggingSocket{math/luamml/save/nNn}{{}\displaystyle {mtd}}%
3114 $%
3115 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3116 \@eqnsel
3117 &\global\@eqcnt\@ne\hskip \tw@\arraycolsep \hfil${##}%
3118 \UseTaggingSocket{math/luamml/save/nNn}{{}\displaystyle {mtd}}%
3119 $%
3120 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3121 \hfil
3122 &\global\@eqcnt\tw@ \hskip \tw@\arraycolsep $\displaystyle{##}%
3123 \UseTaggingSocket{math/luamml/save/nNn}{{} \displaystyle {mtd}}%
3124 $%
3125 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3126 \hfil\tabskip\@centering
3127 &\global\@eqcnt\thr@@ \hb@xt@\z@\bgroup\hss##\egroup
3128 \UseTaggingSocket{math/luamml/mtable/tag/set}
3129 \tabskip\z@skip
3130 \cr
3131 }
3132 \def\endeqnarray{%
3133 \@@eqncr
3134 \UseExpandableTaggingSocket{math/luamml/mtable/finalize} {eqnarray}%
3135 \egroup
3136 \global\advance\c@equation\m@ne
3137 \dollardollar@end\@ignoretrue
3138 }
3139 \AddToHook{file/fleqn.clo/after}{
3140 \renewenvironment{eqnarray}{%
3141 \stepcounter{equation}%
3142 \def\@currentlabel{\p@equation\theequation}%
3143 \def\@currentcounter{equation}%
3144 \global\@eqnswtrue\m@th
3145 \global\@eqcnt\z@
3146 \tabskip\mathindent
3147 \let\\=\@eqncr
3148 \setlength\abovedisplayskip{\topsep}%
3149 \ifvmode
3150 \addtolength\abovedisplayskip{\partopsep}%
3151 \fi
3152 \addtolength\abovedisplayskip{\parskip}%
3153 \setlength\belowdisplayskip{\abovedisplayskip}%
3154 \setlength\belowdisplayshortskip{\abovedisplayskip}%
3155 \setlength\abovedisplayshortskip{\abovedisplayskip}%
3156 \dollardollar@begin\everycr{}\halign to\linewidth%
3157 \bgroup
3158 \hskip\@centering
3159 $\displaystyle\tabskip\z@skip{##}%
3160 \UseTaggingSocket{math/luamml/save/nNn}{{} \displaystyle {mtd}}%
3161 $%
3162 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3163 \@eqnsel&%
3164 \global\@eqcnt\@ne \hskip \tw@\arraycolsep \hfil${##}%
3165 \UseTaggingSocket{math/luamml/save/nNn}{{} \displaystyle {mtd}}%
3166 $%
3167 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3168 \hfil&%
3169 \global\@eqcnt\tw@ \hskip \tw@\arraycolsep $\displaystyle{##}%
3170 \UseTaggingSocket{math/luamml/save/nNn}{{} \displaystyle {mtd}}%
3171 $%
3172 \UseTaggingSocket{math/luamml/mtable/finalizecol}{last}%
3173 \hfil \tabskip\@centering&%
3174 \global\@eqcnt\thr@@
3175 \hb@xt@\z@\bgroup\hss##\egroup
3176 \UseTaggingSocket{math/luamml/mtable/tag/set}
3177 \tabskip\z@skip\cr
3178 }%
3179 {%
3180 \@@eqncr
3181 \UseExpandableTaggingSocket{math/luamml/mtable/finalize} {eqnarray}%
3182 \egroup
3183 \global\advance\c@equation\m@ne
3184 \dollardollar@end
3185 \@ignoretrue
3186 }
3187 }
3188 %<@@=math>
3189 % \end{macrocode}
3190 % \end{macro}
3191 % \end{macro}
3192 %
3193 % \subsection{Disable math grabbing in the begindocument hook}
3194 % For example amsart uses math to measure text there.
3195 %
3196 % \begin{macrocode}
3197 \tl_gput_right:Nn\@kernel@before@begindocument
3198 {
3199 \bool_set_true:N\l_@@_collected_bool
3200 }
3201 \tl_gput_right:Nn\@kernel@after@begindocument
3202 {
3203 \bool_set_false:N\l_@@_collected_bool
3204 }
3205 % \end{macrocode}
3206 %
3207 %
3208 % \begin{macrocode}
3209 \ExplSyntaxOff
3210 % \end{macrocode}
3211 %
3212 % \begin{macrocode}
3213 %<@@=>
3214 % \end{macrocode}
3215 %
3216 % \begin{macrocode}
3217 %</kernel>
3218 % \end{macrocode}
3219 %
3220 % \Finale
3221 %
3222 %