1

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.

3

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

| improve this answer | |
1

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}

enter image description here

| improve this answer | |

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.