1

I would like to set out my division sums as in section 2 in this code. How can I do this with random numbers, and not so messy code, or even without using tables at all?

    \documentclass{article}
    
    \usepackage{amsmath}
    \usepackage[makeroom]{cancel}
    \usepackage{longdivision}
    
    \usepackage{cancel}
    \usepackage{xcolor}
    \newcommand\Ccancel[2][black]{\renewcommand\CancelColor{\color{#1}}\cancel{#2}}
    
    \begin{document}
    
    \section{}
    
    \longdivision[stage=0]{168}{3}
    \longdivision[stage=1]{168}{3}
    \longdivision[stage=2]{168}{3}
    \longdivision[stage=3]{168}{3}
    
    
    \section{}
    
    \begin{tabular}{ccccc}
    0&5&56&&\\
    
    $3 \overline{)168}$
    &
    $3 \overline{)\Ccancel[red]{1}{^1}68}$
    &
    $3 \overline{)\Ccancel[red]{1}{{^1}\Ccancel[red]{6}{^1}}}8$\\
    \end{tabular}
    
    \end{document}

1 Answer 1

1

I'm not familiar with the longdivision package but the sagetex package will let you work with random numbers that I would consider not so messy. The sagetex package links the Python based computer algebra system, called Sage,with LaTeX. Here is a simple illustration:

\documentclass{article}
\usepackage{amsmath,sagetex}
\usepackage[makeroom]{cancel}
\usepackage{longdivision}
\usepackage{cancel}
\usepackage{xcolor}
\newcommand\Ccancel[2][black]{\renewcommand    \CancelColor{\color{#1}}\cancel{#2}}
\begin{document}
\section{}
\longdivision[stage=0]{168}{3}
\longdivision[stage=1]{168}{3}
\longdivision[stage=2]{168}{3}
\longdivision[stage=3]{168}{3}
\section{}
\begin{sagesilent}
output = r""
for i in range(0,2): 
    big = randint(100,200)
    small = randint(3,9)
    output += r"\longdivision[stage=0]{%s}{%s}"%(big,small)
    output += r"\longdivision[stage=1]{%s}{%s}"%(big,small)
    output += r"\longdivision[stage=2]{%s}{%s}"%(big,small)
    output += r"\longdivision[stage=3]{%s}{%s}\\\\"%(big,small)
\end{sagesilent}
\sagestr{output}
\end{document}

The result running in Cocalc is shown below: enter image description here

After entering the sagesilent mode the line for i in range(0,2): is setting up 2 random problems to produce because in Python range(a,b) runs includes a but doesn't include b. Change 2 to 10 and we now have 10 problems with no extra code. The big number is a random integer between 100 and 200, inclusive (for both). The small number is a random integer between 3 and 9, inclusive. It puts this information into a raw string, r"", which lets us work with problematic characters such as \.

Sage is not part of a LaTeX distribution. The easiest way to experiment with it is with a free Cocalc account. Search this site for sagetex for more examples.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .