带有随机数的工作表生成器

带有随机数的工作表生成器

我想在 Latex 中实现一个具有以下功能的工作表生成器:

  1. 它应包含生成带有随机整数的加、减、乘、除练习的命令。除法练习不应包含余数,并且应在必要时自动设置括号。
  2. 此外,它还应提供一个命令,以生成具有随机小数和 10 的随机倍数(例如 1,52 x 1000)的乘法和除法练习。

这是我迄今为止尝试过的:

\documentclass{article}
\usepackage{tikz}
\usepackage{tasks}
\settasks{label-width=4ex}
\usepackage{ifthen}
\pgfmathsetseed{\number\pdfrandomseed}

\newcommand*{\NewNumbers}{%
    \pgfmathsetmacro{\A}{random(-10, 50)}
    \pgfmathsetmacro{\B}{random(-10, 10)}
}

\newcommand{\addition}{
    \NewNumbers
    \setcounter{firstNumber}{\A}
    \setcounter{secondNumber}{\B}
    \ifthenelse{\A < 0 \AND \B < 0}{                            
        $(\thefirstNumber) + (\thesecondNumber)$}{}
    \ifthenelse{\A < 0 \AND \B > 0}{
        $(\thefirstNumber) + \\thesecondNumber$}{}
    \ifthenelse{\A > 0 \AND \B < 0}{
        $\thefirstNumber + (\thesecondNumber)$}{}
    \ifthenelse{\A > 0 \AND \B > 0}{
        $\thefirstNumber + \thesecondNumber$}{}
}

\newcommand{\division}{
    \NewNumbers
    \setcounter{firstNumber}{\A}
    \setcounter{secondNumber}{\B}
    \multiply \value{firstNumber} by \value{secondNumber}
    \ifthenelse{\A < 0 \AND \B < 0}{                            
    $(\thefirstNumber) : (\thesecondNumber)$}{}
    \ifthenelse{\A < 0 \AND \B > 0}{
    $(\thefirstNumber) : \thesecondNumber$}{}
    \ifthenelse{\A > 0 \AND \B < 0}{
    $\thefirstNumber : (\thesecondNumber)$}{}
    \ifthenelse{\A > 0 \AND \B > 0}{
    $\thefirstNumber : \thesecondNumber$}{}
}

\newcommand{\subtraction}{
    \NewNumbers
    \setcounter{firstNumber}{\A}
    \setcounter{secondNumber}{\B}
    \ifthenelse{\A < 0 \AND \B < 0}{                            
        $(\thefirstNumber) - (\thesecondNumber)$}{}
    \ifthenelse{\A < 0 \AND \B > 0}{
        $(\thefirstNumber) - \thesecondNumber$}{}
    \ifthenelse{\A > 0 \AND \B < 0}{
        $\thefirstNumber - (\thesecondNumber)$}{}
    \ifthenelse{\A > 0 \AND \B > 0}{
        $\thefirstNumber - \thesecondNumber$}{}
}

\newcommand{\multiplication}{
    \NewNumbers
    \setcounter{firstNumber}{\A}
    \setcounter{secondNumber}{\B}
    \ifthenelse{\A < 0 \AND \B < 0}{                            
        $(\thefirstNumber) \cdot (\thesecondNumber)$}{}
    \ifthenelse{\A < 0 \AND \B > 0}{
        $(\thefirstNumber) \cdot \thesecondNumber$}{}
    \ifthenelse{\A > 0 \AND \B < 0}{
        $\thefirstNumber \cdot (\thesecondNumber)$}{}
    \ifthenelse{\A > 0 \AND \B > 0}{
        $\thefirstNumber \cdot \thesecondNumber$}{}
}

\newcounter{firstNumber}
\newcounter{secondNumber}

\begin{document}
    \begin{tasks}(3)
        \task \multiplication
        \task \subtraction
        \task \addition 
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \task \division
        \task \multiplication
        \task \division
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \task \addition
        \task \subtraction
        \task \division
    \end{tasks}
\end{document}

这种方法基本上满足了第一个要求。但是,实际练习和任务命令生成的标签的对齐存在问题。我无法使用任务包提供的选项更改它们之间的距离,实际上我想将每个练习都对齐到同一点。

