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

enter image description here

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.


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.

% -----------------------------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 ------------------------------
\fontsize{17pt}{20pt}\selectfont %sets font size
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])
                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])
                output += r"\underline{-  %s}\\\\\\"%(subtrahend[j])

    output += r"\end{tabular}"

    return output

outputP = r""
outputP += Problems(4,5)

The output running in Cocalc is shown: enter image description here

Some comments:

  • 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.



 {% #1 = rows, #2 = columns
  \zuriel_randomsubtractions:nn { #1 } { #2 }
  \int_set:Nn \l_zuriel_first_lb_int { #1 }
  \int_set:Nn \l_zuriel_first_ub_int { #2 }
  \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
  \begin{tabular}{ @{} *{#2}{>{$}c<{$}} c @{\hspace{-\tabcolsep}} }
  \prg_replicate:nn { #1 } { \__zuriel_randomsubtraction_row:n { #2 } \\[6ex] }

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



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



enter image description here

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.