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