1
2
3\startcomponent metafunconventions
4
5\environment metafunenvironment
6
7\startchapter[title={Conventions}]
8
9\startsection[title={Suffixes}]
10
11One characteristic of using \METAFUN\ in \CONTEXT\ is that it is basically one
12long run. The code snippets become figures that then get converted to \PDF\ and
13embedded. If text is involved, each figure is actually processed twice, once to
14identify what needs to be typeset, and once with the result(ing metrics).
15Normally that gets unnoticed. You can check for the state by consulting the
16boolean \type {mfuntrialrun}.
17
18A consequence of the one run cq.\ multiple runs is that you need to be careful with
19undefined or special variables. Consider the following:
20
21\starttyping
22vardef foo@#(text t) =
23 save s ; string s ; s := str @# ;
24 if length(s) > 0 :
25 textext(s)
26 else :
27 nullpicture
28 fi
29enddef ;
30\stoptyping
31
32The following works ok in the first run when bar is undefined:
33
34\starttyping
35draw foo.bar("a") ;
36\stoptyping
37
38But if afterwards we say:
39
40\starttyping
41vardef bar(expr x) =
42 123
43enddef ;
44\stoptyping
45
46and expand \type {foo.bar} again we will get an error message because this time
47\type {bar} expands. Suffixes are always expanded!
48
49The lesson is: when you get unexpected results or errors, check your variable
50definitions. You can use the \type {begingroup} and \type {endgroup} primitives
51to protect your variables but then you also need to explicitly use \type {save}
52to store their meanings and allocate new ones after that inside the group.
53
54\stopsection
55
56\startsection[title=Templates]
57
58This section is a bit offtopic and thereby also qualifies as sneaky. At the
59\TEX\ end we have a couple of alternative input methods, like \XML\ and templates.
60Just because we want to be consistent, the \METAPOST\ end also offers this.
61
62The first example resembled the \type{btex ... etex} approach:
63
64\startbuffer
65\startbuffer[testa]
66 blua for i=0,100,20 do elua
67 draw fullcircle scaled (blua p(i) elua * cm)
68 withcolor "darkgreen" withpen pencircle scaled 1cm ;
69 blua end elua
70
71 blua for i=10,100,20 do elua
72 draw fullcircle scaled (blua p(i) elua * cm)
73 withcolor "darkred" withpen pencircle scaled 1cm ;
74 blua end elua
75\stopbuffer
76
77\startMPcode
78 draw image (
79 loadfile ("mpstemplate://buffer?name=test-a") ;
80 ) ysized 3cm ;
81\stopMPcode
82\stopbuffer
83
84\typebuffer[test1][option=TEX]
85
86The filename is specified as a \URI\ and the \type {mpstemplate} does the magic
87here.
88
89\startlinecorrection[blank] \getbuffer \stoplinecorrection
90
91The second example is like the \type {lmx} files that you can find in the distibution:
92
93\startbuffer
94\startbuffer[testb]
95 <?lua for i=0,100,20 do ?>
96 draw fullcircle scaled (<?lua p(i) ?> * cm)
97 withcolor "darkblue" withpen pencircle scaled 1cm ;
98 <?lua end ?>
99
100 <?lua for i=10,100,20 do ?>
101 draw fullcircle scaled (<?lua p(i) ?> * cm)
102 withcolor "darkyellow" withpen pencircle scaled 1cm ;
103 <?lua end ?>
104\stopbuffer
105
106\startMPcode
107 draw image (
108 loadfile ("mpstemplate://buffer?name=test-b") ;
109 ) ysized 3cm ;
110\stopMPcode
111\stopbuffer
112
113\typebuffer[testb][option=TEX]
114
115The filename is again specified as a \URI:
116
117\startlinecorrection[blank] \getbuffer \stoplinecorrection
118
119\startbuffer
120\startMPcode
121 picture p[] ;
122 loadfile("mpstemplate://buffer?name=test-a&method=metapost") ;
123 p[1] := currentpicture ; currentpicture := nullpicture ;
124 loadfile("mpstemplate://buffer?name=test-b&method=xml") ;
125 p[2] := currentpicture ; currentpicture := nullpicture ;
126 draw p[1] ysized 3cm ;
127 draw p[2] ysized 4cm shifted (4cm,0) ;
128\stopMPcode
129\stopbuffer
130
131\typebuffer[option=TEX]
132
133The combination comes out as:
134
135\startlinecorrection[blank] \getbuffer \stoplinecorrection
136
137Another approach is to load as image, which saves some typing:
138
139\startbuffer
140\startMPpage[offset=10pt]
141 draw image (loadfile("mpstemplate://buffer?name=test-a&method=metapost"))
142 xsized 2cm shifted ( 3cm,0) ;
143 draw image (loadfile("mpstemplate://buffer?name=test-b&method=xml"))
144 xsized 2cm shifted ( 6cm,0) ;
145 draw loadimage ("mpstemplate://buffer?name=test-a&method=metapost")
146 xsized 2cm shifted ( 9cm,0) ;
147 draw loadimage ("mpstemplate://buffer?name=test-b&method=xml")
148 xsized 2cm shifted (12cm,0) ;
149\stopMPpage
150\stopbuffer
151
152\typebuffer[option=TEX]
153
154The result is predictable:
155
156\startlinecorrection[blank] \getbuffer \stoplinecorrection
157
158Of course there is also a \type {cld} approach possible:
159
160
161
162
163\startbuffer
164\startluacode
165 context.startMPcode()
166 for i=0,100,20 do
167 context ( [[draw fullcircle scaled (%s * cm)
168 withcolor "darkmagenta" withpen pencircle scaled 1cm ;]], i)
169 end
170 for i=10,100,20 do
171 context ( [[draw fullcircle scaled (%s * cm)
172 withcolor "darkcyan" withpen pencircle scaled 1cm ;]], i)
173 end
174 context.stopMPcode()
175\stopluacode
176\stopbuffer
177
178\typebuffer[option=TEX]
179
180The commented commands will create a page. This is a convenient way to make
181graphics that can be used in other documents (programs). For practical reasons the
182example is scaled down:
183
184\startlinecorrection[blank] \scale[height=3cm]{\getbuffer} \stoplinecorrection
185
186All these methods are rather efficient because all happens in memory and without
187intermediate runs. It is this kind of features that the tight integration of \TEX,
188\METAPOST\ and \LUA\ make possible.
189
190\stopsection
191
192\stopchapter
193
194\stopcomponent
195 |