4

Consider the following example:

\documentclass[
  a4paper,
  landscape,
  12pt
]{article}


\usepackage[sc]{mathpazo}
\usepackage[
  hmargin = 4cm,
  tmargin = 1.5cm
]{geometry}
\usepackage{fancyhdr}
\usepackage{background}
\usepackage[
  lastpage
]{zref}
\usepackage{mathtools}
\usepackage[
  first = 0,
  last = 50
]{lcg}


\newcommand*\mlrHa{\qquad\qquad\qquad\enskip}
\newcommand*\mlrHb{\qquad\quad}
\newcommand*\mlrV{0.68ex}
\newcommand*\random{\rand\arabic{rand}}

\backgroundsetup{
  contents = Name: \underline{\hspace{10em}},
  scale = 1,
  color = black,
  angle = 0,
  opacity = 1,
  position = current page.north west,
  vshift = -2.02cm,
  hshift = 5cm
}

\pagestyle{fancy}
\renewcommand*\headrulewidth{0pt}
\setlength\headheight{14.5pt}
\makeatletter
\zref@newprop*{numpage}{\the\value{page}}
\zref@addprop{main}{numpage}
\fancyhf{}
\fancyfoot[C]{%
  \ifnum \zref@extractdefault{LastPage}{numpage}{0} > 1
    \thepage
  \fi
    %
}
\makeatother


\begin{document}

\BgThispage
\begin{center}
  \Huge\textbf{Training -- addition}
\end{center}

\begin{alignat*}{5}
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &=
\end{alignat*}
\newpage

\NoBgThispage
\begin{alignat*}{5}
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= \mlrHb \\[\mlrV]
  \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &= &\mlrHa \random + \random &=
\end{alignat*}

\end{document}

Is it possible to automatically calculate the values of each pair of \randoms when added and get the output on pages 3 and 4, i.e., after all the arithmetical problems?

