# How to create a subtraction worksheet with random numbers in each question?

I would like to create a worksheet which looks like the following:

Here instead of manually typing every question, I would like to create random questions, where the subtrahend is from a set of integers (say {-10,-9,..,10}) and minuend is from another set of integers where the subtrahend may be greater than, equal to or smaller than the minuend.

Any suggestions are greatly appreciated.

• – Mico
Dec 26 '19 at 22:33
• Should there be exactly 5 rows and 5 columns per page?
– Mico
Dec 26 '19 at 22:35
• @Mico, yes. It helps! Not necessarily 5 by 5; I am creating this for a small child and I would like to make the numbers bigger. Dec 26 '19 at 22:36
• I use the TiKz/PGF package to do this kind of random exercices. It has a mathematic library that could help you. I can't do a full answer right now, I'll make one as soon as I can. Dec 27 '19 at 7:05
• When I did this sort of thing, I used C++ to create the random problems and LaTeX to do the formatting. That way I avoided questions like 0+0=?. See elfsoft2000.com/worksheets/index.htm Dec 27 '19 at 16:34

Here's an implementation using the sagetex package. It utilizes the open source computer algebra system, SAGE, which is not part of the LaTeX distribution. This gives access to programming in Python as well.

\documentclass{article}%
% -----------------------------The preamble ------------------------------------
\usepackage{fix-cm}% allow for a bigger font
%  mathematical typesetting, fonts, and symbols
\usepackage{amsmath, amsfonts, amssymb}
\usepackage[T1]{fontenc}% change the fonts
\usepackage[margin=.5in]{geometry}%  sets the margins
\usepackage{sagetex}%  use Sage for it's math ability
\pagestyle{empty} % remove the page numbers
% -----------------------------End of the preamble ------------------------------
\begin{document}
\fontsize{17pt}{20pt}\selectfont %sets font size
\noindent
\begin{sagesilent}
def Problems(rows,columns):
output = r""
output += r"\begin{tabular}{r"+"r"*(2*columns)+"}"
for i in range(0,rows):
minuend = [Integer(randint(5,25)) for i in range(0,columns)]
subtrahend = [Integer(randint(-10,10)) for i in range(0,columns)]
for j in range(0,columns):
if j<columns-1:
output += r"%s & \hspace{.2in} &"%(minuend[j])
else:
output += r"%s \\"%(minuend[j])
##### second row #####
for j in range(0,columns):
if j<columns-1:
output += r"\underline{-  %s} & \hspace{.5in} &"%(subtrahend[j])
else:
output += r"\underline{-  %s}\\\\\\"%(subtrahend[j])

output += r"\end{tabular}"

return output

outputP = r""
outputP += Problems(4,5)
\end{sagesilent}
\begin{center}
\sagestr{outputP}
\end{center}
\end{document}


The output running in Cocalc is shown:

• output += r"\begin{tabular}{r"+"r"*(2*columns)+"}" uses Python to set the number of columns. Since there should be space between the problems, extra columns are added. Being able to adjust the columns used makes the sagetex approach very convenient.
• minuend = [Integer(randint(5,25)) for i in range(0,columns)] sets the minuend as a random integer between 5 and 25 while subtrahend = [Integer(randint(-10,10)) for i in range(0,columns)] values are set between -10 and 10.
• The fontsize has been increased to make the worksheet easier to read.
• outputP += Problems(4,5) results in the creation of 4 rows of 5 problems (20 problems). By adjusting these numbers you can easily change the number of problems in your worksheet.

The easiest way to work with sagetex is by opening a free Cocalc account.

Here's an implementation with expl3.

\documentclass{article}
\usepackage{xparse,array}

\ExplSyntaxOn

\NewDocumentCommand{\randomsubtractions}{mm}
{% #1 = rows, #2 = columns
\zuriel_randomsubtractions:nn { #1 } { #2 }
}
\NewDocumentCommand{\boundsfirst}{mm}
{
\int_set:Nn \l_zuriel_first_lb_int { #1 }
\int_set:Nn \l_zuriel_first_ub_int { #2 }
}
\NewDocumentCommand{\boundssecond}{mm}
{
\int_set:Nn \l_zuriel_second_lb_int { #1 }
\int_set:Nn \l_zuriel_second_ub_int { #2 }
}

\int_new:N \l_zuriel_first_lb_int
\int_new:N \l_zuriel_first_ub_int
\int_new:N \l_zuriel_second_lb_int
\int_new:N \l_zuriel_second_ub_int

\cs_new_protected:Nn \zuriel_randomsubtractions:nn
{
\group_begin:
\setlength{\tabcolsep}{2em}
\begin{tabular}{ @{} *{#2}{>{$}c<{$}} c @{\hspace{-\tabcolsep}} }
\prg_replicate:nn { #1 } { \__zuriel_randomsubtraction_row:n { #2 } \\[6ex] }
\end{tabular}
\group_end:
}

\cs_new_protected:Nn \__zuriel_randomsubtraction_row:n
{
\prg_replicate:nn { #1 } { \__zuriel_randomsubtraction: & }
}

\cs_new_protected:Nn \__zuriel_randomsubtraction:
{
\begin{array}{@{} r @{}}
\int_rand:nn { \l_zuriel_first_lb_int } { \l_zuriel_first_ub_int } \\
- \int_rand:nn { \l_zuriel_second_lb_int } { \l_zuriel_second_ub_int } \\
\hline
\end{array}
}

\ExplSyntaxOff

\begin{document}

\boundsfirst{-10}{10} \boundssecond{1}{10}

\randomsubtractions{5}{5}

\end{document}