Debian Codegraph @

texmf-dist/source/latex/unicode-math/um-code-mathtext.dtx

raw
1 %%^^A%% um-code-mathtext.dtx -- part of UNICODE-MATH <wspr.io/unicode-math>
2 %%^^A%% The "math text" commands such as `\mathbf` and co.
3
4 % \section{Maths text commands}
5 %
6 % \begin{macrocode}
7 %<*package>
8 % \end{macrocode}
9 %
10 % \subsection{\cs{setmathfontface}}
11 %
12 % \begin{macro}{\@@_setmathfontface:Nnn}
13 % Interface around \cs{SetMathAlphabet}.
14 % \begin{macrocode}
15 \keys_define:nn {@@_mathface}
16 {
17 version .tl_set:N = \l_@@_mversion_tl
18 }
19 % \end{macrocode}
20 %
21 % \begin{macrocode}
22 \@@_cs_new:Nn \@@_setmathfontface:Nnn
23 {
24 \tl_clear:N \l_@@_mversion_tl
25
26 \keys_set_known:nnN {@@_mathface} {#2} \l_@@_keyval_clist
27
28 \fontspec_set_family:Nxx \l_@@_tmpa_tl
29 { ItalicFont={}, BoldFont={}, SmallCapsFont={}, \exp_not:V \l_@@_keyval_clist }
30 { #3 }
31
32 \tl_if_empty:NT \l_@@_mversion_tl
33 {
34 \tl_set:Nn \l_@@_mversion_tl {normal}
35 \DeclareMathAlphabet #1 {\g_fontspec_encoding_tl} {\l_@@_tmpa_tl} {\mddefault} {\shapedefault}
36 }
37
38 \SetMathAlphabet #1 {\l_@@_mversion_tl} {\g_fontspec_encoding_tl} {\l_@@_tmpa_tl} {\mddefault} {\shapedefault}
39
40 % integrate with fontspec's \setmathrm etc:
41 \token_case_meaning:Nn #1
42 {
43 \mathrm { \cs_gset_eq:NN \g__fontspec_mathrm_tl \l_@@_tmpa_tl }
44 \mathsf { \cs_gset_eq:NN \g__fontspec_mathsf_tl \l_@@_tmpa_tl }
45 \mathtt { \cs_gset_eq:NN \g__fontspec_mathtt_tl \l_@@_tmpa_tl }
46 }
47 }
48 % \end{macrocode}
49 % \end{macro}
50 %
51 %
52 % \subsection{Hooks into \LaTeXe}
53 %
54 % Switching to a different style of alphabetic symbols was traditionally performed with
55 % commands like \cmd\mathbf, which literally changes fonts to access alternate symbols.
56 % This is not as simple with Unicode fonts.
57 %
58 % In traditional \TeX{} maths font setups, you simply switch between different `families' (\cmd\fam), which is analogous to changing from one font to another---a symbol such as `a' will be upright in one font, bold in another, and so on.
59 % In pkg{unicode-math}, a different mechanism is used to switch between styles. For every letter (start with ascii a-zA-Z and numbers to keep things simple for now), they are assigned a `mathcode' with \cmd\Umathcode\ that maps from input letter to output font glyph slot. This is done with the equivalent of
60 % \begin{Verbatim}
61 % \Umathcode`\a = 7 1 "1D44E\relax
62 % \Umathcode`\b = 7 1 "1D44F\relax
63 % \Umathcode`\c = 7 1 "1D450\relax
64 % ...
65 % \end{Verbatim}
66 % When switching from regular letters to, say, \cmd\mathrm, we now need to execute a new mapping:
67 % \begin{Verbatim}
68 % \Umathcode`\a = 7 1 `\a\relax
69 % \Umathcode`\b = 7 1 `\b\relax
70 % \Umathcode`\c = 7 1 `\c\relax
71 % ...
72 % \end{Verbatim}
73 % This is fairly straightforward to perform when we're defining our own commands such as \cmd\symbf\ and so on. However, this means that `classical' \TeX\ font setups will break, because with the original mapping still in place, the engine will be attempting to insert unicode maths glyphs from a standard font.
74 %
75 % \begin{macro}{\use@mathgroup}
76 % To overcome this, we patch \cs{use@mathgroup}, which is only used inside of commands
77 % such as \cs{mathXYZ}, so this shouldn't have any side-effects.
78 % Omit the test for math mode because this is only called \emph{inside} \cs{mathrm} or similar,
79 % which already has a math mode check.
80 % \begin{macrocode}
81 \cs_set:Npn \use@mathgroup #1 #2
82 {
83 \math@bgroup
84 \cs_if_eq:cNF {M@\f@encoding} #1 {#1}
85 \@@_switch_to:n {literal}
86 \@@_mathgroup_set:n {#2}
87 \math@egroup
88 }
89 % \end{macrocode}
90 % \end{macro}
91 %
92 % In LaTeX maths, the command |\operator@font| is defined that switches to the |operator| mathgroup. The classic example is the |\sin| in |$\sin{x}$|; essentially we’re using |\mathrm| to typeset the upright symbols, but the syntax is |{\operator@font sin}|.
93 % I thought that hooking into |\operator@font| would be hard because all other maths font selection in 2e uses |\mathrm{...}| style.
94 % Then reading source2e a little more I stumbled upon \cs{@fontswitch}.
95 % Reimplement that here to avoid \cs{bgroup}/\cs{egroup}.
96 % \begin{macro}{\operator@font}
97 % \begin{macrocode}
98 \cs_set_protected:Npn \operator@font
99 {
100 \@@_switch_to:n {literal}
101 \@@_fontswitch:n { \g_@@_operator_mathfont_tl }
102 }
103 % \end{macrocode}
104 % \end{macro}
105 %
106 % \begin{macro}{\@@_fontswitch:n}
107 % Omit the check for math mode as \verb|#1| should do that for us.
108 % \begin{macrocode}
109 \cs_set:Nn \@@_fontswitch:n
110 {
111 \cs_set_eq:NN \math@bgroup \scan_stop:
112 \cs_set_eq:NN \@@_group_begin: \scan_stop:
113 \cs_set:Npn \@@_group_end:n % takes no argument in this case
114 {
115 \cs_set_eq:NN \@@_group_begin: \@@_group_begin_frozen:
116 \cs_set_eq:NN \@@_group_end:n \@@_group_end_frozen:n
117 \cs_set_eq:NN \math@bgroup \@@@@math@bgroup
118 \cs_set_eq:NN \math@egroup \@@@@math@egroup
119 }
120 \cs_set_eq:NN \math@egroup \@@_group_end:n
121 #1 \scan_stop:
122 }
123 % \end{macrocode}
124 % \end{macro}
125 %
126 %
127 % \subsection{Hooks into \pkg{fontspec}}
128 %
129 % Historically, \cs{mathrm} and so on were completely overwritten by \pkg{unicode-math}, and \pkg{fontspec}'s methods for setting these fonts in the classical manner were bypassed.
130 %
131 % While we could now re-activate the way that \pkg{fontspec} does the following, because we can now change maths fonts whenever it's better to define new commands in \pkg{unicode-math} to define the \cs{mathXYZ} fonts.
132 %
133 % \subsubsection{Text font}
134 %
135 % \begin{macrocode}
136 \cs_generate_variant:Nn \tl_if_eq:nnT {o}
137 \@@_cs_set:Nn \__fontspec_setmainfont_hook:nn
138 {
139 \tl_if_eq:onT {\g__fontspec_mathrm_tl} {\rmdefault}
140 {
141 %<XE> \fontspec_gset_family:Nnn \g__fontspec_mathrm_tl {#1} {#2}
142 %<LU> \fontspec_gset_family:Nnn \g__fontspec_mathrm_tl {Renderer=Basic,#1} {#2}
143 \__fontspec_setmathrm_hook:nn {#1} {#2}
144 }
145 }
146 % \end{macrocode}
147 % \begin{macrocode}
148 \@@_cs_set:Nn \__fontspec_setsansfont_hook:nn
149 {
150 \tl_if_eq:onT {\g__fontspec_mathsf_tl} {\sfdefault}
151 {
152 %<XE> \fontspec_gset_family:Nnn \g__fontspec_mathsf_tl {#1} {#2}
153 %<LU> \fontspec_gset_family:Nnn \g__fontspec_mathsf_tl {Renderer=Basic,#1} {#2}
154 \__fontspec_setmathsf_hook:nn {#1} {#2}
155 }
156 }
157 % \end{macrocode}
158 % \begin{macrocode}
159 \@@_cs_set:Nn \__fontspec_setmonofont_hook:nn
160 {
161 \tl_if_eq:onT {\g__fontspec_mathtt_tl} {\ttdefault}
162 {
163 %<XE> \fontspec_gset_family:Nnn \g__fontspec_mathtt_tl {#1} {#2}
164 %<LU> \fontspec_gset_family:Nnn \g__fontspec_mathtt_tl {Renderer=Basic,#1} {#2}
165 \__fontspec_setmathtt_hook:nn {#1} {#2}
166 }
167 }
168 % \end{macrocode}
169 %
170 % \subsubsection{Maths font}
171 %
172 % If the maths fonts are set explicitly, then the text commands above will not execute
173 % their branches to set the maths font alphabets.
174 %
175 % Helper macro for looking up customisable series' by family (new \LaTeXe\ feature 2020).
176 % \begin{macrocode}
177 \cs_new:Nn \@@_rm_series_default:n
178 {
179 \ifcsname #1series@rm\endcsname
180 \csname #1series@rm\endcsname
181 \else
182 \csname #1default\endcsname
183 \fi
184 }
185 % \end{macrocode}
186 % \begin{macrocode}
187 \@@_cs_set:Nn \__fontspec_setmathrm_hook:nn
188 {
189 \SetMathAlphabet\mathrm{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl{\@@_rm_series_default:n {md}}\shapedefault
190 \SetMathAlphabet\mathit{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl{\@@_rm_series_default:n {md}}\itdefault
191 \SetMathAlphabet\mathbf{normal}\g_fontspec_encoding_tl\g__fontspec_mathrm_tl{\@@_rm_series_default:n {bf}}\shapedefault
192 }
193 % \end{macrocode}
194 % \begin{macrocode}
195 \@@_cs_set:Nn \__fontspec_setboldmathrm_hook:nn
196 {
197 \SetMathAlphabet\mathrm{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl{\@@_rm_series_default:n {md}}\shapedefault
198 \SetMathAlphabet\mathbf{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl{\@@_rm_series_default:n {bf}}\shapedefault
199 \SetMathAlphabet\mathit{bold}\g_fontspec_encoding_tl\g__fontspec_bfmathrm_tl\mddefault\itdefault
200 }
201 % \end{macrocode}
202 % \begin{macrocode}
203 \@@_cs_set:Nn \__fontspec_setmathsf_hook:nn
204 {
205 \SetMathAlphabet\mathsf{normal}\g_fontspec_encoding_tl\g__fontspec_mathsf_tl{\@@_rm_series_default:n {md}}\shapedefault
206 \SetMathAlphabet\mathsf{bold} \g_fontspec_encoding_tl\g__fontspec_mathsf_tl{\@@_rm_series_default:n {bf}}\shapedefault
207 }
208 % \end{macrocode}
209 % \begin{macrocode}
210 \@@_cs_set:Nn \__fontspec_setmathtt_hook:nn
211 {
212 \SetMathAlphabet\mathtt{normal}\g_fontspec_encoding_tl\g__fontspec_mathtt_tl{\@@_rm_series_default:n {md}}\shapedefault
213 \SetMathAlphabet\mathtt{bold} \g_fontspec_encoding_tl\g__fontspec_mathtt_tl{\@@_rm_series_default:n {bf}}\shapedefault
214 }
215 % \end{macrocode}
216 %
217 % I can't quite remember the logic behind the following two.
218 %
219 % If \pkg{fontspec} has been loaded and \verb|\setmathsf| (etc) run, this
220 % syncs things up:
221 % \begin{macrocode}
222 \tl_if_eq:onF {\g__fontspec_mathrm_tl} {\rmdefault} { \__fontspec_setmathrm_hook:nn {} {} }
223 \tl_if_eq:onF {\g__fontspec_mathsf_tl} {\sfdefault} { \__fontspec_setmathsf_hook:nn {} {} }
224 \tl_if_eq:onF {\g__fontspec_mathtt_tl} {\ttdefault} { \__fontspec_setmathtt_hook:nn {} {} }
225 % \end{macrocode}
226 %
227 % I suppose this is to make things work if neither fontspec or unicode-math
228 % load any fonts: (I should check that)
229 % \begin{macrocode}
230 \AtBeginDocument
231 {
232 \tl_if_eq:onT {\g__fontspec_mathrm_tl} {\rmdefault} { \__fontspec_setmathrm_hook:nn {} {} }
233 \tl_if_eq:onT {\g__fontspec_mathsf_tl} {\sfdefault} { \__fontspec_setmathsf_hook:nn {} {} }
234 \tl_if_eq:onT {\g__fontspec_mathtt_tl} {\ttdefault} { \__fontspec_setmathtt_hook:nn {} {} }
235 }
236 % \end{macrocode}
237 %
238 % \begin{macrocode}
239 %</package>
240 % \end{macrocode}
241
242 \endinput
243
244 % /©
245 %
246 % ------------------------------------------------
247 % The UNICODE-MATH package <wspr.io/unicode-math>
248 % ------------------------------------------------
249 % This package is free software and may be redistributed and/or modified under
250 % the conditions of the LaTeX Project Public License, version 1.3c or higher
251 % (your choice): <http://www.latex-project.org/lppl/>.
252 % ------------------------------------------------
253 % Copyright 2006-2019 Will Robertson, LPPL "maintainer"
254 % Copyright 2010-2017 Philipp Stephani
255 % Copyright 2011-2017 Joseph Wright
256 % Copyright 2012-2015 Khaled Hosny
257 % ------------------------------------------------
258 %
259 % ©/