# Long Division with Boxes

I need to make a division worksheet for a specific student with ADHD. He often gets confused with the placement of the digits so this worksheet has to have boxes aligned in a very precise way. There are no available printed worksheets out there that have what I need. I've tried out a code for long division with boxes on my own, but it does not give me the desired image. I need to create an image similar to the one below with horizontal lines and minus signs.

I tried so far:

\documentclass[11pt]{article}
\begin{document}
\newcommand\divspace{\,}
$\arraycolsep=0em \begin{array}{r@{\divspace}c@{\divspace}lllll} & & \multicolumn{1}{r}{15} & \\ \cline{2-6} 15 & \big) & \multicolumn{4}{l}{225} & \\ & & \fbox{1} \fbox{5} & & & & \\ \cline{3-4} & & \fbox{7}\fbox{5} & & & \\ & & \fbox{7}\fbox{5} & & & \\ \cline{3-5} & & & 0 & & \\ \end{array}$
\end{document}

-
Seems like a got job for a matrix. What have you tried so far? –  Qrrbrbirlbel Feb 19 '13 at 19:44
\documentclass[11pt]{article} \begin{document} \newcommand\divspace{\,} [ \arraycolsep=0em \begin{array}{r@{\divspace}c@{\divspace}lllll} & & \multicolumn{1}{r}{15}&\ \cline{2-6} 15&\big)&\multicolumn{4}{l}{225}&\ & & \fbox{1} \fbox{5}&&&&\\cline{3-4} & & \fbox{7}\fbox{5}&&&\ & & \fbox{7}\fbox{5}&&&\\cline{3-5} & & &0&& \end{array} ] \end{document} –  Janice Feb 19 '13 at 19:51
At the bottom of your question is an “Edit” button. You can add this code to your question. A tip: If you indent lines by 4 spaces or enclose words in backticks , they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it). –  Qrrbrbirlbel Feb 19 '13 at 19:58
Depending on how fancy you'd like the output, the xlop package seems to be designed for that kind of things. –  cjorssen Feb 19 '13 at 20:16
You may also be interested in dpcarlisle.blogspot.co.uk/2012/10/mathml-long-division.html especially the link at the end that generates mathjax compatible latex markup for long division –  David Carlisle Feb 19 '13 at 22:04

\documentclass{article}

\usepackage{color}

\newsavebox\wbox
\savebox\wbox{\kern2pt{\color{white}\rule{.5cm}{.5cm}}\kern2pt}

\begin{document}

\colorbox[rgb]{0,0,.3}{\parbox{5cm}{%
\baselineskip0pt
\lineskip4pt
\def\n#1{\b\raisebox{3pt}{\llap{\large#1\kern7pt}}}
\def\b{\usebox\wbox}
\def\,{\kern4pt}
\def\_{\mbox{}\kern4pt\kern.5cm}
\def\r#1{\par\kern3pt\mbox{}\_\kern1pt{%
\color{white}\smash{\vrule height1pt width 1pt depth \dimexpr 1cm+7pt\relax}%
\vrule height1pt depth0pt width\dimexpr .5cm*#1+4pt*#1+4pt\relax}\par\kern1pt}
\_\,\b\b\b\b\,\b\\
\r4
\n5\,\n3\n3\n8\n8\\
\_\,\b\\
\_\,\b\b\\
\_\,\b\b\\
\_\,\_\b\b\\
\_\,\_\b\b\\
\_\,\_\_\b\b\\
\_\,\_\_\b\b\\
\_\,\_\_\_\b
}}

\end{document}

-

A semi-automatic TikZ solution.

The styles are set according to the keys divisor length and dividend length. Though the actual input is still provided by the user.

It is important that cells that should get the assigned style has to have an text (which can be a space, e.g. \␣ or ~).

The same holds true for the additional row and column separation. The last end-row command \\ for the TikZ matrix should not be input. The longdivision environment does this automatically.

## Interface

The line can be modified by:

\ldset{every line/.tyle={<style options for the line>}}
% or
\ldset{every line/.append style={<options to add to the existing line>}}


The line can be removed by \ldset{every line/.style={}}.

All boxes can be modified by the using the keys boxes. The style of the different kinds of boxes can be set by the keys divisors, dividends, results, rests and others.

