math-rad.mklx /size: 9248 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=math-rad,
3%D        version=2013.07.13,
4%D          title=\CONTEXT\ Math Macros,
5%D       subtitle=Radicals,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Math Macros / Radicals}
15
16\unprotect
17
18%D \starttyping
19%D $\sqrt[3]{10}$
20%D \stoptyping
21%D
22%D This root command will be overloaded later:
23
24%D Old stuff:
25
26% \def\rootradical{\Uroot    0 "221A } % can be done in char-def
27% \def\surdradical{\Uradical 0 "221A } % can be done in char-def
28
29\permanent\protected\def\root#1\of{\rootradical{#1}} % #2
30
31\protected\def\sqrt{\doifelsenextoptionalcs\rootwithdegree\rootwithoutdegree} % will be redefined
32
33\permanent\protected\def\styledrootradical#1#2% so that \text works ok ... \rootradical behaves somewhat weird
34  {\normalexpanded{\rootradical
35     {\normalunexpanded{#1}}%
36     {\noexpand\triggermathstyle{\number\normalmathstyle}%
37      \normalunexpanded{#2}}}}
38
39% experimental new keyword: (maybe keywords should come after the symbol)
40%
41% \permanent\protected\def\styledrootradical#1#2% so that \text works ok ... \rootradical behaves somewhat weird
42%   {\Uroot style \normalmathstyle "0 "221A {#1}{#2}}
43
44\permanent\protected\def\rootwithdegree[#1]{\rootradical{#1}}
45\permanent\protected\def\rootwithoutdegree {\rootradical  {}}
46
47%D Even older stuff:
48
49% % is now a ordinary character
50%
51% \let\normalsurd\surd % \Uradical "0 "221A
52% \permanent\protected\def\surd{\normalsurd{}}
53
54%D The real thing. If needed one can control matters with one of the many
55%D \type {\Umath...} parameters.
56%D
57%D \starttyping
58%D \def\R {\Umathradicaldegreeafter\textstyle0pt}
59%D \def\RR{\Umathradicaldegreeafter\textstyle\dimexpr
60%D    \Umathradicaldegreeafter\textstyle+.1em\relax}
61%D \def\RRR{\frozen\Umathradicaldegreeafter\textstyle\dimexpr
62%D    \Umathradicaldegreeafter\textstyle+.2em\relax}
63%D
64%D $    \sqrt[3]{5}   \RR\sqrt[3]{5}  \sqrt[3]{5} $\par
65%D $    \sqrt[3]{5} {\RRR\sqrt[3]{5}} \sqrt[3]{5} $\par
66%D $ \RR\sqrt[3]{5} {\RRR\sqrt[3]{5}} \sqrt[3]{5} $\par
67%D
68%D \def\R  {\Umathradicaldegreeafter\textstyle
69%D            0pt}
70%D \def\RR {\Umathradicaldegreeafter\textstyle
71%D            0.95\Umathradicaldegreeafter\textstyle}
72%D \def\RRR{\frozen\Umathradicaldegreeafter\textstyle
73%D            0.9\Umathradicaldegreeafter\textstyle}
74%D
75%D $    \sqrt[3]{5}   \RR\sqrt[3]{5}  \sqrt[3]{5} $\par
76%D $    \sqrt[3]{5} {\RRR\sqrt[3]{5}} \sqrt[3]{5} $\par
77%D $ \RR\sqrt[3]{5} {\RRR\sqrt[3]{5}} \sqrt[3]{5} $\par
78%D
79%D \def\RR {\mathopenupparameter\Umathradicaldegreeafter{.1}}
80%D
81%D $ \sqrt[3]{5}  \RR\sqrt[3]{5}  \sqrt[3]{5} $\par
82%D $ \sqrt[3]{5} {\RR\sqrt[3]{5}} \sqrt[3]{5} $\par
83%D \stoptyping
84
85\installcorenamespace{mathradical}
86\installcorenamespace{mathradicalalternative}
87
88\installcommandhandler \??mathradical {mathradical} \??mathradical
89
90\setupmathradical
91  [\c!alternative=\v!normal,
92   \c!mpoffset=.25\exheight]
93
94\appendtoks
95    \frozen\protected\instance\edefcsname\currentmathradical\endcsname{\math_radical_handle{\currentmathradical}}
96\to \everydefinemathradical
97
98\mutable\let\currentmathradical      \empty
99\mutable\let\currentmathradicaldegree\empty
100
101\tolerant\protected\def\math_radical_handle#tag#*[#degree]#:#body%
102  {\begingroup
103   \edef\currentmathradical{#tag}%
104   \edef\currentmathradicaldegree{#degree}%
105   \ifempty\currentmathradicaldegree
106     \let\currentmathradicaldegree\m_math_no_degree
107   \fi
108   \math_radical_alternative{#body}%
109   \endgroup}
110
111\def\m_math_no_degree{{}}
112
113% style width [options: left middle right]
114
115\def\math_radical_alternative{\csname\??mathradicalalternative\mathradicalparameter\c!alternative\endcsname}
116
117\defcsname\??mathradicalalternative\v!default\endcsname %  #body%
118  {\rootradical{\currentmathradicaldegree}}   % {#body}}
119
120\defcsname\??mathradicalalternative\v!normal\endcsname#body%
121  {\edef\p_color{\mathradicalparameter\c!color}%
122   \ifempty\p_color
123     \styledrootradical{\currentmathradicaldegree}{#body}% {} really needed as \rootradical expands first
124   \orelse\ifempty\currentmathradicaldegree
125     \pushcolor[\p_color]%
126     \styledrootradical{\currentmathradicaldegree}%
127     {\popcolor#body}%
128   \else
129     \pushcolor[\p_color]%
130     \styledrootradical{\popcolor\currentmathradicaldegree\pushcolor[\p_color]}%
131     {\popcolor#body}%
132   \fi}
133
134\startMPextensions
135    vardef math_radical_simple(expr w,h,d,o) =
136        (-h/2-o,h/2-o) --
137        (-h/4-o,-d-o)  --
138        (-o,h+o)       --
139        (w+o,h+o)      --
140        (w+o,h-h/10+o)
141    enddef ;
142\stopMPextensions
143
144\startuniqueMPgraphic{minifun::math:radical:default}%{...}
145    draw
146        math_radical_simple(OverlayWidth,OverlayHeight,OverlayDepth,OverlayOffset)
147      % withpen pencircle xscaled (2OverlayLineWidth) yscaled (3OverlayLineWidth/4) rotated 30
148        withpen pencircle scaled OverlayLineWidth
149      % dashed evenly
150        withcolor OverlayLineColor ;
151\stopuniqueMPgraphic
152
153% todo: spacing .. this is just an experiment (article driven)
154
155\defcsname\??mathradicalalternative\v!mp\endcsname#body% we could use dowithnextbox
156  {\begingroup
157   \scratchoffset\mathradicalparameter\c!mpoffset
158   \setbox\nextbox\mathstylehbox{#body}%
159   % we use the \overlay variables as these are passes anyway and
160   % it's more efficient than using parameters
161   \d_overlay_width    \wd\nextbox
162   \d_overlay_height   \ht\nextbox
163   \d_overlay_depth    \dp\nextbox
164   \d_overlay_offset   \scratchoffset
165   \d_overlay_linewidth\the\dimexpr\triggeredmathstyleparameter\Umathfractionrule
166   %
167   \edef\overlaylinecolor{\mathradicalparameter\c!color}%
168   %
169   \edef\p_mp{\mathradicalparameter\c!mp}%
170   %
171   \setbox\scratchbox\hpack\bgroup % todo: tag this box as sqrt
172     \uniqueMPgraphic
173       {\p_mp}%
174      %{...}%
175   \egroup
176   \scratchdimen       \wd\scratchbox
177   \scratchtopoffset   \dimexpr\scratchoffset+\dp\nextbox\relax
178   \scratchbottomoffset\dimexpr\scratchoffset+\ht\nextbox/2\relax
179   \hpack to \scratchdimen{\hss\box\nextbox\hskip\scratchoffset}%
180   \hskip-\scratchdimen
181   \lower\dimexpr\scratchtopoffset\box\scratchbox%
182   \ifx\currentmathradicaldegree\empty \else
183     \setbox\scratchbox\mathstylehbox{\scriptscriptstyle\currentmathradicaldegree\hss}%
184     \wd\scratchbox\scratchdimen
185     \hskip-\scratchdimen
186     \raise\dimexpr\scratchbottomoffset\box\scratchbox
187   \fi
188   \endgroup}
189
190\definemathradical[sqrt][mp=minifun::math:radical:default]
191
192% \setupmathradical[sqrt][alternative=normal,color=darkblue]
193% \setupmathradical[sqrt][alternative=mp,color=darkgreen]
194
195%D Because I wanted to illustrate some more fun stuff another mechanism
196%D is provided as well ... let's put some dangerous tools in the hand of
197%D math jugglers like Aditya.
198
199\installcorenamespace{mathornament}
200\installcorenamespace{mathornamentalternative}
201
202\installcommandhandler \??mathornament {mathornament} \??mathornament
203
204\setupmathornament
205  [\c!alternative=\v!mp, % currently mp only .. maybe some day layer too
206   \c!mpoffset=.25\exheight]
207
208\appendtoks
209    \frozen\instance\setuevalue{\currentmathornament}{\math_ornament_handle{\currentmathornament}}%
210\to \everydefinemathornament
211
212\protected\def\math_ornament_handle#tag#body%
213  {\begingroup
214   \edef\currentmathornament{#tag}%
215   \csname\??mathornamentalternative\mathornamentparameter\c!alternative\endcsname{#body}%
216   \endgroup}
217
218\defcsname\??mathornamentalternative\v!mp\endcsname#body% we could use dowithnextbox
219  {\begingroup
220   \scratchoffset\mathornamentparameter\c!mpoffset
221   \setbox\nextbox\mathstylehbox{#body}%
222   \d_overlay_width    \wd\nextbox
223   \d_overlay_height   \ht\nextbox
224   \d_overlay_depth    \dp\nextbox
225   \d_overlay_offset   \scratchoffset
226   \d_overlay_linewidth\linewidth
227   \edef\overlaylinecolor{\mathornamentparameter\c!color}%
228   \edef\p_mp{\mathornamentparameter\c!mp}%
229   % the width of the graphic determines the width of the final result
230   \setbox\scratchbox\hpack{\uniqueMPgraphic{\p_mp}}% todo: add code key + tag
231   \hpack to \wd\scratchbox{\hss\box\nextbox\hss}%
232   \hskip-\wd\scratchbox
233   \box\scratchbox
234   \endgroup}
235
236% \startMPextensions
237%     vardef math_ornament_hat(expr w,h,d,o,l) =
238%         image ( path p ; p :=
239%             (w/2,h + 10l) --
240%             (o + w,h + o) --
241%             (w/2,h + 7l) --
242%             (-o,h + o) --
243%             cycle ;
244%             fill p ;
245%             setbounds currentpicture to (-o,0) -- (w+o,0) -- (w+o,h+2o) -- (-o,h+2o) -- cycle ;
246%         )
247%     enddef ;
248% \stopMPextensions
249%
250% \startuniqueMPgraphic{math:ornament:hat}
251%     draw
252%         math_ornament_hat(
253%             OverlayWidth,
254%             OverlayHeight,
255%             OverlayDepth,
256%             OverlayOffset,
257%             OverlayLineWidth
258%         )
259%     withpen
260%         pencircle
261%             xscaled (2OverlayLineWidth)
262%             yscaled (3OverlayLineWidth/4)
263%             rotated 30
264%     withcolor
265%         OverlayLineColor ;
266%         draw boundingbox currentpicture;
267% \stopuniqueMPgraphic
268%
269% \definemathornament [mathhat] [mp=math:ornament:hat]
270%
271% \dorecurse{8}{$\mathhat{\blackrule[width=#1ex,color=gray]}$ }
272
273\protect \endinput
274