summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/pack-mat.mkxl
blob: 66be25a9d12004378b704671fdd228844583b01b (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
216
217
218
219
220
221
222
223
224
225
226
%D \module
%D   [       file=pack-rul, % was core-rul,
%D        version=2022.02.23,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Ruled Math,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Packaging Macros / Ruled Math}

%D This is a by product of the math improvement activity by Mikael Sundqvist and
%D myself. More might end up here and more control might be added.

\unprotect

\newconditional \c_framed_math_framed

\installtextracker
  {framed.math}
  {\settrue \c_framed_math_framed}
  {\setfalse\c_framed_math_framed}

\tolerant\protected\def\mathframed[#1]#:#2%
  {\begingroup
   \ifconditional\c_framed_math_framed
     \defineboxanchor[temp]%
   \fi
   \setmathatomrule\mathbegincode\mathbincode\allmathstyles\mathbegincode\mathbincode % hm
   \normalexpanded
     {\setbox\scratchbox\hpack
        \ifconditional\c_framed_math_framed
          \s!source \namedboxanchor{temp}
        \fi
        \s!ymove \Umathaxis\Ustyle\mathstyle
        \bgroup
          \framed
            [\c!location=\v!middle,#1]
            {\startimath
             \Ustyle\mathstyle
             #2%
             \stopimath}%
        \egroup}%
   \scratchcounterone\lastleftclass
   \scratchcountertwo\lastrightclass
 % \writestatus{!!}{[\number\lastleftclass,\number\lastrightclass]}%
   \mathatom
     \s!class \mathexplicitcode
     \ifnum\scratchcounterone<\zerocount\else\s!leftclass \scratchcounterone\fi
     \ifnum\scratchcountertwo<\zerocount\else\s!rightclass\scratchcountertwo\fi
     \bgroup
       \ifconditional\c_framed_math_framed
         \setboxanchor[temp][\c!corner={\v!left,\v!bottom},\c!location={\v!right,\v!height}]
           \hpack{\smallinfofont \the\scratchcounterone}%
         \setboxanchor[temp][\c!corner={\v!right,\v!bottom},\c!location={\v!left,\v!height}]
           \hpack{\smallinfofont \the\scratchcountertwo}%
       \fi
       \box\scratchbox
     \egroup
   \endgroup}

%D \starttyping
%D \enabletrackers[framed.math]
%D
%D \startTEXpage[offset=10pt,width=40dk]
%D     \showmakeup[mathglue]
%D     \def\MyTest#1%
%D       {$              x #1                          x $\quad
%D        $              x \mathframed[offset=0pt]{#1} x $\quad
%D        $\displaystyle x #1                          x $\quad
%D        $\displaystyle x \mathframed[offset=0pt]{#1} x $}
%D     \MyTest{>}\blank
%D     \MyTest{+}\blank
%D     \MyTest{!}\blank
%D     \MyTest{+\frac{1}{2}+}\blank
%D     \MyTest{\frac{1}{2}}\blank
%D \stopTEXpage
%D \stoptyping

%D From the list:

%D \starttyping
%D \startformula[spaceinbetween=.5\lineheight]
%D     \left\{
%D         \mparagraph{Quaternion algebras\par over $\blackboard{Q}$ up to isomorphism}
%D     \right\}
%D     \alignhere \leftrightarrow
%D     \left\{
%D         \mparagraph
%D           [foregroundcolor=darkred,offset=.1ex,frame=on]
%D           {Finite subset of places of $\blackboard{Q}$\par of even cardinality}
%D     \right\}
%D     \breakhere \leftrightarrow
%D     \left\{
%D         \mparagraph{more text}
%D     \right\}
%D     \breakhere \leftrightarrow
%D     \left\{
%D         \mparagraph[foregroundcolor=darkblue]{and more text}
%D     \right\}
%D \stopformula
%D \stoptyping

\permanent\tolerant\protected\def\mparagraph[#1]#*#:#2%
  {\vcenter class \mathconstructcode \bgroup % \mathwrappedcode would be like fraction
     \framed[\c!offset=\v!overlay,\c!frame=\v!overlay,\c!align=\v!middle,#1]{#2}%
   \egroup}

%D Moved from pack-rul.mkxl to here:

%D \macros
%D   {mframed, minframed}
%D
%D See mkii and mkiv files for some history on this already rather old mechanism; it
%D dates from the real beginning. In the meantime we used anchoring, and now we're
%D doing it more \LMTX ish.

% mframed   xx\mframed  {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
% inmframed xx\inmframed{x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
% mcframed  xx\mcframed {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte
% mtframed  xx\mtframed {x^2 + \frac{2}{x} + \sqrt{\frac{2}{\sqrt{\frac{2}{x}}}}} \input tufte

\newcount\c_framed_mstyle

\installcorenamespace{mathframed}

\installframedcommandhandler \??mathframed {mathframed} \??mathframed

\newtoks \t_pack_framed_mathframed
\newdimen\d_pack_framed_mathframed

\appendtoks
    \frozen\protected\instance\edefcsname\currentmathframed\endcsname{\pack_framed_mathframed[\currentmathframed]}%
\to \everydefinemathframed

\tolerant\protected\def\pack_framed_mathframed[#1]#*[#2]#:#3% needs testing !
  {\begingroup
   \edef\currentmathframed{#1}%
   \setupcurrentmathframed[#2]%
   \c_framed_mstyle\normalmathstyle
   \edef\m_framed_location{\mathframedparameter\c!location}%
   \ifx\m_framed_location\v!mathematics
     \inheritedmathframedframed\bgroup
       \setbox\scratchbox\hbox\bgroup
         \Ustartmath
         \triggermathstyle\c_framed_mstyle
         \the\t_pack_framed_mathframed
         \mathatom \mathunspacedcode\bgroup
           #3%
         \egroup
         \Ustopmath
       \egroup
       \global\d_pack_framed_mathframed\dp\scratchbox
       \dontleavehmode\box\scratchbox
     \egroup
   \else
     \inheritedmathframedframed\bgroup
       \hbox\bgroup
         \Ustartmath
         \triggermathstyle\c_framed_mstyle
           #3%
         \Ustopmath
       \egroup
     \egroup
   \fi
   \endgroup}

\appendtoks
    \c_strc_math_ragged_status  \plustwo
    \c_strc_formulas_check_width\zerocount
\to \t_pack_framed_mathframed

\installframedlocator \v!mathematics
  {}
  {\pack_framed_locator_set_lo
   \ifzeropt\d_framed_locator_lo\else
     \lower\dimexpr\d_framed_locator_lo+\d_pack_framed_mathframed\relax
   \fi
   \box\b_framed_normal}

\definemathframed[mframed]
\definemathframed[inmframed][\c!location=\v!low]
\definemathframed[mcframed] [\c!location=\v!mathematics] % centered
\definemathframed[mtframed] [\c!location=\v!mathematics,\c!offset=\v!overlay,\c!frame=\v!overlay] % tight

%D So instead of the rather versatile \type {\framed}, we use \type {\mframed}:
%D
%D \startbuffer
%D \startformula
%D   x \times   \mframed{y} \times y^{z_z}
%D   x \times \inmframed{y} \times y^{z_z}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D And:
%D
%D \startbuffer
%D \startformula
%D   x \times \mframed{y} \times y^{\mframed{z}_{\mframed{z}}}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D As usual, one can specify in what way the text should be framed. One should be
%D aware of the fact that, inorder to preserve the proper spacing, the \type
%D {offset} is set to \type {overlay} and \type {frameoffset} is used used instead.
%D
%D \startbuffer
%D \startformula
%D   x \times y^{\mframed[framecolor=red]{z}_{z}}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D For inline use, we also provide the \type {\inmframed} alternative: we want $x
%D \times \inmframed{y}$ in inline math, right?

\protect