3

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:

enter image description here

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?

tern
  • 93
  • You can use pgf functions in the coordinates. However, you will nevertheless need to make a measurement of the remaining distance because of the way grid works. 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

1 Answers1

2

I fail to see what your actual question is. (Why is @frougon's answer not satisfiable) (You mentioned alignment. Of what? Horizontally or vertically? Only you know what you want.)

Anyway, I propose the following construction that uses TikZ to draw squares and uses \leaders to fill in spaces. If you want to customize squares, play with TikZ. If you want to align properly, play with TeX. Have fun.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\newbox\astackofsquares
\def\mysquarefill#1#2{
    \setbox\astackofsquares=\hbox{%
        \tikz[x=#1,y=#1,baseline=#1*#2/2-.5ex]{
            \path(0,0)(1,#1*#2);
            \begin{pgfinterruptboundingbox}
                \draw foreach\i in{1,...,#2}{(0,\i-1)rectangle(1,\i)};
            \end{pgfinterruptboundingbox}
        }%
    }
    \leaders\copy\astackofsquares\hfill\hbox{}
}

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

\end{document}

centered version

replace \leaders by \cleaders.

flush left version

I propose using a clip instead of doing arithmetics.

\documentclass{article}

\usepackage{tikzpagenodes}
\usepackage{showframe}\def\ShowFrameColor{\color{yellow}}

\begin{document}

\def\quarefillleft#1#2{
    \tikz[remember picture,x=#1,y=#1,baseline=#1*#2/2-.5ex]{
        \path(0,0)(0,#1*#2);
        \begin{pgfinterruptboundingbox}
            \clip(-1,-1)rectangle(current page text area.north east);
            \draw(0,0)grid[step=#1](20cm,#2);
        \end{pgfinterruptboundingbox}
    }
}
\begin{itemize}
    \item $\frac{8}{15}+\frac{7}{12}+\frac{5}{12} +2=\quarefillleft{5mm}{2}$
    \item $\frac{3}{4}$ von $\frac{2}{5}=\quarefillleft{4mm}{3}$
    \item $\frac{15}{28}\cdot\frac{14}{30}=\quarefillleft{3mm}{4}$
\end{itemize}

\end{document}
Symbol 1
  • 36,855
  • If you add the % in "...}%\leaders..." no extra-space is added. The first example-line shows the difference. – tern Feb 16 '20 at 10:52
  • My question aimed at a solution, where the second example-line doesn't leave a big horizontal space between the "=" and the start of the squares. Additionally I wanted to have a solution with a flushleft / flushright-option for the squares. – tern Feb 16 '20 at 10:56
  • @tern I add more variants. I highly recommend not to play with arithmetics. Let me know if you really want a flush right version. – Symbol 1 Feb 16 '20 at 18:28
  • Thank you for the approach with the clipping, that's a cool idea and the left-right-alignement of your version nice. The consequence of the clipping is, that the squares are cut off. That maybe ok for a lot of use-cases. – tern Feb 18 '20 at 20:35