3

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

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

Question

Quadpaper (light grey)

Maybe a remark like "review your answer".

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}} \addtolength{\gnat}{0.5\parskip}

\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, -\topmargin-\headheight-\headsep+\gnat) 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.]

Louis
  • 1,471
  • 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

2 Answers2

7

Here's a "TikZ-free" solution

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

\def\leadAbox{\hbox to 1.6mm{\smash{\tiny.}\hfil}}
\def\start{\rlap{\hbox to 16.2cm{\leaders\leadAbox\hfill}}}
\def\leadBbox{\hbox to 8mm{\smash{\tiny.}\hfil\vrule height1mm width 0pt}}
\def\squares{\rlap{\hbox to 16.8cm{\leaders\leadBbox\hfill}}}
\def\leadCbox{\start\kern-.4mm
  \vbox to 8mm{\offinterlineskip\leaders\squares\vfill}
  \nointerlineskip\start}
\def\gridfill{\par\leaders\vbox{\leadCbox}\vfill}
\begin{document}

\lipsum[2]

\gridfill

\lipsum[2]

\end{document}
egreg
  • 1,121,712
3

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}
Caramdir
  • 89,023
  • 26
  • 255
  • 291
  • 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 09:22
  • It just needed a second run through LaTeX, then the header is taken into account. – Louis Jul 23 '11 at 19:19
  • 1
    @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
  • This is still very useful to me, I even changed it a bit to now sometimes give me lines of a certain count x for instance 8mm. – Louis Aug 15 '12 at 21:35