math-framing.tex /size: 6244 b    last modification: 2021-10-28 13:50
1% language=us runpath=texruns:manuals/math
2
3\environment math-layout
4
5\startcomponent math-framing
6
7\startchapter[title=Framing]
8
9The \type {\framed} macro is one of the core constructors in \CONTEXT\ and it's
10used all over the place. This macro is unlikely to change its behaviour and as it
11has evolved over years it comes with quite some options and some can interfere
12with the expectations one has. In general using this macro works out well but you
13need to keep an eye on using struts and alignment.
14
15\startbuffer
16\framed{$e=mc^2$}
17\stopbuffer
18
19\typebuffer
20
21The outcome of this is:
22
23\startlinecorrection \getbuffer \stoplinecorrection
24
25There is a bit of offset (that you can set) but also struts are added as can be
26seen when we visualize them:
27
28\startlinecorrection \showstruts \getbuffer \stoplinecorrection
29
30These struts can be disabled:
31
32\startbuffer
33\framed[strut=no]{$e=mc^2$}
34\stopbuffer
35
36\typebuffer
37
38Now the result is more tight.
39
40\startlinecorrection \showstruts \getbuffer \stoplinecorrection
41
42These struts are the way to get a consistent look and feel and are used
43frequently in \CONTEXT. We mention these struts because they get in the way when
44we frame a display formula. Let's first look at what happens when we
45just package a formula in a box:
46
47\startbuffer
48\vbox\bgroup
49    \startformula
50        e = mc^2
51    \stopformula
52\egroup
53\stopbuffer
54
55\typebuffer
56
57We get:
58
59\startlinecorrection \start \showmakeup \getbuffer \stop \stoplinecorrection
60
61Now there are a few properties of displaymath that one needs to keep in mind when
62messing around with them this way. First of all display math is meant to be used
63as part of the page stream. This means that spacing above and below is adapted to
64what comes before and after. It also means that, because formulas can be numbered,
65we have some settings that relate to horizontal placement.
66
67The default vertical spacing is easy to get rid of:
68
69\startbuffer
70\vbox\bgroup
71    \startformula[packed]
72        e = mc^2
73    \stopformula
74\egroup
75\stopbuffer
76
77\typebuffer
78
79This gives:
80
81\startlinecorrection \start \showmakeup \getbuffer \stop \stoplinecorrection
82
83Another handy keyword is \type {tight}:
84
85\startbuffer
86\vbox\bgroup
87    \startformula[tight]
88        e = mc^2
89    \stopformula
90\egroup
91\stopbuffer
92
93\typebuffer
94
95This gives:
96
97\startlinecorrection \start \showmakeup \getbuffer \stop \stoplinecorrection
98
99We can combine these two:
100
101\startbuffer
102\vbox\bgroup
103    \startformula[packed,tight]
104        e = mc^2
105    \stopformula
106\egroup
107\stopbuffer
108
109\typebuffer
110
111This gives:
112
113\startlinecorrection \start \showmakeup \getbuffer \stop \stoplinecorrection
114
115Just in case you wonder why we need to go through these troubles: keep in mind
116that we are wrapping something (math) that normally goes in a vertical list with
117text above and below.
118
119The \type {packed} and \type {tight} options can help when we want to wrap
120a formula in a frame:
121
122\startbuffer
123\framed
124    [strut=no]
125    {
126        \startformula[packed,tight]
127            e = mc^2
128        \stopformula
129    }
130\stopbuffer
131
132\typebuffer
133
134which renders as:
135
136\startlinecorrection \getbuffer \stoplinecorrection
137
138There is a dedicated math framed instance that is tuned to give better results
139and automatically switches to math mode:
140
141\startbuffer
142\mframed {
143    e = mc^2
144}
145\stopbuffer
146
147\typebuffer
148
149becomes:
150
151\startlinecorrection \getbuffer \stoplinecorrection
152
153Framing a formula is also supported as a option, where the full power of framed can
154be applied to the formula. We will illustrate this in detail on the next pages. For this
155we use the following sample:
156
157\typefile{math-framing-001.tex}
158
159In \in {figure} [framing-flushleft], \in [framing-middle] \in {and}
160[framing-flushright] you see some combinations. You can run this example on your
161machine and see the details.
162
163\startplacefigure[location=page,reference=framing-flushleft,title={Framed formulas flushed left.}]
164    \startcombination[2*2]
165        {\typesetfile[math-framing-001.tex][page=01,height=.45\textheight]} {\tttf right + flushleft}
166        {\typesetfile[math-framing-001.tex][page=02,height=.45\textheight]} {\tttf right + flushleft}
167        {\typesetfile[math-framing-001.tex][page=07,height=.45\textheight]} {\tttf left  + flushleft + tight}
168        {\typesetfile[math-framing-001.tex][page=08,height=.45\textheight]} {\tttf left  + flushleft + tight}
169    \stopcombination
170\stopplacefigure
171
172\startplacefigure[location=page,reference=framing-middle,title={Framed formulas centered.}]
173    \startcombination[2*2]
174        {\typesetfile[math-framing-001.tex][page=03,height=.45\textheight]} {\tttf right + middle}
175        {\typesetfile[math-framing-001.tex][page=04,height=.45\textheight]} {\tttf right + middle}
176        {\typesetfile[math-framing-001.tex][page=09,height=.45\textheight]} {\tttf left  + middle + tight}
177        {\typesetfile[math-framing-001.tex][page=10,height=.45\textheight]} {\tttf left  + middle + tight}
178    \stopcombination
179\stopplacefigure
180
181\startplacefigure[location=page,reference=framing-flushright,title={Framed formulas flushed right.}]
182    \startcombination[2*2]
183        {\typesetfile[math-framing-001.tex][page=05,height=.45\textheight]} {\tttf right + flushright}
184        {\typesetfile[math-framing-001.tex][page=06,height=.45\textheight]} {\tttf right + flushright}
185        {\typesetfile[math-framing-001.tex][page=11,height=.45\textheight]} {\tttf left  + flushright + tight}
186        {\typesetfile[math-framing-001.tex][page=12,height=.45\textheight]} {\tttf left  + flushright + tight}
187    \stopcombination
188\stopplacefigure
189
190With each formula class a framed variants is automatically created:
191
192\startbuffer
193\defineformula
194  [foo]
195
196\setupformulaframed
197  [foo]
198  [frame=on,
199   framecolor=red]
200
201\startfooformula[frame]
202    e=mc^2
203\stopfooformula
204\stopbuffer
205
206\typebuffer
207
208This time you get a red frame:
209
210\getbuffer
211
212You can also frame the number, as in:
213
214\startbuffer
215\setupformulaframed[framecolor=red,frame=on,offset=1ex]
216\setupformula[option=frame,color=blue]
217\setupformula[numbercommand={\inframed[framecolor=green]}]
218
219\startplaceformula
220    \startformula
221        2 + 2 = 2x
222    \stopformula
223\stopplaceformula
224\stopbuffer
225
226\typebuffer
227
228The boxes get properly aligned:
229
230\start \showboxes \getbuffer \stop
231
232\stopchapter
233
234\stopcomponent
235