Mico
  • 506,678
  • How should the results of the calculations be reported: just as numbers (e.g., 2), or by restating the full problem (e.g., 1+1=2)? Please advise. – Mico May 07 '17 at 19:01
  • @Mico Just the number is just fine. P.S. I would like to also have sheets with subtraction and multiplication, if possible. – Svend Tveskæg May 07 '17 at 19:10
  • I see that for the addition exercises, the pseudo-random numbers should fall in the range 1...50. (0 as the minimum integer would be a too trivially easy, right?) What about the ranges for the subtraction (are negative numbers allowed?) and multiplication? – Mico May 07 '17 at 19:50
  • 1
    I would like the numbers to be choosen from within the range 0,...,50. (Some of my young students are not very good at maths and need to learn the adding zero doesn't make a difference.) – Svend Tveskæg May 07 '17 at 19:54
  • 1
    I've taken the liberty of editing the title of your posting to make its objective more readily apparent. Feel free to revert. – Mico May 08 '17 at 16:44

3 Answers3

5

(Modified the code to incorporate the information that the integers should range from 0 to 50; further modification to make Name: _______ show up only on very first page; still further modifications to address the OP's request for subtraction; multiplication, and division tables; yet additional modifications for pretty-printing.)

Here's a LuaLaTeX-based solution. It generates tables of questions and answers for addition, subtraction, multiplication, and division exercises; 16 pages in all. The cells of the longtables are generated via pairs of nested for loops. For extra legibility, a bit more vertical whitespace is inserted after every 5th row.

Here are the first few rows of the addition-questions page:

enter image description here

and here are the first few rows of the corresponding addition-answers page:

enter image description here

% !TeX program = lualatex

%% First, some code from the original posting, streamlined.
\documentclass[a4paper,landscape,12pt]{article}
\usepackage{newpxtext,newpxmath} % newer than mathpazo
\usepackage[hmargin=4cm, tmargin=1.5cm]{geometry}
\usepackage[lastpage]{zref}
\usepackage{fancyhdr,background}
\backgroundsetup{
  contents = ,
  scale = 1,
  color = black,
  angle = 0,
  opacity = 1,
  position = current page.north west,
  vshift = -2.02cm,
  hshift = 5cm
}
\pagestyle{fancy}
\renewcommand*\headrulewidth{0pt}
\setlength\headheight{14.5pt}
\makeatletter
\zref@newprop*{numpage}{\the\value{page}}
\zref@addprop{main}{numpage}
\fancyhf{}
\fancyfoot[C]{%
  \ifnum \zref@extractdefault{LastPage}{numpage}{0} > 1
    \thepage
  \fi}
\makeatother

%% Now the new material (mostly Lua code)

\usepackage{array,longtable}
\newcolumntype{R}{>{$}r<{$}} % right-aligned, math mode
\newcolumntype{C}{>{${}}c<{{}$}} % centered, math mode
\setlength\extrarowheight{0.85ex} % extra vert. space
\setlength\tabcolsep{0pt}

\usepackage{luacode}
\begin{luacode}
-- First, some variables and auxiliary functions

n1 = 50       -- max integer in addition and subtract. ops
n2 = 20       -- max integer in multiplication ops.
nrows = 40    -- number of rows per longtable (20 rows per page)
xtra_sp = "1.5ex" -- extra whitespace after every 5th row

-- What to place at the _end_ of each table cell?
function cell_terminate(i,j) -- i: 1..nrows; j: 1..5
  if j<5 then 
    tex.sprint ( "&" ) 
  elseif i%5==0 then -- extra space after every 5th row
    tex.sprint ( "\\\\[" .. xtra_sp .. "]" )
  else 
    tex.sprint ( "\\\\" )
  end
end

-- The following eight functions do the actual work of
-- calculating the cells and outputting them into `longtable` env.'s

-- Addition
function questions_Addition ()
math.randomseed(123456789) -- select a suitable seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n1)
      x2 = math.random(0,n1)
      tex.sprint ( x1 .. "+" .. x2 .."={}" ) 
      cell_terminate(i,j)
    end
  end
end
function answers_Addition ()
math.randomseed(123456789) -- re-use the same seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n1)
      x2 = math.random(0,n1)
      tex.sprint ( x1 .. "+" .. x2 .."&=&" .. x1 + x2 ) 
      cell_terminate(i,j)
    end
  end
end

-- Subtraction
function questions_Subtraction ()
math.randomseed(123789456) -- select a suitable seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n1)
      x2 = math.random(0,n1)
      tex.sprint ( x1 .. "-" .. x2 .."={}" ) 
      cell_terminate(i,j)
    end
  end
end
function answers_Subtraction ()
math.randomseed(123789456) -- re-use the same seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n1)
      x2 = math.random(0,n1)
      tex.sprint ( x1 .. "-" .. x2 .."&=&" .. x1 - x2 ) 
      cell_terminate(i,j)
    end
  end
end

-- Multiplication
function questions_Multiplication ()
math.randomseed(789123456) -- select a suitable seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n2)
      x2 = math.random(0,n2)
      tex.sprint ( x1 .. "\\cdot" .. x2 .. "={}" ) 
      cell_terminate(i,j)
    end
  end
end
function answers_Multiplication ()
math.randomseed(789123456) -- re-use the same seed
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random(0,n2)
      x2 = math.random(0,n2)
      tex.sprint ( x1 .. "\\cdot" .. x2 .. "&=&" .. x1*x2 ) 
      cell_terminate(i,j)
    end
  end
end

-- Division
function questions_Division ()
math.randomseed(987654321) -- select a suitable seed
local x1, x2, y
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random( 20 ) -- divisor
      x2 = math.random( 25 ) -- quotient ("result")
      y  = x1 * x2
      tex.sprint ( y.."\\div"..x1.."={}" ) 
      cell_terminate(i,j)
    end
  end
