summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/xetex/zhspacing/zhmath.sty
blob: 80a090dd84eff329275074e5d50ae1d16a6834cd (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
%	zhmath.sty	coded by YIN Dian
%	Use basic CJK characters in math environment.
%	Hist:	071202	First coded.
%		071204	Set \XeTeXinterchartokenstate=0 to avoid a XeTeX bug.
%			Added \ProvidesPackage.
%		071210	Added another method to set Chinese math fonts in
%			LaTeX, via option noactive. Improved format detection.
%			Added ability to set alpha and number fonts.
%		071211	Put \setalphanummathfonts in preamble so that
%			\usecustommathfonts can be used in document in LaTeX.
%			Changed \hbox to \mbox. Changed default option to
%			noactive.
%		071215	Removed \XeTeXinterchartokenstate=0, as the XeTeX bug
%			is fixed by JK. Changed \mathcjksizec/d to fit the
%			main text size.
%		071229	Added \XeTeXinterchartokenstate=0 again, because not
%			everyone has the latest version of XeTeX. Added custom
%			and nocustom options.
%		2016/02/10	Improved \@setzhmathfonts.
\catcode`\@=11
\ifx\zhmath@defined\@undefined
\def\zhmath@defined{\relax}
\def\filedate{2007/12/29}
\ifx\zhspacingrevision\@undefined
\input zhspacing.sty
\fi

\def\zhmath@setXeTeXmathcodefam#1#2#3{%
  % set #1..#2's math family to #3
  \count@=#1%
  \loop
    \XeTeXmathcode\count@=7 #3 \count@
    \unless\ifnum\count@>#2%
      \advance\count@ by 1
  \repeat
}

\def\zhmath@setactivemathchar#1#2#3#4#5#6{%
  % set #1..#2 to active math chars, executing \mathchoice #3 #4 #5 #6.
  \count@=#1%
  \loop
    \mathcode\count@="8000
    {\catcode\count@=\active\lccode`\~=\count@
      \lowercase{\xdef~{{\mathchoice{\noexpand#3{\number\count@}}%
      {\noexpand#4{\number\count@}}{\noexpand#5{\number\count@}}
      {\noexpand#6{\number\count@}}}}}%
    }%
    \unless\ifnum\count@>#2%
      \advance\count@ by 1
  \repeat
}
\newif\ifzhmath@active
\newif\ifzhmath@custom

\ifLaTeX@e   % in LaTeX, similar to mathcjk

\ProvidesPackage{zhmath}[\filedate]
\DeclareOption{active}{\zhmath@activetrue}
\DeclareOption{noactive}{\zhmath@activefalse}
\DeclareOption{custom}{\zhmath@customtrue}
\DeclareOption{nocustom}{\zhmath@customfalse}
\zhmath@activefalse
\zhmath@customtrue
\ProcessOptions\relax

\def\mathcjksizea{}
\def\mathcjksizeb{}
\def\mathcjksizec{\dimen@=\dimexpr\f@size pt*6/10\relax\fontsize{\dimen@}{\dimen@}}
\def\mathcjksized{\dimen@=\dimexpr\f@size pt*5/10\relax\fontsize{\dimen@}{\dimen@}}

\@ifundefined{zhmathfont}{\newfontfamily\zhmathfont{SimSun}}{}

\ifzhmath@active
  \def\zhmath@zhfont@disp#1{{\mbox{\XeTeXinterchartokenstate=0\zhmathfont\mathcjksizea\char#1\relax}}}
  \def\zhmath@zhfont@text#1{{\mbox{\XeTeXinterchartokenstate=0\zhmathfont\mathcjksizeb\char#1\relax}}}
  \def\zhmath@zhfont@scrp#1{{\mbox{\XeTeXinterchartokenstate=0\zhmathfont\mathcjksizec\char#1\relax}}}
  \def\zhmath@zhfont@subs#1{{\mbox{\XeTeXinterchartokenstate=0\zhmathfont\mathcjksized\char#1\relax}}}

  \zhmath@setactivemathchar{"2E80}{"9FFF}{\zhmath@zhfont@disp}%
    {\zhmath@zhfont@text}{\zhmath@zhfont@scrp}{\zhmath@zhfont@subs}

  \def\setzhmathfont#1{\let\zhmathfont=#1}
\else
  \def\setzhmathfont#1{%
    \let\zhmathfont=#1%
    \begingroup
    \normalfont
    \zhmathfont
    \xdef\zhmath@fenc{\f@encoding}%
    \xdef\zhmath@ffam{\f@family}%
    \endgroup
    \DeclareSymbolFont{hanzi}{\zhmath@fenc}{\zhmath@ffam}{m}{n}%
  }
  \setzhmathfont\zhmathfont
  \zhmath@setXeTeXmathcodefam{"2E80}{"9FFF}{\symhanzi}
\fi

\ifzhmath@custom
\def\setalphanummathfonts#1{%
  \let\zhmathalphanumfont=#1%
  \begingroup
  \normalfont
  \zhmathalphanumfont
  \xdef\zhmath@@fenc{\f@encoding}%
  \xdef\zhmath@@ffam{\f@family}%
  \endgroup
  \DeclareSymbolFont{@letter}{\zhmath@@fenc}{\zhmath@@ffam}{m}{it}%
  \DeclareSymbolFont{@number}{\zhmath@@fenc}{\zhmath@@ffam}{m}{n}%
  \SetSymbolFont{@letter}{bold}{\zhmath@@fenc}{\zhmath@@ffam}{bx}{it}%
  \SetSymbolFont{@number}{bold}{\zhmath@@fenc}{\zhmath@@ffam}{bx}{n}%
}
\@ifundefined{zhmathalphanumfont}{\newfontfamily\zhmathalphanumfont{Times New Roman}}{}
\setalphanummathfonts\zhmathalphanumfont
\def\usecustommathfonts{%
  \zhmath@setXeTeXmathcodefam{`\0}{`\9}{\sym@number}
  \zhmath@setXeTeXmathcodefam{`\a}{`\z}{\sym@letter}
  \zhmath@setXeTeXmathcodefam{`\A}{`\Z}{\sym@letter}
}
\fi

\else 	%in plain TeX

\@ifundefined{zhmathfont}{\font\zhmathfont="SimSun" at 10pt}{}
\newfam\zhmath@fam
\newfam\zhmath@letterfam
\newfam\zhmath@numberfam

\begingroup
  \catcode`\A=12 %
  \catcode`\P=12 %
  \catcode`\T=12 %
\lowercase{\endgroup
  \def\@setzhmathfonts#1#2{%
    \expandafter\@setzhmathfonts@auxi\fontname#1 AT PT\relax#2}
  \def\@setzhmathfonts@auxi #1 AT #2PT#3\relax#4{%
    \ifx\relax#2\relax
      \@setzhmathfonts@auxii#4{#1}{10pt}%
    \else
      \@setzhmathfonts@auxii#4{#1}{#2pt}%
    \fi}%
}
\def\@setzhmathfonts@auxii#1#2#3{%
  \font\zhmath@textfont=#2 at #3 \relax
  \font\zhmath@scriptfont=#2 at .7\dimexpr#3\relax\relax
  \font\zhmath@scriptscriptfont=#2 at .5\dimexpr#3\relax\relax
  \textfont#1=\zhmath@textfont
  \scriptfont#1=\zhmath@scriptfont
  \scriptscriptfont#1=\zhmath@scriptscriptfont}
\def\setzhmathfont#1{\@setzhmathfonts{#1}\zhmath@fam}
\setzhmathfont\zhmathfont

\def\setalphanummathfonts#1#2{% #1: alpha, #2, num
  \@setzhmathfonts{#1}\zhmath@letterfam
  \@setzhmathfonts{#2}\zhmath@numberfam
}

\zhmath@setXeTeXmathcodefam{"2E80}{"9FFF}{\zhmath@fam}

\def\usecustommathfonts{%
  \@ifundefined{zhmathletterfont}{\font\zhmathletterfont="Times New Roman/I" at 10pt}{}
  \@ifundefined{zhmathnumberfont}{\font\zhmathnumberfont="Times New Roman" at 10pt}{}
  \setalphanummathfonts\zhmathletterfont\zhmathnumberfont
  \zhmath@setXeTeXmathcodefam{`\0}{`\9}{\zhmath@numberfam}
  \zhmath@setXeTeXmathcodefam{`\a}{`\z}{\zhmath@letterfam}
  \zhmath@setXeTeXmathcodefam{`\A}{`\Z}{\zhmath@letterfam}
}

\fi % end if LaTeX
\fi