Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have just began using MetaPost and I would liked to be able to put a grid on my images, so I could easily read coordinates from it. I would liked to write a macro, that I could import while creating new figures and draw a grid with a single command.

My grid macro is the following:

def drawgrid(expr width, height, nx, ny) = 
begingroup
ny * u = width;
nx * v = height;
lu = 2 * u;
lv = 2 * v;
my = floor(width / lu);
mx = floor(height / lv);

for y = 0 upto nx:
    draw (0,y*v)--(width,y*v);
endfor
for y = 0 upto mx:
    draw (0,y*lv)--(width,y*lv) withpen pencircle scaled 2;
    label(decimal(y*lv/v), (-0.5u, y*lv));
endfor

for x = 0 upto my:
    draw (x*lu,0)--(x*lu,height) withpen pencircle scaled 2;
    label(decimal(x*lu/u), (x*lu, -0.5v));
endfor
for x = 0 upto ny:
    draw (x*u,0)--(x*u,height);
endfor
endgroup
enddef;
end

It works fine, when called from the same file (grid.mp), but fails to draw anything if I call it from my another figure. My other figures code is:

input grid

beginfig(1); textwidth := 345.0;
width := 0.5textwidth;
radius := 0.25width;

fill fullcircle scaled radius withcolor blob;
draw fullcircle scaled radius withcolor blobBorder;
clip currentpicture to (-0.5,-0.5)--(-0.5,0.5radius+0.5)--(0.5radius+0.5,0.5radius+0.5)--(0.5radius+0.5,-0.5)--cycle;
draw (-0.5, 0.5radius)--(-0.5, -0.5)--(0.5radius, -0.5) withcolor blobBorder;
draw (0, 0)--(0.25radius, 0.45radius);
drawgrid(width,width,2,2);
endfig
end

It does not draw anything (even do not produce an .1 file). What am I doing wrong?

share|improve this question
    
Additionnally, if you want to make some variables local, it is not enough to include them in begingroupendgroup. You also have to use the save instruction. For example, save M; pair M; if you want to make a M pair variable local into a group. –  fpast Mar 29 at 13:38
    
Colors blob and blobBorder are not defined either… –  fpast Mar 29 at 14:01
    
… and you shouldn't include and end command at the end of your grid.mp. It makes MetaPost ignore whatever is written after it, included in the main file. It's probably the main cause of the problems. –  fpast Mar 29 at 14:02
    
@WebMonster: Did fpast's answer help you? If so, please consider pressing the “mark as accepted answer” button. –  morbusg Apr 7 at 6:16

1 Answer 1

up vote 3 down vote accepted

Here is a macro file following my comments:

def drawgrid(expr width, height, nx, ny) = 
begingroup
  save u, v, mx, my, lv, lu;
  ny * u = width;
  nx * v = height;
  lu = 2 * u;
  lv = 2 * v;
  my = floor(width / lu);
  mx = floor(height / lv);

  for y = 0 upto nx:
    draw (0,y*v)--(width,y*v);
  endfor
  for y = 0 upto mx:
    draw (0,y*lv)--(width,y*lv) withpen pencircle scaled 2;
    label(decimal(y*lv/v), (-0.5u, y*lv));
  endfor

  for x = 0 upto my:
    draw (x*lu,0)--(x*lu,height) withpen pencircle scaled 2;
    label(decimal(x*lu/u), (x*lu, -0.5v));
  endfor
  for x = 0 upto ny:
    draw (x*u,0)--(x*u,height);
  endfor
endgroup
enddef;

And here is a main file that produces something (not sure of if the drawing is what was intended):

input grid;

beginfig(1); 
textwidth := 345.0;
width := 0.5textwidth;
radius := 0.25width;
color blob, blobBorder; blob=red; blobBorder = blue;
draw fullcircle scaled radius withcolor blob;
draw fullcircle scaled radius withcolor blobBorder;
clip currentpicture to (-0.5,-0.5)--(-0.5,0.5radius+0.5)--(0.5radius+0.5,0.5radius+0.5)--(0.5radius+0.5,-0.5)--cycle;
draw (-0.5, 0.5radius)--(-0.5, -0.5)--(0.5radius, -0.5) withcolor blobBorder;
draw (0, 0)--(0.25radius, 0.45radius);
drawgrid(width,width,2,2);
endfig;
end;

The result:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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