4

For generating a large number of Exercise-sheets for my math class I'd like to set the problem in a grid so the kids have a given structure to add their calculation.

Example with quite a large code chunk from Gonzalo Medina's answer to Change display style in xlop package

\documentclass{article}
\usepackage[german]{babel}

\usepackage[]{xlop}


%% this part is from 
%% https://tex.stackexchange.com/questions/110050/change-display-style-in-xlop-package
%% Answer by Gonzalo Medina

\makeatletter
\def\op@divdisplayone{%
  \begingroup
    \op@count@z=\z@\relax
    \op@count@i=\OP@a@w
    \loop
    \ifnum\@nameuse{OP@a@\the\op@count@i}=0
      \advance\op@count@i by-1
      \advance\op@count@z by1
    \repeat
    \op@count@i=\op@firstlength
    \advance\op@count@i by\op@count@z
    \xdef\op@firstlength{\the\op@count@i}%
    \setbox2=\vbox{%
      \op@count@i=\op@firstlength
      \advance\op@count@i by1
      \hbox{%
        \ifx\op@displayintermediary\op@string@none\else
% add 0.25
          \ifx\op@displayintermediary\op@string@None\else
% end add 0.25
            \kern\opcolumnwidth
            \advance\op@count@i by1
% add 0.25
          \fi
% end add 0.25
        \fi
        \ifop@dividendbridge
          \vrule width0pt depth0pt height\oplineheight 
          \opvline(0,1){0.4}%
          \ophline(0,1){\op@firstlength}%
          \opvline(\op@firstlength,1){0.4}%
        \fi
        \op@display{operandstyle.1}{a}%
        \ifnum\op@shift=0\relax\else
          \op@count@v=\op@shift
          \advance\op@count@v by\OP@a@d
          \oplput(-\the\op@count@v,0){\op@strikedecimalsepsymbol}%
        \fi
      }%
      \op@count@z=\OP@q@w
      \op@count@ii=1\relax
      \ifx\op@displayintermediary\op@string@none
        \advance\op@count@i by-1\relax
% add 0.25
      \else\ifx\op@displayintermediary\op@string@None
        \advance\op@count@i by-1\relax
% end add 0.25
      \else
        \op@count@iv=\@nameuse{OP@q@\the\op@count@z}\relax
        \op@count@iii=\op@count@i
        \advance\op@count@iii by-\@nameuse{OP@T\the\op@count@iv @w}%
        \hbox{%
          % \ifx\op@voperator\op@string@center
          %   \oplput(0,0.5){\op@subsymbol}%
          % \else\ifx\op@voperator\op@string@top
          %   \oplput(0,1){\op@subsymbol}%
          % \else
            \oplput(0,0){\op@subsymbol}%
          % \fi\fi
          \ophline(1,-0.25){\op@firstlength}%
          \advance\op@count@ii by1
          \kern\op@count@iii\opcolumnwidth
          \op@display{intermediarystyle.1}%
                     {T\the\op@count@iv}%
        }%
% modif 0.25
      \fi\fi
% end modif 0.25
      {\global\setbox4=\hbox{}\setbox4=\box4}%
      \loop
        \advance\op@count@z by-1
        \advance\op@count@i by1\relax
      \ifnum\op@count@z>0\relax
        \op@count@iv=\@nameuse{OP@q@\the\op@count@z}\relax
        \ifnum\op@count@iv=0
          \ifx\op@displayintermediary\op@string@all
            \op@count@v=\op@count@i
            \advance\op@count@v by-\@nameuse{OP@R\the\op@count@ii @w}%
            \hbox{%
              \kern\op@count@v\opcolumnwidth
              \op@display{remainderstyle.\the\op@count@ii}%
                         {R\the\op@count@ii}%
            }%
            \op@count@iii=\op@count@i
            \advance\op@count@iii by-\@nameuse{OP@T\the\op@count@iv @w}%
            \hbox{%
              \kern\op@count@v\opcolumnwidth
              % \ifx\op@voperator\op@string@center
              %   \oplput(-1,0.5){\op@subsymbol}%
              % \else\ifx\op@voperator\op@string@top
              %   \oplput(-1,1){\op@subsymbol}%
              % \else
                \oplput(-1,0){\op@subsymbol}%
              % \fi\fi
              \ophline(0,-0.25){\@nameuse{OP@R\the\op@count@ii @w}}%
              \kern-\op@count@v\opcolumnwidth
              \kern\op@count@iii\opcolumnwidth
              \op@display{intermediarystyle.\the\op@count@ii}%
                         {T\the\op@count@iv}%
            }%
          \else
            \ifnum\@nameuse{OP@R\the\op@count@ii @w}=2
              \ifnum\@nameuse{OP@R\the\op@count@ii @2}=0
                \op@makebox{1}{0}{remainderstyle.\the\op@count@ii.1}{zero}%
                \setbox4=\hbox to\opcolumnwidth{\hss\box0\hss}%
              \fi
            \fi
          \fi
          \advance\op@count@ii by1\relax
        \else
          \op@count@v=\op@count@i
          \advance\op@count@v by-\@nameuse{OP@R\the\op@count@ii @w}%
          \ifvoid4\relax\else
            \advance\op@count@v by-1
          \fi
          \hbox{%
            \kern\op@count@v\opcolumnwidth\copy4
            \op@display{remainderstyle.\the\op@count@ii}%
                       {R\the\op@count@ii}%
          }%
          \advance\op@count@ii by1\relax
          \ifx\op@displayintermediary\op@string@none
% add 0.25
          \else\ifx\op@displayintermediary\op@string@None
% end add 0.25
          \else
            \op@count@iii=\op@count@i
            \advance\op@count@iii by-\@nameuse{OP@T\the\op@count@iv @w}%
            \hbox{%
              \kern\op@count@v\opcolumnwidth
              % \ifx\op@voperator\op@string@center
              %   \oplput(-1,0.5){\op@subsymbol}%
              % \else\ifx\op@voperator\op@string@top
              %   \oplput(-1,1){\op@subsymbol}%
              % \else
                \oplput(-1,0){\op@subsymbol}%
              % \fi\fi
              \advance\op@count@ii by-1
              \ifvoid4\relax
                \ophline(0,-0.25){\@nameuse{OP@R\the\op@count@ii @w}}%
              \else
                \ophline(0,-0.25){\@nameuse{OP@T\the\op@count@iv @w}}%
                \ophline(\@nameuse{OP@T\the\op@count@iv @w},-0.25){1}%
                {\setbox4=\box4}%
              \fi
              \advance\op@count@ii by1
              \kern-\op@count@v\opcolumnwidth
              \kern\op@count@iii\opcolumnwidth
              \op@display{intermediarystyle.\the\op@count@ii}%
                         {T\the\op@count@iv}%
            }%
% modif 0.25
          \fi\fi
% end modif 0.25
        \fi
      \repeat
      \advance\op@count@i by-1
      \op@count@iii=\op@count@i
      \advance\op@count@iii by-\@nameuse{OP@R\the\op@count@ii @w}%
      \hbox{%
        \kern\op@count@iii\opcolumnwidth
        \op@display{remainderstyle.\the\op@count@ii}%
                   {R\the\op@count@ii}%
      }%
    }%
    \setbox3=\vbox{%
      \op@count@v=\op@max{\OP@qq@w}{\OP@b@w}
      \hbox{%\ophline(-0.5,-0.25){\the\op@count@v.5}%
            \op@display{operandstyle.2}{b}%
        \ifnum\op@shift=0\relax\else
          \op@count@v=\op@shift
          \advance\op@count@v by\OP@b@d
          \oplput(-\the\op@count@v,0){\op@strikedecimalsepsymbol}%
        \fi}
      %\hbox{XXX\op@display{resultstyle}{qq}}
    }%
    \dimen0=\ht2
    \ifdim\dimen0>\ht3
      \ht3=\ht2
    \else
      \ht2=\ht3
    \fi
    \ifx\op@voperation\op@string@top
      \dimen0=\ht2
      \advance\dimen0 by-0.5\oplineheight
      \ht2=0.75\oplineheight
      \ht3=0.75\oplineheight
      \dp2=\dimen0
      \dp2=\dimen0
    \fi
    \setbox1=\hbox{%
      \box2
      \kern0.5\opcolumnwidth
      \kern-0.5\op@hrulewidth
      \ifx\voperation\op@string@top
        KKK\vrule width\op@hrulewidth
      \else
        : %\vrule width\op@hrulewidth :@:
      \fi
      \kern0.5\opcolumnwidth
      \kern-0.5\op@hrulewidth
      \box3
      = \hbox{\op@display{resultstyle}{qq}}
    }%
    \ifx\op@voperation\op@string@center
      \setbox1=\hbox{$\vcenter{\box1}$}%
    \fi
    \leavevmode\box1
  \endgroup
}
\makeatother

