I try to reuse this code from fpast (Generating Random arrows in tikz) in context but I found a error message associated with the freelabel() function. If I try to add the labels with label() function or by "draw textext()" method I found the same error. I think that this error is related to context side.

! Declared variable conflicts with previous vardef.

               ; <*> ...idate, random_vector[], bb_candidate, bb[];
                                               vardef bb(expr p) = save ...

! Inconsistent equation (off by -20). ; <*> ...enddef; pickup pencircle scaled 2bp; i = 1; forever: exitunless (i <=...


numeric u, nb_vectors, wd;
u := 1cm; % unit length
nb_vectors := 20; wd = .5; 
boolean intersect; pair random_origin; color random_color; path candidate, random_vector[], bb_candidate, bb[];
vardef bb(expr p) =
  save orig, extr; pair orig, extr; orig = point 0 of p; extr = point 1 of p;
  pair v,w; v = wd*unitvector(extr-orig)*u; w = v rotated 90;
  (orig - v + w) -- (extr + v + w) -- (extr + v - w) -- (orig - v - w) -- cycle

  pickup pencircle scaled 2bp;
  i = 1; 
    exitunless (i <=  nb_vectors);
    intersect := false;
    random_length := u*(2+uniformdeviate(1));
    random_angle := uniformdeviate(359);
    random_origin := u*(uniformdeviate(wd*nb_vectors), uniformdeviate(wd*nb_vectors));
    random_color := (uniformdeviate(1), uniformdeviate(1), uniformdeviate(1));
    candidate := random_origin -- (random_origin + random_length*dir(random_angle));
    j := 1;
      exitif (j = i) or (intersect = true);
      if bb(random_vector[j]) intersectiontimes bb(candidate) <>  (-1, -1):
        intersect := true;
      j := j+1;
    if intersect = false: 
      random_vector[i] = candidate;
      drawarrow candidate withcolor random_color;
      freelabel(btex $v$ etex, point .5 of candidate, point .5 of candidate + dir (random_angle + 90));
      i := i+1;

  • Compiling just the Metapost code loading metafun runs without errors. – egreg Jan 31 '15 at 22:44
  • You are right. I think that error is related with ConTeXt definitions. – Jorge Jan 31 '15 at 22:57
  • Please don't post the same question simultaneously at the context mailing list and on this site. Pick one, and if you don't get a response within a reasonable time, only then post on the other. – Aditya Feb 1 '15 at 17:51
  • I'm sorry for that. I have not posted simultaneously. In StackExchange I posted yesterday, in the the mailing list I posted this morning. Usually I go first to the mailing list for help, but this piece of code is not mine and for that I decided to ask for help in StackExchange (where the code was first introduced by fpast). However, despite fpast efforts, to whom I am very grateful, the error persisted. Hence I have decided to ask to mailing list. – Jorge Feb 1 '15 at 21:25

Removing the path variable bb[] (which is not used in the following code anyway, certainly a relic of my first attempts on this subject) makes it work with ConTeXt-MKIV. Maybe it was conflict detected with the bb() macro defined just after. But in this case, I've no idea about why it did work with direct MetaFun and not with ConTeXt.

Replace thus

 path candidate, random_vector[], bb_candidate, bb[];


path candidate, random_vector[] ;

(the bb_candidate path variable is of no use either).


enter image description here

EDIT I've just checked: while it works well now with ConTeXt-MKIV, it still doesn't work with ConTeXt-MKII. I'm at a loss for guessing why, since I'm not a regular ConTeXt user.

EDIT (bis) It seems that the actual reason it works now (at least with ConTeXt-MKIV) is that I've replaced the equation i = 1 by an assignment i := 1. See the comments below. I would suppose that the i variable is already defined somewhere else in ConTeXt-Metafun… But it still does not work with ConTeXt-MKII anyway. (No more error in that case, but the picture does not appear.)

| improve this answer | |
  • I have no luck . Still does not work. ConTeXt ver: 2015.01.23 22:11 MKIV beta fmt: 2015.1.25 int: english/english > system > 'cont-new.mkiv' loaded – Jorge Feb 1 '15 at 11:13
  • I just saw it did not work either with another installation (Mac OS X 10.8 instead of Mac OS X 10.10)… Seems that I've found the actual culprit: the i = 1 equation. Replace it by i := 1 or numeric i ; i = 1, and it works fine again with ConTeXt-MKIV, at least in my TeX installation. But it still doesn't work with ConTeXt-MKII, and I've still no idea about why this equation had to be replaced by an assignment, since the equation worked well with direct Metafun… Any ConTeXt wizard here? – Franck Pastor Feb 1 '15 at 13:05
  • I found the same solution by Hans Hagen: i := 1 ; % never assume variable is not set global (such is mp) – Jorge Feb 1 '15 at 15:28
  • Thanks again.There is a guy who successfully compiled this code mkii. He said:Interestingly it seems to work in mkii, probably because mkii calls external metafun: context --pdftex --passon=--shell-escape test.tex – Jorge Feb 1 '15 at 15:35
  • @Jorge - the variable scope design is quite deliberate! And generally very effective. If you want local variables put them inside a begingroup, endgroup pair, and use save to protect the outside value. – Thruston Feb 1 '15 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.