Debian Codegraph @

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

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