4

Consider the following code:

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

A few year ago, when I compiled the above code the last time, everything was fine but now I get the following error:

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}

How do I fix the code in order to make it compilable again?

2
  • 1
    use math.random( math.floor(\svar) ) (twice) Commented Nov 1, 2022 at 17:18
  • @UlrikeFischer Brilliant -- thank you! If you create an answer, I'll accept it. Commented Nov 1, 2022 at 19:54

1 Answer 1

5

That is due to a change in Lua. A float is no longer allowed in math.random. Use

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

twice.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .