Debian Codegraph @

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

raw
1 %%^^A%% um-code-setchar.dtx -- part of UNICODE-MATH <wspr.io/unicode-math>
2 %%^^A%% General assignment of maths symbols.
3
4 % \section{Setting up maths chars}
5 %
6 % \begin{macrocode}
7 %<*package>
8 % \end{macrocode}
9 %
10 % \subsection{A token list to contain the data of the math table}
11 %
12 % Instead of \cmd\input-ing the unicode math table every time we
13 % want to re-read its data, we save it within a macro. This has two
14 % advantages: 1.~it should be slightly faster, at the expense of memory;
15 % 2.~we don't need to worry about catcodes later, since they're frozen
16 % at this point.
17 %
18 % In time, the case statement inside |set_mathsymbol| will be moved in here
19 % to avoid re-running it every time.
20 %
21 % \begin{macrocode}
22 \group_begin:
23 \file_get:nnN {unicode-math-table.tex} {} \l_@@_mathtable_tl
24 \cs_set:Npn \UnicodeMathSymbol #1#2#3#4
25 {
26 \exp_not:n { \_@@_sym:nnn {#1} {#2} {#3} }
27 }
28 \tl_gset:Nx \g_@@_mathtable_tl {\l_@@_mathtable_tl}
29 \group_end:
30 % \end{macrocode}
31 %
32 %
33 % \begin{macro}{\@@_input_math_symbol_table:}
34 % This function simply expands to the token list containing all the data.
35 % \begin{macrocode}
36 \@@_cs_new:Nn \@@_input_math_symbol_table: {\g_@@_mathtable_tl}
37 % \end{macrocode}
38 % \end{macro}
39 %
40 %
41 % \subsection{Definitions of the active math characters}
42 %
43 % Ensure catcodes are appropriate;
44 % make sure |#| is an `other' so that we don't get confused with \cs{mathoctothorpe}.
45 % \begin{macrocode}
46 \AtBeginDocument{\@@_define_math_chars:}
47 \@@_cs_new:Nn \@@_define_math_chars:
48 {
49 \group_begin:
50 \cs_set:Npn \_@@_sym:nnn ##1##2##3
51 {
52 \tl_if_in:nnT
53 { \mathord \mathalpha \mathbin \mathrel \mathpunct \mathop \mathfence }
54 {##3}
55 {
56 \exp_last_unbraced:NNx \cs_gset_eq:NN ##2 { \char_generate:nn {##1} {12} }
57 }
58 }
59 \@@_input_math_symbol_table:
60 \group_end:
61 }
62 % \end{macrocode}
63 %
64 %
65 %
66 % \subsection{Commands for each symbol/glyph/char}
67 %
68 % \begin{macro}{\@@_set_mathsymbol:nNNn}
69 % \darg{A \LaTeX\ symbol font, e.g., \texttt{operators}}
70 % \darg{Symbol macro, \eg, \cmd\alpha}
71 % \darg{Type, \eg, \cmd\mathalpha}
72 % \darg{Slot, \eg, \texttt{"221E}}
73 % There are a bunch of tests to perform to process the various characters.
74 % The following assignments should all be fairly straightforward.
75 %
76 % The catcode setting is to work around (strange?) behaviour in LuaTeX in which catcode 11 characters don't have italic correction for maths.
77 % We don't adjust ascii chars, however, because certain punctuation should not have their catcodes changed.
78 % \begin{macrocode}
79 \cs_set:Nn \@@_set_mathsymbol:nNNn
80 {
81 \bool_lazy_and:nnT
82 {
83 \int_compare_p:nNn {#4} > {127}
84 }
85 {
86 \int_compare_p:nNn { \char_value_catcode:n {#4} } = {11}
87 }
88 { \char_set_catcode_other:n {#4} }
89
90 \token_case_meaning:Nn #3
91 {
92 \mathord { \@@_set_mathcode:nnn {#4} {#3} {#1} }
93 \mathalpha { \@@_set_mathcode:nnn {#4} {#3} {#1} }
94 \mathbin { \@@_set_mathcode:nnn {#4} {#3} {#1} }
95 \mathrel { \@@_set_mathcode:nnn {#4} {#3} {#1} }
96 \mathpunct { \@@_set_mathcode:nnn {#4} {#3} {#1} }
97 \mathop { \@@_set_big_operator:nnn {#1} {#2} {#4} }
98 \mathopen { \@@_set_math_open:nnn {#1} {#2} {#4} }
99 \mathclose { \@@_set_math_close:nnn {#1} {#2} {#4} }
100 \mathfence { \@@_set_math_fence:nnnn {#1} {#2} {#3} {#4} }
101 \mathaccent
102 { \@@_set_math_accent:Nnnn #2 {fixed} {#1} {#4} }
103 \mathbotaccent
104 { \@@_set_math_accent:Nnnn #2 {bottom~ fixed} {#1} {#4} }
105 \mathaccentwide
106 { \@@_set_math_accent:Nnnn #2 {} {#1} {#4} }
107 \mathbotaccentwide
108 { \@@_set_math_accent:Nnnn #2 {bottom} {#1} {#4} }
109 \mathover
110 { \@@_set_math_overunder:Nnnn #2 {} {#1} {#4} }
111 \mathunder
112 { \@@_set_math_overunder:Nnnn #2 {bottom} {#1} {#4} }
113 \mathaccentoverlay
114 %<LU> { \@@_set_math_accent:Nnnn #2 {overlay~ fixed} {#1} {#4} }
115 %<XE> { \@@_set_math_accent:Nnnn #2 {} {#1} {#4} }
116 }
117 }
118 % \end{macrocode}
119 % \end{macro}
120 %
121 % \begin{macrocode}
122 \edef\mathfence{\string\mathfence}
123 \edef\mathover{\string\mathover}
124 \edef\mathunder{\string\mathunder}
125 \edef\mathbotaccent{\string\mathbotaccent}
126 \edef\mathaccentwide{\string\mathaccentwide}
127 \edef\mathaccentoverlay{\string\mathaccentoverlay}
128 \edef\mathbotaccentwide{\string\mathbotaccentwide}
129 % \end{macrocode}
130 %
131 %
132 % \begin{macro}{\@@_set_big_operator:nnn}
133 % \darg{Symbol font name}
134 % \darg{Macro to assign}
135 % \darg{Glyph slot}
136 % In the examples following, say we're defining for the symbol \cmd\sum\ ($\sum$).
137 % In order for literal Unicode characters to be used in the source and still
138 % have the correct limits behaviour, big operators are made math-active.
139 % This involves three steps:
140 % \begin{itemize}
141 % \item
142 % The active math char is defined to expand to the macro \cs{sum_sym}.
143 % (Later, the control sequence \cs{sum} will be assigned the math char.)
144 % \item
145 % Declare the plain old mathchardef for the control sequence \cmd\sumop.
146 % (This follows the convention of \LaTeX/\pkg{amsmath}.)
147 % \item
148 % Define \cs{sum_sym} as \cmd\sumop, followed by \cmd\nolimits\ if necessary.
149 % \end{itemize}
150 % Whether the \cmd\nolimits\ suffix is inserted is controlled by the
151 % token list \cs{l_@@_nolimits_tl}, which contains a list of such characters.
152 % This list is checked dynamically to allow it to be updated mid-document.
153 %
154 % Examples of expansion, by default, for two big operators:
155 % \begin{quote}
156 % (~\cs{sum} $\to$~) $\sum$ $\to$ \cs{sum_sym} $\to$ \cs{sumop}\cs{nolimits}\par
157 % (~\cs{int} $\to$~) $\int$ $\to$ \cs{int_sym} $\to$ \cs{intop}
158 % \end{quote}
159 % \begin{macrocode}
160 \cs_new:Nn \@@_set_big_operator:nnn
161 {
162 \@@_char_gmake_mathactive:n {#3}
163 \cs_set_protected_nopar:Npx \@@_tmpa: { \exp_not:c { \cs_to_str:N #2 _sym } }
164 \char_gset_active_eq:nN {#3} \@@_tmpa:
165
166 \@@_set_mathchar:cNnn {\cs_to_str:N #2 op} \mathop {#1} {#3}
167
168 \cs_gset:cpx { \cs_to_str:N #2 _sym }
169 {
170 \exp_not:c { \cs_to_str:N #2 op }
171 \exp_not:n { \tl_if_in:NnT \l_@@_nolimits_tl {#2} \nolimits }
172 }
173 }
174 % \end{macrocode}
175 % \end{macro}
176 %
177 % \begin{macro}{\@@_set_math_open:nnn}
178 % \darg{Symbol font name}
179 % \darg{Macro to assign}
180 % \darg{Glyph slot}
181 % \begin{macrocode}
182 \cs_new:Nn \@@_set_math_open:nnn
183 {
184 \tl_if_in:NnTF \l_@@_radicals_tl {#2}
185 {
186 \cs_if_exist:NF #2
187 {
188 %% todo: check if the check is necessary
189 \cs_gset_protected_nopar:Npx #2 { \exp_not:c { \cs_to_str:N #2 sign } }
190 }
191 \cs_gset_protected_nopar:cpx { \cs_to_str:N #2 sign }
192 {
193 \@@_radical:nn {#1} {#3}
194 }
195 \tl_if_exist:cF {c_@@_radical_\cs_to_str:N #2_tl}
196 {
197 \tl_const:cn {c_@@_radical_\cs_to_str:N #2_tl} {\use:c{sym #1}~ #3}
198 }
199 }
200 {
201 \@@_set_delcode:nnn {#1} {#3} {#3}
202 \@@_set_mathcode:nnn {#3} \mathopen {#1}
203 \cs_gset_protected_nopar:Npx #2
204 { \@@_delimiter:Nnn \mathopen {#1} {#3} }
205 }
206 }
207 % \end{macrocode}
208 % \end{macro}
209 %
210 % \begin{macro}{\@@_set_math_close:nnn}
211 % \darg{Symbol font name}
212 % \darg{Macro to assign}
213 % \darg{Glyph slot}
214 % \begin{macrocode}
215 \cs_new:Nn \@@_set_math_close:nnn
216 {
217 \@@_set_delcode:nnn {#1} {#3} {#3}
218 \@@_set_mathcode:nnn {#3} \mathclose {#1}
219 \cs_gset_protected_nopar:Npx #2
220 { \@@_delimiter:Nnn \mathclose {#1} {#3} }
221 }
222 % \end{macrocode}
223 % \end{macro}
224 %
225 % \begin{macro}{\@@_set_math_fence:nnnn}
226 % \darg{Symbol font name}
227 % \darg{Macro to assign}
228 % \darg{Type, \eg, \cmd\mathalpha}
229 % \darg{Glyph slot}
230 % \begin{macrocode}
231 \cs_new:Nn \@@_set_math_fence:nnnn
232 {
233 \@@_set_mathcode:nnn {#4} {#3} {#1}
234 \@@_set_delcode:nnn {#1} {#4} {#4}
235 \cs_gset_protected_nopar:cpx {l \cs_to_str:N #2}
236 { \@@_delimiter:Nnn \mathopen {#1} {#4} }
237 \cs_gset_protected_nopar:cpx {r \cs_to_str:N #2}
238 { \@@_delimiter:Nnn \mathclose {#1} {#4} }
239 }
240 % \end{macrocode}
241 % \end{macro}
242 %
243 % \begin{macro}{\@@_set_math_accent:Nnnn}
244 % \darg{Accend command}
245 % \darg{Accent type (string)}
246 % \darg{Symbol font name}
247 % \darg{Glyph slot}
248 % \begin{macrocode}
249 \cs_new:Nn \@@_set_math_accent:Nnnn
250 {
251 \cs_gset_protected_nopar:Npx #1
252 { \@@_accent:nnn {#2} {#3} {#4} }
253 }
254 % \end{macrocode}
255 % \end{macro}
256 %
257 % \begin{macro}{\@@_set_math_overunder:Nnnn}
258 % \darg{Accend command}
259 % \darg{Accent type (string)}
260 % \darg{Symbol font name}
261 % \darg{Glyph slot}
262 % \begin{macrocode}
263 \cs_new:Nn \@@_set_math_overunder:Nnnn
264 {
265 \cs_gset_protected_nopar:Npx #1 ##1
266 {
267 \mathop
268 { \@@_accent:nnn {#2} {#3} {#4} {{}##1} }
269 % TODO: remove braces above ^^ which work around a LuaTeX bug
270 \limits
271 }
272 }
273 % \end{macrocode}
274 % \end{macro}
275 %
276 %
277 % \begin{macrocode}
278 %</package>
279 % \end{macrocode}
280
281 \endinput
282
283 % /©
284 %
285 % ------------------------------------------------
286 % The UNICODE-MATH package <wspr.io/unicode-math>
287 % ------------------------------------------------
288 % This package is free software and may be redistributed and/or modified under
289 % the conditions of the LaTeX Project Public License, version 1.3c or higher
290 % (your choice): <http://www.latex-project.org/lppl/>.
291 % ------------------------------------------------
292 % Copyright 2006-2019 Will Robertson, LPPL "maintainer"
293 % Copyright 2010-2017 Philipp Stephani
294 % Copyright 2011-2017 Joseph Wright
295 % Copyright 2012-2015 Khaled Hosny
296 % ------------------------------------------------
297 %
298 % ©/