Long Division with Boxes

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}

Output

enter image description here enter image description here


enter image description here

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