Debian Codegraph @

texmf-dist/source/lualatex/luamml/luamml.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% Copyright (C) 2020-2026 by Marcel Krueger
4 %%
5 %% This file may be distributed and/or modified under the
6 %% conditions of the LaTeX Project Public License, either
7 %% version 1.3c of this license or (at your option) any later
8 %% version. The latest version of this license is in:
9 %%
10 %% http://www.latex-project.org/lppl.txt
11 %%
12 %% and version 1.3 or later is part of all distributions of
13 %% LaTeX version 2005/12/01 or later.
14 %
15 %<*batch>
16 %<*gobble>
17 \ifx\jobname\relax\let\documentclass\undefined\fi
18 \ifx\documentclass\undefined
19 \csname fi\endcsname
20 %</gobble>
21 \input docstrip.tex
22 \keepsilent
23 \generate{
24 \file{luamml.sty}{\from{luamml.dtx}{package,luatex}}
25 \file{luamml-pdf.sty}{\from{luamml.dtx}{package,pdftex}}
26 }
27 \endbatchfile
28 %</batch>
29 %<*gobble>
30 \fi
31 \expandafter\ifx\csname @currname\endcsname\empty
32 \csname fi\endcsname
33 %</gobble>
34 %<*driver>
35 \documentclass{l3doc}
36 \usepackage{luamml}
37 \usepackage{csquotes,luacolor,array}
38 \MakeShortVerb{\|}
39 \RecordChanges
40 \ProvideDocElement[printtype=\textit{socket},idxtype=socket,idxgroup=Sockets]{Socket}{socketdecl}
41 \ProvideDocElement[printtype=\textit{plug},idxtype=plug,idxgroup=Plugs]{Plug}{plugdecl}
42
43 \begin{document}
44 \tracingmathml2
45 \DocInput{luamml.dtx}
46 \PrintIndex
47 \PrintChanges
48 \end{document}
49 %</driver>
50 %<*gobble>
51 \fi
52 %</gobble>
53 % \fi
54 %
55 % \GetFileInfo{luamml.sty}
56 % \title{The \pkg{luamml} package%
57 % \thanks{This document corresponds to \pkg{luamml}~\fileversion, dated~\filedate.}%
58 % }
59 % \author{Marcel Krüger}
60 %
61 % \maketitle
62 %
63 % \begin{documentation}
64 % \section{Use case}
65 % When generating output for the web or tagged output, mathematical content should often be represented as MathML.
66 % This uses Lua\TeX~callbacks to automatically attempt to convert Lua\TeX~math mode output into MathML.
67 %
68 % \section{Usage}
69 % The \pkg{luamml} package is designed to be used in automated ways by other packages and usually should not be invoked directly by the end user.
70 %
71 % Typically you will use it through \pkg{latex-lab-math}. See the \pkg{latex-lab-math} documentation for user instructions.
72 %
73 % If you want to use \pkg{luamml} in another context, the \pkg{luamml} package can be included directly, followed by enclosing blocks which should generate files with \cmd{luamml_begin_single_file:} and \cmd{luamml_end_single_file:}.
74 % The filename can be set with \cmd{luamml_set_filename:n}.
75 %
76 % \section{Improving MathML conversion}
77 % When using constructs which do not automatically get converted in acceptable form, conversion hints can be provided with \cmd{luamml_annotate:en}.
78 % This allows to provide a replacement MathML structure in Lua table form, for example
79 % \begin{verbatim}
80 % \luamml_annotate:en {
81 % nucleus = true,
82 % core = {[0] = 'mi', 'TeX'},
83 % }{
84 % \hbox{\TeX}
85 % }
86 % \end{verbatim}
87 % produces a |<mi>TeX</mi>| element in the output instead of trying to import \TeX~as a mathematical expression.
88 %
89 % It it possible to add a structure around the construct, stash that structure
90 % and then to tell \cmd{luamml_annotate:en} to insert it later inside the math.
91 % For this the keys \texttt{struct} (which takes a label as argument) or \texttt{structnum}
92 % (which takes a structure number) can be used. For example
93 % \begin{verbatim}
94 % $a = b \quad
95 % \tagstructbegin{tag=mtext,stash}\tagmcbegin{}
96 % \luamml_annotate:en{nucleus=true,structnum=\tag_get:n{struct_num}}
97 % {\mbox{some~text~with~\emph{structure}}}
98 % \tagmcend\tagstructend
99 % $
100 % \end{verbatim}
101 % Such a construction should check that the flag for structure elements has actually
102 % been set to avoid orphaned structures if the stashed structure is ignored.
103 %
104 % More about the table structure is explained in an appendix.
105 %
106 % \section{Features \& Limitations}
107 % Currently all mathematical expressions which purely contain Unicode encoded math mode material without embedded non-math should get converted successfully.
108 % Usage with non-Unicode math (\TeX's 8-bit math fonts) is highly experimental and undocumented.
109 % Any attempt to build complicated structures by embedding arbitrary \TeX\ code in the middle of math mode needs to have a MathML replacement specified.
110 % We try to automate more cases in the future.
111 %
112 % \appendix
113 % \input{luamml-algorithm}
114 % \end{documentation}
115 %
116 % \begin{implementation}
117 % \section{Package Implementation}
118 % \subsection{Initialization}
119 % \iffalse
120 %<*package>
121 % \fi
122 % \begin{macrocode}
123 %<@@=luamml>
124 %<*luatex>
125 \ProvidesExplPackage {luamml} {2026-02-27} {0.8.0}
126 {Automatically generate presentational MathML from LuaTeX math expressions}
127 %</luatex>
128 %<*pdftex>
129 \ProvidesExplPackage {luamml-pdf} {2026-02-27} {0.8.0}
130 {MathML generation for L̶u̶a̶pdfLaTeX}
131 %</pdftex>
132 % \end{macrocode}
133 %
134 % \subsection{Initialization}
135 % These variable have to appear before the Lua module is loaded and will be used to
136 % communicate information to the callback.
137 %
138 % Here \cs{tracingmathml} does not use a expl3 name since it is not intended for
139 % programming use but only as a debugging helper for the user.
140 % The other variables are internal, but we provide public interfaces for setting
141 % them later.
142 % \begin{macrocode}
143 \int_new:N \l__luamml_flag_int
144 % \end{macrocode}
145 % This integer decides if the xml uses pretty indentation if output to a file
146 % or to the terminal. It is used in luamml-tex.lua with a bitwise operation.
147 % Indentation in files is activated if bit 0 is set.
148 % Indentation in the log is activated if bit 1 is set.
149 % Bit 2 sets indentation for code added with the \cs{luamml_register_output_hook:N}.
150 % Bit 3 sets indentation for MathML written as stream to the PDF with
151 % \cs{luamml_get_last_mathml_stream:e}.
152 % \begin{macrocode}
153 \int_new:N \l__luamml_pretty_int
154 %<luatex>\tl_new:N \l__luamml_filename_tl
155 \tl_new:N \l__luamml_root_tl
156 \tl_set:Nn \l__luamml_root_tl { mrow }
157 \tl_new:N \l__luamml_label_tl
158 %<pdftex>\int_new:N \g__luamml_formula_id_int
159 %<luatex>\int_new:N \tracingmathml
160
161 \int_set:Nn \l__luamml_pretty_int { 1 }
162 % \end{macrocode}
163 %
164 % Currently we disable flattening in Lua\TeX{} to avoid annotations getting lost.
165 % We will consider later if flattening needs to be reenabled depending, but then
166 % with a different implementation.
167 % \begin{macrocode}
168 %<luatex>\tex_mathflattenmode:D = 0 \scan_stop:
169 % \end{macrocode}
170 %
171 % Now we can load the Lua module which defines the callback.
172 % Of course until pdf\TeX starts implementing \cs{directlua} this is only
173 % done in Lua\TeX.
174 % \begin{macrocode}
175 %<luatex>\lua_now:n { require'luamml-tex' }
176 % \end{macrocode}
177 %
178 % \subsection{Hook}
179 % We also call a hook with arguments at the end of every MathML conversion with the result.
180 % Currently only implemented in Lua\TeX{} since it immediately provides the output.
181 % TODO: the latex-lab-math code registers its own function and so effectly kills the
182 % hook here. This should be sorted \ldots.
183 % \begin{macrocode}
184 %<*luatex>
185 \hook_new_with_args:nn { luamml / converted } { 1 }
186
187 \cs_new_protected:Npn \__luamml_output_hook:n {
188 \hook_use:nnw { luamml / converted } { 1 }
189 }
190 \__luamml_register_output_hook:N \__luamml_output_hook:n
191 %</luatex>
192 % \end{macrocode}
193
194 %
195 % \subsection{Flags}
196 % The most important interface is for setting the flag which controls how the
197 % formulas should be converted.
198 %
199 % \begin{macro}{\luamml_process:}
200 % Consider the current formula to be a complete, free-standing mathematical
201 % expression which should be converted to MathML. Additionally, the formula
202 % is also saved in the \texttt{start\_math} node as with
203 % \cs{luamml_save:}.
204 % \begin{macrocode}
205 \cs_new_protected:Npn \luamml_process: {
206 \tl_set:Nn \l__luamml_label_tl {}
207 \int_set:Nn \l__luamml_flag_int { 3 }
208 }
209 % \end{macrocode}
210 % Temporarly for compatibility
211 % \begin{macrocode}
212 \cs_set_eq:NN \luamml_flag_process: \luamml_process:
213 % \end{macrocode}
214 % \end{macro}
215 %
216 % \begin{macro}{\__luamml_maybe_structelem:}
217 % A internal helper which can be added to a tag to preserve the external state
218 % of the structelem flag.
219 % \begin{macrocode}
220 \cs_new:Npn \__luamml_maybe_structelem: {
221 (
222 8 * \int_mod:nn {
223 \int_div_truncate:nn { \l__luamml_flag_int } {8}
224 } {2}
225 ) +
226 }
227 % \end{macrocode}
228 % \end{macro}
229 %
230 % \begin{macro}{\__luamml_style_to_num:N}
231 % \begin{macrocode}
232 \cs_new:Npn \__luamml_style_to_num:N #1 {
233 %<luatex> 32 * #1
234 %<*pdftex>
235 \token_case_meaning:NnF #1 {
236 \displaystyle {0}
237 \textstyle {32}
238 \scriptstyle {64}
239 \scriptscriptstyle {96}
240 } {
241 \Invalid_mathstyle
242 }
243 %</pdftex>
244 }
245 % \end{macrocode}
246 % \end{macro}
247 %
248 %
249 % \begin{macro}{\luamml_save:n,
250 % \luamml_save:nN,
251 % \luamml_save:nn,
252 % \luamml_save:nNn}
253 % Convert the current formula but only save it's representation in the math
254 % node without emitting it as a complete formula. This is useful when the
255 % expression forms part of a bigger formula and will be integrated into it's
256 % MathML tables later by special code.
257 % It optionally accepts three parameters: A label, one math style command
258 % (\cs{displaystyle}, \cs{textstyle}, etc.) which is the implicit math style
259 % (so the style which the surrounding code expects this style to have) and a
260 % name for the root element (defaults to \texttt{mrow}).
261 % If the root element name is \texttt{mrow}, it will get suppressed in some
262 % cases.
263 % \begin{macrocode}
264 \cs_new_protected:Npn \luamml_save:n #1 {
265 \tl_set:Nn \l__luamml_label_tl {#1}
266 \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 1 }
267 }
268 \cs_new_protected:Npn \luamml_save:nN #1#2 {
269 \tl_set:Nn \l__luamml_label_tl {#1}
270 \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 17 + \__luamml_style_to_num:N #2 }
271 }
272 \cs_new_protected:Npn \luamml_save:nn #1 {
273 \tl_set:Nn \l__luamml_label_tl {#1}
274 \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 5 }
275 \tl_set:Nn \l__luamml_root_tl
276 }
277 \cs_new_protected:Npn \luamml_save:nNn #1#2 {
278 \tl_set:Nn \l__luamml_label_tl {#1}
279 \int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 21 + \__luamml_style_to_num:N #2 }
280 \tl_set:Nn \l__luamml_root_tl
281 }
282 % \end{macrocode}
283 % Temporarly for compatibility
284 % \begin{macrocode}
285 \cs_set_eq:NN \luamml_flag_save:n \luamml_save:n
286 \cs_set_eq:NN \luamml_flag_save:nN \luamml_save:nN
287 \cs_set_eq:NN \luamml_flag_save:nn \luamml_save:nn
288 \cs_set_eq:NN \luamml_flag_save:nNn \luamml_save:nNn
289 % \end{macrocode}
290 % \end{macro}
291 %
292 % \begin{macro}{\luamml_ignore:}
293 % Completely ignore the math mode material.
294 % \begin{macrocode}
295 \cs_new_protected:Npn \luamml_ignore: {
296 \int_set:Nn \l__luamml_flag_int { 0 }
297 }
298 % \end{macrocode}
299 % Temporarly for compatibility
300 % \begin{macrocode}
301 \cs_set_eq:NN \luamml_flag_ignore: \luamml_ignore:
302 % \end{macrocode}
303 % \end{macro}
304 %
305 % \begin{macro}{\luamml_structelem:}
306 % Like \cs{luamml_process:}, but additionally adds PDF structure
307 % elements. This only works in Lua\TeX\ and requires that the \pkg{tagpdf} package
308 % has been loaded \emph{before} \texttt{luamml}.
309 % \begin{macrocode}
310 %<*luatex>
311 \cs_new_protected:Npn \luamml_structelem: {
312 \tl_set:Nn \l__luamml_label_tl {}
313 \int_set:Nn \l__luamml_flag_int { 11 }
314 }
315 % \end{macrocode}
316 % Temporarly for compatibility
317 % \begin{macrocode}
318 \cs_set_eq:NN \luamml_flag_structelem: \luamml_structelem:
319 %</luatex>
320 % \end{macrocode}
321 % \end{macro}
322 %
323 % \begin{macro}{\luamml_set_filename:n}
324 % Allows to set a filename to which the generated MathML gets written.
325 % Previous content from the file will get overwritten. This includes results
326 % written by a previous formula. Therefore this has to be called separately
327 % for every formula or it must expand to different values to be useful.
328 % The value is fully expanded when the file is written.
329 %
330 % Only complete formulas get written into files (so formulas where
331 % \cs{luamml_process:} or \cs{luamml_structelem:} are in effect).
332 %
333 % Only implemented in Lua\TeX, in pdf\TeX\ the arguments for \texttt{pdfmml}
334 % determine the output location.
335 % \begin{macrocode}
336 %<*luatex>
337 \cs_new_protected:Npn \luamml_set_filename:n {
338 \tl_set:Nn \l__luamml_filename_tl
339 }
340 %</luatex>
341 % \end{macrocode}
342 % \end{macro}
343 %
344 % \begin{macro}{\luamml_begin_single_file:, \luamml_end_single_file:}
345 % Everything between these two commands gets written into the same XML file.
346 % The filename is expanded when \cs{luamml_begin_single_file:} gets executed.
347 %
348 % (Implemented in Lua)
349 % \end{macro}
350 %
351 % By default, the flag is set to assume complete formulas.
352 % \begin{macrocode}
353 \luamml_process:
354 % \end{macrocode}
355 %
356 % \subsection{Annotations}
357 % These are implemented very differently depending on the engine, but the interface
358 % should be the same.
359 % \subsubsection{Lua\TeX}
360 % \begin{macrocode}
361 %<*luatex>
362 % \end{macrocode}
363 % \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
364 % A simple annotation scheme: The first argument is the number of top level
365 % noads to be annotated, the second parameter the annotation and the third
366 % parameter the actual list of math tokens. The first argument can be omitted to
367 % let Lua\TeX determine the number itself.
368 %
369 % Passing the first parameter explicitly is useful for any annotations which
370 % should be compatible with future pdf\TeX versions of this functionality.
371 % \begin{macrocode}
372 \cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
373 \__luamml_annotate_begin:
374 #3
375 \__luamml_annotate_end:we \tex_numexpr:D #1 \scan_stop: {#2}
376 }
377
378 \cs_new_protected:Npn \luamml_annotate:en #1#2 {
379 \__luamml_annotate_begin:
380 #2
381 \__luamml_annotate_end:e {#1}
382 }
383 % \end{macrocode}
384 % \end{macro}
385 %
386 % \begin{macro}{\luamml_attribute:nnn, \luamml_attribute_core:nnn}
387 % Set a MathML attribute on a MathML node.
388 % \begin{macrocode}
389 \cs_new_protected:Npn \luamml_attribute:een #1#2#3 {
390 \__luamml_annotate_begin:
391 #3
392 \__luamml_annotate_attribute_end:wee {#1} {#2}
393 }
394
395 \cs_new_protected:Npn \luamml_attribute_core:een #1#2#3 {
396 \__luamml_annotate_begin:
397 #3
398 \__luamml_annotate_attribute_end:wee core {#1} {#2}
399 }
400 % \end{macrocode}
401 % \end{macro}
402 %
403 % \begin{macrocode}
404 %</luatex>
405 % \end{macrocode}
406
407 % \subsubsection{pdf\TeX}
408 % \begin{macrocode}
409 %<*pdftex>
410 % \end{macrocode}
411 % \begin{macro}{\__luamml_pdf_showlists:}
412 % Here and in many other locations the \pdfTeX{} implementation is based on \cs{showlists},
413 % so we define a internal wrapper which sets all relevant parameters.
414 % \begin{macrocode}
415 \cs_if_exist:NTF \showstream {
416 \iow_new:N \l__luamml_pdf_stream
417 \iow_open:Nn \l__luamml_pdf_stream { \jobname .tml }
418 \cs_new_protected:Npn \__luamml_pdf_showlists: {
419 \group_begin:
420 \int_set:Nn \tex_showboxdepth:D { \c_max_int }
421 \int_set:Nn \tex_showboxbreadth:D { \c_max_int }
422 \showstream = \l__luamml_pdf_stream
423 \tex_showlists:D
424 \group_end:
425 }
426 } {
427 \cs_set_eq:NN \l__luamml_pdf_stream \c_log_iow
428 \cs_set_eq:NN \__luamml_pdf_set_showstream: \scan_stop:
429 \cs_new_protected:Npn \__luamml_pdf_showlists: {
430 \group_begin:
431 \int_set:Nn \l_tmpa_int { \tex_interactionmode:D }
432 \int_set:Nn \tex_interactionmode:D { 0 }
433 \int_set:Nn \tex_showboxdepth:D { \c_max_int }
434 \int_set:Nn \tex_showboxbreadth:D { \c_max_int }
435 \tex_showlists:D
436 \int_set:Nn \tex_interactionmode:D { \l_tmpa_int }
437 \group_end:
438 }
439 }
440 % \end{macrocode}
441 % \end{macro}
442 %
443 %
444 % \begin{macro}{\luamml_annotate:nen, \luamml_annotate:en}
445 % Now we can define the annotation commands for pdf\TeX.
446 % \begin{macrocode}
447 \cs_generate_variant:Nn \tl_to_str:n { e }
448 \int_new:N \g__luamml_annotation_id_int
449 \cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
450 \int_gincr:N \g__luamml_annotation_id_int
451 \iow_shipout_x:Nx \l__luamml_pdf_stream {
452 LUAMML_MARK_REF:
453 \int_use:N \g__luamml_annotation_id_int
454 :
455 }
456 \iow_now:Nx \l__luamml_pdf_stream {
457 LUAMML_MARK:
458 \int_use:N \g__luamml_annotation_id_int
459 :
460 count = \int_eval:n {#1},
461 #2
462 \iow_newline:
463 LUAMML_MARK_END
464 }
465 #3
466 }
467 \cs_new_protected:Npn \luamml_annotate:en #1#2 {
468 \int_gincr:N \g__luamml_annotation_id_int
469 \iow_shipout_x:Nx \l__luamml_pdf_stream {
470 LUAMML_MARK_REF:
471 \int_use:N \g__luamml_annotation_id_int
472 :
473 }
474 \iow_now:Nx \l__luamml_pdf_stream {
475 LUAMML_MARK:
476 \int_use:N \g__luamml_annotation_id_int
477 :
478 count = data.count[\int_use:N \g__luamml_annotation_id_int],
479 #1
480 \iow_newline:
481 LUAMML_MARK_END
482 }
483 \use:x {
484 \iow_now:Nn \l__luamml_pdf_stream {
485 LUAMML_COUNT:
486 \int_use:N \g__luamml_annotation_id_int
487 }
488 \__luamml_pdf_showlists:
489 \exp_not:n {#2}
490 \iow_now:Nn \l__luamml_pdf_stream {
491 LUAMML_COUNT_END:
492 \int_use:N \g__luamml_annotation_id_int
493 }
494 \__luamml_pdf_showlists:
495 }
496 }
497 % \end{macrocode}
498 % \end{macro}
499 %
500 % \begin{macrocode}
501 %</pdftex>
502 % \end{macrocode}
503 %
504 % \subsection{Trigger for specific formula}
505 % This only applies for pdf\TeX\ since in Lua\TeX\ everything is controlled by the callback,
506 % but for compatibility the function is defined anyway.
507 %
508 % \begin{macro}{\luamml_pdf_write:}
509 % We could accept parameters for the flag and tag here, but for compatibility
510 % with Lua\TeX they are passed in macros instead.
511 % \begin{macrocode}
512 %<*pdftex>
513 \cs_new_protected:Npn \luamml_pdf_write: {
514 \int_gincr:N \g__luamml_formula_id_int
515 \iow_now:Nx \l__luamml_pdf_stream {
516 LUAMML_FORMULA_BEGIN:
517 \int_use:N \g__luamml_formula_id_int
518 :
519 \int_use:N \l__luamml_flag_int
520 :
521 \l__luamml_root_tl
522 :
523 \l__luamml_label_tl
524 }
525 \__luamml_pdf_showlists:
526 \iow_now:Nx \l__luamml_pdf_stream {
527 LUAMML_FORMULA_END
528 }
529 }
530 %</pdftex>
531 %<luatex>\cs_new_eq:NN \luamml_pdf_write: \scan_stop:
532 % \end{macrocode}
533 % \end{macro}
534 %
535 % \begin{macrocode}
536 % \end{macrocode}
537 %
538 % \subsection{Further helpers}
539 %
540 % \begin{macro}{\RegisterFamilyMapping}
541 % The Lua version of this is defined in the Lua module.
542 % \begin{macrocode}
543 %<*pdftex>
544 \NewDocumentCommand \RegisterFamilyMapping {m m} {
545 \iow_now:Nx \l__luamml_pdf_stream {
546 LUAMML_INSTRUCTION:REGISTER_MAPPING: \int_use:N #1 : #2
547 }
548 }
549 %</pdftex>
550 % \end{macrocode}
551 % \end{macro}
552 %
553 % \subsection{Sockets}
554 % In various places luamml has to add code to kernel commands. This is done through
555 % tagging sockets which are predeclared in lttagging.
556 %
557 % \begin{tabular}{>{\ttfamily}ll}
558 % name & number of argument\\\hline
559 % math/luamml/save/nNn & 1\\
560 % math/luamml/save/nn & 1\\
561 % math/luamml/annotate/false & 2\\
562 % math/luamml/array/save & 0\\
563 % math/luamml/array/finalize &0\\
564 % math/luamml/array/initcol & 0\\
565 % math/luamml/array/finalizecol & 1\\
566 % math/luamml/mtable/finalizecol & 1\\
567 % math/luamml/mtable/innertable/save&0\\
568 % math/luamml/mtable/smallmatrix/save&0\\
569 % math/luamml/mtable/innertable/finalize&0\\
570 % math/luamml/mtable/finalize&1\\
571 % math/luamml/mtable/aligncol&1\\
572 % math/luamml/mtable/tag/save&0\\
573 % math/luamml/mtable/tag/set&0\\
574 % math/display/tag/begin&0\\
575 % math/display/tag/end&0\\
576 % math/luamml/hbox&2\\
577 % math/luamml/artifact&0\\
578 % math/luamml/finph@nt&2\\
579 % math/luamml/finsm@sh&2\\
580 % \end{tabular}
581 %
582 % \subsubsection{Save sockets}
583 % These sockets are wrappers around the \cs{luamml_save:...} commands
584 %
585 % \begin{macrocode}
586 \NewTaggingSocketPlug{math/luamml/save/nNn}{luamml}
587 {
588 \luamml_save:nNn #1
589 }
590 \AssignTaggingSocketPlug{math/luamml/save/nNn}{luamml}
591 \NewTaggingSocketPlug{math/luamml/save/nn}{luamml}
592 {
593 \luamml_save:nn #1
594 }
595 \AssignTaggingSocketPlug{math/luamml/save/nn}{luamml}
596 % \end{macrocode}
597 %
598 % \subsubsection{sockets to annotate content}
599 %
600 % \begin{macrocode}
601 %<*luatex>
602 \NewTaggingSocketPlug{math/luamml/annotate/false}{luamml}
603 {
604 \luamml_annotate:en { core = false }
605 {
606 #2
607 }
608 }
609 \AssignTaggingSocketPlug{math/luamml/annotate/false}{luamml}
610 %</luatex>
611 % \end{macrocode}
612 % \subsubsection{socket plugs for the array package}
613 %
614 % The luamml support makes only sense with luatex.
615 % \begin{macrocode}
616 %<*luatex>
617 \AddToHook{package/array/after}{\lua_now:n { require'luamml-array' }}
618 % \end{macrocode}
619 % \begin{plugdecl}{tagsupport/math/luamml/array/save}
620 % The socket of this plug is used in \cs{endarray}.
621 % \begin{macrocode}
622 \NewTaggingSocketPlug{math/luamml/array/save}{luamml}
623 {
624 \__luamml_array_save_array:
625 }
626 % \end{macrocode}
627 % \end{plugdecl}
628 %
629 % \begin{plugdecl}{tagsupport/math/luamml/array/finalize}
630 % This socket of this plug is used in \cs{endarray}.
631 % \begin{macrocode}
632 \NewTaggingSocketPlug{math/luamml/array/finalize}{luamml}
633 {
634 \mode_if_math:T { \__luamml_array_finalize_array: }
635 }
636 % \end{macrocode}
637 % \end{plugdecl}
638 %
639 % \begin{plugdecl}{tagsupport/math/luamml/array/initcol}
640 % The socket of this plug is used in \cs{@classz}.
641 % \begin{macrocode}
642 \NewTaggingSocketPlug{math/luamml/array/initcol}{luamml}
643 {
644 \__luamml_array_init_col:
645 }
646 % \end{macrocode}
647 % \end{plugdecl}
648 %
649 %
650 % \begin{plugdecl}{tagsupport/math/luamml/array/finalizecol}
651 % The socket of this plug is used in \cs{@classz}.
652 % \begin{macrocode}
653 \NewTaggingSocketPlug{math/luamml/array/finalizecol}{luamml}
654 {
655 \__luamml_array_finalize_col:w #1~
656 }
657 % \end{macrocode}
658 % \end{plugdecl}
659 % \begin{macrocode}
660 \AssignTaggingSocketPlug{math/luamml/array/save}{luamml}
661 \AssignTaggingSocketPlug{math/luamml/array/finalize}{luamml}
662 \AssignTaggingSocketPlug{math/luamml/array/initcol}{luamml}
663 \AssignTaggingSocketPlug{math/luamml/array/finalizecol}{luamml}
664 %</luatex>
665 % \end{macrocode}
666 % \subsubsection{amsmath alignments}
667 %
668 % This socket is used at the end of alignment cells and adds the content to
669 % the current row.
670 %
671 % \begin{macrocode}
672 %<*luatex>
673 \NewTaggingSocketPlug{math/luamml/mtable/finalizecol}{luamml}
674 {
675 \use:c{__luamml_amsmath_add_#1_to_row:}
676 }
677 \AssignTaggingSocketPlug{math/luamml/mtable/finalizecol}{luamml}
678
679 %</luatex>
680 % \end{macrocode}
681 %
682 % These sockets save an inner table
683 %
684 % \begin{macrocode}
685 %<*luatex>
686 \NewTaggingSocketPlug{math/luamml/mtable/innertable/save}{luamml}
687 {
688 \__luamml_amsmath_save_inner_table:n \@currenvir
689 }
690 \AssignTaggingSocketPlug{math/luamml/mtable/innertable/save}{luamml}
691 \NewTaggingSocketPlug{math/luamml/mtable/smallmatrix/save}{luamml}
692 {
693 \__luamml_amsmath_save_smallmatrix:
694 }
695 \AssignTaggingSocketPlug{math/luamml/mtable/smallmatrix/save}{luamml}
696 \NewTaggingSocketPlug{math/luamml/mtable/innertable/finalize}{luamml}
697 {
698 \__luamml_amsmath_finalize_inner_table:
699 }
700 \AssignTaggingSocketPlug{math/luamml/mtable/innertable/finalize}{luamml}
701 %</luatex>
702 % \end{macrocode}
703 %
704 %
705 % This socket finalize the \texttt{mtable} in alignments like align or gather.
706 % It takes an argument, the environment.
707 % It should be used normally with \cs{UseExpandableTaggingSocket}.
708 %
709 % \begin{macrocode}
710 %<*luatex>
711 \NewTaggingSocketPlug{math/luamml/mtable/finalize}{luamml}
712 {
713 \__luamml_amsmath_finalize_table:n {#1}
714 }
715 \AssignTaggingSocketPlug{math/luamml/mtable/finalize}{luamml}
716 %</luatex>
717 % \end{macrocode}
718 %
719 % This socket adds attributes for the alignment in \texttt{multline}.
720 % It takes an argument, the alignment.
721 %
722 % \begin{macrocode}
723 %<*luatex>
724 \NewTaggingSocketPlug{math/luamml/mtable/aligncol}{luamml}
725 {
726 \__luamml_amsmath_set_row_columnalign:n {#1}
727 }
728 \AssignTaggingSocketPlug{math/luamml/mtable/aligncol}{luamml}
729 %</luatex>
730 % \end{macrocode}
731
732 %
733 % \subsubsection{Tags and labels}
734 % These sockets save and set tags and labels in alignments.
735 %
736 % \begin{macrocode}
737 %<*luatex>
738 \int_new:N \l__luamml_amsmath_tag_struct_num_int
739 \NewTaggingSocketPlug{math/luamml/mtable/tag/save}{luamml}
740 {
741 \__luamml_amsmath_save_tag_with_struct_elem:N \l__luamml_amsmath_tag_struct_num_int
742 }
743 \AssignTaggingSocketPlug{math/luamml/mtable/tag/save}{luamml}
744 \NewTaggingSocketPlug{math/luamml/mtable/tag/set}{luamml}
745 {
746 \__luamml_amsmath_set_tag:
747 }
748 \AssignTaggingSocketPlug{math/luamml/mtable/tag/set}{luamml}
749
750 %</luatex>
751 % \end{macrocode}
752 %
753 % If math structure elements are created the Lbl-structure of a tag
754 % must be moved inside the math structure, typically as an additional column in an
755 % \texttt{mtable} with an intent \texttt{:equation-label} or \texttt{:no-equation-label}.
756 %
757 % The luamml-code handles this by stashing the Lbl-structure, storing the
758 % structure number in an array and reusing it once it creates the math structure elements.
759 %
760 % This should only be done for specific environments, we define
761 % a constant to test:
762 %
763 % \begin{macrocode}
764 %<*luatex>
765 \clist_map_inline:nn
766 {
767 eqnarray,
768 eqnarray*,
769 align,
770 align*,
771 alignat,
772 alignat*,
773 xalignat,
774 xalignat*,
775 % \end{macrocode}
776 % there is never a tag/label in xxalignat, so does it make sense to add a label column?
777 % Left out for now.
778 % \begin{macrocode}
779 %xxalignat,
780 flalign,
781 flalign*,
782 gather,
783 gather*,
784 % \end{macrocode}
785 % multline have at most one tag, so we do not use a label column
786 % but rely on the external Lbl for now.
787 % \begin{macrocode}
788 %multline, % NO
789 %multline*, % NO
790 equation,
791 equation*,
792 % \end{macrocode}
793 % split has never a numbering so is ignored
794 % \begin{macrocode}
795 %split, % NO
796 }
797 {\tl_const:cn { c__luamml_label_#1_tl}{}}
798 % \end{macrocode}
799 %
800 %
801 % \begin{macrocode}
802 \NewTaggingSocketPlug{math/display/tag/begin}{luamml}
803 {
804 \tag_mc_end:
805 \bool_lazy_and:nnT
806 { \tl_if_exist_p:c { c__luamml_label_ \@currenvir _tl } }
807 { \int_if_odd_p:n { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } } }
808 {
809 \tag_struct_begin:n {tag=mtext,stash}
810 \int_set:Nn \l__luamml_amsmath_tag_struct_num_int { \tag_get:n {struct_num} }
811 }
812 \tag_mc_begin:n {}
813 }
814 \AssignTaggingSocketPlug{math/display/tag/begin}{luamml}
815 \NewTaggingSocketPlug{math/display/tag/end}{luamml}
816 {
817 \tag_mc_end:
818 \bool_lazy_and:nnT
819 { \tl_if_exist_p:c { c__luamml_label_ \@currenvir _tl } }
820 { \int_if_odd_p:n { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } } }
821 {
822 \tag_struct_end:
823 }
824 \tag_mc_begin:n {}
825 }
826 \AssignTaggingSocketPlug{math/display/tag/end}{luamml}
827 %</luatex>
828 % \end{macrocode}
829 %
830 %
831 % \subsubsection{Horizontal boxes}
832 % This socket annotates an \cs{hbox} inside box commands used in math.
833 % We test for the socket until the release 2025-06-01.
834 % \begin{macrocode}
835 %<*luatex>
836 \NewTaggingSocketPlug{math/luamml/hbox}{luamml}
837 {
838 \bool_lazy_and:nnTF
839 { \mode_if_math_p: }
840 { \int_if_odd_p:n { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } } }
841 {
842 \tag_struct_begin:n
843 {
844 tag=mtext,
845 stash,
846 }
847 \tag_mc_begin:n {}
848 \luamml_annotate:en
849 {
850 nucleus = true,
851 structnum=\tag_get:n{struct_num}
852 }
853 { #2 }
854 \tag_mc_end:
855 \tag_struct_end:
856 }
857 { #2 }
858 }
859 \AssignTaggingSocketPlug{math/luamml/hbox}{luamml}
860 %</luatex>
861 % \end{macrocode}
862 %
863 % \subsubsection{Artifact characters}
864 % Unicode characters like a root sign should be marked as artifacts
865 % to avoid duplication e.g. in derivation if mathml
866 % structure elements are used that imply the meaning.
867 % \begin{macrocode}
868 %<*luatex>
869 \NewTaggingSocketPlug{math/luamml/artifact}{luamml}
870 {
871 \int_if_odd:nT { \int_div_truncate:nn { \l__luamml_flag_int } { 8 } }
872 {
873 \tag_mc_begin:n{artifact}
874 }
875 }
876 \AssignTaggingSocketPlug{math/luamml/artifact}{luamml}
877 %</luatex>
878 % \end{macrocode}
879 %
880 % \subsubsection{Math phantom socket}
881 % This socket is used around \cs{finph@nt}.
882 %
883 % \begin{macrocode}
884 %<*luatex>
885 \NewTaggingSocketPlug{math/luamml/finph@nt}{luamml}
886 {
887 \luamml_annotate:nen {1}
888 {
889 nucleus = true,
890 mathml =
891 {
892 [0] = 'mpadded',
893 \ifh@\else
894 width = 0,
895 \fi
896 \ifv@\else
897 height = 0, depth = 0,
898 \fi
899 consume_label'mathphant',
900 },
901 }
902 { #2 }
903 }
904 \AssignTaggingSocketPlug{math/luamml/finph@nt}{luamml}
905 %</luatex>
906 % \end{macrocode}
907
908 % \subsubsection{Math smash socket}
909 % This socket is used around \cs{finsm@sh}.
910 %
911 % \begin{macrocode}
912 %<*luatex>
913 \NewTaggingSocketPlug{math/luamml/finsm@sh}{luamml}
914 {
915 \luamml_annotate:nen {2}
916 {
917 nucleus = true,
918 mathml = consume_label('mathsmash',
919 function(padded, core)
920 padded.height, padded.depth = 0, 0~
921 return~padded, core~
922 end),
923 }
924 { #2 }
925 }
926 \AssignTaggingSocketPlug{math/luamml/finsm@sh}{luamml}
927 %</luatex>
928 % \end{macrocode}
929 % \subsection{Patching}
930 % For some packages, we ship with patches to make them more compatible and to
931 % demonstrate how other code can be patched to work with \texttt{luamml}.
932 %
933 % These are either loaded directly if the packages are loaded or delayed using
934 % \LaTeX's hook system otherwise.
935 % \begin{macro}{\__luamml_patch_package:nn, \__luamml_patch_package:n}
936 % For this, we use two helpers: First a wrapper which runs arbitrary code either
937 % now (if the package is already loaded) or as soon as the package loads, second
938 % an application of the first one to load packages following \texttt{luamml}'s
939 % naming scheme for these patch packages.
940 % \begin{macrocode}
941 \cs_new_protected:Npn \__luamml_patch_package:nn #1 #2 {
942 \@ifpackageloaded {#1} {#2} {
943 \hook_gput_code:nnn {package/#1/after} {luamml} {#2}
944 }
945 }
946 \cs_new_protected:Npn \__luamml_patch_package:n #1 {
947 \__luamml_patch_package:nn {#1} {
948 \RequirePackage { luamml-patches-#1 }
949 }
950 }
951 % \end{macrocode}
952 % \end{macro}
953 %
954 % We currently provide minimal patching for the kernel, \pkg{amsmath}.
955 % Currently only the kernel code supports pdf\TeX, but it's planned to extend this.
956 % \begin{macrocode}
957 \RequirePackage { luamml-patches-kernel }
958 %<*luatex>
959 \__luamml_patch_package:n {amsmath}
960 %</luatex>
961 % \end{macrocode}
962
963 % \iffalse
964 %</package>
965 % \fi
966 % \end{implementation}
967 % \Finale