6

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}%
\mbox{}\leaders\hbox{\vbox to #1{\cleaders\hbox{\mySE}\vfil}}\hfill}}

The result of

B\mysquarefill{2.5cm}E

results in

squarefill first approach

, 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}

squarefill with tikz

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?

1

1 Answer 1

6

I propose the following:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newcommand*{\mysquarefill}[3]{%
  \begingroup
  \color{black!50}\mbox{}%
  \cleaders\vbox to #1{%
      \cleaders\hbox to #2{%
        \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}

screenshot

6
  • Very very nice :-). For my humble opinion is good.
    – Sebastiano
    Jan 24, 2020 at 22:43
  • 1
    @Sebastiano At last someone sensitive to Beauty. :-)
    – frougon
    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.
    – Sebastiano
    Jan 24, 2020 at 23:51
  • 1
    @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.
    – frougon
    Jan 25, 2020 at 2:04
  • Thank you @frougon, that is what I was searching for.
    – tern
    Jan 25, 2020 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.