Debian Codegraph @

texmf-dist/tex/latex/latex-lab/latex-lab-math.ltx

raw
1 %%
2 %% This is file `latex-lab-math.ltx',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% latex-lab-math.dtx (with options: `kernel')
8 %%
9 %% This is a generated file.
10 %%
11 %% Copyright 2021-2025 LaTeX Project
12 %%
13 %% This file was generated from file(s) of the `LaTeX-lab Bundle'.
14 %% ------------------------------------------------------------------------------------
15 %%
16 %% It may be distributed and/or modified under the
17 %% conditions of the LaTeX Project Public License, either version 1.3c
18 %% of this license or (at your option) any later version.
19 %% The latest version of this license is in
20 %% http://www.latex-project.org/lppl.txt
21 %% and version 1.3c or later is part of all distributions of LaTeX
22 %% version 2008 or later.
23 %%
24 %% This file may only be distributed together with a copy of the LaTeX
25 %% `LaTeX-lab Bundle'. You may however distribute the `LaTeX-lab Bundle'
26 %% without such generated files.
27 %%
28 %% The newest sources can be found below
29 %%
30 %% https://github.com/latex3/latex2e/required/latex-lab
31 %%
32 %% where one can also log issues in case there are any.
33 %%
34 %%
35 %% File: latex-lab-math.dtx
36
37 \def\ltlabmathdate{2025-10-02}
38 \def\ltlabmathversion{0.6w}
39 \ProvidesFile{latex-lab-math.ltx}
40 [\ltlabmathdate\space
41 v\ltlabmathversion\space
42 Grab all the math(s) and tag it (experiments)]
43 \AddToHook{begindocument/before}{\RequirePackage{latex-lab-testphase-block}}
44 \ExplSyntaxOn
45 \AddToHook{begindocument/before}{ \RequirePackage { amsmath } }
46 \bool_new:N \g__math_debug_bool
47 \cs_new_eq:NN \__math_debug:n \use_none:n
48 \cs_new_eq:NN \__math_debug_typeout:n \use_none:n
49 \cs_new_protected:Npn \math_debug_on:
50 {
51 \bool_gset_true:N \g__math_debug_bool
52 \__math_debug_gset:
53 }
54 \cs_new_protected:Npn \math_debug_off:
55 {
56 \bool_gset_false:N \g__math_debug_bool
57 \__math_debug_gset:
58 }
59 \cs_new_protected:Npn \__math_debug_gset:
60 {
61 \cs_gset_protected:Npe \__math_debug:n ##1
62 { \bool_if:NT \g__math_debug_bool {##1} }
63 \cs_gset_protected:Npe \__math_debug_typeout:n ##1
64 { \bool_if:NT \g__math_debug_bool { \typeout{[Math]~ ==>~ ##1} } }
65 }
66 \cs_new_protected:Npn \DebugMathOn { \math_debug_on: }
67 \cs_new_protected:Npn \DebugMathOff { \math_debug_off: }
68 \DebugMathOff
69 \bool_new:N \l__math_collected_bool
70 \bool_new:N \l__math_fakemath_bool
71 \tl_new:N \g__math_grabbed_env_tl
72 \tl_new:N \g__math_grabbed_math_tl
73 \tl_new:N \l__math_tmpa_tl
74 \skip_new:N \l__math_tmpa_skip
75 \str_new:N \l__math_tmpa_str
76 \tl_new:N \l__math_content_alt_tl
77 \tl_new:N \l__math_content_actual_tl
78 \tl_new:N \l__math_content_AF_source_tl
79 \tl_new:N \l__math_content_AF_source_tmpa_tl
80 \tl_new:N \l__math_content_AF_mathml_tl
81 \cs_new_protected:Npn \__tag_tool_close_P:
82 {
83 \tag_if_active:T
84 {
85 \tag_mc_end: %end P-chunk, should perhaps be \tag_mc_end_push: ...
86 \__tag_gincr_para_end_int:
87 \__tag_check_para_end_show:nn{red}{} %debug: show para
88 \tag_struct_end:
89 }
90 }
91 \tl_new:N\l__math_attribute_class_tl
92 \tagpdfsetup
93 {role/new-attribute = {inline} {/O /Layout /Placement/Inline},
94 role/new-attribute = {display} {/O /Layout /Placement/Block},
95 }
96 \bool_new:N\l__tag_math_texsource_AF_bool
97 \bool_new:N\l__tag_math_texsource_pane_bool
98 \bool_new:N\l__tag_math_mathml_AF_bool
99 \bool_new:N\g__tag_math_mathml_AF_bool
100 \bool_new:N\l__tag_math_mathml_pane_bool
101 \bool_new:N\l__tag_math_alt_bool
102 \tl_new:N\g__tag_math_luamml_tl
103 \tl_gset:Nn\g__tag_math_luamml_tl {0}
104 \int_new:N\g__math_mathml_total_int
105 \int_new:N\g__math_mathml_int
106 \int_new:N\g__math_math_total_int
107 \int_new:N\g__math_mathml_AF_found_int
108 \int_new:N\g__math_mathml_AF_attached_int
109 \clist_new:N\l__tag_math_mathml_files_clist
110 \clist_put_right:Ne\l__tag_math_mathml_files_clist
111 {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
112 \cs_new_protected:Npn \__math_AF_mml:nnnn #1 #2 #3 #4
113 {
114 \int_gincr:N \g__math_mathml_total_int
115 \tl_if_exist:cF { g__math_mathml_#3_tl }
116 {
117 \int_gincr:N \g__math_mathml_int
118 \pdfdict_put:nne {l_pdffile/Filespec} {Desc}{(mathml-#1)}
119 \pdffile_embed_stream:nnN {#4}{mathml-#1.xml}\l__math_tmpa_tl
120 \bool_if:NT \l__tag_math_mathml_pane_bool
121 {\pdfmanagement_add:nne {Catalog/Names}{EmbeddedFiles}{\l__math_tmpa_tl}}
122 \tl_new:c{g__math_mathml_#3_tl}
123 \tl_gset_eq:cN{g__math_mathml_#3_tl}\l__math_tmpa_tl
124 }
125 }
126 \cs_new_protected:Npn \__math_AF_html_reader:w#1</h2>#2<p>#3</p>#4<p>#5</p>#6<math{
127 \begingroup
128 \char_set_catcode_other:N\{
129 \char_set_catcode_other:N\}
130 \char_set_catcode_other:N\#
131 \char_set_catcode_other:N\%
132 \char_set_catcode_other:N\^
133 \__math_AF_html_reader_verb:w{#1}{#3}{#5}<math
134 }
135 \cs_new_protected:Npn\__math_AF_html_reader_verb:w#1#2#3#4~</div>{
136 \endgroup
137 \__math_AF_mml:nnnn{#1}{#2}{#3}{#4}
138 }
139 \tl_const:Nn \c__math_mathml_write_init_tl
140 {
141 <!DOCTYPE~html>
142 \iow_newline:
143 <html~ xmlns="http://www.w3.org/1999/xhtml">
144 \iow_newline:
145 }
146 \tl_new:N \l__math_mathml_write_before_tl
147 \tl_const:Nn \c__math_mathml_write_after_tl
148 {
149 \iow_newline:
150 </div>
151 \iow_newline:
152 }
153 \tl_const:Nn \c__math_mathml_write_final_tl
154 {
155 </html>
156 }
157
158 \NewTaggingSocket{math/mathml/write/prepare}{0}
159 \NewTaggingSocketPlug{math/mathml/write/prepare}{On}
160 {
161 \str_set:NV\l__math_tmpa_str\l__math_content_AF_source_tl
162 \str_replace_all:Nnn\l__math_tmpa_str{&}{&amp;}
163 \str_replace_all:Nnn\l__math_tmpa_str{<}{&lt;}
164 \tl_set:Nn \l__math_mathml_write_before_tl
165 {
166 <div>
167 \iow_newline:
168 <h2>\c_backslash_str mml\c_space_tl \int_use:N \g__math_math_total_int </h2>
169 \iow_newline:
170 <p>\l__math_tmpa_str</p>
171 \iow_newline:
172 <p>\l__math_content_hash_tl </p>
173 \iow_newline:
174 }
175 }
176 \cs_new_protected:Npn\__math_provide_luamml_commands:
177 {
178 \providecommand\luamml_flag_structelem:{}
179 \cs_if_free:NT \luamml_structelem:
180 {
181 \cs_set_eq:NN\luamml_structelem:\luamml_flag_structelem:
182 }
183 \providecommand\luamml_flag_process:{}
184 \cs_if_free:NT \luamml_process:
185 {
186 \cs_set_eq:NN\luamml_process:\luamml_flag_process:
187 }
188 \providecommand\luamml_flag_ignore:{}
189 \cs_if_free:NT \luamml_ignore:
190 {
191 \cs_set_eq:NN\luamml_ignore:\luamml_flag_ignore:
192 }
193 }
194 \sys_if_engine_luatex:TF
195 {
196 \AddToHook{begindocument/before}
197 {
198 \str_case:on \g__math_luamml_load_tl
199 {
200 { 1 } {
201 \RequirePackage { luamml }
202 \AddToHook{begindocument/end}
203 {
204 \__math_luamml_activate_write:
205 }
206 }
207 {-1 } {
208 \AddToHook{begindocument/end}
209 {
210 \msg_note:nnnn { tag }
211 { luamml-status }{ disabled }{ not~create }
212 }
213 }
214 { 0 }
215 {
216 \@ifpackageloaded { unicode-math }
217 {
218 \RequirePackage { luamml }
219 \AddToHook{begindocument/end}
220 {
221 \__math_luamml_activate_write:
222 }
223 }
224 { \msg_warning:nn { tag }{ unicode-math-missing } }
225 }
226 }
227 \__math_provide_luamml_commands:
228 }
229 }
230 {
231 \AddToHook{begindocument/before}
232 {
233 \__math_provide_luamml_commands:
234 }
235 }
236 \msg_new:nnn { tag }{ luamml-status }
237 {
238 luamml~has~been~#1~and~will~#2~an~MathML~file.
239 }
240
241 \msg_new:nnn { tag }{ unicode-math-missing }
242 {
243 The~package~unicode-math~is~missing\\
244 luamml~will~not~create~an~MathML~file.\\
245 To~avoid~this~warning~load~unicode-math~\\
246 or~disable~luamml~with~\\
247 \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=false}}\\
248 or~force~luamml~with~\\
249 \tl_to_str:n{\tagpdfsetup{math/mathml/luamml/load=true}}
250 }
251 \cs_new_protected:Npn \__math_luamml_activate_write:
252 {
253 \bool_if:NT \g__math_luamml_write_bool
254 {
255 \bool_gset_true:N\g__tag_math_mathml_AF_bool
256 \AssignTaggingSocketPlug{math/struct/begin}{mathml-AF}
257 \AssignTaggingSocketPlug{math/struct/end}{mathml-AF}
258 \int_set:Nn \l__luamml_pretty_int { 7 }
259 \RegisterFamilyMapping\symsymbols{oms}
260 \RegisterFamilyMapping\symletters{oml}
261 \AssignTaggingSocketPlug{math/mathml/write/prepare}{On}
262 \iow_new:N \g__math_luamml_iow
263 \iow_open:Nn \g__math_luamml_iow {\c_sys_jobname_str-luamml-mathml.html}
264 \iow_now:Ne \g__math_luamml_iow { \c__math_mathml_write_init_tl }
265 \cs_new:Npn \__math_luamml_output_hook:n ##1
266 {
267 \tl_if_empty:NF \l__math_mathml_write_before_tl
268 {
269 \int_compare:nNnT
270 { \@math@level } = { 1 }
271 {
272 \iow_now:Ne \g__math_luamml_iow
273 {
274 \l__math_mathml_write_before_tl
275 ##1
276 \c__math_mathml_write_after_tl
277 }
278 }
279 }
280 }
281 \__luamml_register_output_hook:N \__math_luamml_output_hook:n
282 \AddToHook{enddocument/afterlastpage}
283 {
284 \iow_now:Ne \g__math_luamml_iow
285 { \c__math_mathml_write_final_tl }
286 \iow_close:N \g__math_luamml_iow
287 }
288 \msg_note:nnnn { tag }
289 { luamml-status }{ enabled }{ create }
290 }
291 }
292 \cs_set_protected:Npn \UseMathForPositioningText $ {
293 \bool_if:NTF \l__math_collected_bool
294 { $ }
295 {
296 \bool_set_true:N \l__math_collected_bool
297 $
298 \bool_set_false:N \l__math_collected_bool
299 \int_decr:N \@math@level
300 }
301 }
302 \def\@textsuperscript#1{%
303 {\m@th\@unreal@math{^{\mbox{\fontsize\sf@size\sf@size#1}}}}}
304 \def\@textsubscript#1{%
305 {\m@th\@unreal@math{\sb{\mbox{\fontsize\sf@size\sf@size#1}}}}}
306 \protected\def\@unreal@math{%
307 \ifmmode
308 \expandafter\@firstofone
309 \else
310 \expandafter\@ensured@unreal@math
311 \fi}
312 \long\def\@ensured@unreal@math#1{
313 $
314 \bool_set_false:N \l__math_collected_bool
315 \int_decr:N \@math@level
316 #1 $
317 }
318 \tl_new:N \g__math_luamml_load_tl
319 \tl_gset:Nn \g__math_luamml_load_tl {0}
320 \bool_new:N \g__math_luamml_write_bool
321 \bool_gset_true:N \g__math_luamml_write_bool
322 \cs_new:Npn\__math_luamml_structelem:{}
323 \cs_new:Npn\__math_luamml_ignore:{}
324 \msg_new:nnn { tag }{ PDF-2.0-recommended }
325 {
326 The~key~#1~will~not~work~properly~with~PDF~#2.\\
327 Switching~to~PDF~2.0~is~recommended.
328 }
329 \keys_define:nn { __tag / setup }
330 {
331 math/mathml/luamml/load .choice: ,
332 math/mathml/luamml/load/true .code:n = {\tl_gset:Nn \g__math_luamml_load_tl{1}},
333 math/mathml/luamml/load/false .code:n = {\tl_gset:Nn \g__math_luamml_load_tl{-1}},
334 math/mathml/luamml/load .default:n = true,
335 math/mathml/luamml/load .usage:n=preamble,
336 math/mathml/structelem .choice:,
337 math/mathml/structelem/true .code:n =
338 {
339 \pdf_version_compare:NnT < {2.0}
340 {
341 \msg_warning:nnne { tag }{ PDF-2.0-recommended }
342 { math/mathml/structelem }{ \pdf_version: }
343 }
344 \cs_set:Npn\__math_luamml_structelem:{\luamml_structelem:}
345 \cs_set:Npn\__math_luamml_ignore:{\luamml_ignore:}
346 },
347 math/mathml/structelem/false .code:n =
348 {
349 \cs_set_eq:NN\__math_luamml_structelem:\prg_do_nothing:
350 \cs_set_eq:NN\__math_luamml_ignore:\prg_do_nothing:
351 },
352 math/mathml/structelem .default:n = true,
353 math/mathml/ignore .code:n = {\luamml_ignore:},
354 math/mathml/luamml/write .choice:,
355 math/mathml/luamml/write/true .code:n =
356 {
357 \tl_gset:Nn \g__math_luamml_load_tl{1}
358 \bool_gset_true:N \g__math_luamml_write_bool
359 },
360 math/mathml/luamml/write/false .code:n =
361 {
362 \bool_gset_false:N \g__math_luamml_write_bool
363 },
364 math/mathml/luamml/write .default:n = true,
365 math/mathml/luamml/write .usage:n=preamble,
366 math/mathml/luamml .bool_gset:N = \g__math_luamml_write_bool,
367 math/mathml/luamml .usage:n=preamble
368 }
369 \NewTaggingSocket{math/mathml/write}{0}
370 \NewTaggingSocketPlug{math/mathml/write}{On}
371 {
372 \iow_now:Ne \g__math_writedummy_iow
373 {
374 \l__math_mathml_write_before_tl
375 <math~ xmlns="http://www.w3.org/1998/Math/MathML"></math>
376 \c__math_mathml_write_after_tl
377 }
378 }
379
380 \keys_define:nn { __tag / setup }
381 {
382 math/mathml/write-dummy .code:n =
383 {
384 \bool_gset_true:N \g__tag_math_mathml_AF_bool
385 \tl_if_exist:NF\g__math_writedummy_iow
386 {
387 \iow_new:N \g__math_writedummy_iow
388 \iow_open:Nn \g__math_writedummy_iow
389 {
390 \c_sys_jobname_str-mathml-dummy.html
391 }
392 \iow_now:Ne \g__math_writedummy_iow
393 {
394 \c__math_mathml_write_init_tl
395 }
396 \AssignTaggingSocketPlug{math/mathml/write/prepare}{On}
397 \AssignTaggingSocketPlug{math/mathml/write}{On}
398 \AddToHook{enddocument/afterlastpage}
399 {
400 \iow_now:Ne \g__math_writedummy_iow
401 { \c__math_mathml_write_final_tl }
402 \iow_close:N \g__math_writedummy_iow
403 }
404 }
405 },
406 math/mathml/write-dummy .usage:n=preamble
407 }
408 \box_new:N\l__math_tmpa_box
409 \cs_new_protected:Npn \__math_AF_process_mathml_files:
410 {
411 \hbox_set:Nn \l__math_tmpa_box
412 {
413 \pdfdict_put:nnn { l_pdffile/Filespec }{AFRelationship} { /Supplement }
414 \pdfdict_put:nne
415 { l_pdffile }{Subtype}
416 { \pdf_name_from_unicode_e:n{application/mathml+xml} }
417 \char_set_catcode_other:N \#
418 \cs_set_eq:NN\mml \__math_AF_html_reader:w
419 \clist_map_inline:Nn \l__tag_math_mathml_files_clist
420 {
421 \file_if_exist:nTF {##1.html}
422 {
423 \typeout{Info:~reading~mathml~file~##1}
424 \file_input:n {##1.html}
425 \bool_gset_true:N\g__tag_math_mathml_AF_bool
426 }
427 {
428 \typeout{Info:~mathml~file~##1~does~not~exist}%info message
429 }
430 }
431 }
432 \box_clear:N \l__math_tmpa_box
433 \bool_if:NT\g__tag_math_mathml_AF_bool
434 {
435 \typeout{Info:~Activating~mathml~support}
436 \AssignTaggingSocketPlug{math/struct/begin}{mathml-AF}
437 \AssignTaggingSocketPlug{math/struct/end}{mathml-AF}
438 \AddToHook{enddocument/info}
439 {
440 \iow_term:n{MathML~statistic}
441 \iow_term:n{================}
442 \iow_term:e{==>~\int_use:N\g__math_mathml_total_int\c_space_tl
443 MathML~fragments~read}
444 \iow_term:e{==>~\int_use:N\g__math_mathml_int\c_space_tl
445 different~MathML~fragments}
446 \iow_term:e{==>~\int_use:N\g__math_math_total_int\c_space_tl
447 math~fragments~found}
448 \iow_term:e{==>~\int_use:N\g__math_mathml_AF_found_int\c_space_tl
449 fitting~MathML~AF~found}
450 \iow_term:e{==>~\int_use:N\g__math_mathml_AF_attached_int\c_space_tl
451 MathML~AF~attached}
452 }
453 }
454 }
455 \AddToHook{begindocument}{\__math_AF_process_mathml_files:}
456 \int_new:N \l__math_mathstyle_int
457 \int_new:N \g__math_mathchoice_int
458 \property_new:nnnn{mathstyle}{now}{-1}{\int_use:N \l__math_mathstyle_int }
459 \cs_new_protected:Npn \__math_tag_if_mathstyle:nn #1 #2
460 %#1 refers to label
461 %#2 is a number for the mathstyle (typically 0,2,4,6)
462 {
463 \int_set:Nn \l__math_mathstyle_int {#2}
464 \property_record:nn {#1} { mathstyle }
465 \int_compare:nNnTF { \property_ref:nn {#1}{ mathstyle} } = { #2 }
466 { \tag_resume:n{\mathchoice} }{ \tag_suspend:n{\mathchoice} }
467 }
468 \cs_generate_variant:Nn \__math_tag_if_mathstyle:nn {en}
469 \keys_define:nn { __tag / setup }
470 {
471 math/mathml/sources .clist_set:N = \l__tag_math_mathml_files_clist,
472 math/alt/use .bool_set:N = \l__tag_math_alt_bool,
473 viewer/pane/mathml .bool_set:N = \l__tag_math_mathml_pane_bool,
474 viewer/pane/mathml .initial:n = true,
475 viewer/pane/mathsource .bool_set:N = \l__tag_math_texsource_pane_bool,
476 math/mathml/AF .bool_set:N = \l__tag_math_mathml_AF_bool,
477 math/mathml/AF .initial:n = true,
478 math/tex/AF .bool_set:N = \l__tag_math_texsource_AF_bool,
479 math/tex/AF .initial:n = true
480 }
481 \AddToHook{begindocument/end}
482 {
483 \str_if_eq:eeT
484 {1}
485 {
486 \exp_last_unbraced:Ne\use_i:nn
487 {\GetDocumentProperties{document/pdfstandard-UA}}
488 \c_empty_tl\c_empty_tl
489 }
490 {
491 \bool_if:NF \l__tag_math_alt_bool
492 {
493 \typeout{PDF/UA-1~detected.~Enabling~alt~text~on~Formula}
494 }
495 \bool_set_true:N\l__tag_math_alt_bool
496 }
497 }
498 \keys_define:nn {__tag / setup}
499 {
500 math/setup .code:n =
501 {
502 %deactivate loading of luamml
503 \tl_gset:Nn \g__math_luamml_load_tl{-1}
504 \keys_set:nn {__tag / setup}
505 {
506 %deactivate tex source AF
507 math/tex/AF = false,
508 %deactivate reading of mathml-AF
509 math/mathml/sources=,
510 math/mathml/AF=false,
511 %deactivate structelem
512 math/mathml/structelem=false,
513 %handle value
514 }
515 \clist_map_inline:nn { #1}
516 {
517 \keys_set:nn {__tag/ setup}{math/__setup/##1}
518 }
519 },
520 math/__setup / mathml-SE .code:n =
521 {
522 \tl_gset:Nn \g__math_luamml_load_tl{1}
523 \keys_set:nn {__tag / setup}
524 {
525 math/mathml/structelem=true
526 }
527 },
528 math/__setup / mathml-AF .code:n =
529 {
530 \tl_gset:Nn \g__math_luamml_load_tl{1}
531 \clist_put_right:Ne\l__tag_math_mathml_files_clist
532 {\c_sys_jobname_str-mathml,\c_sys_jobname_str-luamml-mathml}
533 \keys_set:nn {__tag / setup}
534 {
535 math/mathml/AF=true
536 }
537 },
538 math/__setup / tex-AF .code:n =
539 {
540 \keys_set:nn {__tag / setup}
541 {
542 math/tex/AF =true
543 }
544 },
545 }
546 \NewTaggingSocketPlug
547 {math/inline/begin}
548 {MC}
549 {\tag_mc_end_push:}
550 \NewTaggingSocketPlug
551 {math/inline/end}
552 {MC}
553 {\tag_mc_begin_pop:n{}}
554 \NewTaggingSocketPlug
555 {math/inline/formula/begin}
556 {default}
557 { \tagpdfparaOff
558 \__math_luamml_structelem:
559 \tag_socket_use:n{math/content}
560 \tag_socket_use:n{math/struct/begin}
561 #2
562 \tag_socket_use:n{math/end}
563 }
564 \NewTaggingSocketPlug
565 {math/inline/formula/end}
566 {default}
567 {
568 \tag_socket_use:n{math/struct/end}
569 }
570
571 \NewTaggingSocketPlug
572 {math/display/begin}
573 {default}
574 { \__tag_tool_close_P: }
575 \NewTaggingSocketPlug
576 {math/display/end}
577 {default}
578 {
579 }
580
581 \NewTaggingSocketPlug
582 {math/display/formula/begin}
583 {default}
584 {
585 \tagpdfparaOff
586 \__math_luamml_structelem:
587 \tag_socket_use:n{math/content}
588 \tag_socket_use:n{math/struct/begin}
589 #2
590 \tag_socket_use:n{math/end}
591 }
592 \NewTaggingSocketPlug
593 {math/display/formula/end}
594 {default}
595 {
596 \tag_socket_use:n{math/struct/end}
597 }
598 \NewTaggingSocketPlug
599 {math/display/tag/begin}
600 {default}
601 {
602 \tag_mc_end:
603 \tag_struct_begin:n {tag=Lbl}
604 \tag_mc_begin:n {}
605 }
606 \NewTaggingSocketPlug
607 {math/display/tag/end}
608 {default}
609 {
610 \tag_mc_end:
611 \tag_struct_end:
612 \tag_mc_begin:n{}
613 }
614 \AssignTaggingSocketPlug{math/display/tag/begin}{default}
615 \AssignTaggingSocketPlug{math/display/tag/end}{default}
616 \tl_new:N\l__math_content_template_tl
617 \tl_set:Nn \l__math_content_template_tl
618 {
619 LaTeX~ formula~ starts~
620 \exp_not:N\begin{\g__math_grabbed_env_tl}
621 \c_space_tl
622 \exp_not:V\g__math_grabbed_math_tl
623 \c_space_tl
624 \exp_not:N\end{\g__math_grabbed_env_tl}
625 \c_space_tl LaTeX~ formula~ ends~
626 }
627
628 \tl_new:N\l__math_texsource_template_tl
629 \tl_const:Nn\c__math_inline_env_tl {math}
630 \tl_set:Nn \l__math_texsource_template_tl
631 {
632 \tl_if_eq:NNTF\g__math_grabbed_env_tl\c__math_inline_env_tl
633 {
634 $
635 \exp_not:V\g__math_grabbed_math_tl
636 $
637 }
638 {
639 \exp_not:N\begin{\g__math_grabbed_env_tl}
640 \exp_not:V\g__math_grabbed_math_tl
641 \exp_not:N\end{\g__math_grabbed_env_tl}
642 }
643 }
644
645 \NewTaggingSocket{math/content}{0}
646 \NewTaggingSocketPlug
647 {math/content}
648 {actual+source}
649 {
650 \tl_set:Ne\l__math_content_actual_tl
651 {
652 \l__math_content_template_tl
653 }
654 \tl_set:Ne \l__math_content_AF_source_tl
655 {
656 \l__math_texsource_template_tl
657 }
658 \tl_set:Nn \l__math_content_AF_mathml_tl {}
659 \tl_set:Nn \l__math_content_alt_tl {}
660 }
661 \NewTaggingSocketPlug
662 {math/content}
663 {alt+source}
664 {
665 \tl_set:Ne\l__math_content_alt_tl
666 {
667 \l__math_content_template_tl
668 }
669 \tl_set:Ne \l__math_content_AF_source_tl
670 {
671 \l__math_texsource_template_tl
672 }
673 \tl_set:Nn \l__math_content_AF_mathml_tl {}
674 \tl_set:Nn \l__math_content_actual_tl {}
675 }
676 \AssignTaggingSocketPlug{math/content}{alt+source}
677 \NewTaggingSocket{math/struct/begin}{0}
678 \NewTaggingSocket{math/struct/end}{0}
679 \NewTaggingSocketPlug
680 {math/struct/begin}
681 {default}
682 {
683 \bool_if:NTF\l__tag_math_texsource_AF_bool
684 { \tl_set_eq:NN \l__math_content_AF_source_tmpa_tl \l__math_content_AF_source_tl }
685 { \tl_clear:N \l__math_content_AF_source_tmpa_tl }
686 \tl_if_eq:NnTF\g__math_grabbed_env_tl {math}
687 {
688 \tl_set:Nn\l__math_attribute_class_tl{inline}
689 }
690 {
691 \tl_set:Nn\l__math_attribute_class_tl{display}
692 }
693 \bool_if:NF\l__tag_math_alt_bool
694 { \tl_set:Nn \l__math_content_alt_tl{} }
695 \tag_struct_begin:n
696 {
697 tag=Formula,
698 attribute-class=\l__math_attribute_class_tl,
699 texsource = \l__math_content_AF_source_tmpa_tl,
700 title-o = \g__math_grabbed_env_tl,
701 actualtext = \l__math_content_actual_tl,
702 alt = \l__math_content_alt_tl
703 }
704 \__math_debug_typeout:n{grabbed~math=\meaning\g__math_grabbed_math_tl}
705 \tag_mc_begin:n{}
706 }
707 \NewTaggingSocketPlug
708 {math/struct/end}
709 {default}
710 { \tag_mc_end: \tag_struct_end: }
711
712 \AssignTaggingSocketPlug{math/struct/begin}{default}
713 \AssignTaggingSocketPlug{math/struct/end}{default}
714 \cs_generate_variant:Nn \str_mdfive_hash:n {o}
715 \tl_new:N\l__math_content_hash_tl
716 \tl_new:N\l__math_grabbed_math_tl
717 \NewTaggingSocketPlug
718 {math/struct/begin}
719 {mathml-AF}
720 {
721 \int_gincr:N\g__math_math_total_int
722 \tl_set:Ne\l__math_content_hash_tl
723 {\str_mdfive_hash:o { \l__math_content_AF_source_tl }}
724 \tl_set_eq:NN\l__math_grabbed_math_tl\g__math_grabbed_math_tl
725 \tl_if_eq:NnTF\g__math_grabbed_env_tl {math}
726 {
727 \tl_set:Nn\l__math_attribute_class_tl{inline}
728 }
729 {
730 \tl_set:Nn\l__math_attribute_class_tl{display}
731 }
732 \bool_if:NF\l__tag_math_alt_bool
733 { \tl_set:Nn \l__math_content_alt_tl{} }
734 \tl_if_exist:cTF { g__math_mathml_ \l__math_content_hash_tl _tl }
735 {
736 \int_gincr:N\g__math_mathml_AF_found_int
737 \bool_if:NTF \l__tag_math_mathml_AF_bool
738 {
739 \int_gincr:N\g__math_mathml_AF_attached_int
740 \__math_debug_typeout:n {Inserting~mathml~with~Hash~\l__math_content_hash_tl}
741 }
742 {
743 \__math_debug_typeout:n {Ignoring~mathml~with~Hash~\l__math_content_hash_tl}
744 }
745 }
746 {
747 \bool_if:NT \l__tag_math_mathml_AF_bool
748 {
749 \typeout {WARNING:~mathml~missing~for~hash~\l__math_content_hash_tl}
750 }
751 }
752 \tag_socket_use:n {math/mathml/write/prepare}
753 \tag_socket_use:n {math/mathml/write} % write hash if request
754 \bool_if:NTF\l__tag_math_texsource_AF_bool
755 { \tl_set_eq:NN \l__math_content_AF_source_tmpa_tl \l__math_content_AF_source_tl }
756 { \tl_clear:N \l__math_content_AF_source_tmpa_tl }
757 \tag_struct_begin:n
758 {
759 tag=Formula,
760 attribute-class=\l__math_attribute_class_tl, %
761 AFref =
762 \bool_if:NT\l__tag_math_mathml_AF_bool
763 {
764 \cs_if_exist_use:c {g__math_mathml_ \l__math_content_hash_tl _tl}
765 },
766 texsource = \l__math_content_AF_source_tmpa_tl, % should be after mathml AF!
767 title-o = \g__math_grabbed_env_tl, %
768 alt = \l__math_content_alt_tl
769 }
770 \__math_debug_typeout:n {grabbed~math=\meaning\g__math_grabbed_math_tl}
771 \tag_mc_begin:n{}
772 }
773 \NewTaggingSocketPlug
774 {math/struct/end}
775 {mathml-AF}
776 {
777 \tag_mc_end:
778 \tag_struct_end:
779 }
780 \NewTaggingSocket{math/end}{0}
781 \AssignTaggingSocketPlug{math/inline/begin}{MC}
782 \AssignTaggingSocketPlug{math/inline/end}{MC}
783 \AssignTaggingSocketPlug{math/inline/formula/begin}{default}
784 \AssignTaggingSocketPlug{math/inline/formula/end}{default}
785 \AssignTaggingSocketPlug{math/display/begin}{default}
786 \AssignTaggingSocketPlug{math/display/end}{default}
787 \AssignTaggingSocketPlug{math/display/formula/begin}{default}
788 \AssignTaggingSocketPlug{math/display/formula/end}{default}
789 \newif\ifmeasuring@
790 \cs_new_protected:Npn \__math_process:nn #1#2
791 {
792 \legacy_if:nF { measuring@ }
793 {
794 \tl_if_in:nnTF {#2} { \m@th }
795 { \bool_set_true:N\l__math_fakemath_bool }
796 { \tl_trim_spaces_apply:nN {#2} \__math_process_auxi:nn {#1} }
797 }
798 }
799 \cs_generate_variant:Nn \__math_process:nn { V }
800 \cs_new_protected:Npn \__math_process_auxi:nn #1#2
801 {
802 \tl_gset:Nn \g__math_grabbed_env_tl {#2}
803 \tl_gset:Nn \g__math_grabbed_math_tl {#1}
804 \__math_process_auxii:nn {#2} {#1}
805 }
806 \cs_new_protected:Npn \__math_process_auxii:nn #1#2 { }
807 \cs_new_protected:Npn \math_processor:n #1
808 { \cs_set_protected:Npn \__math_process_auxii:nn ##1##2 {#1} }
809 \cs_set_protected:Npn\MathCollectTrue{\bool_set_false:N \l__math_collected_bool}
810 \cs_set_protected:Npn\MathCollectFalse{\bool_set_true:N \l__math_collected_bool}
811 \cs_new_protected:Npn \__math_grab_dollar:w { \__math_grab_dollar_delim:w }
812 \cs_new_protected:Npn \__math_grab:n #1
813 {
814 \__math_process:nn { math } {#1}
815 \bool_lazy_any:nTF
816 {
817 {\legacy_if_p:n { measuring@ }}
818 { \l__math_fakemath_bool }
819 { \tl_if_blank_p:n {#1} }
820 }
821 {
822 \__math_luamml_ignore:
823 #1 $ % $
824 }
825 {
826 \tag_socket_use:n {math/inline/begin} %end P-MC
827 \tag_socket_use:nnn {math/inline/formula/begin}{}{#1}
828 $ % $
829 \tag_socket_use:n {math/inline/formula/end}
830 \tag_socket_use:n {math/inline/end} % restart P-MC
831 }
832 }
833 \cs_new_protected:Npn \__math_grab_dollar_delim:w #1 $ % $
834 { \__math_grab:n {#1} }
835 \cs_new_protected:Npn \__math_grab_dollardollar:w #1 $$ {
836 \tl_if_blank:nF {#1}
837 {
838 \__math_process:nn { equation* } {#1}
839 \tag_socket_use:n {math/display/begin}
840 \tag_socket_use:nn{math/display/formula/begin}{}{#1}
841 }
842 \__math_prepare_display_end:
843 $$
844 }
845 \cs_new_protected:Npn \__math_grab_inline_delim:w % \(
846 #1 \)
847 {
848 \tl_if_blank:nF {#1}
849 {
850 $ #1 $
851 }
852 \bool_set_false:N \l__math_collected_bool
853 }
854 \cs_new_protected:Npn \__math_grab_inline:w { \__math_grab_inline_delim:w }
855 \cs_new_protected:Npn \__math_grab_eqn:w % \[
856 #1 \]
857 {
858 \begin { equation* } #1 \end { equation* }
859 }
860 \tl_new:N \l__math_grabbed_tl
861 \int_new:N \l__math_grab_env_int
862 \cs_new_protected:Npn \__math_grab_loop_aux:
863 {
864 \group_begin:
865 \tl_clear:N \l__math_grabbed_tl
866 \__math_grab_loop:
867 }
868 \cs_new_protected:Npn \__math_grab_loop:
869 {
870 \peek_remove_spaces:n
871 {
872 \peek_meaning:NTF \c_group_begin_token
873 { \__math_grab_loop_group:n }
874 { \__math_grab_loop_token:N }
875 }
876 }
877 \cs_new_protected:Npn \__math_grab_loop_group:n #1
878 { \__math_grab_loop_store:n { {#1} } }
879 \cs_new_protected:Npn \__math_grab_loop_store:n #1
880 {
881 \tl_put_right:Nn \l__math_grabbed_tl {#1}
882 \__math_grab_loop:
883 }
884 \cs_new_protected:Npn \__math_grab_loop_token:N #1
885 {
886 \cs_if_exist_use:cF
887 { __math_grab_loop_ \token_to_str:N #1 : }
888 { \__math_grab_loop_store:n {#1} }
889 }
890 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N $ : }
891 { \__math_grab_loop_end: }
892 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \) : }
893 { \__math_grab_loop_end: }
894 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \\ : }
895 {
896 \int_compare:nNnTF \l__math_grab_env_int = 0
897 { \__math_grab_loop_newline: }
898 { \__math_grab_loop_store:n { \\ } }
899 }
900 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \begin : }
901 {
902 \int_incr:N \l__math_grab_env_int
903 \__math_grab_loop_store:n { \begin }
904 }
905 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N \end : }
906 {
907 \int_compare:nNnTF \l__math_grab_env_int = 0
908 {
909 \__math_grab_loop_newline:
910 \end
911 }
912 {
913 \int_decr:N \l__math_grab_env_int
914 \__math_grab_loop_store:n { \end }
915 }
916 }
917 \tl_map_inline:nn { \ignorespaces \unskip \textonly@unskip }
918 {
919 \cs_new_protected:cpn { __math_grab_loop_ \token_to_str:N #1 : }
920 { \__math_grab_loop: }
921 }
922 \cs_new_protected:Npn \__math_grab_loop_newline:
923 {
924 \if_false: { \fi:
925 \cs_set_protected:Npn \cr
926 {
927 \__math_grab_loop:
928 \tex_cr:D
929 }
930 \if_false: } \fi:
931 \\
932 }
933 \cs_new_protected:Npn \__math_grab_loop_end:
934 {
935 \exp_args:NNV \group_end:
936 \__math_grab:n \l__math_grabbed_tl
937 }
938 \tl_new:N \l__math_env_name_tl
939 \keys_define:nn { __math }
940 {
941 arg-spec .code:n =
942 {
943 \ExpandArgs { c } \DeclareDocumentCommand
944 { __math_env \l__math_env_name_tl _aux: }
945 {#1}
946 { \__math_env_forward:w }
947 }
948 }
949 \cs_new_protected:Npn \math_register_env:nn #1#2
950 {
951 \tl_set:Nn \l__math_env_name_tl {#1}
952 \keys_set:nn { __math } {#2}
953 \cs_gset_eq:cc { __math_env_ #1 _begin: } {#1}
954 \cs_gset_eq:cc { __math_env_ #1 _end: } { end #1 }
955 \ExpandArgs { nne } \RenewDocumentEnvironment {#1} { b }
956 {
957 \exp_not:N \bool_if:NTF \exp_not:N \l__math_collected_bool
958 {
959 }
960 {
961 \cs_if_exist:cTF { __math_env #1 _aux: }
962 {
963 \exp_not:c { __math_env #1 _aux: }
964 ##1 \exp_not:N \__math_env_end: {#1}
965 }
966 { \exp_not:N \__math_process:nn {#1} {##1} }
967 \exp_not:n { \@kernel@math@registered@begin }
968 \bool_set_true:N \exp_not:N \l__math_collected_bool
969 }
970 \exp_not:c { __math_env_ #1 _begin: }
971 ##1
972 \exp_not:c { __math_env_ #1 _end: }
973 }
974 {
975 }
976 }
977 \cs_new_protected:Npn \math_register_halign_env:nn #1#2
978 {
979 \tl_set:Nn \l__math_env_name_tl {#1}
980 \keys_set:nn { __math } {#2}
981 \cs_gset_eq:cc { __math_env_ #1 _begin: } {#1}
982 \cs_gset_eq:cc { __math_env_ #1 _end: } { end #1 }
983 \ExpandArgs { nnee } \RenewDocumentEnvironment {#1} { b }
984 {
985 \exp_not:N \bool_if:NTF \exp_not:N \l__math_collected_bool
986 {
987 }
988 {
989 \cs_if_exist:cTF { __math_env #1 _aux: }
990 {
991 \exp_not:c { __math_env #1 _aux: }
992 ##1 \exp_not:N \__math_env_end: {#1}
993 }
994 { \exp_not:N \__math_process:nn {#1} {##1} }
995 \exp_not:n { \@kernel@math@registered@begin }
996 \bool_set_true:N \exp_not:N \l__math_collected_bool
997 }
998 \exp_not:c { __math_env_ #1 _begin: }
999 ##1
1000 }
1001 {
1002 \exp_not:c { __math_env_ #1 _end: }
1003 }
1004 }
1005
1006 \cs_new:Npn \@kernel@math@registered@begin {
1007 \mode_if_vertical:TF
1008 {
1009 \tag_if_active:T
1010 {
1011 \exp_args:Noo\str_if_eq:nnF \g__tag_struct_tag_tl { \l__tag_para_main_tag_tl } % needs correction!
1012 {
1013 \__block_beginpar_vmode:
1014 } % needs correction!
1015 }
1016 }
1017 {
1018 \__tag_tool_close_P:
1019 }
1020 \tag_socket_use:nn{math/display/formula/begin}{}{}
1021 }
1022
1023 \cs_new_protected:Npn \math_register_env:n #1
1024 { \math_register_env:nn {#1} { } }
1025
1026 \NewDocumentCommand \RegisterMathEnvironment { O{} m }
1027 { \math_register_env:nn {#2} {#1} }
1028 \cs_new_protected:Npn \__math_env_forward:w #1 \__math_env_end: #2
1029 { \__math_process:nn {#2} {#1} }
1030 \cs_new_protected:Npn \__math_prepare_display_end: {
1031 \bool_lazy_or:nnTF
1032 { \dim_compare_p:nNn \belowdisplayskip < {0pt} }
1033 { \dim_compare_p:nNn \belowdisplayshortskip < {0pt} }
1034 { \tl_gclear:N \g__math_skip_sign_tl }
1035 {
1036 \tl_gset:Nn \g__math_skip_sign_tl {-}
1037 \skip_set:Nn \belowdisplayskip {-\belowdisplayskip}
1038 \skip_set:Nn \belowdisplayshortskip {-\belowdisplayshortskip}
1039 }
1040 \int_gset_eq:NN \g__math_postdisplaypenalty_int \postdisplaypenalty
1041 \int_set_eq:NN \postdisplaypenalty \@M
1042 }
1043 \cs_new_protected:Npn \__math_tag_dollardollar_display_end:
1044 {
1045 % \typeout{== tag dollarldollar display end}
1046 % \ShowTagging{struct-stack}
1047 \para_raw_end:
1048 \l__math_tmpa_skip \lastskip
1049 \tag_socket_use:n{math/display/formula/end}
1050 \nobreak
1051 \skip_vertical:n { -\l__math_tmpa_skip }
1052 \penalty \g__math_postdisplaypenalty_int
1053 \skip_vertical:n
1054 { \g__math_skip_sign_tl \l__math_tmpa_skip }
1055 \skip_vertical:n { -\tex_parskip:D }
1056 \@domathendpetrue
1057 \@doendpe % this has no \end{...} to take care of it
1058 }
1059 \int_new:N \g__math_postdisplaypenalty_int
1060 \tl_new:N \g__math_skip_sign_tl
1061 \def \dollardollar@end { \__math_prepare_display_end: $$ }
1062 \protected\def\eqno{
1063 \__math_prepare_display_end:
1064 \@kernel@eqno
1065 \cs_set_eq:NN \__math_prepare_display_end: \prg_do_nothing:
1066 \aftergroup\ignorespaces
1067 }
1068 \protected\def\leqno{
1069 \__math_prepare_display_end:
1070 \@kernel@leqno
1071 \cs_set_eq:NN \__math_prepare_display_end: \prg_do_nothing:
1072 \aftergroup\ignorespaces
1073 }
1074 \tl_gput_right:Nn \@kernel@before@begindocument
1075 {
1076 \math_register_env:n { equation }
1077 \math_register_env:n { equation* }
1078 \RenewDocumentEnvironment{math} {b}{$#1$}{}
1079 \RenewDocumentEnvironment{displaymath} {b}{\[#1\]}{}
1080 }
1081 \cs_gset_protected:Npn \( % \)
1082 {
1083 \bool_if:NTF \l__math_collected_bool
1084 {
1085 \mode_if_math:TF
1086 { \@badmath }
1087 { $ }
1088 }
1089 {
1090 \__math_grab_inline:w
1091 }
1092 } % \(
1093 \cs_gset_protected:Npn \)
1094 {
1095 \mode_if_math:TF
1096 { $ }
1097 { \@badmath }
1098 }
1099 \tl_gput_right:Nn \@kernel@before@begindocument
1100 {
1101 \cs_gset_protected:Npn \[ % \]
1102 {
1103 \__math_grab_eqn:w
1104 % \bool_if:NTF \l__math_collected_bool
1105 } % \[
1106 \cs_gset_protected:Npn \]
1107 {
1108 \@badmath
1109 }
1110 }
1111
1112 \exp_args:No \tex_everymath:D
1113 {
1114 \tex_the:D \tex_everymath:D
1115 \bool_if:NF \l__math_collected_bool
1116 {
1117 \bool_set_true:N \l__math_collected_bool
1118 \__math_grab_dollar:w
1119 }
1120 }
1121
1122 \exp_args:No \tex_everydisplay:D
1123 {
1124 \tex_the:D \tex_everydisplay:D
1125 \group_insert_after:N \__math_tag_dollardollar_display_end:
1126 \bool_if:NF \l__math_collected_bool
1127 {
1128 \bool_set_true:N \l__math_collected_bool
1129 \__math_grab_dollardollar:w
1130 }
1131 }
1132 \tl_gput_right:Nn \@kernel@before@begindocument
1133 {
1134 \math_register_env:n { eqnarray }
1135 \math_register_env:n { eqnarray* }
1136 }
1137 \RequirePackage{array}
1138 \tl_if_exist:NT\@kernel@tabular@init
1139 {
1140 \tl_put_right:Nn\@kernel@tabular@init
1141 {
1142 \cs_set_protected:Npn \__math_grab_dollar:w { \__math_grab_loop_aux: }
1143 \cs_set_protected:Npn \__math_grab_inline:w { $ }
1144 }
1145 }
1146 \cs_new_eq:NN \__math_m@th: \m@th
1147 \cs_gset_protected:Npn \m@th
1148 {
1149 \bool_set_true:N \l__math_collected_bool
1150 \__math_m@th:
1151 }
1152 \sys_if_engine_luatex:T
1153 {
1154 \def\mathpalette#1#2{%
1155 \ifcase\mathstyle
1156 #1\displaystyle{#2}\or
1157 #1\displaystyle{#2}\or
1158 #1\textstyle{#2}\or
1159 #1\textstyle{#2}\or
1160 #1\scriptstyle{#2}\or
1161 #1\scriptstyle{#2}\or
1162 #1\scriptscriptstyle{#2}\or
1163 #1\scriptscriptstyle{#2}
1164 \fi}
1165 }
1166 \def\mathsm@sh#1#2{%
1167 \setbox\z@\hbox{$\m@th#1{#2}
1168 \UseTaggingSocket{math/luamml/save/nNn}{{mathsmash} #1 {mpadded}}
1169 $}%
1170 \UseTaggingSocket{math/luamml/finsm@sh}{}{{}\finsm@sh}}
1171 \tl_gput_right:Nn\@kernel@before@begindocument
1172 {
1173 \bool_set_true:N\l__math_collected_bool
1174 }
1175 \tl_gput_right:Nn\@kernel@after@begindocument
1176 {
1177 \bool_set_false:N\l__math_collected_bool
1178 }
1179 \ExplSyntaxOff
1180 \endinput
1181 %%
1182 %% End of file `latex-lab-math.ltx'.