# autofill with square-pattern (leaders vs. tikz)

I want to autofill the rest of the line with a gray square-pattern in order give students an area to submit their mathimatical thoughts.

My solution is so far:

\newcommand\mySE{\vrule height 1pt width 5mm depth 0pt
\vrule height 5mm width 1pt depth 0pt}
\newcommand\myNW{\vrule height 5mm width 1pt depth 0pt
\raise5mm\hbox{\vrule height 0pt width 5mm depth 1pt}}

\newcommand\mysquarefill[1]{{\color{black!50}%


The result of

B\mysquarefill{2.5cm}E


results in

, which is quite nice exept the missing lines.

If I write \hbox{\framebox(5,5){}} instead of \hbox{\mySE} the middlelines a drawn twice and thus darkgray.

Does anyone has an idea how to use \leaders to draw the missing vertical und horizontal line in \mysquarefillcorrectly?

Update and refraised:

In the meantime I learned from "How to align consecutive \leaders properly?" that \[x|c]leaders aren't the optimal solution for my problem, because they somehow align to a global grid. That's why I couldn't find a raggedleft- / raggedright-solution.

Because I would like to measure the remaining space and to calcute how many complete squares to use, I think this is a better way: Is there a way to measure the remaining space of a line of text?

My solution so far:

    \documentclass{article}

\usepackage{tikzpagenodes}
\usepackage{etoolbox}

\usetikzlibrary{calc}

\newlength{\whatsleft}
\newcommand{\measureremainder}[2]{%
\begin{tikzpicture}[overlay,remember picture]
% Measure distance to right text border
\path let \p0 = (0,0), \p1 = (current page text area.east) in
[/utils/exec={\pgfmathsetlength#1{floor((\x1-\x0)/#2)*#2}\global#1=#1}];
\end{tikzpicture}%
}

\newcommand{\mysquarefill}[4][r]{%
\measureremainder{\whatsleft}{#2}%
\ifstrequal{#1}{l}{}{\hfill}%
\lower#4\hbox{\begin{tikzpicture}%
\draw[step=5mm,color=gray](0,0) grid (\whatsleft,#3);
\end{tikzpicture}}%
\ifstrequal{#1}{c}{\hfill\mbox{}}{}%
}

\begin{document}

\begin{itemize}
\item $\frac{8}{15} + \frac{7}{12}+ \frac{5}{12} +2=$\mysquarefill[l]{5mm}{10mm}{4mm}
\item $\frac{3}{4}$ von $\frac{2}{5}=$\mysquarefill[r]{5mm}{15mm}{7mm}
\item $\frac{15}{28}\cdot\frac{14}{30}=$\mysquarefill[r]{5mm}{10mm}{4mm}
\end{itemize}

\end{document}


My new question is: How could I directly use (current page text area.east) ? If I all do it in one macro, then sometimes the calculations aren't correctly updated, thus producing to small boxes.

It should be possible to use something like

\draw[step=5mm,color=gray](0,0) grid (floor(x((current page text area.east))/#2)*#2,#3);


And does exist an easier solution for the raggedleft/raggedright/center-mechanism?

• Welcome to TeX SE. Could you expand your code to a complete minimal example starting with a \doucmentclass{...} ending with a \end{document}? Jan 24, 2020 at 18:16

I propose the following:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand*{\mysquarefill}[3]{%
\begingroup
\color{black!50}\mbox{}%
\vrule height #2 width #3 depth \z@
\kern -#3\relax
\vbox to #2{\hrule width \dimexpr #2+#3 height #3\vfil
\hrule width \dimexpr #2+#3 height #3%
\kern -#3}%
\kern -#3\relax
\vrule height #2 width #3 depth \z@
\hss}%
\vfil
\kern #3\relax
}\hfill\kern #3\relax
\endgroup
}
\makeatother

\newcommand*{\test}[1]{%
\makebox[0pt][r]{B}% protrudes to the left, takes no space
#1%
\makebox[0pt][l]{E}% protrudes to the right, takes no space
}

\setlength{\parindent}{0pt}

\begin{document}

Exactly 10 squares of size $5\,$mm and border width one point fit in
an \verb|\hsize| of $5\,$cm plus one point (this is not the \verb|plus|
keyword of \TeX!):

\smallskip \hsize=\dimexpr 5cm+1pt\relax
\test{\mysquarefill{\dimexpr 2.5cm+1pt\relax}{5mm}{1pt}}

\bigskip \hsize=5.3cm
\test{\mysquarefill{2.8cm}{5mm}{1pt}}

\bigskip \hsize=5.6cm
\test{\mysquarefill{3.1cm}{5mm}{1pt}}

\bigskip \hsize=5.9cm
\test{\mysquarefill{3.4cm}{5mm}{1pt}}

\bigskip \hsize=\textwidth
\mysquarefill{\dimexpr 2.8cm+10pt\relax}{2.8cm}{10pt}%

\end{document}


• Very very nice :-). For my humble opinion is good. Jan 24, 2020 at 22:43
• @Sebastiano At last someone sensitive to Beauty. :-) Jan 24, 2020 at 23:34
• Lol :-) I try to watch more questions and answers. Obviously beauty is subjective, but in these cases it is necessary to evaluate the effort, the work and the creative idea. Jan 24, 2020 at 23:51
• @Sebastiano I wrote the first version in a hurry, but this one should be perfectly accurate (see the first test); also, it has the property of only printing whole squares. Jan 25, 2020 at 2:04
• Thank you @frougon, that is what I was searching for.
– tern
Jan 25, 2020 at 9:17