pack-mat.mkxl /size: 10 Kb    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=pack-rul, % was core-rul,
3%D        version=2022.02.23,
4%D          title=\CONTEXT\ Packaging Macros,
5%D       subtitle=Ruled Math,
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 Packaging Macros / Ruled Math}
15
16%D This is a by product of the math improvement activity by Mikael Sundqvist and
17%D myself. More might end up here and more control might be added.
18
19\unprotect
20
21% todo \v!mathframed
22% todo \v!mathstrikeout
23
24\newconditional\c_framed_math_framed
25
26\installtextracker
27  {framed.math}
28  {\c_framed_math_framed\conditionaltrue}
29  {\c_framed_math_framed\conditionalfalse}
30
31\integerdef\c_framed_math_last_left \zerocount
32\integerdef\c_framed_math_last_right\zerocount
33
34\installglobalmacrostack\c_framed_math_last_left
35\installglobalmacrostack\c_framed_math_last_right
36
37\installcorenamespace{mathframed}
38
39\defineframed[\??mathframed]
40
41\setupframed[\??mathframed][\c!location=\v!middle]
42
43\tolerant\permanent\protected\def\definemathframed[#1]#*[#2]#*[#S#3]%
44  {\ifarguments\or
45     \defineframed[#1][\??mathframed]%
46   \or
47     \defineframed[#1][\??mathframed][#2]%
48   \or
49     \defineframed[#1][#2][#3]%
50   \fi
51   \tolerant\frozen\protected\defcsname#1\endcsname{\math_framed{#1}}}
52
53\definemathframed[mathframed]
54
55\dimensiondef\d_math_framed_offset\zeropoint
56
57\def\math_framed#1#*[#S#2]#:#3%
58  {\begingroup
59   \ifconditional\c_framed_math_framed
60     \defineboxanchor[temp]%
61   \fi
62   \setmathatomrule\mathbegincode\mathbinarycode\allmathstyles\mathbegincode\mathbinarycode % hm
63   %
64   \scratchdimen\Umathaxis\givenmathstyle\mathstyle
65   %
66   \cdef\currentframed{#1}%
67   \setbox\scratchbox\hbox{\localframedwithsettings[#1][#2]%
68     {\strut\global\dimensiondef\d_math_framed_offset\dimexpr
69         \framedoffset/2
70%          +2\d_framed_linewidth
71      \relax}}%
72   %
73   \setbox\scratchbox\hpack\bgroup
74     \startimath
75     \givenmathstyle\mathstyle
76     #3%
77     \stopimath
78     \normalexpanded{\aftergrouped{%
79       \integerdef\c_framed_math_last_left \the\lastleftclass
80       \integerdef\c_framed_math_last_right\the\lastrightclass
81     }}%
82   \egroup
83   %
84   \boxymove\scratchbox\dimexpr
85     \scratchdimen
86     -\d_math_framed_offset/2
87   \relax
88   %
89   \setbox\scratchbox\hpack
90     \localframedwithsettings
91       [#1][#2]%
92       {\box\scratchbox}%
93   %
94   \boxymove\scratchbox\scratchdimen
95   %
96   \ifconditional\c_framed_math_framed
97     \boxsource\scratchbox \namedboxanchor{temp}% test
98   \fi
99   %
100   \mathatom
101     \s!class \mathexplicitcode
102     \ifnum\c_framed_math_last_left <\zerocount\else\s!leftclass \c_framed_math_last_left \fi
103     \ifnum\c_framed_math_last_right<\zerocount\else\s!rightclass\c_framed_math_last_right\fi
104     \bgroup
105       \ifconditional\c_framed_math_framed
106         \setboxanchor[temp][\c!corner={\v!left ,\v!bottom},\c!location={\v!right,\v!height}]%
107           \hpack{\smallinfofont\the\c_framed_math_last_left}%
108         \setboxanchor[temp][\c!corner={\v!right,\v!bottom},\c!location={\v!left ,\v!height}]%
109           \hpack{\smallinfofont\the\c_framed_math_last_right}%
110       \fi
111       \box\scratchbox
112     \egroup
113   \endgroup}
114
115\defineoverlay[mathstrikeout][\useMPgraphic{\framedparameter{mp}}]
116
117\definemathframed
118  [mathstrikeout]
119  [\c!offset=\v!overlay,
120   \c!mp=mathstrikeout,
121   \c!rulethickness=1.5\linewidth,
122   \c!background={\v!foreground,mathstrikeout},
123  %\c!color=darkgreen,
124   \c!textstyle=\txx,
125   \c!mathstyle=\s!script,
126   \c!frame=\v!off,
127   \c!arrow=\v!yes]
128
129\definemathframed
130  [mathcrossout]
131  [mathstrikeout]
132  [\c!mp=mathcrossout,
133   strike:location=bottom]
134
135\permanent\protected\def\mathstrikeoutlabel
136  {\ifempty{\framedparameter{strike:math}}%
137     \useframedstyleandcolor\c!textstyle\c!color
138     \framedparameter{strike:text}%
139   \else
140     \startimath
141       \dousecolorparameter{\framedparameter\c!color}%  mathcolor
142       \usemathstyleparameter\framedparameter\c!mathstyle
143       \framedparameter{strike:math}%
144     \stopimath
145   \fi}
146
147\startuseMPgraphic{mathstrikeout}
148    path arr ; arr := (llcorner OverlayBox -- urcorner OverlayBox) ;
149    if "\framedparameter{arrow}" = "yes" : drawarrow else : draw fi arr
150        withpen pencircle scaled OverlayLineWidth
151        withcolor OverlayColor
152    ;
153    label.top("\mathstrikeoutlabel", point 1 of arr)
154        withcolor OverlayColor
155    ;
156  % draw OverlayBox
157  %     dashed evenly
158  %     withpen pencircle scaled .5OverlayLineWidth
159  %     withcolor OverlayColor
160  % ;
161    setbounds currentpicture to OverlayBox ;
162\stopuseMPgraphic
163
164\startuseMPgraphic{mathcrossout}
165    draw (
166            (llcorner OverlayBox -- urcorner OverlayBox) &&
167            (ulcorner OverlayBox -- lrcorner OverlayBox)
168        )
169        withpen pencircle scaled OverlayLineWidth
170        withcolor OverlayColor
171    ;
172    if "\framedparameter{strike:location}" = "bottom" :
173        label.bot("\mathstrikeoutlabel", center bottomboundary currentpicture)
174    else :
175        label.top("\mathstrikeoutlabel", center topboundary currentpicture)
176    fi
177        withcolor OverlayColor
178    ;
179    setbounds currentpicture to OverlayBox ;
180\stopuseMPgraphic
181
182%D \starttyping
183%D \enabletrackers[framed.math]
184%D
185%D \startTEXpage[offset=10pt,width=40dk]
186%D     \showmakeup[mathglue]
187%D     \def\MyTest#1%
188%D       {$              x #1                          x $\quad
189%D        $              x \mathframed[offset=0pt]{#1} x $\quad
190%D        $\displaystyle x #1                          x $\quad
191%D        $\displaystyle x \mathframed[offset=0pt]{#1} x $}
192%D     \MyTest{>}\blank
193%D     \MyTest{+}\blank
194%D     \MyTest{!}\blank
195%D     \MyTest{+\frac{1}{2}+}\blank
196%D     \MyTest{\frac{1}{2}}\blank
197%D \stopTEXpage
198%D \stoptyping
199
200%D From the list:
201
202%D \starttyping
203%D \startformula[spaceinbetween=.5\lineheight]
204%D     \left\{
205%D         \mparagraph{Quaternion algebras\par over $\blackboard{Q}$ up to isomorphism}
206%D     \right\}
207%D     \alignhere \leftrightarrow
208%D     \left\{
209%D         \mparagraph
210%D           [foregroundcolor=darkred,offset=.1ex,frame=on]
211%D           {Finite subset of places of $\blackboard{Q}$\par of even cardinality}
212%D     \right\}
213%D     \breakhere \leftrightarrow
214%D     \left\{
215%D         \mparagraph{more text}
216%D     \right\}
217%D     \breakhere \leftrightarrow
218%D     \left\{
219%D         \mparagraph[foregroundcolor=darkblue]{and more text}
220%D     \right\}
221%D \stopformula
222%D \stoptyping
223
224\permanent\tolerant\protected\def\mparagraph[#S#1]#*#:#2%
225  {\vcenter class \mathconstructcode \bgroup % \mathwrappedcode would be like fraction
226     \framed[\c!offset=\v!overlay,\c!frame=\v!overlay,\c!align=\v!middle,#1]{#2}%
227   \egroup}
228
229%D Moved from pack-rul.mkxl to here:
230
231%D \macros
232%D   {mframed, minframed}
233%D
234%D See mkii and mkiv files for some history on this already rather old mechanism; it
235%D dates from the real beginning. In the meantime we used anchoring, and now we're
236%D doing it more \LMTX ish.
237
238% mframed   xx\mframed  {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
239% inmframed xx\inmframed{x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
240% mcframed  xx\mcframed {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
241% mtframed  xx\mtframed {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
242
243\newinteger\c_framed_mstyle
244
245\installcorenamespace{mathframed}
246
247\installframedcommandhandler \??mathframed {mathframed} \??mathframed
248
249\newtoks     \t_pack_framed_mathframed
250\newdimension\d_pack_framed_mathframed
251
252\appendtoks
253    \frozen\protected\instance\edefcsname\currentmathframed\endcsname{\pack_framed_mathframed[\currentmathframed]}%
254\to \everydefinemathframed
255
256\tolerant\protected\def\pack_framed_mathframed[#1]#*[#S#2]#:#3% needs testing !
257  {\begingroup
258   \cdef\currentmathframed{#1}%
259   \setupcurrentmathframed[#2]%
260   \c_framed_mstyle\normalmathstyle
261   \edef\m_framed_location{\mathframedparameter\c!location}%
262   \ifx\m_framed_location\v!mathematics
263     \inheritedmathframedframed\bgroup
264       \setbox\scratchbox\hbox\bgroup
265         \Ustartmath
266         \triggermathstyle\c_framed_mstyle
267         \expand\t_pack_framed_mathframed
268         \mathatom \mathunspacedcode\bgroup
269           #3%
270         \egroup
271         \Ustopmath
272       \egroup
273       \global\d_pack_framed_mathframed\dp\scratchbox
274       \dontleavehmode\box\scratchbox
275     \egroup
276   \else
277     \inheritedmathframedframed\bgroup
278       \hbox\bgroup
279         \Ustartmath
280         \triggermathstyle\c_framed_mstyle
281           #3%
282         \Ustopmath
283       \egroup
284     \egroup
285   \fi
286   \endgroup}
287
288\appendtoks
289    \c_strc_math_ragged_status  \plustwo
290    \c_strc_formulas_check_width\zerocount
291\to \t_pack_framed_mathframed
292
293\installframedlocator \v!mathematics
294  {}
295  {\pack_framed_locator_set_lo
296   \ifzeropt\d_framed_locator_lo\else
297     \lower\dimexpr\d_framed_locator_lo+\d_pack_framed_mathframed\relax
298   \fi
299   \box\b_framed_normal}
300
301\definemathframed[mframed]
302\definemathframed[inmframed][\c!location=\v!low]
303\definemathframed[mcframed] [\c!location=\v!mathematics] % centered
304\definemathframed[mtframed] [\c!location=\v!mathematics,\c!offset=\v!overlay,\c!frame=\v!overlay] % tight
305
306%D So instead of the rather versatile \type {\framed}, we use \type {\mframed}:
307%D
308%D \startbuffer
309%D \startformula
310%D   x \times   \mframed{y} \times y^{z_z}
311%D   x \times \inmframed{y} \times y^{z_z}
312%D \stopformula
313%D \stopbuffer
314%D
315%D \typebuffer \getbuffer
316%D
317%D And:
318%D
319%D \startbuffer
320%D \startformula
321%D   x \times \mframed{y} \times y^{\mframed{z}_{\mframed{z}}}
322%D \stopformula
323%D \stopbuffer
324%D
325%D \typebuffer \getbuffer
326%D
327%D As usual, one can specify in what way the text should be framed. One should be
328%D aware of the fact that, inorder to preserve the proper spacing, the \type
329%D {offset} is set to \type {overlay} and \type {frameoffset} is used used instead.
330%D
331%D \startbuffer
332%D \startformula
333%D   x \times y^{\mframed[framecolor=red]{z}_{z}}
334%D \stopformula
335%D \stopbuffer
336%D
337%D \typebuffer \getbuffer
338%D
339%D For inline use, we also provide the \type {\inmframed} alternative: we want $x
340%D \times \inmframed{y}$ in inline math, right?
341
342\protect
343