simplifying repeated macro calls in metafun
![](https://secure.gravatar.com/avatar/ecffc5c1eb6692198d091ee98e921822.jpg?s=120&d=mm&r=g)
Hi, I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel? Thank you for reading. Best regards, Dalyoung \startbuffer[UDBasic] pair A,B,C,D; pair E, F, G, H; path p,q; numeric ang,maxNum; u:=1in; A=(.95*TextWidth,u); B=(0,u); C=(0,0); D=(.95*TextWidth,0); p := A--B--C--D; q := (TextWidth,1.5u)--(0,1.5u)--(0,-.5u)--(TextWidth,-.5u)--cycle; z0=(.1u,0); vardef nextPt (expr M,N) = if ypart(N) = 0: H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[A,B]; else: H := whatever[N, N + dir(.5*angle(M-N))*u] = whatever[C,D]; fi; H enddef; def grayUD(expr i) = draw G -- if ypart(B)=ypart(G): B -- C else: C -- B fi --z[i]; filldraw z[i]+2*(G-z[i])*u -- z[i] -- z[i+1] -- z[i+1]+(G-z[i])*u -- cycle withcolor .75white; draw G -- z[i+1] dashed evenly; draw z[0] for k=0 upto i: --z[k] endfor dashed evenly; clip currentpicture to q; enddef; def linesUD(expr i) = draw p; draw z0 for k=0 upto i+1: --z[k] endfor dashed evenly; setbounds currentpicture to boundingbox q; enddef; \stopbuffer \startbuffer[UDpts] ang := 40; z[1] = whatever[z[0], z[0]+dir(ang)*u] = whatever[A,B]; G := whatever[z[0], z[0]+dir(2*ang)*u] = whatever[A,B]; for k = 2 upto 20: z[k] = nextPt(z[k-2],z[k-1]); maxNum := k; exitif xpart(z[k]) > TextWidth; endfor; \stopbuffer \startbuffer[UD0] draw p; setbounds currentpicture to boundingbox q; \stopbuffer \startbuffer[UD1] grayUD(0); \stopbuffer \startbuffer[UD11] linesUD(0); \stopbuffer \startbuffer[UD2] G := z0; grayUD(1) \stopbuffer \startbuffer[UD21] linesUD(1); \stopbuffer \startbuffer[UD3] G := z1; grayUD(2); \stopbuffer \startbuffer[UD31] linesUD(2); \stopbuffer \startbuffer[UD4] G := z2; grayUD(3); \stopbuffer \startbuffer[UD41] linesUD(3); \stopbuffer \startbuffer[UD5] G := z3; grayUD(4); \stopbuffer \startbuffer[UD51] linesUD(4); \stopbuffer \startbuffer[UDFinal] draw p; draw z0--z1--z2--z3--z4--z5--z6--z7--z8 dashed evenly; setbounds currentpicture to boundingbox q; \stopbuffer \starttext \processMPbuffer[UDBasic,UDpts,UD0] \processMPbuffer[UDBasic,UDpts,UD1] \processMPbuffer[UDBasic,UDpts,UD11] \processMPbuffer[UDBasic,UDpts,UD2] \processMPbuffer[UDBasic,UDpts,UD21] \processMPbuffer[UDBasic,UDpts,UD3] \processMPbuffer[UDBasic,UDpts,UD31] \processMPbuffer[UDBasic,UDpts,UD4] \processMPbuffer[UDBasic,UDpts,UD41] \processMPbuffer[UDBasic,UDpts,UD5] \processMPbuffer[UDBasic,UDpts,UD51] \processMPbuffer[UDBasic,UDpts,UDFinal] \stoptext
![](https://secure.gravatar.com/avatar/c5ae4f9aa0d3a9bd5376e92b322c3e50.jpg?s=120&d=mm&r=g)
On Mon, 21 Jun 2021, Jeong Dal wrote:
Hi,
I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic. Aditya
![](https://secure.gravatar.com/avatar/5faea8b4fa0adb143383811dc94f0d22.jpg?s=120&d=mm&r=g)
Hi Aditya, Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question. I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name… Best regards: Otared
On 21 Jun 2021, at 06:26, Aditya Mahajan
wrote: On Mon, 21 Jun 2021, Jeong Dal wrote:
Hi,
I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
Aditya
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
![](https://secure.gravatar.com/avatar/3d131c6a351a6b0092c19b389bd99cf0.jpg?s=120&d=mm&r=g)
Hi,
As is written in the MetaPost manual: Whatever generates a new
anonymous variable each time it is used. It is very useful in cases
where you are not interested in the value of a variable, but want to
use it to (let MP) calculate something. Example
you have three points z0, z1, z2 forming a triangle, and you want to
find where the altitude from z0 is intersecting the line between z1
and z2. You can find that intersection z3 by writing
z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
The first equality says only that z3 should be on the line between z1
and z2, the second one says that z3 should be on the line through z0,
perpendicular to the line between z1 and z2. Since MetaPost solves
linear equations, this defines z3, and we need not to think more about
the actual values of the "whatevers" (note that they are not in
general equal in this example!).
Hope it helps!
/Mikael
On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian
Hi Aditya,
Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question. I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
Best regards: Otared
On 21 Jun 2021, at 06:26, Aditya Mahajan
wrote: On Mon, 21 Jun 2021, Jeong Dal wrote:
Hi,
I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
Aditya
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
![](https://secure.gravatar.com/avatar/5faea8b4fa0adb143383811dc94f0d22.jpg?s=120&d=mm&r=g)
Hi Mikael, Thank you so much for the explanation: I am ashamed for not having read this in the MetaPost manual. Just to make sure whether I am beginning to understand, in your example code
z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
should the second « whatever » be z0 + whatever*dir(angle(z2 - z1) + 90)) ; or am I proving that I have not yet understood this wonderful command ? Thanks again and best regards: Otared
On 21 Jun 2021, at 14:21, Mikael Sundqvist
wrote: Hi,
As is written in the MetaPost manual: Whatever generates a new anonymous variable each time it is used. It is very useful in cases where you are not interested in the value of a variable, but want to use it to (let MP) calculate something. Example
you have three points z0, z1, z2 forming a triangle, and you want to find where the altitude from z0 is intersecting the line between z1 and z2. You can find that intersection z3 by writing
z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
The first equality says only that z3 should be on the line between z1 and z2, the second one says that z3 should be on the line through z0, perpendicular to the line between z1 and z2. Since MetaPost solves linear equations, this defines z3, and we need not to think more about the actual values of the "whatevers" (note that they are not in general equal in this example!).
Hope it helps!
/Mikael
On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian
wrote: Hi Aditya,
Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question. I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
Best regards: Otared
On 21 Jun 2021, at 06:26, Aditya Mahajan
wrote: On Mon, 21 Jun 2021, Jeong Dal wrote:
Hi,
I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
Aditya
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
![](https://secure.gravatar.com/avatar/3d131c6a351a6b0092c19b389bd99cf0.jpg?s=120&d=mm&r=g)
Hi,
yes, sure, I should have copied my code from the geometry document
where I do this kind of constructions over and over...
/Mikael
On Mon, Jun 21, 2021 at 4:31 PM Otared Kavian
Hi Mikael,
Thank you so much for the explanation: I am ashamed for not having read this in the MetaPost manual.
Just to make sure whether I am beginning to understand, in your example code
z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
should the second « whatever » be
z0 + whatever*dir(angle(z2 - z1) + 90)) ;
or am I proving that I have not yet understood this wonderful command ?
Thanks again and best regards: Otared
On 21 Jun 2021, at 14:21, Mikael Sundqvist
wrote: Hi,
As is written in the MetaPost manual: Whatever generates a new anonymous variable each time it is used. It is very useful in cases where you are not interested in the value of a variable, but want to use it to (let MP) calculate something. Example
you have three points z0, z1, z2 forming a triangle, and you want to find where the altitude from z0 is intersecting the line between z1 and z2. You can find that intersection z3 by writing
z3 = whatever[z1,z2] = z0+whatever*dir(angle(z1-z0)+90));
The first equality says only that z3 should be on the line between z1 and z2, the second one says that z3 should be on the line through z0, perpendicular to the line between z1 and z2. Since MetaPost solves linear equations, this defines z3, and we need not to think more about the actual values of the "whatevers" (note that they are not in general equal in this example!).
Hope it helps!
/Mikael
On Mon, Jun 21, 2021 at 2:07 PM Otared Kavian
wrote: Hi Aditya,
Thanks for the detailed file you sent to answer Dalyoung’s question and file. And going through both files I have a question. I see that both of you use a function « whatever » (but is it a function ?): what does this do and where can it be used (actually I have seen Hans using this expression, but I had always thought this is just a name…
Best regards: Otared
On 21 Jun 2021, at 06:26, Aditya Mahajan
wrote: On Mon, 21 Jun 2021, Jeong Dal wrote:
Hi,
I defined a macro definition in metafun, and draw figures using \startbuffer[] … \stopbuffer and \processMPbuffer[]. As you see in the attached code, the same macro definition is used repeatedly by changing the numeric argument. Is there a better way to simplify the code, for example, using \dorecurse and recurselevel?
See attached. The main idea is to store the repetitive bits in \startuseMPgraphic{...} and then reuse them using \includeMPgraphic.
Aditya
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___________________________________________________________________________________
participants (4)
-
Aditya Mahajan
-
Jeong Dal
-
Mikael Sundqvist
-
Otared Kavian