MetaFun - Pragma ADE
MetaFun - Pragma ADE MetaFun - Pragma ADE
108 There are more cases where the result may surprise you. Take the following code: drawarrow ((0,0)--(10,0)) withpen pencircle scaled 2pt withcolor red randomized (.4,.9) ; currentpicture := currentpicture scaled 8 ; The arrow is made up out of two pieces and each piece gets a different shade of red. This is because the attributes are collected and applied to each of the components that make up the arrow. Because for each component the attribute code is expanded again, we get two random colors. One way around this is to apply the color afterwards. draw image (drawarrow ((0,0)--(10,0)) withpen pencircle scaled 2pt) scaled 8 withcolor red randomized (.4,.9) ; Here the image macro creates a picture and as you can see, this provides a way to draw within a draw operation. Once you see the benefits of image, you will use it frequently. Another handy (at first sight strange) macro is hide. You can use this in situations where you don't want code to interfere. def mydraw text t = boolean error ; error := false ; def withpencil expr p = hide (error := true) enddef ; draw t ; if error : message "pencils are not supported here" fi ; enddef ; mydraw fullcircle scaled 10cm withpencil sharp ; Here, setting the boolean normally interferes with the draw operation, but by hiding the assignment, this code becomes valid. This code will bring the message to your terminal and log file. Once you start using expressions you have a good chance of encountering messages with regards to redundant expressions. The following code is for instance a recipe for problems: z1 = (1,0) ; z1 = (2,0) ; Changing the = into := helps, but this may not be what you want. A few more details Pitfalls
Because the z--variables are used frequently, they are reset each figure. You can also reset them yourself, using the clearxy macro. The METAFUN version clears all z--variables, unless you explictly specify what variables to reset. 8 If you want to play with this macro, see what happens when you run the following code: show x0 ; z0 = (10,10) ; show x0 ; x0 := whatever ; y0 := whatever ; show x0 ; z0 = (20,20) ; show x0 ; clearxy 0 ; show x0 ; z0 = (30,30) ; So, the following calls are all legal: clearxy ; clearxy 1 ; clearxy 1, 8, 10 ; Keep in mind that for each figure a full clear is done anyway. You should not confuse this command with clearit, which clears currentpicture. 2.12 TEX versus METAPOST 8 9 If you are defining your own TEX and METAPOST macros, you will notice that there are a couple of essential differences between the two macro languages. In TEX the following code is invalid. 9 \def\fancyplied#1% {\ifnum#1=0 \message{zero argument}% \fi \count0=#1 \multiply \count0 by \count0 \count2=#1 \multiply \count2 by 2 \count4=#1 \divide \count4 by 2 \advance \count0 by \count2 \advance \count0 by \count4 \count4 } \hskip \fancyplied{3} pt This is because TEX is very strict in what tokens it expects next. In METAPOST however, you can use vardef'd macros to hide nasty intermediate calculations. vardef fancyplied expr x = if x=0 : message "x is zero" ; (x*x+2x+x/2) enddef ; a := a shifted (fancyplied 3pt,0) ; Hiding intermediate calculations and manipulations is a very strong point of METAPOST. Another important difference between both languages is the way grouping is implemented. Because TEX is dealing with a flow of information, strong grouping is a must and therefore part of This version resulted from a discussion on the METAFONT discussion list and is due to Bogusław Jackowski. In ε-TEX the calculation can be done in less lines using a \numexpr . TEX versus METAPOST A few more details 109
- Page 61 and 62: path p ; p := (0cm,0cm) -- (4cm,1cm
- Page 63 and 64: Here, the image macro creates an (a
- Page 65 and 66: So, in addition to on to specify a
- Page 67 and 68: Watch how we can pass a point (poin
- Page 69 and 70: 2 A few more details In this chapte
- Page 71 and 72: The second line does just as it say
- Page 73 and 74: fill shape ; draw shape ; filldraw
- Page 75 and 76: 2.3 Units 6 As you can see here, as
- Page 77 and 78: 2.4 Scaling and shifting When we dr
- Page 79 and 80: Especially when a path results from
- Page 81 and 82: points first order curve second ord
- Page 83 and 84: first iteration second iteration th
- Page 85 and 86: and get: We don't even need that mu
- Page 87 and 88: Figure 2.3 Circles with minimized i
- Page 89 and 90: The tension specifier can be used t
- Page 91 and 92: The asymetrical tensions are less p
- Page 93 and 94: z0 .. z1 .. z2 z0 {curl 1} .. z1 ..
- Page 95 and 96: 7 In literature concerning POSTSCRI
- Page 97 and 98: currentpicture := currentpicture sl
- Page 99 and 100: We can achieve this by defining poi
- Page 101 and 102: We're still not there. Like in a pr
- Page 103 and 104: Due to the thicker line width used
- Page 105 and 106: left up down right The previous def
- Page 107 and 108: Here the & glues strings together,
- Page 109 and 110: picture p ; p := "MetaFun" normalin
- Page 111: pickup pencircle scaled 2pt ; draw
- Page 115 and 116: 3 Embedded graphics In addition to
- Page 117 and 118: \startMPcode fill fullcircle scaled
- Page 119 and 120: \startuniqueMPgraphic{right or wron
- Page 121 and 122: {\externalfigure[mprun:extrafun::my
- Page 123 and 124: \startlinecorrection[blank] \proces
- Page 125 and 126: In order to prevent problems, we ad
- Page 127 and 128: There are a few more low level swit
- Page 129 and 130: When we convert color to gray, we u
- Page 131 and 132: 4 Enhancing the layout One of the m
- Page 133 and 134: 4.2 Overlay variables [offset=3pt,f
- Page 135 and 136: This background demonstrates that a
- Page 137 and 138: As you may know, TEX's ambassador i
- Page 139 and 140: This works as expected: \RotatedTex
- Page 141 and 142: We will now apply this knowledge of
- Page 143 and 144: p := p shifted (2o,OverlayHeight-yp
- Page 145 and 146: We demonstrated that when defining
- Page 147 and 148: 5 Positional graphics In this chapt
- Page 149 and 150: 5.2 Anchors and layers In a previou
- Page 151 and 152: pab := pab cutafter (pab intersecti
- Page 153 and 154: fill p withcolor \MPcolor{lightgray
- Page 155 and 156: The previous example also demonstra
- Page 157 and 158: \setMPlayer [test] [somepos-1] [loc
- Page 159 and 160: 6 Page backgrounds Especially in in
- Page 161 and 162: As soon as you want to make an elec
108<br />
There are more cases where the result may surprise you. Take the following code:<br />
drawarrow ((0,0)--(10,0))<br />
withpen pencircle scaled 2pt<br />
withcolor red randomized (.4,.9) ;<br />
currentpicture := currentpicture scaled 8 ;<br />
The arrow is made up out of two pieces and each piece gets a different shade of red. This is because<br />
the attributes are collected and applied to each of the components that make up the arrow. Because<br />
for each component the attribute code is expanded again, we get two random colors. One way<br />
around this is to apply the color afterwards.<br />
draw<br />
image (drawarrow ((0,0)--(10,0)) withpen pencircle scaled 2pt)<br />
scaled 8 withcolor red randomized (.4,.9) ;<br />
Here the image macro creates a picture and as you can see, this provides a way to draw within a<br />
draw operation.<br />
Once you see the benefits of image, you will use it frequently. Another handy (at first sight strange)<br />
macro is hide. You can use this in situations where you don't want code to interfere.<br />
def mydraw text t =<br />
boolean error ; error := false ;<br />
def withpencil expr p = hide (error := true) enddef ;<br />
draw t ;<br />
if error : message "pencils are not supported here" fi ;<br />
enddef ;<br />
mydraw fullcircle scaled 10cm withpencil sharp ;<br />
Here, setting the boolean normally interferes with the draw operation, but by hiding the assignment,<br />
this code becomes valid. This code will bring the message to your terminal and log file.<br />
Once you start using expressions you have a good chance of encountering messages with regards<br />
to redundant expressions. The following code is for instance a recipe for problems:<br />
z1 = (1,0) ; z1 = (2,0) ;<br />
Changing the = into := helps, but this may not be what you want.<br />
A few more details Pitfalls