On 2/19/2025 4:31 AM, Gavin via ntg-context wrote:
Hi Pavneet,

I recommend avoiding the mod function. Just draw the two grids (major and 
minor) each with their own spacing and pen.

Indeed. Don't rely on rounding etc because especially in loops (until) you can easily get one step more or less. This is why we often see these within 'eps' range tests.

My guess is that Pavneet runs mp stand alone in scaled mode while context by default uses double mode. For them to run the same one can do. One can try

mtxrun --script metapost foo.mp

instead.

I whole heatedly support converting TikZ to MetaPost!

- Gavin


\startreusableMPgraphic{graph}
  minor=2mm;     % Minor lines, each every 2mm
  major=8mm;     % Major lines, each every 4mm

  z1 = (0,0); % lower left corner
  % z2 = (2.75in,4.25in)-z1;  % upper right corner
  z2 = (2.75in,4.25in)-z1;% upper right corner

   path v, h;
   v := z1--(x1,y2); % vertical line
   h := z1--(x2,y1); % horizontal line

   % Use light gray
   drawoptions(withcolor .4white);

   % Draw the minor grid with the very thin pen
   pickup pencircle scaled 0.1;
   for i=minor step minor until (x2-x1):
      draw v xshifted i;
   endfor;
   for i=minor step minor until (y2-y1):
      draw h yshifted i;
   endfor;

   % Draw the major grid with the thicker pen
   pickup pencircle scaled 0.3;
   for i=major step major until (x2-x1):
      draw v xshifted i;
   endfor;
   for i=major step major until (y2-y1):
      draw h yshifted i;
   endfor;

\stopreusableMPgraphic



On Feb 18, 2025, at 3:28 PM, Pavneet Arora via ntg-context <ntg-context@ntg.nl> 
wrote:

I am in the process of converting a bunch of old TikZ notepaper
templates to native ConTeXt and MetaPost.  This is a part of a project
to generate notebooks with documentation data and then blank pages appended to
fill out full sheets, and then collated in a particular order for trimming and
binding.

As such I rely on Imposition.  I am having issues with the various
techniques of trying to duplicate a MP graphic across the pages using
\[start|stop]MPcode \[start|stop]useMPgraphic, etc.  But that is a
discussion for another time.

The method that I found to work was to use
\[start|stop]reusableMPgraphic along with overlays

I am still struggling with some MetaPost code that works fine in
standalone but not when included in a ConTeXt document.

With standalone MP, we get thicker lines on major increments as
expected.  When the same code is included in a ConTeXt document the
thicker lines are only drawn up a certain number and then stop ---
presumably the mod function returning a non-zero result when it really
shouldn't?

Here are the MWEs:

ConTeXt
% Paper size and layout {{{2
\definepapersize
|   [PocketPage]
|   [width=2.75in,height=4.25in]

\setuppapersize[PocketPage][letter,landscape]
% }}}2

% MetaPost template for graph paper for pocket sized notebook (2.75" W x
% 4.25in H). {{{2

\startreusableMPgraphic{graph}
  minor=2mm;|  |   % Minor lines, each every 2mm
  major=8mm;|  |   % Major lines, each every 4mm
   ┆   ┆   ┆   ┆ % major must be a multiple of minor

  z1 = (0,0);| % lower left corner
  % z2 = (2.75in,4.25in)-z1;|  % upper right corner
  z2 = (2.75in,4.25in)-z1;|% upper right corner

   % Use the very thin pen
   pickup pencircle scaled 0.1;

   % Draw the vertical lines
   for i=minor step minor until (y2-y1):
   ┆   if (i mod major)=0:
   ┆   ┆   pickup pencircle scaled 0.3;
   ┆   fi;
   ┆   draw (z1 shifted (0,i))--((x2,y1) shifted (0,i)) withcolor .4white;
   ┆   if (i mod major)=0:
   ┆   ┆   pickup pencircle scaled 0.1;
   ┆   fi;
   endfor;

   % Draw the horizonal lines
   for i=minor step minor until (x2-x1):
   ┆   if (i mod major)=0:
   ┆   ┆   pickup pencircle scaled 0.3;
   ┆   fi;
   ┆   draw ( z1--(x1,y2) ) shifted (i,0) withcolor .4white;
   ┆   if (i mod major)=0:
   ┆   ┆   pickup pencircle scaled 0.1;
   ┆   fi;
   endfor;

\stopreusableMPgraphic

\defineoverlay[OLgraph][\useMPgraphic{graph}]

\setupbackgrounds[page][background=OLgraph]
% }}}2

\starttext

hello.

\stoptext

<<<

Standalone MP
beginfig(1)

margin=0mm;             % Margin of the graph paper
minor=2mm;              % Minor lines, each every 1mm
major=8mm;              % Major lines, each every 10mm
                       % major must be a multiple of minor

z1 = (margin,margin);   % lower left corner
z2 = (2.75in,4.25in)-z1;        % upper right corner

% % Use the thick pen first
% pickup pencircle scaled 0.5;

% % Draw the frame
% draw z1--(x1,y2)--z2--(x2,y1)--cycle withcolor .5white;

% Use the very thin pen
pickup pencircle scaled 0.1;

% Draw the vertical lines
for i=minor step minor until (y2-y1-minor):
       if (i mod major)=0:
               pickup pencircle scaled 0.3;
       fi;
       draw (z1 shifted (0,i))--((x2,y1) shifted (0,i)) withcolor .4white;
       if (i mod major)=0:
               pickup pencircle scaled 0.1;
       fi;
endfor;

% Draw the horizonal lines
for i=minor step minor until (x2-x1-minor):
       if (i mod major)=0:
               pickup pencircle scaled 0.3;
       fi;
       draw ( z1--(x1,y2) ) shifted (i,0) withcolor .4white;
       if (i mod major)=0:
               pickup pencircle scaled 0.1;
       fi;
endfor;

endfig;
end;

<<<

advTHANKance

--
Pavneet Arora           m: +1 647-406-6843
Waroc Informatik        e: pavneet_ar...@waroc.com
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________


--

-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
       tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to