\opset{divsymbol={$\colon$}}

\newcommand\hole[1]{\texttt{\_}}

\usepackage{tikz}
\newcommand{\karos}[2]{
  \begin{tikzpicture}
    \draw[step=0.5cm,color=gray] (0,0) grid (#1 cm ,#2 cm);
  \end{tikzpicture}
}


%% here is my sample

\begin{document}   

I'd like to set this

$$154548 \colon 86 =$$

so that each numeral uses one cell of the grid starting top--left

\karos{9}{5} % Karos der Breite 8cm und Höhe 3cm

so the kids can fill out the whole calculation:

\opidiv[voperation=top,displayintermediary=all,maxdivstep=30]{154548}{86}

\end{document}

I'd like to set this like

\karos{8}{5}{154548 \colon 86 =}

so I can generate a large number of execise sheets with a series of identical commands (I intend to write a shell or perl script using random number generator for this task).

4
3

Thanks to Steven I found quite a nice solution.

I reduced the Solution found in How to fit characters (numbers) into a graph paper? to my needs:

\documentclass{article} 
\usepackage{tikz}
\usepackage{ifthen}

\newcounter{gridypos}
\newenvironment{squaredpaper}[3][.5cm]{\obeyspaces% 
  \setcounter{gridypos}{#3}
  \newcommand\gridtext[1]{
    \node[anchor=west,black] at (0,0) [yshift=\value{gridypos}*#1-.5*#1]%
    {%
      \hspace{-.25\dimexpr#1\relax}%
      \spaceout{#1}{##1}%
    };
    \addtocounter{gridypos}{-1}%Next \gridtext one line lower
  }
  % Make the grid
  % Source: http://texwelt.de/wissen/fragen/2639/wie-kann-ich-kastchenpapier-zeichnen
  \begin{tikzpicture}[gray,step=#1]
    \draw (0,0) rectangle (#2*#1,#3*#1) (0,0) grid (#2*#1,#3*#1);
  }{
  \end{tikzpicture} 
}

\newcommand\spaceout[2]{\def\charwd{#1}\spaceouthelp#2\relax\relax\relax}
\def\spaceouthelp#1#2\relax{%
  \ifx#1\relax\else%
    \makebox[\charwd]{#1}%
    \spaceouthelp#2\relax%
  \fi%
}

\newcommand{\Aufgabe}[3]{
  \begin{squaredpaper}{#1}{#2}
    \gridtext{#3}    
  \end{squaredpaper}
}

\begin{document} 

\ttfamily%\scriptsize% WILL WORK IF UNCOMMENTED
\Aufgabe{16}{10}{154548{$\colon$}86=}

\end{document}

Once I figured out, that Math-Mode will only work when embraced and must fit in one cell, it was quite easy.

Thank you for this quick solution.

This is how it looks like:

\Aufgabe{16}{10}{154548{$\colon$}86=}

enter image description here

1

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.