Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright 2011–2020 by Philipp Stephani, Joseph Wright, and Will Robertson
4 % Copyright 2021, 2022 Google LLC
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either
8 % version 1.3c of this license or (at your option) any later
9 % version. The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.3c or later is part of all distributions of
14 % LaTeX version 2009/09/24 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %<*driver>
20 \documentclass[a4paper, 10pt, ngerman, american]{phst-doc}
21
22 \usepackage{lualatex-math}
23 \newcommand*{\thispackage}{\textsf{lualatex-math}\xspace}
24
25 \begin{document}
26
27 \DocInput{lualatex-math.dtx}
28 \PrintChanges
29 \PrintIndex
30
31 \end{document}
32 %</driver>
33 % \fi
34 %
35 % \CheckSum{322}
36 %
37 % \CharacterTable
38 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
39 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
40 % Digits \0\1\2\3\4\5\6\7\8\9
41 % Exclamation \! Double quote \" Hash (number) \#
42 % Dollar \$ Percent \% Ampersand \&
43 % Acute accent \' Left paren \( Right paren \)
44 % Asterisk \* Plus \+ Comma \,
45 % Minus \- Point \. Solidus \/
46 % Colon \: Semicolon \; Less than \<
47 % Equals \= Greater than \> Question mark \?
48 % Commercial at \@ Left bracket \[ Backslash \\
49 % Right bracket \] Circumflex \^ Underscore \_
50 % Grave accent \` Left brace \{ Vertical bar \|
51 % Right brace \} Tilde \~}
52 %
53 %
54 % \changes{v0.1}{2011/04/22}{Initial version}
55 % \changes{v0.3a}{2011/09/13}{Updated for changes in \pkg{l3kernel}}
56 % \changes{v1.0}{2012/08/27}{Switched to \pkg{l3docstrip}}
57 %
58 % \GetFileInfo{lualatex-math.sty}
59 %
60 % \title{The \thispackage package\thanks{This document corresponds to
61 % \thispackage{}~\fileversion, dated~\filedate.}}
62 %
63 % \author{Philipp Stephani \\ \texttt{[email protected]}}
64 % \date{\filedate}
65 %
66 %
67 % \maketitle
68 % \tableofcontents
69 %
70 %
71 % \section{Introduction}
72 %
73 % \Hologo{LuaTeX} brings major improvements to all areas of \hologo{TeX}
74 % typesetting and programming. They are made available through new primitives
75 % or the embedded Lua interpreter, and combining them with existing
76 % \hologo{LaTeX2e} packages is not a task the average \hologo{LaTeX} user
77 % should have to care about. Therefore a multitude of \hologo{LaTeX2e}
78 % packages have been written to bridge the gap between documents and the new
79 % features. The \thispackage package focuses on the additional possibilities
80 % for mathematical typesetting. The most eminent of the new features is the
81 % ability to use Unicode and OpenType fonts, as provided by \Robertson’s
82 % \pkg{unicode-math} package. However, there is a smaller group of changes
83 % unrelated to Unicode: these are to be dealt with in this package. While in
84 % principle most \hologo{TeX} documents written for traditional engines should
85 % work just fine with \hologo{LuaTeX}, there is a small number of breaking
86 % changes that require the attention of package authors. The \thispackage
87 % package tries to fix some of the issues encountered while porting traditional
88 % macro packages to \hologo{LuaLaTeX}.
89 %
90 % The decision to write patches for existing macro packages should not be
91 % made lightly: monkey patching done by somebody different from the original
92 % package author ties the patching package to the implementation details of
93 % the patched functionality and breaks all rules of encapsulation. However,
94 % due to the lack of alternatives, it has become an accepted way of providing
95 % new functionality in \hologo{LaTeX}. To keep the negative impact as small
96 % as possible, the \thispackage package patches only the \hologo{LaTeX2e}
97 % kernel and a small number of popular packages. In general, this package
98 % should be regarded as a temporary kludge that should be removed once the
99 % math-related packages are updated to be usable with \hologo{LuaTeX}. By
100 % its very nature, the package is likely to cause problems; in such cases,
101 % please refer to the issue
102 % tracker\footnote{\url{https://github.com/phst/lualatex-math/issues}}.
103 %
104 %
105 % \section{Interface}
106 %
107 % The \thispackage package can be loaded with \cmd{\usepackage} or
108 % \cmd{\RequirePackage}, as usual. It has no options and no public
109 % interface; the patching is always done when the package is loaded and
110 % cannot be controlled. As a matter of course, the \thispackage package
111 % needs \hologo{LuaLaTeX} to function; it will produce error messages and
112 % refuse to load under other engines and formats. The package depends on the
113 % \pkg{expl3} bundle, the \pkg{etoolbox} package and the
114 % \pkg{filehook} package. The \thispackage package is independent of
115 % the \pkg{unicode-math} package; the fixes provided here are valid for both
116 % Unicode and legacy math typesetting.
117 %
118 % Currently patches for the \hologo{LaTeX2e} kernel and the \pkg{amsmath},
119 % \pkg{mathtools} and \pkg{icomma} packages are provided. It is not relevant
120 % whether you load these packages before or after \thispackage. They should
121 % work as expected (and ideally you shouldn’t notice anything), but if you load
122 % other packages that by themselves overwrite commands patched by this package,
123 % bad things may happen, as it is usual with \hologo{LaTeX}.
124 %
125 % One user-visible change is that the new
126 % \DescribeMacros{\mathstyle}\cmd{\mathstyle} primitive
127 % should work in
128 % all cases after the \thispackage package has been loaded, provided you use
129 % the high-level macros \DescribeMacros{\frac\binom\genfrac}\cmd{\frac},
130 % \cmd{\binom}, and \cmd{\genfrac}. The fraction-like \hologo{TeX}
131 % primitives like \cmd{\over} or \cmd{\atopwithdelims} and the
132 % \hologo{plainTeX} leftovers like \cmd{\brack} or \cmd{\choose} cannot be
133 % patched, and you shouldn’t use them.
134 %
135 % \StopEventually{}
136 %
137 %
138 % \section{Implementation of the \hologo{LaTeX2e} package}
139 %
140 % \changes{v1.4a}{2015/08/24}{Use \pkg{expl3} versions of \hologo{LuaTeX}
141 % math primitives}
142 % \changes{v1.4a}{2015/08/24}{Avoid \cs{RequireLuaModule}}
143 % \changes{v1.4a}{2015/09/16}{Load \pkg{luatexbase} only if required}
144 % \changes{v1.9}{2020/09/25}{Require 2020 version of \hologo{LaTeX2e}}
145 % \changes{v1.9}{2020/09/25}{Use builtin \hologo{LaTeX2e} hooks if available}
146 %
147 % \subsection{Requirements}
148 %
149 % \begin{macrocode}
150 %<*package>
151 %<@@=lltxmath>
152 \NeedsTeXFormat{LaTeX2e}[2020/02/02]
153 \RequirePackage{expl3}[2018/06/18]
154 \ProvidesExplPackage{lualatex-math}{2022/01/01}{1.12}%
155 {Patches for mathematics typesetting with LuaLaTeX}
156 \RequirePackage { etoolbox } [ 2007/10/08 ]
157 \cs_if_exist:NF \newluabytecode
158 { \RequirePackage { luatexbase } [ 2010/05/27 ] }
159 \directlua{require("lualatex-math")}
160 % \end{macrocode}
161 %
162 % \begin{macro}{\@@_restore_catcode:N}
163 % Executing the exhaustive expansion of
164 % \cmd{\@@_restore_catcode:N}\meta{character token} restores the category
165 % code of the \meta{character token} to its current value.
166 % \begin{macrocode}
167 \cs_new_nopar:Npn \@@_restore_catcode:N #1 {
168 \char_set_catcode:nn { \int_eval:n { `#1 } }
169 { \char_value_catcode:n { `#1 } }
170 }
171 % \end{macrocode}
172 % \end{macro}
173 % We use the macro defined above to restore the category code of the dollar
174 % sign. There are packages that make the dollar sign active; hopefully they
175 % get loaded after the packages we are trying to patch.
176 % \begin{macrocode}
177 \exp_args:Nx \AtEndOfPackage {
178 \@@_restore_catcode:N \$
179 }
180 \char_set_catcode_math_toggle:N \$
181 % \end{macrocode}
182 %
183 %
184 % \subsection{Messages}
185 %
186 % \begin{l3message}{luatex-required}
187 % Issued when not running under \hologo{LuaTeX}.
188 % \begin{macrocode}
189 \msg_new:nnn { lualatex-math } { luatex-required } {
190 The~ lualatex-math~ package~ requires~ LuaTeX. \\
191 I~ will~ stop~ loading~ now.
192 }
193 % \end{macrocode}
194 % \end{l3message}
195 %
196 % \begin{l3message}{macro-expected}
197 % Issued when trying to patch a non-macro. The first argument must be the
198 % detokenized macro name.
199 % \begin{macrocode}
200 \msg_new:nnn { lualatex-math } { macro-expected } {
201 I've~ expected~ that~ #1~ is~ a~ macro,~ but~ it~ isn't.
202 }
203 % \end{macrocode}
204 % \end{l3message}
205 %
206 % \begin{l3message}{wrong-meaning}
207 % Issued when trying to patch a macro with an unexpected meaning. The first
208 % argument must be the detokenized macro name; the second argument must be
209 % the actual detokenized meaning; and the third argument must be the expected
210 % detokenized meaning.
211 % \begin{macrocode}
212 \msg_new:nnn { lualatex-math } { wrong-meaning } {
213 I've~ expected~ #1~ to~ have~ the~ meaning \\
214 #3, \\
215 but~ it~ has~ the~ meaning \\
216 #2.
217 }
218 % \end{macrocode}
219 % \end{l3message}
220 %
221 % \begin{l3message}{patch-macro}
222 % Issued when a macro is patched. The first argument must be the detokenized
223 % macro name.
224 % \begin{macrocode}
225 \msg_new:nnn { lualatex-math } { patch-macro } {
226 I'm~ going~ to~ patch~ macro~ #1.
227 }
228 % \end{macrocode}
229 % \end{l3message}
230 %
231 %
232 % \subsection{Initialization}
233 %
234 % Unless we are running under \hologo{LuaTeX}, we issue an error and quit
235 % immediately.
236 % \begin{macrocode}
237 \sys_if_engine_luatex:F {
238 \msg_error:nn { lualatex-math } { luatex-required }
239 \endinput
240 }
241 % \end{macrocode}
242 %
243 % \subsection{Patching}
244 %
245 % \begin{macro}{\@@_temp:w}
246 % A scratch macro.
247 % \begin{macrocode}
248 \cs_new_eq:NN \@@_temp:w \prg_do_nothing:
249 % \end{macrocode}
250 % \end{macro}
251 %
252 % \begin{macro}{\@@_patch:NNnnn}
253 % \begin{macro}{\@@_patch:cNnnn}
254 % The auxiliary macro \cmd{\@@_patch:NNnnn}\meta{command}\meta{factory
255 % command}\marg{parameter text}\marg{expected replacement text}\marg{new
256 % replacement text} tries to patch \meta{command}. If \meta{command} is
257 % undefined, do nothing. Otherwise it must be a macro with the given
258 % \meta{parameter text} and \meta{expected replacement text}, created by the
259 % given \meta{factory command} or equivalent. In this case it will be
260 % overwritten using the \meta{parameter text} and the \meta{new replacement
261 % text}. Otherwise issue a warning and don’t overwrite.
262 % \begin{macrocode}
263 \cs_new_protected_nopar:Npn \@@_patch:NNnnn #1 #2 #3 #4 #5 {
264 \cs_if_exist:NT #1 {
265 \token_if_macro:NTF #1 {
266 \group_begin:
267 #2 \@@_temp:w #3 { #4 }
268 \cs_if_eq:NNTF #1 \@@_temp:w {
269 \msg_info:nnx { lualatex-math } { patch-macro }
270 { \token_to_str:N #1 }
271 \group_end:
272 #2 #1 #3 { #5 }
273 } {
274 \msg_warning:nnxxx { lualatex-math } { wrong-meaning }
275 { \token_to_str:N #1 } { \token_to_meaning:N #1 }
276 { \token_to_meaning:N \@@_temp:w }
277 \group_end:
278 }
279 } {
280 \msg_warning:nnx { lualatex-math } { macro-expected }
281 { \token_to_str:N #1 }
282 }
283 }
284 }
285 \cs_generate_variant:Nn \@@_patch:NNnnn { c }
286 % \end{macrocode}
287 % \end{macro}
288 % \end{macro}
289 %
290 % \begin{macro}{\@@_set_mathchar:NN}
291 % The macro \cmd{\@@_set_mathchar:NN}\meta{control sequence}\meta{token}
292 % defines the \meta{control sequence} as an extended mathematical character
293 % shorthand whose mathematical code is given by the mathematical code of the
294 % character \texttt{\textasciigrave}\meta{token}. We cannot use the
295 % |\Umathcharnumdef| primitive here since we would then rely on the
296 % |\Umathcodenum| primitive which is currently
297 % broken.\footnote{\url{http://tug.org/pipermail/luatex/2012-October/003794.html}}
298 % \changes{v0.3c}{2012/08/23}{\pkg{l3kernel} renamed \cs{lua_now:x} to
299 % \cs{lua_now_x:n}}
300 % \changes{v1.1}{2012/10/13}{Update reasoning why \cs{Umathcharnumdef} is not
301 % used here}
302 % \changes{v1.3a}{2014/06/18}{\pkg{l3kernel} has (currently) dropped
303 % \cs{lua_now_x:n}}
304 % \changes{v1.4a}{2015/08/24}{\cs{lua_now_x:n} is back}
305 % \changes{v1.8}{2019/01/21}{\cs{lua_now_x:n} is now called \cs{lua_now:e}}
306 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
307 % \begin{macrocode}
308 \cs_new_protected_nopar:Npn \@@_set_mathchar:NN #1 #2 {
309 \Umathchardef #1
310 \lua_now:e {
311 lualatex.math.print_class_fam_slot( \int_eval:n { `#2 } )
312 }
313 \scan_stop:
314 }
315 % \end{macrocode}
316 % \end{macro}
317 %
318 % \begin{macro}{\@@_before_package:nn}
319 % \begin{macro}{\@@_after_package:nn}
320 % The macro \cmd{\@@_before_package:nn}\marg{package}\marg{code} executes the
321 % \meta{code} before the \meta{package} is loaded. Accordingly,
322 % \cmd{\@@_after_package:nn}\marg{package}\marg{code} executes the
323 % \meta{code} after the \meta{package} is loaded. If the \meta{package} is
324 % already loaded, nothing happens. We prefer using native \hologo{LaTeX2e}
325 % hooks if possible.
326 % \changes{v1.12}{2022-01-01}{Use the new generic hook names if available}
327 % \begin{macrocode}
328 \@ifl@t@r \fmtversion { 2021/11/15 } {
329 \cs_new_protected_nopar:Npn \@@_before_package:nn #1 #2 {
330 \AddToHook { package/#1/before } { #2 }
331 }
332 \cs_new_protected_nopar:Npn \@@_after_package:nn #1 #2 {
333 \AddToHook { package/#1/after } { #2 }
334 }
335 }{
336 \@ifl@t@r \fmtversion { 2020/10/01 } {
337 \cs_new_protected_nopar:Npn \@@_before_package:nn #1 #2 {
338 \AddToHook { package/before/#1 } { #2 }
339 }
340 \cs_new_protected_nopar:Npn \@@_after_package:nn #1 #2 {
341 \AddToHook { package/after/#1 } { #2 }
342 }
343 } {
344 \RequirePackage { filehook } [ 2011/03/09 ]
345 \cs_new_protected_nopar:Npn \@@_before_package:nn #1 #2 {
346 \AtBeginOfPackageFile { #1 } { #2 }
347 }
348 \cs_new_protected_nopar:Npn \@@_after_package:nn #1 #2 {
349 \AtEndOfPackageFile { #1 } { #2 }
350 }
351 }
352 }
353 % \end{macrocode}
354 % \end{macro}
355 % \end{macro}
356 %
357 % \begin{macro}{\@@_after_package_or_now:nn}
358 % The macro \cmd{\@@_after_package_or_now:nn}\marg{package}\marg{code}
359 % executes the \meta{code} after the \meta{package} is loaded. If the
360 % \meta{package} is already loaded, the \meta{code} is executed immediately.
361 % \begin{macrocode}
362 \cs_new_protected_nopar:Npn \@@_after_package_or_now:nn #1 #2 {
363 \@ifpackageloaded { #1 } { #2 } { \@@_after_package:nn { #1 } { #2 } }
364 }
365 % \end{macrocode}
366 % \end{macro}
367 %
368 %
369 % \subsection{\Hologo{LaTeX2e} kernel}
370 %
371 % \changes{v0.3}{2011/08/07}{Patched math group allocation to gain access to
372 % all families}
373 % \changes{v1.4}{2015/04/04}{Removed patch for math group allocation; the
374 % kernel itself now supports all available math families}
375 % \Hologo{LuaTeX} enables access to the current mathematical style via the
376 % \cmd{\mathstyle} primitive. For this to work, fraction-like constructs (\eg,
377 % \meta{numerator} \cmd{\over} \meta{denominator}) have to be enclosed in a
378 % \cmd{\Ustack} group. \cmd{\frac} can be patched to do this, but the
379 % \hologo{plainTeX} remnants \cmd{\choose}, \cmd{\brack} and \cmd{\brace}
380 % should be discouraged.
381 %
382 % \begin{macro}{\frac}
383 % Here we assume that nobody except \pkg{amsmath} redefines \cmd{\frac}.
384 % This is obviously not the case, but we ignore other packages (\eg,
385 % \pkg{nath}) for the moment. We only patch the \hologo{LaTeX2e} kernel
386 % definition if the \pkg{amsmath} package is not loaded; the corresponding
387 % patch for \pkg{amsmath} follows below. Since \cmd{\frac} is declared by
388 % \cmd{\DeclareRobustCommand}, we must patch the macro
389 % \cmd{\frac\textvisiblespace}.
390 % \changes{v1.9}{2020/09/25}{Adapt to changes in \hologo{LaTeX2e} kernel}
391 % \begin{macrocode}
392 \AtEndPreamble {
393 \@ifpackageloaded { amsmath } { } {
394 \@@_patch:cNnnn { frac~ } \cs_set:Npn { #1 #2 } {
395 {
396 \begingroup #1 \endgroup \over #2
397 }
398 } {
399 % \end{macrocode}
400 % To do: do we need the additional set of braces around \cmd{\Ustack}?
401 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
402 % \begin{macrocode}
403 {
404 \Ustack { \group_begin: #1 \group_end: \over #2 }
405 }
406 }
407 }
408 }
409 % \end{macrocode}
410 % \end{macro}
411 %
412 %
413 % \subsection{\pkg{amsmath}}
414 %
415 % The popular \pkg{amsmath} package is subject to three \hologo{LuaTeX}-related problems:
416 % \begin{itemize}
417 % \item The \cmd{\mathcode} primitive is used several times, which fails for
418 % Unicode math characters. \cmd{\Umathcode} should be used instead.
419 % \item Legacy font dimensions are used for constructing stacks in the
420 % \cmd{\substack} command and the \env{subarray} environment. This doesn’t
421 % work if a Unicode math font is selected.
422 % \item The fraction commands \cmd{\frac} and \cmd{\genfrac} don’t use the
423 % \cmd{\Ustack} primitive.
424 % \end{itemize}
425 % These problems have been fixed in version~2.17i of \pkg{amsmath}, so we don’t
426 % attempt to patch it if that version is loaded.
427 %
428 % \begin{macro}{\c_@@_std_minus_mathcode_int}
429 % \begin{macro}{\c_@@_std_equal_mathcode_int}
430 % These constants contain the standard \hologo{TeX} mathematical codes for
431 % the minus and the equal signs. We temporarily set the math codes to these
432 % constants before loading the \pkg{amsmath} package so that it can request
433 % the legacy math code without error.
434 % \begin{macrocode}
435 \int_const:Nn \c_@@_std_minus_mathcode_int { "2200 }
436 \int_const:Nn \c_@@_std_equal_mathcode_int { "303D }
437 % \end{macrocode}
438 % \end{macro}
439 % \end{macro}
440 %
441 % \changes{v1.5}{2016/03/07}{Removed unused helper macro \cs{@@_char_dim:NN}}
442 %
443 % \begin{macro}{\l_@@_minus_mathchar}
444 % \begin{macro}{\l_@@_equal_mathchar}
445 % These mathematical characters are saved before \pkg{amsmath} is loaded so
446 % that we can temporarily assign the \hologo{TeX} values to the mathematical
447 % codes of the minus and equals signs. The \pkg{amsmath} package queries
448 % these codes, and if they represent Unicode characters, the package loading
449 % will fail. If \pkg{amsmath} has already been loaded, there is nothing we
450 % can do, therefore we use the non-starred version of
451 % \cmd{\AtBeginOfPackageFile}.
452 % \changes{v1.2}{2013/01/13}{Replace removed macro \cs{chk_if_free_cs:N}}
453 % \changes{v1.9}{2020/09/25}{Don’t patch newer versions of \pkg{amsmath}}
454 % \begin{macrocode}
455 \tl_new:N \l_@@_minus_mathchar
456 \tl_new:N \l_@@_equal_mathchar
457 \@@_before_package:nn { amsmath } {
458 \@ifpackagelater { amsmath } { 2020/08/24 } { } {
459 \@@_set_mathchar:NN \l_@@_minus_mathchar \-
460 \@@_set_mathchar:NN \l_@@_equal_mathchar \=
461 % \end{macrocode}
462 % \end{macro}
463 % \end{macro}
464 % Now we temporarily reset the mathematical codes.
465 % \begin{macrocode}
466 \char_set_mathcode:nn { `\- } { \c_@@_std_minus_mathcode_int }
467 \char_set_mathcode:nn { `\= } { \c_@@_std_equal_mathcode_int }
468 \@@_after_package:nn { amsmath } {
469 % \end{macrocode}
470 % \begin{macro}{\std@minus}
471 % \begin{macro}{\std@equals}
472 % The \pkg{amsmath} package defines the control sequences \cmd{\std@minus}
473 % and \cmd{\std@equal} as mathematical character shorthands while loading,
474 % but uses our restored mathematical codes, which must be fixed.
475 % \begin{macrocode}
476 \cs_set_eq:NN \std@minus \l_@@_minus_mathchar
477 \cs_set_eq:NN \std@equal \l_@@_equal_mathchar
478 % \end{macrocode}
479 % \end{macro}
480 % \end{macro}
481 % Finally, we restore the original mathematical codes of the two signs.
482 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
483 % \begin{macrocode}
484 \Umathcodenum `\- \l_@@_minus_mathchar
485 \Umathcodenum `\= \l_@@_equal_mathchar
486 }
487 }
488 }
489 % \end{macrocode}
490 % All of the following fixes work even if \pkg{amsmath} is already loaded.
491 % \begin{macro}{\@begindocumenthook}
492 % \changes{v0.3b}{2011/09/18}{Another update for a change in \pkg{l3kernel}}
493 % \pkg{amsmath} repeats the definiton of \cmd{\std@minus} and
494 % \cmd{\std@equal} at the beginning of the document, so we also have to patch
495 % the internal kernel macro \cmd{\@begindocumenthook} which contains the hook
496 % code.
497 % \changes{v1.9}{2020/09/25}{Don’t patch newer versions of \pkg{amsmath}}
498 % \begin{macrocode}
499 \@@_after_package_or_now:nn { amsmath } {
500 \@ifpackagelater { amsmath } { 2020/08/24 } { } {
501 \tl_replace_once:Nnn \@begindocumenthook {
502 \mathchardef \std@minus \mathcode `\- \relax
503 \mathchardef \std@equal \mathcode `\= \relax
504 } {
505 \@@_set_mathchar:NN \std@minus \-
506 \@@_set_mathchar:NN \std@equal \=
507 }
508 }
509 % \end{macrocode}
510 % \end{macro}
511 %
512 % \changes{v1.5}{2016/03/07}{Removed patch for \cs{Mathstrutbox@};
513 % \pkg{amsmath} now has a definition usable in \Hologo{LuaLaTeX}}
514 %
515 % \begin{environment}{subarray}
516 % The \env{subarray} environment uses legacy font dimensions. We simply
517 % patch it to use \hologo{LuaTeX} font parameters (and \hologo{LaTeX3}
518 % expressions instead of \hologo{TeX} arithmetic). Since subscript arrays
519 % are conceptually vertical stacks, we use the sum of top and bottom shift
520 % for the default vertical baseline distance (\cmd{\baselineskip}) and the
521 % minimum vertical gap for stack for the minimum baseline distance
522 % (\cmd{\lineskip}).
523 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
524 % \changes{v1.9}{2020/09/25}{Don’t patch newer versions of \pkg{amsmath}}
525 % \changes{v1.9}{2020/09/25}{Stop using \cs{…:D} control sequences}
526 % \begin{macrocode}
527 \@ifpackagelater { amsmath } { 2020/09/23 } { } {
528 \@@_patch:NNnnn \subarray \cs_set:Npn { #1 } {
529 \vcenter
530 \bgroup
531 \Let@
532 \restore@math@cr
533 \default@tag
534 \baselineskip \fontdimen 10~ \scriptfont \tw@
535 \advance \baselineskip \fontdimen 12~ \scriptfont \tw@
536 %<@@=>
537 \lineskip \thr@@ \fontdimen 8~ \scriptfont \thr@@
538 %<@@=lltxmath>
539 \lineskiplimit \lineskip
540 \ialign
541 \bgroup
542 \ifx c #1 \hfil \fi
543 $ \m@th \scriptstyle ## $
544 \hfil
545 \crcr
546 } {
547 \vcenter
548 \c_group_begin_token
549 \Let@
550 \restore@math@cr
551 \default@tag
552 \skip_set:Nn \baselineskip {
553 \Umathstacknumup \scriptstyle
554 + \Umathstackdenomdown \scriptstyle
555 }
556 \lineskip \Umathstackvgap \scriptstyle
557 \lineskiplimit \lineskip
558 \ialign
559 \c_group_begin_token
560 \token_if_eq_meaning:NNT c #1 { \hfil }
561 \Ustartmath
562 \m@th
563 \scriptstyle
564 \alignmark \alignmark
565 \Ustopmath
566 \hfil
567 \crcr
568 }
569 % \end{macrocode}
570 % \end{environment}
571 %
572 % \begin{macro}{\frac}
573 % Since \cmd{\frac} is declared by \cmd{\DeclareRobustCommand}, we must patch
574 % the macro \cmd{\frac\textvisiblespace}.
575 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
576 % \begin{macrocode}
577 \@@_patch:cNnnn { frac~ } \cs_set:Npn { #1 #2 } {
578 {
579 %<@@=>
580 \begingroup #1 \endgroup \@@over #2
581 }
582 } {
583 {
584 \Ustack { \group_begin: #1 \group_end: \@@over #2 }
585 %<@@=lltxmath>
586 }
587 }
588 % \end{macrocode}
589 % \end{macro}
590 %
591 % \begin{macro}{\genfrac}
592 % Generalized fractions are typeset by the \cmd{\genfrac} command.
593 % Since \cmd{\genfrac} is declared by \cmd{\DeclareRobustCommand},
594 % we have to patch the macro \cmd{\genfrac\textvisiblespace}.
595 % \changes{v1.7}{2017/06/15}{Adapt patch to changes in \pkg{amsmath}}
596 % \changes{v1.8}{2019/01/21}{Stop using \cs{…:D} control sequences}
597 % \begin{macrocode}
598 \@@_patch:cNnnn { genfrac~ } \cs_set:Npn {
599 #1 #2 #3 #4 #5 #6
600 } {
601 {
602 \@mathstyle { #4 }
603 \genfrac@choice o { #1 }
604 {
605 \begingroup #5 \endgroup
606 %<@@=>
607 \ifx @ #3 @ \@@over \else \@@above \fi #3 \relax
608 #6
609 }
610 \genfrac@choice c { #2 }
611 }
612 } {
613 {
614 \@mathstyle { #4 }
615 \genfrac@choice o { #1 }
616 {
617 \Ustack {
618 \group_begin: #5 \group_end:
619 \tl_if_empty:nTF { #3 } {
620 \@@over
621 } {
622 \@@above #3 \scan_stop:
623 }
624 %<@@=lltxmath>
625 #6
626 }
627 }
628 \genfrac@choice c { #2 }
629 }
630 }
631 }
632 }
633 % \end{macrocode}
634 % \end{macro}
635 %
636 %
637 % \changes{v1.1}{2012/10/13}{Add fix and unit test for \pkg{amsopn}}
638 % \changes{v1.6}{2016/04/16}{Removed patch for \cs{newmcodes@};
639 % \pkg{amsmath} now has a definition usable in \Hologo{LuaLaTeX}}
640 %
641 %
642 % \subsection{\pkg{mathtools}}
643 %
644 % \pkg{mathtools}’ \cmd{\cramped} command and others that make use of its
645 % internal version use a hack involving a null radical. \Hologo{LuaTeX} has
646 % primitives for setting material in cramped mode, so we make use of them.
647 %
648 % In newer versions of \pkg{mathtools}, this issue is fixed, in which case we
649 % skip the patch.
650 % \changes{v1.10}{2021/03/28}{Skip patch if \pkg{mathtools} is recent enough}
651 % \changes{v1.11}{2021/07/05}{Adapt to March 2021 changes to \pkg{mathtools}}
652 %
653 % \begin{macro}{\MT_cramped_internal:Nn}
654 % The macro \cmd{\MT_cramped_internal:Nn}\meta{style}\marg{expression}
655 % typesets the \meta{expression} in the cramped style corresponding to the
656 % given \meta{style} (\cmd{\displaystyle} etc.); all we have to do in
657 % \hologo{LuaTeX} is to select the correct primitive. Rewriting the
658 % user-level \cmd{\cramped} command and employing \cmd{\mathstyle} would be
659 % possible as well, but we avoid this way since we want to patch only a
660 % single command.
661 % \begin{macrocode}
662 \@@_after_package_or_now:nn { mathtools } {
663 \@ifpackagelater { mathtools } { 2021/03/28 } { } {
664 \@@_patch:NNnnn \MT_cramped_internal:Nn
665 \cs_set_nopar:Npn { #1 #2 } {
666 \setbox \z@ \hbox {
667 $
668 \m@th
669 #1
670 \kern -\nulldelimiterspace
671 \radical \z@ { #2 }
672 $
673 }
674 \ifx #1 \displaystyle
675 \dimen@ = \fontdimen 8 \textfont 3
676 \advance \dimen@ .25 \fontdimen 5 \textfont 2
677 \else
678 \dimen@ = 1.25 \fontdimen 8
679 \ifx #1 \textstyle
680 \textfont
681 \else
682 \ifx #1 \scriptstyle
683 \scriptfont
684 \else
685 \scriptscriptfont
686 \fi
687 \fi
688 3
689 \fi
690 \advance \dimen@ -\ht\z@
691 \ht\z@ = -\dimen@
692 \ifvmode \leavevmode \fi
693 { }
694 \box\z@
695 } {
696 % \end{macrocode}
697 % \changes{v1.4}{2014/08/18}{Added \cs{ensuremath} to work around
698 % \href{https://github.com/phst/lualatex-math/issues/11}{issue~11}}
699 % Here the additional set of braces is absolutely necessary, otherwise the
700 % changed mathematical style would be applied to the material after the
701 % \cmd{\mathchoice} construct. As the original command works in both text and
702 % math mode, we use |\ensuremath| here.
703 % \changes{v1.9}{2020/09/25}{Stop using \cs{…:D} control sequences}
704 % \begin{macrocode}
705 {
706 \ensuremath {
707 \use:c { cramped \cs_to_str:N #1 } #2
708 }
709 }
710 }
711 }
712 }
713 % \end{macrocode}
714 % \end{macro}
715 %
716 %
717 % \subsection{\pkg{icomma}}
718 %
719 % \changes{v0.2}{2011/07/03}{Added patch for the \pkg{icomma} package}
720 % The \pkg{icomma} package uses |\mathchardef| to save the mathematical code of
721 % the comma character. This breaks for Unicode fonts. The incompatibility was
722 % noticed by
723 % \Breitfeld.\footnote{\url{https://groups.google.com/forum/\#!topic/de.comp.text.tex/Cputk-AJS5I/discussion}}
724 % \changes{v1.10}{2021/03/28}{Use new \Hologo{LaTeX2e} hook management if
725 % available}
726 %
727 % \begin{macro}{\mathcomma}
728 % \pkg{icomma} defines the mathemathical character shorthand \cmd{\icomma} at
729 % the beginning of the document, therefore we again patch
730 % \cmd{\@begindocumenthook}.
731 % \begin{macrocode}
732 \@@_after_package_or_now:nn { icomma } {
733 \@ifl@t@r \fmtversion { 2020/10/01 } {
734 \hook_gput_code:nnn { begindocument } { lualatex-math } {
735 \@@_set_mathchar:NN \mathcomma \,
736 \mathcode `\, = "8000 ~
737 }
738 \hook_gset_rule:nnnn
739 { begindocument } { lualatex-math } { voids } { icomma }
740 } {
741 \tl_replace_once:Nnn \@begindocumenthook {
742 \mathchardef \mathcomma \mathcode `\,
743 } {
744 \@@_set_mathchar:NN \mathcomma \,
745 }
746 }
747 }
748 %</package>
749 % \end{macrocode}
750 % \end{macro}
751 %
752 %
753 % \section{Implementation of the \hologo{LuaLaTeX} module}
754 %
755 % For the Lua module, we use the standard \pkg{luatexbase-modutils} template.
756 % \changes{v1.3}{2013/08/03}{Stop using the deprecated \func{module} function}
757 % \changes{v1.4a}{2015/08/24}{Load all of \pkg{luatexbase}}
758 % \begin{macrocode}
759 %<*lua>
760 lualatex = lualatex or {}
761 lualatex.math = lualatex.math or {}
762 luatexbase.provides_module({
763 name = "lualatex-math",
764 date = "2013/08/03",
765 version = 1.3,
766 description = "Patches for mathematics typesetting with LuaLaTeX",
767 author = "Philipp Stephani",
768 licence = "LPPL v1.3+"
769 })
770 % \end{macrocode}
771 % \begin{function}{unpack}
772 % \changes{v1.3}{2013/08/03}{Integrate Philipp Gesang’s patch to make the
773 % \func{unpack} function compatible with Lua~5.2}
774 % The function \func{unpack} needs to be treated specially as it got moved
775 % around in Lua~5.2.
776 % \begin{macrocode}
777 local unpack = unpack or table.unpack
778 % \end{macrocode}
779 % \end{function}
780 % \changes{v1.4a}{2015/08/24}{Pick up new name for string catcode table
781 % where available}
782 % \begin{macrocode}
783 local cctb = luatexbase.catcodetables or
784 {string = luatexbase.registernumber("catcodetable@string")}
785 % \end{macrocode}
786 %
787 % \changes{v1.5}{2016/03/07}{Removed unused Lua function \func{print_fam_slot}}
788 %
789 % \begin{function}{print_class_fam_slot}
790 % The function \func{print_class_fam_slot} takes one argument which must be a
791 % number. It interprets the argument as a Unicode code point whose
792 % mathematical code is printed in the form
793 % \meta{class}\texttt{\textvisiblespace}\meta{family}\texttt{\textvisiblespace}\meta{slot},
794 % suitable for the right-hand side of \cmd{\Umathchardef}.
795 % \begin{macrocode}
796 function lualatex.math.print_class_fam_slot(char)
797 local code = tex.getmathcode(char)
798 local class, family, slot = unpack(code)
799 local result = string.format("%i %i %i ", class, family, slot)
800 tex.sprint(cctb.string, result)
801 end
802 % \end{macrocode}
803 % \end{function}
804 % \begin{macrocode}
805 return lualatex.math
806 %</lua>
807 % \end{macrocode}
808 %
809 % \Finale
810 \endinput
811
812 % Local Variables:
813 % mode: doctex
814 % coding: utf-8-unix
815 % TeX-engine: luatex
816 % End: