summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/fontspec/fontspec-code-math.dtx
blob: bbe70ef437c839530c8376c07ad29bba9f951b3f (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
%%^^A%%  fontspec-code-math.dtx -- part of FONTSPEC <wspr.io/fontspec>
%
% \section{Selecting maths fonts}
% Here, the fonts used in math mode are redefined to correspond to the
% default roman, sans serif and typewriter fonts. Unfortunately, you
% can only define maths fonts in the preamble, otherwise I'd run this
% code whenever \cs{setmainfont} and friends was run.
%
% \iffalse
%    \begin{macrocode}
%<*fontspec>
%    \end{macrocode}
% \fi
%
% \begin{macro}{\fontspec_setup_maths:}
% Everything here is performed \cs{AtBeginDocument} in order to
% overwrite \pkg{euler}'s attempt. This means \pkg{fontspec} must be
% loaded \emph{after} \pkg{euler}. We set up a conditional to return
% an error if this rule is violated.
%
% Since every maths setup is slightly different, we also take
% different paths for defining various math glyphs depending which
% maths font package has been loaded.
%
%    \begin{macrocode}
\@ifpackageloaded{euler}
  { \bool_gset_true:N  \g_@@_pkg_euler_loaded_bool }
  { \bool_gset_false:N \g_@@_pkg_euler_loaded_bool }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \fontspec_setup_maths:
 {
  \@ifpackageloaded{euler}
   {
    \bool_if:NTF \g_@@_pkg_euler_loaded_bool
     { \bool_gset_true:N \g_@@_math_euler_bool }
     { \@@_error:n {euler-too-late} }
   }
   {}
  \@ifpackageloaded{lucbmath}{ \bool_gset_true:N \g_@@_math_lucida_bool }{}
  \@ifpackageloaded{lucidabr}{ \bool_gset_true:N \g_@@_math_lucida_bool }{}
  \@ifpackageloaded{lucimatx}{ \bool_gset_true:N \g_@@_math_lucida_bool }{}
%    \end{macrocode}
% Knuth's CM fonts fonts are all squashed together, combining letters,
% accents, text symbols and maths symbols all in the one font,
% \texttt{cmr}, plus other things in other fonts. Because we are
% changing the roman font in the document, we need to redefine all of
% the maths glyphs in \LaTeX's \texttt{operators} maths font to still
% go back to the legacy \texttt{cmr} font for all these random glyphs,
% unless a separate maths font package has been loaded instead.
%
% In every case, the maths accents are always taken from the
% \texttt{operators} font, which is generally the main text
% font. (Actually, there is a \cmd\hat\ accent in
% \texttt{EulerFractur}, but it's \emph{ugly}. So I ignore it. Sorry
% if this causes inconvenience.)
%    \begin{macrocode}
  \DeclareSymbolFont{legacymaths}{OT1}{cmr}{m}{n}
  \SetSymbolFont{legacymaths}{bold}{OT1}{cmr}{bx}{n}
  \DeclareMathAccent{\acute}   {\mathalpha}{legacymaths}{19}
  \DeclareMathAccent{\grave}   {\mathalpha}{legacymaths}{18}
  \DeclareMathAccent{\ddot}    {\mathalpha}{legacymaths}{127}
  \DeclareMathAccent{\tilde}   {\mathalpha}{legacymaths}{126}
  \DeclareMathAccent{\bar}     {\mathalpha}{legacymaths}{22}
  \DeclareMathAccent{\breve}   {\mathalpha}{legacymaths}{21}
  \DeclareMathAccent{\check}   {\mathalpha}{legacymaths}{20}
  \DeclareMathAccent{\hat}     {\mathalpha}{legacymaths}{94} % too bad, euler
  \DeclareMathAccent{\dot}     {\mathalpha}{legacymaths}{95}
  \DeclareMathAccent{\mathring}{\mathalpha}{legacymaths}{23}
%    \end{macrocode}
% \paragraph{\cmd\colon: what's going on?} Okay, so \verb":" and \cmd\colon\ in maths mode are defined in a few places, so I need to work out what does what. Respectively, we have:
% \begin{Verbatim}
% % fontmath.ltx:
% \DeclareMathSymbol{\colon}{\mathpunct}{operators}{"3A}
% \DeclareMathSymbol{:}{\mathrel}{operators}{"3A}
%
% % amsmath.sty:
% \renewcommand{\colon}{\nobreak\mskip2mu\mathpunct{}\nonscript
%  \mkern-\thinmuskip{:}\mskip6muplus1mu\relax}
%
% % euler.sty:
% \DeclareMathSymbol{:}\mathrel  {EulerFraktur}{"3A}
%
% % lucbmath.sty:
% \DeclareMathSymbol{\@tempb}{\mathpunct}{operators}{58}
% \ifx\colon\@tempb
%   \DeclareMathSymbol{\colon}{\mathpunct}{operators}{58}
% \fi
% \DeclareMathSymbol{:}{\mathrel}{operators}{58}
% \end{Verbatim}
% ($\mathrm{3A_{16}}=\mathrm{58_{10}}$) So I think, based on this summary, that it is fair to tell \pkg{fontspec} to `replace' the operators font with legacymaths for this symbol, except when \pkg{amsmath} is loaded since we want to keep its definition.
%
%
%    \begin{macrocode}
  \group_begin:
    \mathchardef\@tempa="603A \relax
    \ifx\colon\@tempa
      \DeclareMathSymbol{\colon}{\mathpunct}{legacymaths}{58}
    \fi
  \group_end:
%    \end{macrocode}
% The following symbols are only defined specifically in \pkg{euler}, so skip them
% if that package is loaded.
%    \begin{macrocode}
  \bool_if:NF \g_@@_math_euler_bool
   {
    \DeclareMathSymbol{!}{\mathclose}{legacymaths}{33}
    \DeclareMathSymbol{:}{\mathrel}  {legacymaths}{58}
    \DeclareMathSymbol{;}{\mathpunct}{legacymaths}{59}
    \DeclareMathSymbol{?}{\mathclose}{legacymaths}{63}
%    \end{macrocode}
% And these ones are defined both in \pkg{euler} and \pkg{lucbmath},
% so we only need to run this code if no extra maths package has been
% loaded.
%    \begin{macrocode}
    \bool_if:NF \g_@@_math_lucida_bool
     {
      \DeclareMathSymbol{0}{\mathalpha}{legacymaths}{`0}
      \DeclareMathSymbol{1}{\mathalpha}{legacymaths}{`1}
      \DeclareMathSymbol{2}{\mathalpha}{legacymaths}{`2}
      \DeclareMathSymbol{3}{\mathalpha}{legacymaths}{`3}
      \DeclareMathSymbol{4}{\mathalpha}{legacymaths}{`4}
      \DeclareMathSymbol{5}{\mathalpha}{legacymaths}{`5}
      \DeclareMathSymbol{6}{\mathalpha}{legacymaths}{`6}
      \DeclareMathSymbol{7}{\mathalpha}{legacymaths}{`7}
      \DeclareMathSymbol{8}{\mathalpha}{legacymaths}{`8}
      \DeclareMathSymbol{9}{\mathalpha}{legacymaths}{`9}
      \DeclareMathSymbol{\Gamma}{\mathalpha}{legacymaths}{0}
      \DeclareMathSymbol{\Delta}{\mathalpha}{legacymaths}{1}
      \DeclareMathSymbol{\Theta}{\mathalpha}{legacymaths}{2}
      \DeclareMathSymbol{\Lambda}{\mathalpha}{legacymaths}{3}
      \DeclareMathSymbol{\Xi}{\mathalpha}{legacymaths}{4}
      \DeclareMathSymbol{\Pi}{\mathalpha}{legacymaths}{5}
      \DeclareMathSymbol{\Sigma}{\mathalpha}{legacymaths}{6}
      \DeclareMathSymbol{\Upsilon}{\mathalpha}{legacymaths}{7}
      \DeclareMathSymbol{\Phi}{\mathalpha}{legacymaths}{8}
      \DeclareMathSymbol{\Psi}{\mathalpha}{legacymaths}{9}
      \DeclareMathSymbol{\Omega}{\mathalpha}{legacymaths}{10}
      \DeclareMathSymbol{+}{\mathbin}{legacymaths}{43}
      \DeclareMathSymbol{=}{\mathrel}{legacymaths}{61}
      \DeclareMathDelimiter{(}{\mathopen} {legacymaths}{40}{largesymbols}{0}
      \DeclareMathDelimiter{)}{\mathclose}{legacymaths}{41}{largesymbols}{1}
      \DeclareMathDelimiter{[}{\mathopen} {legacymaths}{91}{largesymbols}{2}
      \DeclareMathDelimiter{]}{\mathclose}{legacymaths}{93}{largesymbols}{3}
      \DeclareMathDelimiter{/}{\mathord}{legacymaths}{47}{largesymbols}{14}
      \DeclareMathSymbol{\mathdollar}{\mathord}{legacymaths}{36}
      \renewcommand{\hbar}{{\mathchar"AF\mkern-9mu h}}% TODO: test with other fonts
    }
  }
%    \end{macrocode}
% Finally, we change the font definitions for \cmd\mathrm\ and so
% on. These are defined using the \cmd\g_@@_mathrm_tl\ (\dots) macros,
% which default to \cmd\rmdefault\ but may be specified with the
% \cmd\setmathrm\ (\dots) commands in the preamble.
%
% Since \LaTeX\ only generally defines one level of boldness, we omit
% \cmd\mathbf\ in the \textsf{bold} maths series. It can be specified
% as per usual with \cmd\setboldmathrm, which stores the appropriate
% family name in \cmd\g_@@_bfmathrm_tl.
%    \begin{macrocode}
  \DeclareSymbolFont{operators}\g_fontspec_encoding_tl\g_@@_mathrm_tl\mddefault\shapedefault
  \SetSymbolFont{operators}{normal}\g_fontspec_encoding_tl\g_@@_mathrm_tl\mddefault\shapedefault
  \DeclareSymbolFontAlphabet\mathrm{operators}
  \SetMathAlphabet\mathit{normal}\g_fontspec_encoding_tl\g_@@_mathrm_tl\mddefault\itdefault
  \SetMathAlphabet\mathbf{normal}\g_fontspec_encoding_tl\g_@@_mathrm_tl\bfdefault\shapedefault
  \SetMathAlphabet\mathsf{normal}\g_fontspec_encoding_tl\g_@@_mathsf_tl\mddefault\shapedefault
  \SetMathAlphabet\mathtt{normal}\g_fontspec_encoding_tl\g_@@_mathtt_tl\mddefault\shapedefault
  \SetSymbolFont{operators}{bold}\g_fontspec_encoding_tl\g_@@_mathrm_tl\bfdefault\shapedefault
  \tl_if_empty:NTF \g_@@_bfmathrm_tl
   {
    \SetMathAlphabet\mathit{bold}\g_fontspec_encoding_tl\g_@@_mathrm_tl\bfdefault\itdefault
   }
   {
    \SetMathAlphabet\mathrm{bold}\g_fontspec_encoding_tl\g_@@_bfmathrm_tl\mddefault\shapedefault
    \SetMathAlphabet\mathbf{bold}\g_fontspec_encoding_tl\g_@@_bfmathrm_tl\bfdefault\shapedefault
    \SetMathAlphabet\mathit{bold}\g_fontspec_encoding_tl\g_@@_bfmathrm_tl\mddefault\itdefault
   }
  \SetMathAlphabet\mathsf{bold}\g_fontspec_encoding_tl\g_@@_mathsf_tl\bfdefault\shapedefault
  \SetMathAlphabet\mathtt{bold}\g_fontspec_encoding_tl\g_@@_mathtt_tl\bfdefault\shapedefault
 }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_maybe_setup_maths:}
% We're a little less sophisticated about not executing the maths setup if various other maths font packages are loaded. This list is based on the wonderful `\LaTeX Font Catalogue': \url{http://www.tug.dk/FontCatalogue/mathfonts.html}. I'm sure there are more I've missed. Do the \TeX\ Gyre fonts have maths support yet?
%
% Untested: would |\unless\ifnum\Gamma=28672\relax\bool_set_false:N \g_@@_math_bool\fi| be a better test?
% This needs more cooperation with euler and lucida, I think.
%    \begin{macrocode}
\cs_new:Nn \fontspec_maybe_setup_maths:
 {
  \@ifpackageloaded{anttor}
   {
    \ifx\define@antt@mathversions a\bool_gset_false:N \g_@@_math_bool\fi
   }{}
  \@ifpackageloaded{arevmath}       {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{eulervm}        {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{mathdesign}     {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{concmath}       {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{cmbright}       {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{mathesf}        {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{gfsartemisia}   {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{gfsneohellenic} {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{iwona}
   {
    \ifx\define@iwona@mathversions a\bool_set_false:N \g_@@_math_bool\fi
   }{}
  \@ifpackageloaded{kpfonts}{\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{kmath}  {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{kurier}
   {
    \ifx\define@kurier@mathversions a\bool_set_false:N \g_@@_math_bool\fi
   }{}
  \@ifpackageloaded{fouriernc}    {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{fourier}      {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{lmodern}      {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{mathpazo}     {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{mathptmx}     {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{MinionPro}    {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{unicode-math} {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{breqn}        {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{pxfonts}      {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{txfonts}      {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{newpxmath}    {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{newtxmath}    {\bool_gset_false:N \g_@@_math_bool}{}
  \@ifpackageloaded{mtpro2}       {\bool_gset_false:N \g_@@_math_bool}{}
  \bool_if:NT \g_@@_math_bool
   {
    \@@_info:n {setup-math}
    \fontspec_setup_maths:
   }
 }
\AtBeginDocument{\fontspec_maybe_setup_maths:}
%    \end{macrocode}
% \end{macro}
%
% \iffalse
%    \begin{macrocode}
%</fontspec>
%    \end{macrocode}
% \fi


\endinput

% /©
% ------------------------------------------------
% The FONTSPEC package  <wspr.io/fontspec>
% ------------------------------------------------
% Copyright  2004-2020  Will Robertson, LPPL "maintainer"
% Copyright  2009-2015  Khaled Hosny
% Copyright  2013       Philipp Gesang
% Copyright  2013-2016  Joseph Wright
% ------------------------------------------------
% 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/>.
% ------------------------------------------------
% ©/