summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/unicode-math/um-code-sym-commands.dtx
blob: cd7263ecf9e81505f214ef0432d111ebdd2ec179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
%%^^A%% um-code-sym-commands.dtx -- part of UNICODE-MATH <wspr.io/unicode-math>
%%^^A%% Definition of "math symbol alphabet" commands such as `\symbf` and co.

% \section{Mapping in maths alphabets}
% \label{sec:symcmd}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% Switching to a different style of alphabetic symbols was traditionally performed with
% commands like \cmd\mathbf, which literally changes fonts to access alternate symbols.
% This is not as simple with Unicode fonts.
%
% 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.
% 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
% \begin{Verbatim}
% \Umathcode`\a = 7 1 "1D44E\relax
% \Umathcode`\b = 7 1 "1D44F\relax
% \Umathcode`\c = 7 1 "1D450\relax
% ...
% \end{Verbatim}
% When switching from regular letters to, say, \cmd\mathrm, we now need to execute a new mapping:
% \begin{Verbatim}
% \Umathcode`\a = 7 1 `\a\relax
% \Umathcode`\b = 7 1 `\b\relax
% \Umathcode`\c = 7 1 `\c\relax
% ...
% \end{Verbatim}
% 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.
%
% \subsection{Hooks into \LaTeXe}
%
% To overcome this, we patch \cs{use@mathgroup}.
% (An alternative is to patch \cs{extract@alph@from@version}, which constructs the \cs{mathXYZ} commands, but this method fails if the command has been defined using \cs{DeclareSymbolFontAlphabet}.)
% As far as I can tell, this is only used inside of commands such as \cs{mathXYZ}, so this shouldn't have any major side-effects.
%
%    \begin{macrocode}
\cs_set:Npn \use@mathgroup #1 #2
  {
    \mode_if_math:T % <- not sure if this is really necessary since we've just checked for mmode and raised an error if not!
      {
        \math@bgroup
          \cs_if_eq:cNF {M@\f@encoding} #1 {#1}
          \@@_switchto_literal:
          \mathgroup #2 \relax
        \math@egroup
      }
  }
%    \end{macrocode}
%
% 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}|.
% I thought that hooking into |\operator@font| would be hard because all other maths font selection in 2e uses |\mathrm{...}| style.
% Then reading source2e a little more I stumbled upon \cs{@fontswitch}.
% Reimplement that here to avoid \cs{bgroup}/\cs{egroup}.
% \begin{macro}{\operator@font}
%    \begin{macrocode}
\cs_set:Npn \operator@font
  {
    \@@_switchto_literal:
    \@@_fontswitch:n { \g_@@_operator_mathfont_tl }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_fontswitch:n}
%    \begin{macrocode}
\cs_set:Nn \@@_fontswitch:n
  {
    \mode_if_math:T
      {
        \cs_set_eq:NN \math@bgroup     \scan_stop:
        \cs_set_eq:NN \@@_group_begin: \scan_stop:
        \cs_set:Npn \@@_group_end:
          {
            \cs_set_eq:NN \@@_group_begin: \@@_group_begin_frozen:
            \cs_set_eq:NN \@@_group_end:   \@@_group_end_frozen:
            \cs_set_eq:NN \math@bgroup \@@math@bgroup
            \cs_set_eq:NN \math@egroup \@@math@egroup
          }
        \cs_set_eq:NN \math@egroup \@@_group_end:
        #1 \scan_stop:
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{Setting styles}
%
% Algorithm for setting alphabet fonts.
% By default, when |range| is empty, we are in \emph{implicit} mode.
% If |range| contains the name of the math alphabet, we are in \emph{explicit}
% mode and do things slightly differently.
%
% Implicit mode:
% \begin{itemize}
% \item Try and set all of the alphabet shapes.
% \item Check for the first glyph of each alphabet to detect if the font supports each
%       alphabet shape.
% \item For alphabets that do exist, overwrite whatever’s already there.
% \item For alphabets that are not supported, \emph{do nothing}.
%       (This includes leaving the old alphabet definition in place.)
% \end{itemize}
%
% Explicit mode:
% \begin{itemize}
% \item Only set the alphabets specified.
% \item Check for the first glyph of the alphabet to detect if the font contains
%       the alphabet shape in the Unicode math plane.
% \item For Unicode math alphabets, overwrite whatever’s already there.
% \item Otherwise, use the \ascii\ glyph slots instead.
% \end{itemize}
%
%
%
% \subsection{Defining the math style macros}
%
% We call the different shapes that a math alphabet can be a `math style’.
% Note that different alphabets can exist within the same math style. E.g.,
% we call `bold’ the math style |bf| and within it there are upper and lower
% case Greek and Roman alphabets and Arabic numerals.
%
% \begin{macro}{\@@_prepare_mathstyle:n}
% \darg{math style name (e.g., \texttt{it} or \texttt{bb})}
% Define the high level math alphabet macros (\cs{mathit}, etc.) in terms of
% unicode-math definitions. Use \cs{bgroup}/\cs{egroup} so s’scripts scan the
% whole thing.
%
% The flag \cs{l_@@_mathstyle_tl} is for other applications to query the
% current math style.
%    \begin{macrocode}
\cs_new:Nn \@@_prepare_mathstyle:n
  {
    \seq_gput_right:Nn \g_@@_mathstyles_seq {#1}
    \@@_init_alphabet:n {#1}
    \cs_set_protected:cpx {sym#1} ##1
      {
        \@@_group_begin:
          \exp_not:n
            {
              \mode_if_math:F
                {
                  \exp_args:Nc \non@alpherr {sym#1}
                }
              \tl_set:Nn \l_@@_mathstyle_tl {#1}
            }
          \exp_not:c {@@_switchto_#1:} ##1
        \@@_group_end:
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_init_alphabet:n}
% \darg{math alphabet name (e.g., \texttt{it} or \texttt{bb})}
% This macro initialises the macros used to set up a math alphabet.
% First used when the math alphabet macro is first defined, but then used
% later when redefining a particular maths alphabet.
%    \begin{macrocode}
\cs_set:Nn \@@_init_alphabet:n
  {
    \@@_log:nx {alph-initialise} {#1}
    \cs_set_eq:cN {@@_switchto_#1:} \prg_do_nothing:
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Definition of alphabets and styles}
%
%
% The linking between named ranges and symbol style commands happens here.
% It’s currently not using all of the machinery we’re in the process of setting up above.
% Baby steps.
%    \begin{macrocode}
\cs_new:Nn \@@_default_mathalph:nnn
  {
    \prop_new:c {g_@@_named_range_#1_prop}
    \seq_gput_right:Nn \g_@@_default_mathalph_seq {{#1}{#2}{#3}}
    \prop_gput:cnn { g_@@_named_range_#1_prop } { default-alpha } {#2}
  }
%    \end{macrocode}
%    \begin{macrocode}
\@@_default_mathalph:nnn {up    } {latin,Latin,greek,Greek,num,misc} {up    }
\@@_default_mathalph:nnn {it    } {latin,Latin,greek,Greek,misc}     {it    }
\@@_default_mathalph:nnn {bb    } {latin,Latin,num,misc}             {bb    }
\@@_default_mathalph:nnn {bbit  } {misc}                             {bbit  }
\@@_default_mathalph:nnn {scr   } {latin,Latin}                      {scr   }
\@@_default_mathalph:nnn {cal   } {Latin}                            {scr   }
\@@_default_mathalph:nnn {bfcal } {Latin}                            {bfscr }
\@@_default_mathalph:nnn {frak  } {latin,Latin}                      {frak  }
\@@_default_mathalph:nnn {tt    } {latin,Latin,num}                  {tt    }
\@@_default_mathalph:nnn {sfup  } {latin,Latin,num}                  {sfup  }
\@@_default_mathalph:nnn {sfit  } {latin,Latin}                      {sfit  }
\@@_default_mathalph:nnn {bfup  } {latin,Latin,greek,Greek,num,misc} {bfup  }
\@@_default_mathalph:nnn {bfit  } {latin,Latin,greek,Greek,misc}     {bfit  }
\@@_default_mathalph:nnn {bfscr } {latin,Latin}                      {bfscr }
\@@_default_mathalph:nnn {bffrak} {latin,Latin}                      {bffrak}
\@@_default_mathalph:nnn {bfsfup} {latin,Latin,greek,Greek,num,misc} {bfsfup}
\@@_default_mathalph:nnn {bfsfit} {latin,Latin,greek,Greek,misc}     {bfsfit}
%    \end{macrocode}
%
% \subsubsection{Define symbol style commands}
% Finally, all of the `symbol styles’ commands are set up, which are the commands to access each of the named alphabet styles. There is not a one-to-one mapping between symbol style commands and named style ranges!
%    \begin{macrocode}
\clist_map_inline:nn
  {
    up, it, bfup, bfit, sfup, sfit, bfsfup, bfsfit, bfsf,
    tt, bb, bbit, scr, bfscr, cal, bfcal, frak, bffrak,
    normal, literal, sf, bf,
  }
  {
    \@@_prepare_mathstyle:n {#1}
  }
%    \end{macrocode}
%
%
% \subsubsection{New names for legacy textmath alphabet selection}
% In case a package option overwrites, say, \cs{mathbf} with \cs{symbf}.
%    \begin{macrocode}
\clist_map_inline:nn
  { rm, it, bf, sf, tt }
  { \cs_set_eq:cc { mathtext #1 } { math #1 } }
%    \end{macrocode}
% Perhaps these should actually be defined using a hypothetical unicode-math interface to creating new such styles. To come.
%
%
% \subsubsection{Replacing legacy pure-maths alphabets}
% The following are alphabets which do not have a math/text ambiguity.
%    \begin{macrocode}
\clist_map_inline:nn
  {
    normal, bb , bbit, scr, bfscr, cal, bfcal, frak, bffrak, tt,
    bfup, bfit, sfup, sfit, bfsfup, bfsfit, bfsf
  }
  {
    \cs_set:cpx { math #1 } { \exp_not:c { sym #1 } }
  }
%    \end{macrocode}
%
%
% \subsubsection{New commands for ambiguous alphabets}
%
%    \begin{macrocode}
\AtBeginDocument
  {
    \clist_map_inline:nn
      { rm, it, bf, sf, tt }
      {
        \cs_set_protected:cpx { math #1 }
        {
          \exp_not:n { \bool_if:NTF  } \exp_not:c { g_@@_ math #1 _text_bool}
            { \exp_not:c { mathtext #1 } }
            { \exp_not:c { sym #1 } }
        }
      }
  }
%    \end{macrocode}
%
% \paragraph{Alias \cs{mathrm} as legacy name for \cs{mathup}}
%    \begin{macrocode}
\cs_set_protected:Npn \mathup { \mathrm }
\cs_set_protected:Npn \symrm  { \symup  }
%    \end{macrocode}
%
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}

\endinput

% /©
%
% ------------------------------------------------
% The UNICODE-MATH package  <wspr.io/unicode-math>
% ------------------------------------------------
% This package is free software and may be redistributed and/or modified under
% the conditions of the LaTeX Project Public License, version 1.3c or higher
% (your choice): <http://www.latex-project.org/lppl/>.
% ------------------------------------------------
% Copyright 2006-2018  Will Robertson, LPPL "maintainer"
% Copyright 2010-2017  Philipp Stephani
% Copyright 2011-2017  Joseph Wright
% Copyright 2012-2015  Khaled Hosny
% ------------------------------------------------
%
% ©/