如何创建涉及伪随机数的二进制算术问题(及相关答案)的多页表?

如何创建涉及伪随机数的二进制算术问题(及相关答案)的多页表?

请考虑以下示例:

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

是否可以自动计算每对\randoms 在添加时的值并在第 3 页和第 4 页(即所有算术问题之后)获得输出?

答案1

(修改代码以包含整数应在 0 到 50 之间的信息;进一步修改使其Name: _______仅显示在第一页;进一步修改以满足 OP 对减法、乘法和除法表的要求;还进行了其他修改以实现漂亮的打印。)

这是一个基于 LuaLaTeX 的解决方案。它生成加法、减法、乘法和除法练习的问题和答案表;总共 16 页。s 的单元格longtable是通过嵌套for循环对生成的。为了提高可读性,每 5 行后插入一些垂直空格。

以下是加法问题页面的前几行:

在此处输入图片描述

以下是相应加法答案页的前几行:

在此处输入图片描述

% !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}

答案2

下面是用纯 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

我们循环将训练文本保存到框中\training,并将答案保存到框中。然后我们通过 将这些框打印到所需的列中。\results\vsplit

答案3

在打印加法之前保存结果。

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

在此处输入图片描述

相关内容