## Code

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{matrix,calc}
\newcommand*{\ldset}[1]{\pgfkeys{/ld/.cd,#1}}
\ldset{
every long division/.style={
fill=teal,
column sep=.5em,
row sep=.5em,
},
every box/.style={
shape=rectangle,
draw=none,
fill=white,
text width=1.3em,
align=center,
text height=0.95em,
text depth=0.35em,
rounded corners,
execute at begin node=,%
execute at end node=,
name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn%
},
every divisor/.style={
/ld/every box,
fill=red!50,
},
every dividend/.style={
/ld/every box,
fill=yellow!30,
},
every result/.style={
/ld/every box,
fill=gray!20,
},
every rest/.style={
/ld/every box,
fill=teal!20,
},
every other/.style={
/ld/every box,
},
boxes/.style={/ld/every box/.append style={#1}},
divisors/.style={/ld/every divisor/.append style={#1}},
dividends/.style={/ld/every dividend/.append style={#1}},
results/.style={/ld/every result/.append style={#1}},
rests/.style={/ld/every rest/.append style={#1}},
others/.style={/ld/every other/.append style={#1}},
divisor length/.code={%
\def\qrrLDdivisorLength{#1}%
\def\qrrLDrestLength{#1}%
},
dividend length/.code={%
\def\qrrLDdividendLength{#1}%
\def\qrrLDresultLength{#1}%
},
result length/.estore in=\qrrLDresultLength,
rest length/.estore in=\qrrLDrestLength,
every line/.style={
draw=black,
rounded corners=2pt,
thick,
line cap=round,
}
}
\tikzset{
execute style/.style={#1},
invisible text/.style={text opacity=0},
}
\newenvironment{longdivision}[2][]{%
\ldset{#2}%
\def\QrrLDdivisorStyle{}%
\foreach \i in {1,...,\qrrLDdivisorLength}{%
\xdef\QrrLDdivisorStyle{\QrrLDdivisorStyle, row 2 column \i/.style={every node/.style={/ld/every divisor}}}%
}%
\pgfmathtruncatemacro\QrrLDfirstRestColumn{\qrrLDdivisorLength+\qrrLDresultLength+1}%
\edef\QrrLDrestStyle{}%
\foreach \i in {\QrrLDfirstRestColumn,...,\numexpr\QrrLDfirstRestColumn+\qrrLDrestLength\relax}{%
\xdef\QrrLDrestStyle{\QrrLDrestStyle, row 1 column \i/.style={every node/.style={/ld/every rest}}}%
}%
\tikzpicture[#1]
\matrix[
name=m,
/ld/every long division,
every node/.style={/ld/every other},
row 1/.style={every node/.style={/ld/every result}},
row 2/.style={every node/.style={/ld/every dividend}},
execute style/.expand once={\QrrLDdivisorStyle},
execute style/.expand once={\QrrLDrestStyle},
matrix of nodes,
] \bgroup
}{
\pgfmatrixendrow\egroup;
\pgfmathtruncatemacro\QrrLDbeforeFirstRestColumn{\QrrLDfirstRestColumn-1}
\pgfmathtruncatemacro\QrrLDafterLastDivisorColumn{\qrrLDdivisorLength+1}
\path[/ld/every line] ($(m-1-\QrrLDfirstRestColumn.south west)!.5!(m-2-\QrrLDbeforeFirstRestColumn.north east)$) -|
($(m-2-\qrrLDdivisorLength.south east)! .5!(m-3-\QrrLDafterLastDivisorColumn.north west)$);
\endtikzpicture%
}
\begin{document}
\begin{longdivision}{
divisor length=1,
dividend length=4,
others={invisible text},
results={invisible text},
rests={invisible text},
}
&[1em] 0 & 6 & 7 & 7 &[1em] 3\\[1em]
5 &      3 & 3 & 8 & 8 & \\
&      0 \\
&      3 & 3 \\
&      3 & 0 \\
&        & 3 & 8 \\
&        & 3 & 5 \\
&        &   & 3 & 8 \\
&        &   & 3 & 5 \\
&        &   &   & 3
\end{longdivision}

\begin{longdivision}{
divisor length=1,
dividend length=4,
}
&[1em] 0 & 6 & 7 & 7 &[1em] 3\\[1em]
5 &      3 & 3 & 8 & 8 & \\
&      0 \\
&      3 & 3 \\
&      3 & 0 \\
&        & 3 & 8 \\
&        & 3 & 5 \\
&        &   & 3 & 8 \\
&        &   & 3 & 5 \\
&        &   &   & 3
\end{longdivision}
\end{document}
`

-