summaryrefslogtreecommitdiff
path: root/tex/context/base/math-scr.mkiv
blob: 43355679f45bb721cd7281d8903e0f526ca6ef52 (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
%D \module
%D   [       file=math-scr,
%D        version=2007.07.19,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Scripts,
%D         author=Hans Hagen,
%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 / Scripts}

\unprotect

%D \macros
%D   {super, sub}
%D
%D \TEX\ uses \type{^} and \type{_} for entering super- and
%D subscript mode. We want however a bit more control than
%D normally provided, and therefore provide \type {\super}
%D and \type{sub}.

\global\let\normalsuper=^
\global\let\normalsuber=_

\newcount\supersubmode

\newevery\everysupersub \EverySuperSub

\appendtoks \advance\supersubmode \plusone \to \everysupersub

\appendtoks
  \gridsupsubstyle
\to \everysupersub

\appendtoks
  \doifelse\@@mtsize\v!small
    {\let\gridsupsubstyle    \scriptscriptstyle
     \let\gridsupsubbodyfont \setsmallbodyfont}%
    {\let\gridsupsubstyle    \scriptstyle
     \let\gridsupsubbodyfont \relax}%
\to \everysetuptextformulas

\setuptextformulas
  [\c!size=\v!normal]

\def\dogridsupsub#1#2%
  {\begingroup
   \setbox\nextbox\iftracegridsnapping\ruledhbox\else\hbox\fi
     {\gridsupsubbodyfont
      $\strut^{\the\everysupersub#1}_{\the\everysupersub#2}$}%
   \nextboxht\strutheight
   \nextboxdp\strutdepth
   \flushnextbox
   \endgroup}

\def\gridsupsub
  {\ifconditional\crazymathsnapping
     \ifgridsnapping
       \@EAEAEA\dogridsupsub
     \else
       \@EAEAEA\normalsupsub
     \fi
   \else
     \@EA\normalsupsub
   \fi}

\def\normalsupsub#1#2%
  {^{\the\everysupersub#1}_{\the\everysupersub#2}}

\appendtoks
  \let\gridsupsubstyle   \relax
  \let\gridsupsubbodyfont\relax
  \let\gridsupsub        \normalsupsub
\to \everydisplay

\def\super#1{^{\the\everysupersub#1}}
\def\suber#1{_{\the\everysupersub#1}}
\def\supsub#1#2{\super{#1}\suber{#2}}
\def\subsup#1#2{\suber{#1}\super{#2}}

%\def\super#1{\gridsupsub{#1}{}} %
%\def\suber#1{\gridsupsub{}{#1}} %
%
%\def\supsub#1#2{\gridsupsub{#1}{#2}}
%\def\subsup#1#2{\gridsupsub{#2}{#1}}

\def\gridsuper#1{\gridsupsub{#1}{}}
\def\gridsuber#1{\gridsupsub{}{#1}}

% \let\sup\super % math char
% \let\sub\suber

% test set:
%
% \startbuffer
% \sform{x\frac{1}{2}}
% \sform{x\sup{\frac{1}{2}} + x\sup{2} + 2}
% \sform{x\supsub{\frac{1}{2}}{\frac{1}{2}} + x\sup{2} + 2}
% \stopbuffer
%
% \typebuffer
%
% \startlines
% \getbuffer
% \stoplines
%
% \startbuffer
% $x\frac{1}{2}$
% $x\sup{\frac{1}{2}} + x^2 + 2$
% $x\supsub{\frac{1}{2}}{\frac{1}{2}} + x^2 + 2$
% \stopbuffer
%
% \typebuffer
%
% \start
% \enablesupersub
% \enableautomath
% \startlines
% \getbuffer
% \stoplines
% \stop

%D \macros
%D   {enablesupersub,enablesimplesupersub}
%D
%D We can let \type {^} and \type {_} act like \type {\super}
%D and \type {\sub} by saying \type {\enablesupersub}.

\bgroup
\catcode`\^=\@@active
\catcode`\_=\@@active
\gdef\enablesupersub
  {\catcode`\^=\@@active
   \def^{\ifmmode\expandafter\super\else\expandafter\normalsuper\fi}%
   \catcode`\_=\@@active
   \def_{\ifmmode\expandafter\suber\else\expandafter\normalsuber\fi}}
\egroup

%D \macros
%D   {restoremathstyle}
%D
%D We can pick up the current math style by calling \type
%D {\restoremathstyle}.

\def\restoremathstyle
  {\ifmmode
     \ifcase\supersubmode
       \textstyle
     \or
       \scriptstyle
     \else
       \scriptscriptstyle
     \fi
   \fi}

%D These macros were first needed by Frits Spijker (also
%D known as Gajes) for typesetting the minus sign that is
%D keyed into scientific calculators.

% This is the first alternative, which works okay for the
% minus, but less for the plus.
%
% \def\dodoraisedmathord#1#2#3%
%   {\mathord{{#2\raise.#1ex\hbox{#2#3}}}}
%
% \def\doraisedmathord#1%
%   {\mathchoice
%      {\dodoraisedmathord5\tf  #1}%
%      {\dodoraisedmathord5\tf  #1}%
%      {\dodoraisedmathord4\tfx #1}%
%      {\dodoraisedmathord3\tfxx#1}}
%
% \def\negative{\doraisedmathord-}
% \def\positive{\doraisedmathord+}
%
% So, now we use the monospaced signs, that we also
% define as symbol, so that they can be overloaded.

\def\dodoraisedmathord#1#2#3%
  {\mathord{{#2\raise.#1ex\hbox{#2\symbol[#3]}}}}

\def\doraisedmathord#1%
  {\mathchoice
     {\dodoraisedmathord5\tf {#1}}%
     {\dodoraisedmathord5\tf {#1}}%
     {\dodoraisedmathord4\tx {#1}}%
     {\dodoraisedmathord3\txx{#1}}}

\def\dodonumbermathord#1#2%
  {\setbox\scratchbox\hbox{0}%
   \mathord{\hbox to \wd\scratchbox{\hss#1\symbol[#2]\hss}}}

\def\donumbermathord#1%
  {\mathchoice
     {\dodonumbermathord\tf {#1}}%
     {\dodonumbermathord\tf {#1}}%
     {\dodonumbermathord\tx {#1}}%
     {\dodonumbermathord\txx{#1}}}

\definesymbol[positive]  [\getglyph{Mono}{+}]
\definesymbol[negative]  [\getglyph{Mono}{-}]
\definesymbol[zeroamount][\getglyph{Mono}{-}]

\def\negative  {\doraisedmathord{negative}}
\def\positive  {\doraisedmathord{positive}}
\def\zeroamount{\donumbermathord{zeroamount}}

%D How negative such a symbol looks is demonstrated in:
%D $\negative 10^{\negative 10^{\negative 10}}$.

\protect \endinput