1
2
3
4
5\usemodule[systemtokens]
6
7\usemodule[presentboring,abbreviationslogos]
8
9\definehighlight[nb][style=bold,color=middlegray,define=no]
10
11\startdocument
12 [title={PROGRAMMING},
13 banner={local control},
14 location={context\enspace {\bf 2021}\enspace meeting}]
15
16\starttitle[title=Expansion]
17
18\TEX\ can be in several so called input reading modes:
19
20Users mostly see it reading from the source file(s). Characters are picked up and
21interpreted. Depending on what token it becomes some action takes place.
22
23\starttyping
241 \setbox0\hbox to 10pt{2} \count0=3 \the\count0 \multiply\count0 by 4
25\stoptyping
26
27\startitemize
28
29\startitem
30 The \type {1} gets typeset because characters like that are seen as text.
31\stopitem
32
33\startitem
34 The \type {\setbox} primitive triggers picking up a register number, then
35 goes on scanning for a box specification and that itself will typeset a
36 sequence of whatever until the group ends.
37\stopitem
38
39\startitem
40 The \type {count} primitive triggers scanning for a register number (or
41 reference) and then scans for a number; the equal sign is optional.
42\stopitem
43
44\startitem
45 The \type {the} primitive injects some value into the current input stream
46 (it does so by entering a new input level).
47\stopitem
48
49\startitem
50 The \type {multiply} primitive picks up a register specification and
51 multiplies that by the next scanned number. The \type {by} is optional.
52\stopitem
53
54\startitem
55 Printing from \LUA\ and scanning tokens with e.g.\ \type {\scantokens} is like
56 reading (pseudo) files.
57\stopitem
58
59\stopitemize
60
61\stoptitle
62
63\starttitle[title=Expansion]
64
65\startbuffer[def]
66\def\TestA {1 \setbox0\hbox{2} \count0=3 \the\count0}
67\stopbuffer
68
69\startbuffer[edef]
70\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
71\stopbuffer
72
73\typebuffer[def]
74\typebuffer[edef]
75
76\getbuffer[def]
77\getbuffer[edef]
78
79\blank[2*big]
80
81\startcolumns[n=2] \switchtobodyfont[8pt]
82\luatokentable\TestA
83\column
84\luatokentable\TestB
85\stopcolumns
86
87\stoptitle
88
89\starttitle[title=Local control]
90
91\startbuffer[edef]
92\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
93\stopbuffer
94
95\startbuffer[ldef]
96\edef\TestC{1 \setbox0\hbox{2} \localcontrolled{\count0=3} \the\count0}
97\stopbuffer
98
99\typebuffer[edef]
100\typebuffer[ldef]
101
102\getbuffer[edef]
103\getbuffer[ldef]
104
105\blank[2*big]
106
107\startcolumns[n=2] \switchtobodyfont[8pt]
108\luatokentable\TestB
109\column
110\luatokentable\TestC
111\stopcolumns
112
113\stoptitle
114
115\starttitle[title=Side effects]
116
117\startbuffer[edef]
118\edef\TestB{1 \setbox0\hbox{2} \count0=3 \the\count0}
119\stopbuffer
120
121\startbuffer[ldef]
122\edef\TestD{\localcontrolled{1 \setbox0\hbox{2} \count0=3 \the\count0}}
123\stopbuffer
124
125\typebuffer[edef]
126\typebuffer[ldef]
127
128\getbuffer[edef]\getbuffer[ldef]\quad{\darkgray\leftarrow\space Watch how the results end up here!}
129
130\blank[2*big]
131
132\startcolumns[n=2] \switchtobodyfont[8pt]
133\luatokentable\TestB
134\column
135\luatokentable\TestD
136\stopcolumns
137
138\stoptitle
139
140\starttitle[title=Usage]
141
142\startbuffer[def]
143\def\WidthOf#1
144 {\beginlocalcontrol
145 \setbox0\hbox{#1}
146 \endlocalcontrol
147 \wd0 }
148\stopbuffer
149
150\startbuffer[use]
151\scratchdimen\WidthOf{The Rite Of Spring}
152
153\the\scratchdimen
154\stopbuffer
155
156\typebuffer[def]
157\typebuffer[use]
158
159\getbuffer[def]\getbuffer[use]
160
161\stoptitle
162
163\starttitle[title=Not always pretty]
164
165\startbuffer[def]
166\def\WidthOf#1
167 {\dimexpr
168 \beginlocalcontrol
169 \begingroup
170 \setbox0\hbox{#1}
171 \expandafter
172 \endgroup
173 \expandafter
174 \endlocalcontrol
175 \the\wd0
176 \relax}
177\stopbuffer
178
179\startbuffer[use]
180\scratchdimen\WidthOf{The Rite Of Spring}
181
182\the\scratchdimen
183\stopbuffer
184
185\typebuffer[def]
186\typebuffer[use]
187
188\getbuffer[def]\getbuffer[use]
189
190\stoptitle
191
192\starttitle[title=The \LUA\ end]
193
194Right from the start the way to get something into \TEX\ from \LUA\ has been the
195print functions. But we can also go local (immediate). There are several methods:
196
197\startitemize
198\startitem via a set token register \stopitem
199\startitem via a defined macro \stopitem
200\startitem via a string \stopitem
201\stopitemize
202
203Among the things to keep in mind are catcodes, scope and expansion (especially in
204when the result itself ends up in macros).
205
206\stoptitle
207
208\starttitle[title=Via a token register]
209
210
211
212\startbuffer[set]
213\toks0={\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}}
214\toks2={\setbox0\hbox{The Rite Of Spring (Joe Parrish)}}
215\stopbuffer
216
217\typebuffer[set]
218
219\startbuffer[run]
220\startluacode
221tex.runlocal(0) context("[1: %p]",tex.box[0].width)
222tex.runlocal(2) context("[2: %p]",tex.box[0].width)
223\stopluacode
224\stopbuffer
225
226\typebuffer[run]
227
228\start \getbuffer[set,run] \stop
229
230\stoptitle
231
232\starttitle[title=Via a token macro]
233
234\startbuffer[set]
235\def\TestA{\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}}
236\def\TestB{\setbox0\hbox{The Rite Of Spring (Joe Parrish)}}
237\stopbuffer
238
239\typebuffer[set]
240
241\startbuffer[run]
242\startluacode
243tex.runlocal("TestA") context("[3: %p]",tex.box[0].width)
244tex.runlocal("TestB") context("[4: %p]",tex.box[0].width)
245\stopluacode
246\stopbuffer
247
248\typebuffer[run]
249
250\start \getbuffer[set,run] \stop
251
252\stoptitle
253
254\starttitle[title=Via a string]
255
256\startbuffer[run]
257\startluacode
258tex.runstring([[\setbox0\hbox{The Rite Of Spring (Igor Stravinsky)}]])
259
260context("[5: %p]",tex.box[0].width)
261
262tex.runstring([[\setbox0\hbox{The Rite Of Spring (Joe Parrish)}]])
263
264context("[6: %p]",tex.box[0].width)
265\stopluacode
266\stopbuffer
267
268\typebuffer[run]
269
270\start \getbuffer[run] \stop
271
272\blank[2*big]
273
274A bit more high level:
275
276\starttyping
277context.runstring([[\setbox0\hbox{(Here \bf 1.2345)}]])
278context.runstring([[\setbox0\hbox{(Here \bf
279\stoptyping
280
281\stoptitle
282
283\starttitle[title=Locked in \LUA]
284
285\startbuffer[run]
286\startluacode
287token.setmacro("TestX",[[\setbox0\hbox{The Rite Of Spring (Igor)}]])
288tex.runlocal("TestX")
289context("[7: %p]",tex.box[0].width)
290\stopluacode
291\stopbuffer
292
293\typebuffer[run]
294
295\start \getbuffer[run] \stop
296
297\startbuffer[run]
298\startluacode
299tex.scantoks(0,tex.ctxcatcodes,[[\setbox0\hbox{The Rite Of Spring (Joe)}]])
300tex.runlocal(0)
301context("[8: %p]",tex.box[0].width)
302\stopluacode
303\stopbuffer
304
305\typebuffer[run]
306
307\start \getbuffer[run] \stop
308
309\stoptitle
310
311\starttitle[title=Order matters]
312
313A lot this relates to pushing stuff into the input which is stacked. Compare:
314
315\startbuffer[run]
316\startluacode
317context("[HERE 1]")
318context("[HERE 2]")
319\stopluacode
320\stopbuffer
321
322\typebuffer[run]
323
324\start \getbuffer[run] \stop
325
326with this:
327
328\startbuffer[run]
329\startluacode
330tex.pushlocal() context("[HERE 1]") tex.poplocal()
331tex.pushlocal() context("[HERE 2]") tex.poplocal()
332\stopluacode
333\stopbuffer
334
335\typebuffer[run]
336
337\start \getbuffer[run] \stop
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369\stopdocument
370 |