Creating a grid which fills available text area on a page in ConTeXt?

I have some pages containing only a small paragraph at the top and a large empty area where readers can add notes or draw, e.g.:

 _______________
|               |
| This is some  |
| text in a pa- |
| ragraph.      |
|               |
|               |
|               |
|               |
|               |
|               |
|               |
|_______________|


A page break is added, so that the next paragraph appears on the next page. I would like to fill the empty text area with a grid made of light lines, e.g.:

 _______________
|               |
| This is some  |
| text in a pa- |
| ragraph.      |
|  ___________  |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
| |_|_|_|_|_|_| |
|_______________|


How can I create such a grid which fills the width of the text area and remaining height available on the page?

-
You can do this easily with TikZ which should work with ConTeXt and LaTeX. Search this page for [tikz-pgf] and "rest of page" etc. –  Martin Scharrer May 27 '12 at 14:17

As described in How to define a figure size so that it consumes the rest of a page?, first define a measure to measure the remaining space on the page

\definemeasure[page][\dimexpr\pagegoal-\pagetotal-\lineheight\relax]


Then use a frame with its height equal to this measure

\framed[height=\measure{page}, width=\textwidth]{}


Now, to add a grid, you can create a Metapost background using

\startuseMPgraphic{page:grid}
....
\stopuseMPgraphic


and use it as a background for the frame

\defineoverlay[page:grid][\useMPgraphic{page:grid}]
\framed[background=page:grid]{}


Combing all this, we have

\definemeasure[topoffset][\bigskipamount] % We add this space before the answer
\definemeasure[page][\dimexpr\pagegoal-\pagetotal-\measure{topoffset}-5pt\relax]
% The 5pt is to avoid roundoff effects that can force the frame to the next
% page

% Add an option for grid size
\setupframed
[gridsize=10pt]

\startuseMPgraphic{page:grid}
begingroup;
newnumeric grid_size;
grid_size = \frameddimension{gridsize};

newnumeric x_count, y_count;

x_count := floor(OverlayWidth/grid_size);
y_count := floor(OverlayHeight/grid_size);

% The requested width and height may not a multiple of grid_size
% So, we center the grid horizontally and top align it vertically

newnumeric y_offset; y_offset := OverlayHeight - y_count*grid_size;
newnumeric x_offset; x_offset := (OverlayWidth  - x_count*grid_size)/2 ;

newpath x_axis, y_axis;

x_axis := (x_offset, 0) -- (x_offset + x_count * grid_size, 0);
y_axis := (0, y_offset) -- (0, OverlayHeight);

pickup pensquare scaled OverlayLineWidth;

for i = 0 upto x_count :
draw y_axis shifted (x_offset + grid_size*i,0) ;
endfor ;

for i = 0 upto y_count :
draw x_axis shifted (0,y_offset + grid_size*i) ;
endfor ;

setbounds currentpicture to OverlayBox;

endgroup;
\stopuseMPgraphic

\defineoverlay[page:grid][\useMPgraphic{page:grid}]

% Define a new framed for answers
\defineframed
[width=\textwidth, height=\measure{page},
frame=off,        background={page:grid}]

{\blank[\the\dimexpr\measure{topoffset}]%

\setupwhitespace[big]
\showframe % to visualize the result
\starttext

\input knuth

\stoptext


which gives

You can adjust the size of the grid using the gridsize parameter. If you want, you can also define a gridcolor parameter, and draw the rules using \MPcolor{\framedparameter{gridcolor}}.

-
It's a lot of code for a grid, doesn't ConTeXt support PGF? –  Yiannis Lazarides May 27 '12 at 20:44
Most of the code is to get the positioning right. (I don't know how pgf handles grids when the size of box is not a multiple of grid distance). The code can be shortened, and I think that Metafun has a grid macro, but it was just easier to write my own code. –  Aditya May 27 '12 at 21:04
ConTeXt does handle pgf, but there is not inbuilt way to define overlays using pgf. One would need to write code for passing the overlay box size to pgf...with metapost, ConTeXt does that automatically. –  Aditya May 27 '12 at 21:05
Have a look at tex.stackexchange.com/questions/56498/… for the grid positioning. –  Yiannis Lazarides May 27 '12 at 21:09
PGF comes with a grid macro, so the solution is shorter. But the ConTeXt solution is doing much more than just defining a grid. For example you can use \externalfigure[cow][gridsize=10pt, background=page:grid] to get a grid background for an image, and \setupbackgrounds[page][background=page:grid] to get a grid background for the entire page. There is no such close coupling between ConTeXt and PGF, and that is why I prefer Metapost for drawing backgrounds when using ConTeXt. –  Aditya May 27 '12 at 21:40