end
function answers_Division ()
math.randomseed(987654321) -- re-use the same seed
local x1, x2, y
  for i=1,nrows do
    for j=1,5 do
      x1 = math.random( 20 ) -- divisor
      x2 = math.random( 25 ) -- quotient ("result")
      y  = x1 * x2
      tex.sprint ( y.. "\\div" .. x1 .. "&=&" .. x2 ) 
      cell_terminate(i,j)
    end
  end
end
\end{luacode}

%% Latex code for question and answer longtables
\newcommand\TableQ[1]{%
    \begin{longtable}{@{} *{4}{R@{\hspace{2.75cm}}} R @{}}
        \bigskip\endfirsthead
        \bigskip\bigskip\endhead
        \directlua{questions_#1()}
    \end{longtable}}

\newcommand\TableA[1]{%
    \begin{longtable}{@{} *{4}{RCR@{\hspace{2.25cm}}} RCR @{}}
        \bigskip\endfirsthead
        \bigskip\bigskip\endhead
        \directlua{answers_#1()}
    \end{longtable}}

%% Headers above question and answer tables
\newcommand\HeaderQ[1]{%
    \clearpage
    \noindent
    Name: \underline{\hspace{10em}}\hfill%
    {\Huge\textbf{Training -- #1}}\hfill%
    \phantom{Name: \underline{\hspace{10em}}}}

\newcommand\HeaderA[1]{%
    \clearpage
    \begin{center}
      \Huge\textbf{Answers -- #1}
    \end{center}}


\begin{document}
%% the macros \TableQ and \TableA invoke the Lua functions

%%%% Addition
\HeaderQ{Addition}
\TableQ{Addition}
\HeaderA{Addition}
\TableA{Addition}

%%%% Subtraction
\HeaderQ{Subtraction}
\TableQ{Subtraction}
\HeaderA{Subtraction}
\TableA{Subtraction}

%%%% Multiplication
\HeaderQ{Multiplication}
\TableQ{Multiplication}
\HeaderA{Multiplication}
\TableA{Multiplication}

%%%% Division
\HeaderQ{Division}
\TableQ{Division}
\HeaderA{Division}
\TableA{Division}

\end{document}
Mico
  • 506,678
  • 1
    @SvendTveskæg - Incidentally, this is the hardest I've ever had to work to garner 3 upvotes and an accept check-mark... If you have further requests on this subjects, please post a new query. – Mico May 17 '17 at 13:31
5

Here is example how to do this in plain TeX.

\input random

\newbox\training  \newbox\results
\newcount\A \newcount\B \newcount\C  \newcount\tmpnum
\def\maxA{50}  \def\numtasks{69}
\def\addtobox#1#2{\setbox#1=\vbox{
   \unvbox#1\lastbox\hbox to.14\hsize{\hss$#2{}$}}}
\def\setcolumns#1{\par
   \A=\numtasks \advance\A by-1 \divide\A by5 \advance\A by1
   \setbox0=\vsplit#1to0pt \sbox1#1 \sbox2#1 \sbox3#1 \sbox4#1 \sbox5#1
   \line{\col1\hfil\col2\hfil\col3\hfil\col4\hfil\col5}
}
\def\sbox#1#2{\setbox#1=\vsplit#2to\the\A\baselineskip}
\def\col#1{\vtop{\unvbox#1}}
\baselineskip=20pt plus1pt

\tmpnum=0
\loop
   \setrannum\A{0}{\maxA} \setrannum\B{0}{\maxA} \C=\A \advance\C by\B
   \addtobox\training{\the\A+\the\B=}
   \addtobox\results{\the\A+\the\B=\the\C}
   \ifnum\tmpnum<\numtasks
      \advance\tmpnum by1
      \repeat

{\bf Training -- addition}

\setcolumns\training
\vfil\break

{\bf Results -- addition}

\setcolumns\results

\bye

We save training text to the box \training and answers to the box \results in loop. Then we print these boxes to the desired columns by \vsplit.

wipet
  • 74,238
4

Save the results before printing the addition.

\documentclass[
  a4paper,
  landscape,
  12pt
]{article}


\usepackage[sc]{mathpazo}
\usepackage[
  hmargin = 4cm,
  tmargin = 1.5cm
]{geometry}
\usepackage{fancyhdr}
\usepackage{background}
\usepackage[
  lastpage
]{zref}
\usepackage{mathtools}
\usepackage{calc}
\usepackage{xparse}

\backgroundsetup{
  contents = Name: \underline{\hspace{10em}},
  scale = 1,
  color = black,
  angle = 0,
  opacity = 1,
  position = current page.north west,
  vshift = -2.02cm,
  hshift = 5cm
}

\pagestyle{fancy}
\renewcommand*\headrulewidth{0pt}
\setlength\headheight{14.5pt}
\makeatletter
\zref@newprop*{numpage}{\the\value{page}}
\zref@addprop{main}{numpage}
\fancyhf{}
\fancyfoot[C]{%
  \ifnum \zref@extractdefault{LastPage}{numpage}{0} > 1
    \thepage
  \fi
}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\randomadditions}{m}
 {
  \par
  {
   \linespread{1.5}\selectfont\noindent
   \svend_randomadditions:n { #1 }
   \par
  }
 }

\cs_new_protected:Nn \svend_randomadditions:n
 {
  \prg_replicate:nn { #1 }
   {
    \makebox[.2\textwidth][l]
     {
      \makebox[\widthof{$00+00={}$}][r]
       {
        \svend_randomaddition:
       }
     }
    \hspace{1pt minus 1pt}
   }
 }

\seq_new:N \g_svend_randomadditions_seq
\tl_new:N \l_svend_addition_first_tl
\tl_new:N \l_svend_addition_second_tl
\tl_new:N \l_svend_addition_total_tl

\cs_new_protected:Nn \svend_randomaddition:
 {
  \tl_set:Nx \l_svend_addition_first_tl { \fp_eval:n { randint(0,50) } }
  \tl_set:Nx \l_svend_addition_second_tl { \fp_eval:n { randint(0,50) } }
  \tl_set:Nx \l_svend_addition_total_tl
   {
    \fp_eval:n { \l_svend_addition_first_tl + \l_svend_addition_second_tl }
   }
  \seq_gput_right:Nx \g_svend_randomadditions_seq
   {
    $\l_svend_addition_first_tl+\l_svend_addition_second_tl=\l_svend_addition_total_tl$
   }
  $\l_svend_addition_first_tl+\l_svend_addition_second_tl={}$
 }

\NewDocumentCommand{\printresults}{}
 {
  \par
  {
   \linespread{1.5}\selectfont\noindent
   \seq_map_inline:Nn \g_svend_randomadditions_seq
    {
     \makebox[.2\textwidth][l]
      {
       \makebox[\widthof{$00+00=00$\quad}][r]
        {
         ##1
        }
      }
     \hspace{1pt minus 1pt}
    }
    \par
   }
 }
\ExplSyntaxOff


\begin{document}

\BgThispage
\begin{center}
  \Huge\textbf{Training -- addition}
\end{center}

\randomadditions{100}

\clearpage

\begin{center}
  \Huge\textbf{Results -- addition}
\end{center}

\printresults

\end{document}

enter image description here

egreg
  • 1,121,712
  • Very nice! A small question: How do I get the material of \BgThispage on only the first page and not all pages? Also, can I get only the answers in the result part of the document? – Svend Tveskæg May 07 '17 at 20:31
  • 2
    @SvendTveskæg The part on the background is a completely unrelated question. What you get with \printresults depends on what you store; I'd not list just the number: it would be very difficult for the student to check. – egreg May 07 '17 at 20:41
  • Good point about not just listing the answers. Thanks! – Svend Tveskæg May 08 '17 at 17:33