For my math-lessons I'm searching for an easy way to fill the remaining space of a line with a squarepattern. There are some disadvantages with leaders (alignment, see autofill with square-pattern (leaders vs. tikz)), so I used the explanations in Is there a way to measure the remaining space of a line of text?.
\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}
This produces:
Now I'd like to simplify the code and especially combine the two macros. How do I use something like
\draw[step=5mm,color=gray](0,0) grid (floor((x-coordinate-of(current page text area.east) - x-coordinate-of(0,0))/#2)*#2,#3);
with #2=size of squares and #3=height of squareblock?
And is there an easier solution for the raggedleft/raggedright/center-mechanism?

gridworks. If your aim is to do all of this in one command this can be done of course, but it won't make things way simpler. – Feb 05 '20 at 01:10