# I want to fill all the space between paragraphs on a page with a grid(graphic)

I took an example from another answer to a similar question (How to define a figure size so that is consume the rest of a page?).

On a page of an exam paper I want the following:

Question

So here is my MWE:

\documentclass[svgnames,a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[margin=25mm]{geometry}
\usepackage{lipsum}

\newcommand{\currentsidemargin}{%
\ifodd\value{page}%
\oddsidemargin%
\else%
\evensidemargin%
\fi%
}

\begin{document}
\setlength{\parindent}{0mm} \setlength{\parskip}{6pt}
\newsavebox\mybox
\savebox{\mybox}[\textwidth][c]{  % The text for below the grid
\parbox{\textwidth}{
\textbf{\Huge Some text}

\lipsum[3]
}
}

\newlength\gnat
\settoheight{\gnat}{\usebox{\mybox}}

\lipsum[2-5]% dummy text

\begin{tikzpicture}[overlay,remember picture]

% Helper nodes
\path (current page.north west) ++(\hoffset, -\voffset)
node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth,
minimum height=\paperheight] (pagearea) {};

\path (pagearea.north west) ++(\leftmargin+\currentsidemargin,
node[anchor=north west, shape=rectangle, inner sep=0,
minimum width=\textwidth, minimum height=\textheight] (textarea) {};

% Image
\path let \p0 = (0,0), \p1 = (textarea.south) in
node [inner sep=0pt,outer sep=0pt,anchor=north west] {%
\pgfmathsetmacro\imgheight{(\y0-\y1-0.5\gnat-mod(\y0-\y1-0.5\gnat,8mm))}%
\begin{tikzpicture}[remember picture,overlay]
\node[xshift=+0mm,yshift=1mm] at (\p0){
\begin{tikzpicture}%[overlay,remember picture]
\tikzset{dotted lines/.style={black, loosely dotted,  thick}}
\draw[style=dotted lines,step=.8cm] (0,0) grid (160mm,\imgheight pt+24mm);
\end{tikzpicture}
};
\end{tikzpicture}
%}
};
\end{tikzpicture}
\vfill
\vfill
\usebox{\mybox}
%\the\gnat   % To see the space
\end{document}


My question: Why do I have to add 24mm to the grid to get it fill the full available space?

[I will later add a test to check if the box is empty and then set \gnat to zero. Although any suggestions would be appreciated.]

-
Why are you adding \gnat to the the margin when calculating the position of tho \textarea node? –  Caramdir Jun 24 '11 at 10:50
For some reason the textarea node is not placed where I expect it to be placed (add a draw to its options to see the outline). –  Caramdir Jun 24 '11 at 10:59

There seems to be something wrong with the construction of your helper nodes. However, they can easily be avoided:

\documentclass[svgnames,a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{dotted lines/.style={black, loosely dotted,  thick}}
\usepackage[margin=25mm]{geometry}
\usepackage{lipsum}

\newcommand\drawGrid{%
\tikz[remember picture,overlay] \node[inner sep=0,anchor=base] (tl) {};%
\vfill\hfill%
\tikz[remember picture,overlay] \coordinate (br);%
\tikz[remember picture,overlay]
\draw[dotted lines,step=.8cm] let \p1=(tl.north), \p2=(br) in
[yshift={mod(\y1-\y2,8mm)},xshift=\x1]      % move (0,0) to the bottom left and nudge it a bit upward to get full squares.
(0,0) grid
({\x2-\x1-mod(\x2-\x1,8mm)+0.1pt},{\y1-mod(\y1-\y2,8mm)+0.1pt});    % the +0.1pt are just there to avoid any rounding problems.
}

\begin{document}
\setlength{\parindent}{0mm} \setlength{\parskip}{6pt}

\lipsum[2-5]% dummy text

\drawGrid

\textbf{\Huge Some text}

\lipsum[3]\clearpage

\lipsum[1-2]

\drawGrid

abc
\clearpage
\end{document}

-
Thanks its a short and elegant tikz/solution. The solution by egreg forced me to go back to TeXBook, a gap in my TeX reading I must still fill (Time is only FEW). –  Louis Jun 25 '11 at 10:00
@Louis: I should add that I would prefer egreg's solution because it looks better (the vertical and horizontal dots are aligned correctly). –  Caramdir Jun 25 '11 at 10:09
I used your solution again yesterday and it was inside exam.cls where there is a header on the page. If the grid is alone on the page I have to add a blank line to make sure the grid does not flow over the header. Maybe its because I did look at it only after a first run, I will check and come back. –  Louis Jul 21 '11 at 9:22
It just needed a second run through LaTeX, then the header is taken into account. –  Louis Jul 23 '11 at 19:19
@Louis: You must compile the file (at least) twice to get correct placement (because TikZ needs to know where everything is). –  Caramdir Jul 23 '11 at 19:20

Here's a "TikZ-free" solution

\documentclass[a4paper]{article}
\usepackage[textwidth=16cm]{geometry}
\usepackage{lipsum,array}

\def\leadBbox{\hbox to 8mm{\smash{\tiny.}\hfil\vrule height1mm width 0pt}}
\nointerlineskip\start}
\begin{document}

\lipsum[2]

\gridfill

\lipsum[2]

\end{document}

-
I tried it. It looks promising, but I also want horizontal even (8mm) lines. I will study what you did, maybe I can find a way. –  Louis Jun 23 '11 at 20:33
Perfect the TeX way! Tnx! I am wish I completely understood it. –  Louis Jun 24 '11 at 7:06
I cannot vote, but would have upped the usefulness. –  Louis Jun 24 '11 at 7:12
I also would to see what the tikz/pgf solution to my question would be. –  Louis Jun 24 '11 at 7:13
If one could generalise your solution, it is maybe something that could be added to exam.sty and other similar packages. –  Louis Jun 24 '11 at 7:18