旧的 Lua 代码不再起作用

旧的 Lua 代码不再起作用

考虑以下代码:

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


\usepackage{fontspec}
\setmainfont[
  Extension      = .otf,
  UprightFont    = *-regular,
  BoldFont       = *-bold,
  ItalicFont     = *-italic,
  BoldItalicFont = *-bolditalic,
  Ligatures = {
    TeX,
    CommonOff
  }
]{texgyrepagella}
\usepackage[
  math-style = TeX
]{unicode-math}
\setmathfont{texgyrepagella-math.otf}
\setmathfont[
  version = bold,
  FakeBold = 2
]{texgyrepagella-math.otf}
\usepackage{polyglossia}
\setdefaultlanguage{danish}
\usepackage[
  hmargin = 2.5cm,
  tmargin = 1.3cm,
  bmargin = 2.7cm
]{geometry}
\usepackage{fancyhdr}
\usepackage[
  firstpage = true
]{background}
\usepackage[
  lastpage
]{zref}
\usepackage{
  array,
  longtable
}
\usepackage{luacode}


% parametre
\def\stoersteNaevner{10}
\def\stoersteTaeller{99}
\def\opgaver{2000}
\def\kolonner{5}
% pseudotilfældighedsgeneratorens startinput
\def\startTal{123456789}


% baggrundsmateriale
\backgroundsetup{
  contents = ,
  scale = 1,
  color = black,
  angle = 0,
  opacity = 1,
  position = current page.north west,
  vshift = -2.02cm,
  hshift = 5cm
}

\newcommand*\svar{\fpeval{\stoersteTaeller/\stoersteNaevner}}
\newcommand*\raekker{\fpeval{\opgaver/\kolonner}}

\newcolumntype{R}{>{$}r<{$}}
\newcolumntype{C}{>{${}}c<{{}$}} 
\setlength\extrarowheight{1.28ex}
\setlength\tabcolsep{0pt}

\begin{luacode}
--- variable og hjælpefunktioner ---
n_rows = \raekker -- antal rækker i alt
xtra_sp = "0ex"   -- ekstra mellemrum imellem hver 5. række

--- ekstra mellemrum efter hver 5. række ---
function cell_terminate(i,j) -- i: 1..n_rows; j: 1..5
  if j<5 then 
    tex.sprint ( "&" ) 
  elseif i%5==0 then
    tex.sprint ( "\\\\[" .. xtra_sp .. "]" )
  else 
    tex.sprint ( "\\\\" )
  end
end

--- selve beregningerne foretages og svarene printes ---
function opgaver_dele ()
math.randomseed(\startTal) -- vælger et passende startindput
local x1, x2, y
  for i=1,n_rows do
    for j=1,5 do
      x1 = math.random( \stoersteNaevner ) -- divisor
      x2 = math.random( \svar ) -- quotient ("result")
      y  = x1 * x2
      tex.sprint ( y.."\\div"..x1.."{\\enskip}={}" ) 
      cell_terminate(i,j)
    end
  end
end
function svar_dele ()
math.randomseed(\startTal) -- genbruge det samme startindput
local x1, x2, y
  for i=1,n_rows do
    for j=1,5 do
      x1 = math.random( \stoersteNaevner ) -- divisor
      x2 = math.random( \svar ) -- quotient ("result")
      y  = x1 * x2
      tex.sprint ( y.. "\\div" .. x1 .. "{\\enskip}&=&{\\enskip}" .. x2 ) 
      cell_terminate(i,j)
    end
  end
end
\end{luacode}

% overskrifter og tabeller
\newcommand*\tabelO[1]{%
    \begin{longtable}{@{} *{\fpeval{\kolonner-1}}{R@{\hspace{\fpeval{15/\kolonner}cm}}} R @{}}
        \bigskip\endfirsthead
        \bigskip\bigskip\endhead
        \directlua{opgaver_#1()}
    \end{longtable}}

\newcommand*\tabelS[1]{%
    \begin{longtable}{@{} *{\fpeval{\kolonner-1}}{RCR@{\hspace{\fpeval{12/\kolonner}cm}}} RCR @{}}
        \bigskip\endfirsthead
        \bigskip\bigskip\endhead
        \directlua{svar_#1()}
    \end{longtable}}

\newcommand*\oversO[1]{%
  \clearpage
  \noindent
  Navn: \underline{\hspace{10em}}%
  \hfill{\Huge\textbf{Tabeltræning~A -- #1}}\hfill%
  \phantom{Navn: \underline{\hspace{10em}}}
  \vspace*{-3ex}}

\newcommand*\oversS[1]{%
  \clearpage
  \begin{center}
    \Huge\textbf{Tabeltræning~A -- #1 (svar)}
  \end{center}
  \vspace*{-6ex}}


\begin{document}

\oversO{Dele}
\tabelO{dele}
\oversS{Dele}
\tabelS{dele}

\end{document}

几年前,当我最后一次编译上述代码时,一切都很好,但现在我收到以下错误:

23: bad argument #1 to 'random' (number has no integer representation
)
stack traceback:
    [C]: in function 'math.random'
    [\directlua]:23: in function 'opgaver_dele'
    [\directlua]:1: in main chunk.
\tabelO ...skip \endhead \directlua {opgaver_#1()}
                                                   \end {longtable}
l.157 \tabelO{dele}

我该如何修复代码以使其再次可编译?

答案1

这是由于 Lua 的变更。 中不再允许使用浮点数math.random。使用

math.random( math.floor(\svar) )

两次。

相关内容