对于第二个要求,我看不到可能的解决方案,因为我找不到任何使用可以计算的十进制变量的可能性。

我也改编了 Mico 的版本以使用负数,并确保在必要时放置括号。这导致了以下版本运行良好:

% !TeX program = lualatex

%% First, some code from the original posting, streamlined.
\documentclass[a4paper,landscape,12pt]{article}
\usepackage{newpxtext,newpxmath} % newer than mathpazo
\usepackage[hmargin=2.5cm, 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 = 20       -- max integer in addition and subtract. ops
    n3 = -30      -- min integer in addition and subtract. ops
    n4 = -10      -- min integer in multiplication ops.
    n2 = 9       -- max integer in multiplication ops.
    n5 = -30      -- min integer in division ops.
    n6 = 10       -- max integer in division 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(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "+" .. "(" .. x2 .. ")" .."={}" )
    else
    tex.sprint ( x1 .. "+" .. x2 .."={}" ) 
    end
    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(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "+" .. "(" .. x2 .. ")" .."&=&" .. x1 + x2 )
    else
    tex.sprint ( x1 .. "+" .. x2 .."&=&" .. x1 + x2 )
    end
    cell_terminate(i,j)
    end
    end
    end
    
    -- Subtraction
    function questions_Subtraction ()
    math.randomseed(123456789) -- select a suitable seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "-" .. "(" .. x2 .. ")" .."={}" )
    else
    tex.sprint ( x1 .. "-" .. x2 .."={}" )
    end 
    cell_terminate(i,j)
    end
    end
    end
    function answers_Subtraction ()
    math.randomseed(123456789) -- re-use the same seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "-" .. "(" .. x2 .. ")" .."&=&" .. x1 - x2 )
    else
    tex.sprint ( x1 .. "-" .. x2 .."&=&" .. x1 - x2 )
    end 
    cell_terminate(i,j)
    end
    end
    end
    
    -- Multiplication
    function questions_Multiplication ()
    math.randomseed(123456789) -- select a suitable seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n4,n2)
    x2 = math.random(n4,n2)
    if x2<0 then
    tex.sprint ( x1 .. "\\cdot" .. "(" .. x2 .. ")" .. "={}" )
    else
    tex.sprint ( x1 .. "\\cdot" .. x2 .. "={}" )
    end 
    cell_terminate(i,j)
    end
    end
    end
    function answers_Multiplication ()
    math.randomseed(123456789) -- re-use the same seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n4,n2)
    x2 = math.random(n4,n2)
    if x2<0 then
    tex.sprint ( x1 .. "\\cdot" .. "(" .. x2 .. ")" .. "&=&" .. x1*x2 )
    else
    tex.sprint ( x1 .. "\\cdot" .. x2 .. "&=&" .. x1*x2 ) 
    end
    cell_terminate(i,j)
    end
    end
    end
    
    -- Division
    function questions_Division ()
    math.randomseed(123456789) -- select a suitable seed
    local x1, x2, y
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n5,n6) -- divisor
    x2 = math.random(n5,n6) -- quotient ("result")
    y  = x1 * x2
    if x1<0 then
    tex.sprint ( y..":".. "(" .. x1.. ")" .."={}" )
    else
    tex.sprint ( y..":"..x1.."={}" )
    end 
    cell_terminate(i,j)
    end
    end
    end
    function answers_Division ()
    math.randomseed(123456789) -- re-use the same seed
    local x1, x2, y
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n5,n6) -- divisor
    x2 = math.random(n5,n6) -- quotient ("result")
    y  = x1 * x2
    if x1<0 then
    tex.sprint ( y.. ":" .. "(" .. x1 .. ")" .. "&=&" .. x2 )
    else
    tex.sprint ( y.. ":" .. x1 .. "&=&" .. x2 )
    end 
    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}

我也尝试扩展生成除法练习的函数,使其能够生成带有小数和 10 的随机倍数的除法练习。但是,由于我对 Lua 完全不熟悉,所以我无法理解使用此版本时出现的错误:

% !TeX program = lualatex

