summaryrefslogtreecommitdiff
path: root/tex/context/base/math-frc.mkiv
blob: 2305cec30956d3dd6500feb6f96e449d6a976fc8 (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
%D \module
%D   [       file=math-frc,
%D        version=2007.07.19,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Fractions,
%D         author={Hans Hagen \& Taco Hoekwater},
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Math Macros / Fractions}

\unprotect

%D \macros
%D   {frac, xfrac, xxfrac}
%D
%D This is another one Tobias asked for. It replaces the
%D primitive \type {\over}. We also take the opportunity to
%D handle math style restoring, which makes sure units and
%D chemicals come out ok.
%D The \type {\frac} macro kind of replaces the awkward \type
%D {\over} primitive. Say that we have the following formulas:
%D
%D \startbuffer[sample]
%D test $\frac  {1}{2}$ test $$1 + \frac  {1}{2} = 1.5$$
%D test $\xfrac {1}{2}$ test $$1 + \xfrac {1}{2} = 1.5$$
%D test $\xxfrac{1}{2}$ test $$1 + \xxfrac{1}{2} = 1.5$$
%D \stopbuffer
%D
%D \typebuffer[sample]
%D
%D With the most straightforward definitions, we get:
%D
%D \startbuffer[code]
%D \def\dofrac#1#2#3{\relax\mathematics{{{#1{#2}}\over{#1{#3}}}}}
%D
%D \def\frac  {\dofrac\mathstyle}
%D \def\xfrac {\dofrac\scriptstyle}
%D \def\xxfrac{\dofrac\scriptscriptstyle}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D Since this does not work well, we can try:
%D
%D \startbuffer[code]
%D \def\xfrac #1#2{\hbox{$\dofrac\scriptstyle      {#1}{#2}$}}
%D \def\xxfrac#1#2{\hbox{$\dofrac\scriptscriptstyle{#1}{#2}$}}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D This for sure looks better than:
%D
%D \startbuffer[code]
%D \def\xfrac #1#2{{\scriptstyle      \dofrac\relax{#1}{#2}}}
%D \def\xxfrac#1#2{{\scriptscriptstyle\dofrac\relax{#1}{#2}}}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D So we stick to the next definitions (watch the local
%D overloading of \type {\xfrac}).

% \def\dofrac#1#2#3{\relax\mathematics{{{#1{#2}}\over{#1{#3}}}}}

\def\dofrac#1#2#3{\relax\mathematics{\Ustack{{#1{#2}}\normalover{#1{#3}}}}}
\def\nofrac  #1#2{\relax\mathematics{\Ustack{{#1}\normalover{#2}}}}

% \chardef\mathfracmode=0 $\frac{1}{2}$
% \chardef\mathfracmode=1 $\frac{1}{2}$
% \chardef\mathfracmode=2 $\frac{1}{2}$
% \chardef\mathfracmode=3 $\frac{1}{2}$
% \chardef\mathfracmode=4 $\frac{1}{2}$
% \chardef\mathfracmode=5 $\frac{1}{2}$

\chardef\mathfracmode=0 % 0=auto, 1=displaystyle, 2=textstyle, 3=scriptstyle, 4=scriptscriptstyle, 5=mathstyle

\unexpanded\def\frac
  {\ifcase\mathfracmode
     \expandafter\nofrac
   \or
     \expandafter\dofrac\expandafter\displaystyle
   \or
     \expandafter\dofrac\expandafter\textstyle
   \or
     \expandafter\dofrac\expandafter\scriptstyle
   \or
     \expandafter\dofrac\expandafter\scriptscriptstyle
   \else
     \expandafter\dofrac\expandafter\mathstyle
   \fi}

\unexpanded\def\xfrac#1#2%
  {\begingroup
   \let\xfrac\xxfrac
   \dofrac\scriptstyle{#1}{#2}%
   \endgroup}

\unexpanded\def\xxfrac#1#2%
  {\begingroup
   \dofrac\scriptscriptstyle{#1}{#2}%
   \endgroup}

%D The \type {xx} variant looks still ugly, so maybe it's
%D best to say:

\unexpanded\def\xxfrac#1#2%
  {\begingroup
   \dofrac\scriptscriptstyle{#1}{\raise.25ex\hbox{$\scriptscriptstyle#2$}}%
   \endgroup}

%D Something low level for scientific calculator notation:

\unexpanded\def\scinot#1#2%
  {#1\times10^{#2}}

%D The next macro, \type {\ch}, is \PPCHTEX\ aware. In
%D formulas one can therefore best use \type {\ch} instead of
%D \type {\chemical}, especially in fractions.

% let's see who complains ... \mathstyle is now a primitive
%
% \unexpanded\def\ch#1%
%   {\ifdefined\@@chemicalletter
%      \dosetsubscripts
%      \mathstyle{\@@chemicalletter{#1}}%
%      \doresetsubscripts
%    \else
%      \mathstyle{\rm#1}%
%    \fi}

% \unexpanded\def\ch#1%
%   {\ifdefined\@@chemicalletter
%      \dosetsubscripts
%      \mathematics{\@@chemicalletter{#1}}%
%      \doresetsubscripts
%    \else
%      \mathematics{\rm#1}%
%    \fi}

%D \macros
%D   {/}
%D
%D Just to be sure, we restore the behavior of some typical
%D math characters.

\bgroup

\catcode`\/=\@@other    \global           \let\normalforwardslash/
\catcode`\/=\@@active \doglobal\appendtoks\let/\normalforwardslash\to\everymathematics

\egroup

% to be checked:

\def\exmthfont#1{\symbolicsizedfont#1\plusone{MathExtension}}

\def\domthfrac#1#2#3#4#5#6#7%
  {\begingroup
   \mathsurround\zeropoint
   \setbox0\hbox{$#1 #6$}%
   \setbox2\hbox{$#1 #7$}%
   \dimen0\wd0
   \ifdim\wd2>\dimen0 \dimen0\wd2 \fi
   \setbox4\hbox to \dimen0{\exmthfont#2#3\leaders\hbox{#4}\hss#5}%
   \mathord{\vcenter{{\offinterlineskip
     \hbox to \dimen0{\hss\box0\hss}%
     \kern \ht4%
     \hbox to \dimen0{\hss\copy4\hss}%
     \kern \ht4%
     \hbox to \dimen0{\hss\box2\hss}}}}%
   \endgroup}

\def\domthsqrt#1#2#3#4#5%
  {\begingroup
   \mathsurround\zeropoint
   \setbox0\hbox{$#1 #5$}%
   \dimen0=1.05\ht0 \advance\dimen0 1pt  \ht0 \dimen0
   \dimen0=1.05\dp0 \advance\dimen0 1pt  \dp0 \dimen0
   \dimen0\wd0
   \setbox4\hbox to \dimen0{\exmthfont#2\leaders\hbox{#3}\hfill#4}%
   \delimitershortfall=0pt
   \nulldelimiterspace=0pt
   \setbox2\hbox{$\left\delimiter"0270370 \vrule height\ht0 depth \dp0 width0pt
                  \right.$}%
   \mathord{\vcenter{\hbox{\copy2
                           \rlap{\raise\dimexpr\ht2-\ht4\relax\copy4}\copy0}}}%
   \endgroup}

\def\mthfrac#1#2#3#4#5{\mathchoice
  {\domthfrac\displaystyle     \textface        {#1}{#2}{#3}{#4}{#5}}
  {\domthfrac\textstyle        \textface        {#1}{#2}{#3}{#4}{#5}}
  {\domthfrac\scriptstyle      \scriptface      {#1}{#2}{#3}{#4}{#5}}
  {\domthfrac\scriptscriptstyle\scriptscriptface{#1}{#2}{#3}{#4}{#5}}}

\def\mthsqrt#1#2#3{\mathchoice
  {\domthsqrt\displaystyle     \textface    {#1}{#2}{#3}}
  {\domthsqrt\textstyle        \textface    {#1}{#2}{#3}}
  {\domthsqrt\scriptstyle      \textface    {#1}{#2}{#3}}
  {\domthsqrt\scriptscriptstyle\textface    {#1}{#2}{#3}}}

% temp here

\protect \endinput