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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
%D \module
%D [ file=math-inl,
%D version=2008.10.20,
%D title=\CONTEXT\ Math Macros,
%D subtitle=Inline,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright=PRAGMA-ADE / Hans Hagen]
%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 Math Macros / Inline}
\unprotect
%D \macros
%D {...}
%D
%D New and experimental: snapping big inline math!
\newconditional\halfcrazymathlines % \settrue\halfcrazymathlines
\newconditional\crazymathsnapping % \settrue\crazymathsnapping
\appendtoks
\doifelse\@@mtgrid\v!yes \settrue\setfalse\crazymathsnapping
\doifelse\@@mtstep\v!halfline\settrue\setfalse\halfcrazymathlines
\to \everysetuptextformulas
\setuptextformulas
[\c!grid=\v!yes,
\c!step=\v!line]
\newcount\crazymathhack
\let\lastcrazymathline \!!zeropoint
\let\lastcrazymathpage \!!zerocount
\let\lastcrazymathprelines \!!zerocount
\let\lastcrazymathpostlines\!!zerocount
\def\crazymathtag{amh:\the\crazymathhack}
\def\crazytexttag{\v!text:\lastcrazymathpage}
\def\crazymathindent{\hskip\MPx\crazymathtag\hskip-\MPx\crazytexttag}
\def\flushcrazymathbox
{\nextboxht\strutheight
\nextboxdp\strutdepth
\hbox{\iftracegridsnapping\ruledhbox\fi{\flushnextbox}}}
\def\snappedinlineformula
{\dosingleempty\dosnappedinlineformula}
%D \starttabulate[|Tl|l|]
%D \NC - \NC half lines \NC \NR
%D \NC + \NC full lines \NC \NR
%D \NC = \NC force \NC \NR
%D \NC < \NC force, minus pre \NC \NR
%D \NC > \NC force, minus post \NC \NR
%D \stoptabulate
\def\inlinemathmargin{1pt}
\settrue\autocrazymathsnapping
% FROM NOW ON, CHANGES AS OPTIONS
% TODO: SKYLINE (PREV LINE POS SCAN)
% we can rewrite this in lua but maybe we don't need it
% any more when we have proper snapping anyway
\def\dosnappedinlineformula[#1]#2%
{\ifvmode\dontleavehmode\fi % tricky
\strut % prevents funny space at line break
\begingroup % interesting: \bgroup can make \vadjust disappear
\ifconditional\crazymathsnapping
\ifgridsnapping
\checktextbackgrounds % we need pos tracking, to be made less redundant
\donetrue
\else
\donefalse
\fi
\else
\donefalse
\fi
\!!doneafalse % forced or not auto
\!!donebfalse % too heigh
\!!donecfalse % too low
\!!donedfalse % less before
\!!doneefalse % less after
\ifdone
\setbox\nextbox\hbox{$#2$}%
\iftracegridsnapping
\setbox\nextbox\ruledhbox
{\incolortrue\localcolortrue
\backgroundline[gray]{\showstruts\strut\flushnextbox}}%
\fi
\def\docommand##1%
{\doif{##1}-{\settrue \halfcrazymathlines}%
\doif{##1}+{\setfalse\halfcrazymathlines}%
\doif{##1}={\!!doneatrue}%
\doif{##1}<{\!!donedtrue}%
\doif{##1}>{\!!doneetrue}}%
\processcommalist[#1]\docommand
\if!!doneb
\if!!donec \else
\setfalse\halfcrazymathlines
\fi
\else
\if!!donec
\setfalse\halfcrazymathlines
\fi
\fi
\donefalse
\if!!donea
\donetrue
\scratchdimen \nextboxht
\advance\scratchdimen .5\lineheight
\nextboxht\scratchdimen
\scratchdimen \nextboxdp
\advance\scratchdimen .5\lineheight
\nextboxdp\scratchdimen
\else\ifdim\nextboxht>\strutht
\donetrue
\else\ifdim\nextboxdp>\strutdp
\donetrue
\fi\fi\fi
\ifconditional\autocrazymathsnapping \else \if!!donea \else
% don't compensate, just snap to strut
\donefalse
% signal for next else, snap line to strut
\!!doneatrue
\fi \fi
\fi
\ifdone
% analyze height
\scratchdimen\inlinemathmargin
\advance\scratchdimen \strutht
\ifdim\nextboxht<\scratchdimen \else \!!donebtrue \fi
% analyze depth
\scratchdimen\inlinemathmargin
\advance\scratchdimen \strutdp
\ifdim\nextboxdp<\scratchdimen \else \!!donectrue \fi
% analyzed or forced
\ifdone
\global\advance\crazymathhack\plusone
\donefalse
\ifnum\MPp\crazymathtag=\lastcrazymathpage\relax
\ifdim\MPy\crazymathtag=\lastcrazymathline\relax
\donetrue
\fi
\fi
\ifnum\MPp\crazymathtag=\zerocount \donefalse \fi
\ifdim\MPy\crazymathtag=\zeropoint \donefalse \fi
\ifdone
% same page and same line
\else
\global\let\lastcrazymathprelines \!!zerocount
\global\let\lastcrazymathpostlines\!!zerocount
\xdef\lastcrazymathpage{\MPp\crazymathtag}%
\xdef\lastcrazymathline{\MPy\crazymathtag}%
\fi
\if!!doneb
% \getrawnoflines\nextboxht
\scratchdimen\nextboxht
\advance\scratchdimen-\strutht
\getnoflines\scratchdimen
\if!!doned \advance\noflines\minusone \fi
\scratchcounter\noflines
\advance\noflines-\lastcrazymathprelines\relax
\ifnum\noflines>\zerocount
\xdef\lastcrazymathprelines{\the\scratchcounter}%
\scratchdimen\noflines\lineheight
\ifconditional\halfcrazymathlines
\advance\scratchdimen-.5\lineheight
\fi
\advance\scratchdimen-\strutdepth
\setbox\scratchbox\null
\wd\scratchbox2\bodyfontsize
\ht\scratchbox\scratchdimen
\dp\scratchbox\strutdepth
%%% top correction code (see below)
\normalvadjust pre
{%\allowbreak % sometimes breaks spacing
\forgetall
\crazymathindent
\iftracegridsnapping
\setbox\scratchbox\hbox
{\incolortrue\localcolortrue\green
\ruledhbox{\box\scratchbox}}%
\fi
\box\scratchbox
\endgraf
\nobreak}%
\else\ifnum\scratchcounter>\zerocount
\normalvadjust pre
{\nobreak}%
\fi\fi
\fi
\if!!donec
% \getrawnoflines\nextboxdp
\scratchdimen\nextboxdp
\advance\scratchdimen-\strutdp
\getnoflines\scratchdimen
\if!!donee \advance\noflines\minusone \fi
\scratchcounter\noflines
\advance\noflines-\lastcrazymathpostlines\relax
\ifnum\noflines>\zerocount
\donetrue
\else\ifnum\lastcrazymathpostlines=\zerocount
\donetrue
\else
\donefalse
\fi\fi
\else
\donefalse
\fi
\ifdone
\xdef\lastcrazymathpostlines{\the\scratchcounter}%
\ifnum\lastcrazymathpostlines=\zerocount
\global\let\lastcrazymathpostlines\!!plusone
\fi
\hbox{\setposition\crazymathtag\flushcrazymathbox}%
\scratchdimen\noflines\lineheight
\advance\scratchdimen-\lineheight
\advance\scratchdimen+\strutheight
\ifdim\scratchdimen>\zeropoint \else
\scratchdimen\strutheight % todo : test for half lines
\fi
\ifconditional\halfcrazymathlines
\advance\scratchdimen-.5\lineheight
\fi
\setbox\scratchbox\null
\wd\scratchbox2\bodyfontsize
\ht\scratchbox\scratchdimen
\dp\scratchbox\strutdepth
\normalvadjust
{\forgetall
\crazymathindent
\iftracegridsnapping
\setbox\scratchbox\hbox
{\incolortrue\localcolortrue\color[blue]{\ruledhbox{\box\scratchbox}}}%
\fi
\box\scratchbox
\endgraf
% precaution: else we stick below the text bottom
\ifconditional\halfcrazymathlines
\allowbreak
\else
\vskip-\lineheight
\vskip \lineheight
\fi}%
\else
\hbox{\setposition\crazymathtag\flushcrazymathbox}%
\fi
\else
\flushcrazymathbox
\fi
\else\if!!donea
\flushcrazymathbox
\else
\mathematics{#2}%
\fi\fi
\endgroup}
\let\tform\mathematics
\let\gform\snappedinlineformula
% test set:
%
% \startbuffer
% Crazy math \gform {1+x} or \gform {\dorecurse {100} {1+} 1 =
% 101} and even gore crazy \gform {2^{2^2}_{1_1}}
% again\dorecurse {20} { and again} \gform {\sqrt {\frac
% {x^{5^5}} {\frac {1} {2}}}} even gore\dorecurse {50} { and
% gore} \tform {\dorecurse {12} {\gform {\sqrt {\frac
% {x^{5^5}} {3}}}+\gform {\sqrt {\frac {x^{5^5}} {\frac {1}
% {2}}}}+}x=10}\dorecurse{20} { super crazy math}: \tform
% {\dorecurse {30} {\gform {\sqrt {\frac {x^{5^5}} {3}}}+
% \gform {\sqrt {\frac {x^{5^5}} {\frac {1} {2}}}}+ }x = 10},
% and we're\dorecurse {20} { done}!
% \stopbuffer
%
% \setupcolors[state=start] \setuppapersize[S6][S6]
%
% \showgrid \tracegridsnappingtrue \showstruts
%
% \starttext
% \setuplayout[grid=yes,lines=15]\getbuffer \page
% \setuplayout[grid=yes,lines=16]\getbuffer \page
% \setuplayout[grid=yes,lines=17]\getbuffer \page
% \setuplayout[grid=yes,lines=18]\getbuffer \page
% \setuplayout[grid=yes,lines=19]\getbuffer \page
% \stoptext
%
% test
%
% \startregels
% \gform[<]{35 \cdot p^{\frac{3}{4}} = 70}
% \gform{12{,}4 \cdot d^3 = 200}
% \gform{a \cdot x^b}.
% \gform{12x^6 \cdot \negative 3x^4}
% \gform{\frac{12x^6}{\negative 3x^4}}
% \gform{(4x^2)^3}
% \gform{4x \sqrt{x} \cdot 3x^2}
% \gform{\frac{2x^4}{4x \sqrt{x}}}
% \gform{y = a \cdot x^b}.
% \gform{y_1 = \frac{15x^2}{x}}
% \gform{y_2 = x \cdot \sqrt{x}}
% \gform{y_3 = \frac{6x^3}{x^2}}
% \gform[<]{y_4 = \left(2x^2\right)^{\frac{1}{2}}}
% \gform{y_1 = \frac{4x^5}{x^2}}
% \gform{y_2 = 4 \cdot \sqrt{x}}
% \gform{y_3 = 4x^3}
% \gform{y_4 = \frac{100x}{\sqrt{x}}}
% \gform[<]{y_5 = 4 \cdot x^{\frac{1}{2}}}
% \gform{y_6 = \frac{1}{2} x \cdot 4x^2}
% \gform{y_7 = 2 \cdot x^3}
% \gform{y_8 = 100 \cdot x^{\frac{1}{2}}}
% \gform{4x^8 \cdot 8x^3}
% \gform{\frac{4x^8}{8x^3}}
% \gform{\left(\negative3x^4\right)^3}
% \gform{x^3 \sqrt{x} \cdot 3x^2}
% \gform{\frac{6x^3}{x^2 \sqrt{x}}}
% \gform{\frac{6}{2x^4}}
% \gform{\frac{1}{3x^6}}
% \gform{\frac{12x^8}{4x^{10}}}
% \gform{\frac{4}{\sqrt{x}}}
% \gform{\frac{1}{2x \sqrt{x}}}
% \gform{\frac{2{,}25}{p} = 0{,}35}
% \gform{4{,}50 + \frac{300}{k} = 4{,}70}
% \gform{\frac{1200}{k+12} - 42 = 6}
% \stopregels
%D \macros
%D {enableautomath}
%D
%D The next one can be dangerous, but handy in controlled
%D situations.
\bgroup \catcode`\$=\active
\gdef\enableautomath
{\catcode`\$=\active
\def$##1${\snappedinlineformula{##1}}}
% \gdef\enableautomath
% {\catcode`\$=\active
% \def${\doifnextcharelse$\doautodmath\doautoimath}%
% \def\doautoimath##1${\snappedinlineformula{##1}}%
% \def\doautodmath$##1$${\startformula##1\stopformula}}
\egroup
\protect \endinput
|