details-tuningformulas.tex /size: 5248 b    last modification: 2023-12-21 09:43
1% language=us
2
3\environment details-environment
4
5\startcomponent details-tuningformulas
6
7\startchapter[title={Tuning math formulas}]
8
9Because of its look and feel, a math formula can look too widely spaced when put
10on a grid. There are a few ways to control this. First of all, the default grid
11option bound to math is already more tolerant. But you can control it locally
12too. Take the following formula:
13
14\startbuffer
15\startformula
16  a = b^c
17\stopformula
18\stopbuffer
19
20\blank \fakeline \getbuffer \fakeline \blank
21
22This has been entered as:
23
24\typebuffer
25
26and because it is just a line of math it comes out as expected. The next code
27
28\startbuffer
29\startformula
30  a = \frac {a} {b}
31\stopformula
32\stopbuffer
33
34\typebuffer
35
36produces a higher line:
37
38\blank \fakeline \getbuffer \fakeline \blank
39
40as does:
41
42\startbuffer
43\startformula
44  a = \frac {\frac {b} {c}} {\frac {d} {e}}
45\stopformula
46\stopbuffer
47
48\typebuffer
49
50\blank \fakeline \getbuffer \fakeline \blank
51
52We will now demonstrate three ways to compensate fo rexcessive spacing. The first
53variant just sets a grid parameter:
54
55\startbuffer
56\startformula[grid=math:-halfline]
57    a = \frac {\frac {b} {c}} {\frac {d} {e}}
58\stopformula
59\stopbuffer
60
61\typebuffer
62
63\blank \fakeline \getbuffer \fakeline \blank
64
65You can also pass this as an option. Only a few such grid related options are
66defined: \type {halfline}, \type {line}, \type {-halfline} and \type {-grid}.
67
68\startbuffer
69\startformula[-halfline]
70    a = \frac {\frac {b} {c}} {\frac {d} {e}}
71\stopformula
72\stopbuffer
73
74\typebuffer
75
76\blank \fakeline \getbuffer \fakeline \blank
77
78If you need to compensate frequently you can consider defining an instance:
79
80\startbuffer
81\defineformula[tight][grid=math:-halfline]
82
83\starttightformula
84    a = \frac {\frac {b} {c}} {\frac {d} {e}}
85\stoptightformula
86\stopbuffer
87
88\typebuffer
89
90\blank \fakeline \getbuffer \fakeline \blank
91
92The result can be somewhat unexpected at the top and bottom of a page. When we
93subtract half a line from the height we can end up above the text area. This is
94where the \type {split} directive comes in. So, the compensations are actually
95defined as
96
97\starttabulate[|TCT{blue}|T|]
98\NC math            \NC \theexpandedsnapperset{math} \NC \NR
99\NC math:line       \NC \theexpandedsnapperset{math:line} \NC \NR
100\NC math:halfline   \NC \theexpandedsnapperset{math:halfline} \NC \NR
101\NC math:-line      \NC \theexpandedsnapperset{math:-line} \NC \NR
102\NC math:-halfline  \NC \theexpandedsnapperset{math:-halfline} \NC \NR
103\stoptabulate
104
105You can define your own variants building on top of an existing one:
106
107\starttyping
108\definegridsnapping[math:my][math,....]
109\stoptyping
110
111We demonstrate the effect of the \type {split} directive here. It triggers a
112check at the page boundaries but you need to keep in mind that this is not always
113robust as such boundaries themselves can be triggered by and inject anything.
114
115\startbuffer[a]
116\dorecurse {15} {
117    \startformula[grid={math,-halfline}]
118        a = \frac {\frac {b} {c}} {\frac {d} {e}}
119        (\hbox{top #1 default})
120    \stopformula
121    \blank[samepage]
122    \fakeline
123}
124\stopbuffer
125
126\startbuffer[b]
127\dorecurse {15} {
128    \startformula[grid={math,-halfline,split}]
129        a = \frac {\frac {b} {c}} {\frac {d} {e}}
130        (\hbox{top #1 compensated})
131    \stopformula
132    \blank[samepage]
133    \fakeline
134}
135\stopbuffer
136
137\getbuffer[a]
138\getbuffer[b]
139
140As said, the compensation is achieved with the \type {page} directive. The
141previous pages were rendered using:
142
143\typebuffer[a]
144
145and
146
147\typebuffer[b]
148
149In order to get a consistent result we keep the depth of the formula the same but
150effectively shift it down a bit, still honouring the grid. So what about the
151bottom.
152
153We can decide that the snapped formula doesn't fit and force a new page but we
154can also accept that it sticks out to the bottom, which is less worse than the
155top|-|of|-|the|-|page case.
156
157\startbuffer[a]
158\dorecurse{45}{\fakeline}
159\startformula[grid={math,-halfline}]
160    a = \frac {\frac {b} {c}} {\frac {d} {e}}
161    (\hbox{bottom default})
162\stopformula
163\stopbuffer
164
165\startbuffer[b]
166\dorecurse{45}{\fakeline}
167\startformula[grid={math,-halfline,split}]
168    a = \frac {\frac {b} {c}} {\frac {d} {e}}
169    (\hbox{bottom compensated})
170\stopformula
171\stopbuffer
172
173\page \getbuffer[a] % fits on the page
174\page \getbuffer[b] % forces a new page
175
176These mechanisms might be improved over time but as we don't use it frequently
177that might take a while.
178
179The following formula was posted at the \CONTEXT\ mailing list in a grid snapping
180thread and we will use it to demonstrate how you can mess a bit with the
181snapping.
182
183\startbuffer
184g(x_{*}) = \lim_{n\to\infty} g(a_{n}) \leq 0 \leq \lim_{n\to\infty} g(b_{n}) = g(x_{*})
185\stopbuffer
186
187\typebuffer
188
189We show the given grid parameter as well as its expansion into the low level grid
190directives.
191
192\unexpanded\def\SampleFormula#1%
193  {\definegridsnapping[math:temp][#1]
194   \blank
195   \type{grid=#1} \hfill expanded: \normalexpanded{\type{\theexpandedsnapperset{math:temp}}}
196   \blank[samepage]
197   \fakeline
198   \blank[samepage]
199   \startformula[grid={#1}]
200     \getbuffer
201   \stopformula
202   \blank[samepage]
203   \fakeline
204   \blank}
205
206\SampleFormula{math}
207\SampleFormula{low,halfline}
208\SampleFormula{math,nodepth}
209
210\stopchapter
211
212\stopcomponent
213