Debian Codegraph @

texmf-dist/tex/optex/base/unimath-macros.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \loadmath {Unicode Math fonts <2025-04-03>} % preloaded in format
4
5 \_doc -----------------------------
6 \`\loadmath` `<optional-factor> {<Unicode-math font>}` loads the given font. It does:
7 \begitems
8 * define \`\_unimathfont` as `<Unicode-math font>`,
9 * redefine `\normalmath` and `\boldmath` macros to their Unicode counterparts,
10 * save the `<optional-factor>` as scaling factor, see also \^`\_mfactor`,
11 * load the `\_unimathfont` by `\normalmath`,
12 * print information about the loaded font on the terminal,
13 * redefine all encoding dependent setting by `\input unimath-codes.opm`,
14 * protect new loading by setting \`\_ifmathloading` to false.
15 \enditems
16 \`\noloadmath` disallows Unicode-math loading by \`\_mathloadingfalse`.\nl
17 \`\doloadmath` allows Unicode-math loading by \`\_mathloadingtrue`.
18 \_cod -----------------------------
19
20 \_newifi \_ifmathloading \_mathloadingtrue
21
22 \_def\_noloadmath{\_mathloadingfalse}
23 \_def\_doloadmath{\_mathloadingtrue}
24
25 \_def\_loadmath#1#{\_loadmathA{#1}}
26 \_def\_loadmathA#1#2{%
27 \_ifmathloading
28 \_initunifonts
29 \_isfont{#2}\_iffalse
30 \_opwarning{Math font "#2" not found, skipped...}%
31 \_else
32 \_sdef{_mfactor:1}{#1}\_def\_unimathfont{#2}%
33 \_let\_normalmath = \_normalunimath \_let\_boldmath = \_boldunimath
34 \_normalmath
35 \_wterm {MATH-FONT: "#2" -- unicode math prepared.}%
36 \_ifx\_ncharrmA\_undefined \_opinput {unimath-codes.opm}\_fi
37 \_mathloadingfalse
38 \_fi\_fi}
39
40 \_public \loadmath \noloadmath \doloadmath ;
41
42 \_doc -----------------------------
43 \`\loadboldmath` `{<bold-font>} \to {<normal-font>}`
44 defines `\_bmath:<normal-font>` as `<bold-font>`.
45 It is used when \^`\boldmath` macro is run.
46 When no `\_bmath:<normal-font>` is defined then the `\boldmath` macro
47 use \"fake bold" generated by `embolden` \LuaTeX/ font feature.
48 \_cod -----------------------------
49
50 \_def\_loadboldmath#1#2\to #3{%
51 \_isfont{#1}\_iffalse
52 \_opwarning{Bold-Math font "#1" not found, skipped...}%
53 \_else \_sdef{_bmath:#1}{#3}%
54 \_wterm {BOLD-MATH-FONT: "#1" with "#3".}%
55 \_fi}
56
57 \_public \loadboldmath ;
58
59 \_doc -----------------------------
60 The Unicode version of the \^`\normalmath` and \^`\boldmath` macros
61 are defined here as \`\_normalunimath` and \`\_boldunimath` macros.
62 The base font \^`\_unimathfont` is loaded to family 1 with `script=math`
63 feature. It is generally used for all variables, digits and math symbols.
64 The same font is loaded to family 2 with `script=latn`. It can be used for
65 sequences of letters where we don't want to insert italic correction
66 between them. The default \^`\rm` and \^`\it` macros run \^`\_rmletters` and
67 \^`\_itletters` and they use family 2, so these selectors prohibit italic
68 corrections between letters.
69 \nl
70 You can combine more fonts if you register them to another
71 math families (5, 6, 7, etc.) in the \^`\normalmath` macro.
72 \nl
73 The default value of \^`\_normalunimath` shows a combination of base Unicode-math
74 font at family 1 with 8bit Math font at family 4. See definition of `\script` macro where
75 `\fam4` is used.
76 The final macro \`\_setunimathdimens` does similar work as \^`\_setmathdimens`.
77 \_cod -----------------------------
78
79 \_def\_normalunimath{%
80 \_setmathfamily 0 \_tenrm % font for non-math objects in math mode
81 \_loadumathfamily 1 {\_unimathfont}{script=math;} % Base math font
82 \_loadumathfamily 2 {\_unimathfont}{script=latn;} % Base font, no italic corr. between letters
83 \_loadmathfamily 4 rsfs % script
84 \_setunimathdimens
85 }%
86 \_def\_boldunimath{%
87 \_setmathfamily 0 \_tenbf % font for non-math objects in math mode
88 \_ifcsname _bmath:\_unimathfont \_endcsname
89 \_loadumathfamily 1 {\_cs{_bmath:\_unimathfont}}{script=math;} % Base real bold font
90 \_loadumathfamily 2 {\_cs{_bmath:\_unimathfont}}{script=latn;} % Base bold, no italcorr.
91 \_else
92 \_loadumathfamily 1 {\_unimathfont}{script=math;embolden=1.7;} % Base faked bold
93 \_loadumathfamily 2 {\_unimathfont}{script=latn;embolden=1.7;} % Base faked, no italcor.
94 \_fi
95 \_loadmathfamily 4 rsfs % script
96 \_setunimathdimens
97 }%
98 \_def\_setunimathdimens{% PlainTeX sets these dimens for 10pt size only:
99 \_delimitershortfall=0.5\_fontdimen6\_textfont1
100 \_nulldelimiterspace=0.12\_fontdimen6\_textfont1
101 \_setmathparam\_Umathspaceafterscript \_scriptspacefactor
102 \_setbox0=\_hbox{\_everymath{}$\_fam1\_displaystyle{0\_atop0}$}%
103 \_Umathfractiondelsize\_displaystyle = \_dimexpr(\_ht0-\_Umathaxis\_displaystyle)*2\_relax
104 \_setbox0=\_box\_voidbox
105 }
106
107 \_doc -----------------------------
108 If you try the example above about
109 \~`\loadboldmath``{[xitsmath-bold]} \to {[xitsmath-regular]}`
110 then you can find a bug in XITSMath-Bold font: the symbols for norm
111 $\|x\|$ are missing. So, we have to define `\_boldmath` macro manually.
112 The missing symbol is loaded from family 5 as no-bold variant in our example:
113 \begtt
114 \loadmath{[xitsmath-regular]}
115 \def\_boldmath{%
116 \_loadumathfamily 1 {[xitsmath-bold]}{} % Base font
117 \_loadmathfamily 4 rsfs % script
118 \_loadumathfamily 5 {[xitsmath-regular]}{}
119 \_def\|{\_Udelimiter 0 5 "02016 }% % norm delimiter from family 5
120 \_setmathdimens
121 }
122 \endtt
123
124 \`\_loadumathfamily` `<number> {<font>}{<font features>}`
125 loads the given Unicode-math fonts in three sizes
126 using single <font> with different `mathsize=1,2,3` font features.
127 The math font family is set with given `<number>`.
128 The `<font features>` are added to the default
129 \`\_mfontfeatures` and to the size-dependent features `ssty=1`
130 if script size is asked or `ssty=2` if scriptscriptsize is asked.\nl
131 \`\_mparams` can insert additional font features
132 depending on the current `\_mfam`.\nl
133 The \^`\_mfactor` `<family><space>` sets scaling factor,
134 see section~\ref[math-preload] for more information.\nl
135 The \`\_textmff`, \`\_scriptmff` and \`\_sscriptmff` are font
136 features for text, script and sscript sizes respectively. They are
137 locally re-defined in \^`\mathbox` macro.
138 \_cod -----------------------------
139
140 \_def\_umathname#1#2{"#1:\_mfontfeatures#2"}
141 \_def\_mfontfeatures{mode=base;}
142
143 \_def\_loadumathfamily{\_afterassignment\_loadumathfamilyA \_chardef\_mfam}
144 \_def\_loadumathfamilyA #1#2 {\_mfactor
145 \_font\_mF \_umathname{#1}{\_textmff \_mparams #2} at\_sizemtext \_textfont \_mfam=\_mF
146 \_font\_mF \_umathname{#1}{\_scriptmff \_mparams #2} at\_sizemtext \_scriptfont \_mfam=\_mF
147 \_font\_mF \_umathname{#1}{\_sscriptmff\_mparams #2} at\_sizemtext \_scriptscriptfont \_mfam=\_mF
148 }
149 \_def\_textmff {ssty=0;mathsize=1;}
150 \_def\_scriptmff {ssty=1;mathsize=2;}
151 \_def\_sscriptmff{ssty=2;mathsize=3;}
152 \_def\_mparams{}
153
154 \_doc -----------------------------
155 \`\addUmathfont` `<fam> {[<normal-font>]}{<ffeatures>} {[<bold-font>]}{<ffeatures>} {<factor>}`
156 allocates new <fam> using \^`\newfam` and adds loading this font to the
157 \~`\normalmath` and \~`\boldmath` macros. Note that allocationos using
158 \^`\newfam` starts from 43 because numbers 1--42 are reserved for direct
159 usage without \^`\newfam`.
160 We use \^`\aheadto` here because we want to read the main family 1 as
161 last one (for definitive setting of math parameters).
162 \_cod -----------------------------
163
164 \_def\_addUmathfont #1#2#3#4#5#6{% #1: fam (will be set), #2#3: normal font, #4#5: bold font
165 \_ifx\_ncharrmA\_undefined \_errmessage{basic Unicode math font must be loaded first}%
166 \_else \_isfont{#2}\_iffalse \_opwarning{font #2 is unavailable}%
167 \_else
168 \_newfam#1\_relax
169 \_sdef{_mfactor:\_the\_numexpr#1\_relax}{#6}%
170 \_global\_aheadto\_normalmath{\_loadumathfamily #1{#2}{\_addUmathdefault #3} }%
171 \_ifx\_relax#4\_relax
172 \_global\_aheadto\_boldmath{\_loadumathfamily #1{#2}{\_addUmathdefault embolden=1.7;#3} }%
173 \_else
174 \_global\_aheadto\_boldmath{\_loadumathfamily #1{#4}{\_addUmathdefault #5} }%
175 \_fi
176 \_normalmath
177 \_wterm{add-MATH-FONT: #1=\_the#1, "#2", \_ifx"#4"\_else bold: "#4"\_fi}%
178 \_fi \_fi
179 }
180 \_def\_addUmathdefault {script=math;} % default font features when \_addUmathfont is used
181
182 \_doc -----------------------------
183 The math characters can be given directly (by their Unicode) or by a macro
184 like `\doteq`, `\times`, etc. These macros simply expand to the math character
185 with its Unicode. And this math character has its `\Umathcode` given by
186 <class>, <family>, <slot-number>. Sometimes, we may want to get these
187 quantities from the given Unicode math character by our macros. It is
188 possible by \`\themathcodeclass``<math-char>`,
189 \`\themathcodefam``<math-char>` and \`\themathcodechar``<math-char>` macros.
190 The parameter <math-char> is a math character or it is a macro like `\doteq`, `\times`.
191 Moreower, \`\thedelcodefam``<math-char>` and \`\thedelcodechar``<math-char>`
192 return delcode quaitities of given math character.
193 All these commands use the common Lua code defined in
194 the \`\_getmathcode``<code>{<math or del>}<character number>` macro.
195 \_cod -----------------------------
196
197 \_def\_getmathcode#1#2{\_directlua{tex.print(tex.get#2code(token.scan_int())[#1])}}
198 \_def\_themathcodeclass #1{\_getmathcode 1{math}\_ea`#1 }
199 \_def\_themathcodefam #1{\_getmathcode 2{math}\_ea`#1 }
200 \_def\_themathcodechar #1{\_getmathcode 3{math}\_ea`#1 }
201 \_def\_thedelcodefam #1{\_getmathcode 1{del}\_ea`#1 }
202 \_def\_thedelcodechar #1{\_getmathcode 2{del}\_ea`#1 }
203
204 \_public \themathcodeclass \themathcodefam \themathcodechar \thedelcodefam \thedelcodechar ;
205
206 \_doc -----------------------------
207 \`\mathchars` `<fam> {<list of sequences>}`
208 saves <fam> to \`\_mafam` and runs for each sequence from the <list of sequences>
209 the relevant code settings using `\Umathcode` primitive.
210 In case of `\int`-like operators the <math class>=8 and we only
211 re-declare `\_int:<int-character>` as an operator with the new \^`\_mafam`.
212 Note that the used primitives have the syntax:
213 \begtt \catcode`<=13
214 \Umathchardef <sequence> = <math class> <math family> <slot number>
215 \Umathcode <code> = <math class> <math family> <slot number>
216 \Udelcode <code> = <math family> <slot number>
217 \endtt
218 \_cod -----------------------------
219
220 \_def\_mathchars {\_afterassignment\_mathcharsA \_chardef\_mafam=}
221 \_def\_mathcharsA #1{\_foreach #1\_do{%
222 \_chardef\_tmp=\_themathcodeclass##1\_relax
223 \_ifnum\_tmp=8 % \int, \iint, \oint, etc.
224 \_ea\_Umathchardef \_csname _int:##1\_endcsname =1 \_mafam \_ea`##1
225 \_else
226 \_Umathcode \_ea`##1=\_tmp \_mafam \_themathcodechar##1
227 \_fi
228 }}
229
230 \_doc -----------------------------
231 \`\mathcodes` `<fam> {<list of pairs>}` sets mathcodes of given
232 characters with explicit <class>es. Each pair can be `<class>{<list of chars>}`
233 and `<list of chars>` can include \`\Urange` `<from>-<to>`.
234 This is reason why we apply `\expanded` to the `<list of chars>`
235 before reading it by \^`\foreach`: the \^`\Urange` is expandable
236 and expands to the relevant list of characters.
237 \_cod -----------------------------
238
239 \_def\_mathcodes{\_afterassignment\_mathcodesA\_chardef\_mafam=}
240 \_def\_mathcodesA#1{%
241 \_foreach #1\_do ##1##2{%
242 \_ea\_foreach\_expanded{##2}\_do{\_Umathcode `####1=##1\_mafam \_ea`####1}%
243 }%
244 }
245 \_def\_Urange #1-#2{\_fornum \_ea`#1..\_ea`#2\_do{\_Uchar##1 }}
246
247 \_public \addUmathfont \mathchars \mathcodes \Urange ;
248
249 \_endcode
250
251
252 The \^`\loadmath` `<optional-factor> {<Unicode-math font>}` macro loads the given math font and
253 redefines all default math-codes using `\input unimath-codes.opm`.
254 If Unicode-math font is loaded then \^`\_mathloadingfalse`
255 is set, so the new Unicode-math font isn't loaded until \^`\doloadmath` is used.
256
257 The `<optional-factor>` is scaling factor of loaded font with respect to the
258 size of the text font. It can be used if the used text font and loaded math font have
259 incompatible ex height. If missing then the scaling factor is 1.
260
261 \^`\loadboldmath` `{<bold-font>} \to {<normal-font>}` loads bold variant only
262 if `<normal-font>` was sucessfully loaded by the previous `\loadmath`. For example:
263
264 \begtt
265 \loadmath {[xitsmath-regular]}
266 \loadboldmath {[xitsmath-bold]} \to {[xitsmath-regular]}
267 \endtt
268 There are very few Unicode-math fonts with full \~`\boldmath` support.
269 I know only XITSMath-Bold and KpMath-Bold. If \^`\loadboldmath` is not used
270 then \"faked bold" created from \~`\normalmath` is used by default.
271
272 The {\em main math font} is loaded by \^`\loadmath` (typically indirectly using
273 \^`\fontfam`) and you can load more {\em additional math fonts} by
274 \^`\addUmathfont`:
275 \begtt \catcode`<=13
276 \addUmathfont \famname {[<normal-font>]}{<ffeatures>} {[<bold-font>]}{<ffeatures>} {<factor>}
277 \endtt
278 The `\famname` is a control sequence declared by \^`\addUmathfont` for later
279 use. It gets math family number. The `<factor>` is decimal number for size
280 corrections in view of the main math font. If it is empty then <factor>=1.
281 If <bold-font> is empty, the \"faked bold" derived from <normal-font> is used.
282 Example:
283 \begtt
284 \fontfam[lm] % does \lodmath{[latinmodern-math]}
285 \addUmathfont \xits {[XITSMath-Regular]}{} {[XITSMath-Bold]}{} {}
286 \endtt
287 %
288 declares `latinmodern-math` as main math font (its bold variant is \"faked bold").
289 The additional math font family `\xits` is declared in the example. It uses
290 `XITSMath-Regular` for normal printing and `XITSMath-Bold` for bold printing.
291
292 All characters used in math formula are printed from main math font by
293 default. But you can re-declare characters for printing from
294 additional font by \^`\mathchars` `\famname {<list of sequences>}`. For
295 example:
296
297 \begtt
298 \mathchars \xits {\stareq \triangleq \veeeq \wedgeq}
299 \endtt
300 %
301 sets the characters `\stareq`, `\triangleq`, `\veeeq`, `\wedgeq` from
302 the `\xits` additional font. The <list of sequences> can include
303 control sequences from the `unicode-table.tex`, but no math
304 accents. These control sequences can be printed by `\input print-unimath.opm`.
305
306 The \^`\mathchars` macro keeps the class and slot of declared math objects
307 and re-declares only family of them. It is applied to all control
308 sequences given in the parameter. The relevant math codes are re-declared.
309
310 Use `\addto\selector{\fam\famname}` if you want to print whole math alphabet
311 from an additional math font. For example `\addto\cal{\fam\xits}`
312 declares all `\cal` characters from the `\xits` font loaded by
313 \^`\addUmathfont`.
314
315 The \^`\mathcodes` macro provides comfortable settings of math codes of math
316 objects. Its syntax is \^`\mathcodes` `<family> {<list-of-pairs>}`. Each pair
317 in the <list-of-pairs> is `<class-number><character>` (separated by optional
318 space) or `<class-number>{<list-of-characters>}`. The <list-of-characters>
319 includes declared characters or \^`\Urange` `<from>-<to>` which is equal to the
320 list of characters beginning <from> and ending <to>, for example
321 `\Urange a-d` is equal to `abcd`. The characters can be given directly or
322 by the math sequences like `\times`, `\doteq` too.
323
324 The \^`\mathcodes` macro declares mathcode of given characters internally by
325 \begtt \catcode`<=13
326 \Umathcode `<character> = <class-number> <family> `<character>
327 \endtt
328
329 The \^`\mathcodes` macro sets math codes of given Unicode characters.
330 The relevant control sequence from `unicode-table.tex` changes its
331 behavior too. For example, If you change math code of × then the
332 `\times` control sequence will behave like new declared ×.
333
334
335 \secc Unicode-math macros preloaded in the format
336
337 \printdoc unimath-macros.opm
338
339
340 \secc[unimath-codes] Macros and codes set when `\loadmath` is processed firstly
341
342 The file `unimath-codes.opm` is loaded when the \^`\loadmath` is used. The
343 macros here redefines globally all encoding dependent settings declared in
344 the section~\ref[math-macros].
345
346 \printdoc unimath-codes.opm
347 \printdoctail unimath-codes.opm
348
349 \secc Printing all Unicode math slots in used math font
350
351 \printdoctail print-unimath.opm
352 \printdoc print-unimath.opm
353
354 \_endinput
355
356 2025-04-03 \loadboldmath reimplemented
357 2025-02-06 fam2 introduced: no italic corrections are inserted using fam2
358 2023-09-03 mathsize does work (luaotfload v. 3.36 corrected the bug
359 2023-08-28 mathsize doesn't work now
360 2023-07-03 \loadmath: optional scaling factor introduced
361 2023-01-17 \mathchars: \int operators re-implemented
362 2023-01-15 \_def\_umathrange moved to unimath-codes.opm
363 2023.01-15 \themathcodeclass etc. introduced
364 2022-12-01 reading number by \_loadumathfamily more robust
365 2022-11-18 \mathchars improved, \Udelcodes are set too, \addUmathfont uses \ahead
366 2022-11-17 \Umathchardef replaced by \chardef
367 2022-11-16 \resetmathchars removed, \mathchars, \mathcodes introduced
368 2022-11-13 \_setfontfamily 0 added.
369 2022-07-22 \Umathspaceafterscript setting added.
370 2022-06-10 \addUmathfont and \resetmathchars added.
371 2021-08-16 \_loadumathfamily simplified, used mathsize= font feature.
372 2021-08-16 nomathparam font feature used for families > 3 (bug fixed).
373 2021-08-02 ssty fontfeatures rewritten; fam2, fam3 unused, \scriptspace not set
374 2021-04-04 \_setunimathdimens: \setbox0=\box\voidbox added
375 2021-03-09 \_setunimathdimes: \_begin/end/group instead {}, bug fixed
376 2021-02-15 \_textmff, \_scriptmff and \_sscriptmff introduced
377 2021-02-15 \_expandafter -> \_ea
378 2020-06-07 \Umathfractiondelsize (for \choose brackets) corrected, see https://tug.org/pipermail/luatex/2020-June/007365.html
379 2020-04-15 \_setmathdimens -> \_setuniathdimens
380 2020-02-25 implemented