%% First, some code from the original posting, streamlined.
\documentclass[a4paper,landscape,12pt]{article}
\usepackage{newpxtext,newpxmath} % newer than mathpazo
\usepackage[hmargin=2.5cm, 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 = 20       -- max integer in addition and subtract. ops
    n3 = -30      -- min integer in addition and subtract. ops
    n4 = -10      -- min integer in multiplication ops.
    n2 = 9       -- max integer in multiplication ops.
    n5 = -30      -- min integer in division ops.
    n6 = 10       -- max integer in division 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(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "+" .. "(" .. x2 .. ")" .."={}" )
    else
    tex.sprint ( x1 .. "+" .. x2 .."={}" ) 
    end
    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(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "+" .. "(" .. x2 .. ")" .."&=&" .. x1 + x2 )
    else
    tex.sprint ( x1 .. "+" .. x2 .."&=&" .. x1 + x2 )
    end
    cell_terminate(i,j)
    end
    end
    end
    
    -- Subtraction
    function questions_Subtraction ()
    math.randomseed(123456789) -- select a suitable seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "-" .. "(" .. x2 .. ")" .."={}" )
    else
    tex.sprint ( x1 .. "-" .. x2 .."={}" )
    end 
    cell_terminate(i,j)
    end
    end
    end
    function answers_Subtraction ()
    math.randomseed(123456789) -- re-use the same seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n3,n1)
    x2 = math.random(n3,n1)
    if x2<0 then
    tex.sprint ( x1 .. "-" .. "(" .. x2 .. ")" .."&=&" .. x1 - x2 )
    else
    tex.sprint ( x1 .. "-" .. x2 .."&=&" .. x1 - x2 )
    end 
    cell_terminate(i,j)
    end
    end
    end
    
    -- Multiplication
    function questions_Multiplication ()
    math.randomseed(123456789) -- select a suitable seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n4,n2)
    x2 = math.random(n4,n2)
    if x2<0 then
    tex.sprint ( x1 .. "\\cdot" .. "(" .. x2 .. ")" .. "={}" )
    else
    tex.sprint ( x1 .. "\\cdot" .. x2 .. "={}" )
    end 
    cell_terminate(i,j)
    end
    end
    end
    function answers_Multiplication ()
    math.randomseed(123456789) -- re-use the same seed
    for i=1,nrows do
    for j=1,5 do
    x1 = math.random(n4,n2)
    x2 = math.random(n4,n2)
    if x2<0 then
    tex.sprint ( x1 .. "\\cdot" .. "(" .. x2 .. ")" .. "&=&" .. x1*x2 )
    else
    tex.sprint ( x1 .. "\\cdot" .. x2 .. "&=&" .. x1*x2 ) 
    end
    cell_terminate(i,j)
    end
    end
    end
    
    -- Division
    function questions_Division ()
    math.randomseed(123456789) -- re-use the same seed
    local x1, x2, y, z, w
    for i=1,nrows do
    for j=1,5 do
    w = math.random(0,1)
    if w == 1 then
    x1 = math.random(n5,n6) -- divisor
    x2 = math.random(n5,n6) -- quotient ("result")
    y  = x1 * x2
    else
    z = math.random(1,5)
    x1 = 10^z -- divisor
    x2 = math.random(1, 4000) / 10^z -- quotient ("result")
    y  = x1 * x2
    if x1<0 then
    tex.sprint ( y..":".. "(" .. x1.. ")" .."={}" )
    else
    tex.sprint ( y..":"..x1.."={}" )
    end
    cell_terminate(i,j)
    end
    end
    end
    end
    function answers_Division ()
    math.randomseed(123456789) -- re-use the same seed
    local x1, x2, y, z, w
    for i=1,nrows do
    for j=1,5 do
    w = math.random(0,1)
    if w == 1 then
    x1 = math.random(n5,n6) -- divisor
    x2 = math.random(n5,n6) -- quotient ("result")
    y  = x1 * x2
    else
    z = math.random(1,5)
    x1 = 10^z -- divisor
    x2 = math.random(1, 4000) / 10^z -- quotient ("result")
    y  = x1 * x2
    if x1<0 then
    tex.sprint ( y.. ":" .. "(" .. x1 .. ")" .. "&=&" .. x2 )
    else
    tex.sprint ( y.. ":" .. x1 .. "&=&" .. x2 )
    end
    cell_terminate(i,j)
    end
    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}

相关内容