Generating math exercises for kids with LuaLaTeX

Arno T. has sent me a nice piece of LuaLaTeX code that does more or less the same thing as my Python code. I guess, I should have a closer look at LuaLaTeX.

\documentclass{scrartcl}
\usepackage{booktabs}
\usepackage{luacode}
\usepackage{longtable}
 
\begin{document}
\begin{longtable}{rcrcl}
\toprule
\luaexec{
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
random = math.random
tp = tex.print
%
min = 1
max = 10
math.randomseed(os.time())
for i = 1,20 do
a = random(min,max)
b = random(min,max)
op = random(1,4)
if (op==1) then
c = a+b
tp(a.."&+&"..b.."&=&"..c.."\\\\")
elseif (op==2) then
c = a-b
tp(a.."&-&"..b.."&=&"..c.."\\\\")
elseif (op==3) then
c = a*b
tp(a.."&*&"..b.."&=&"..c.."\\\\")
elseif (op==4) then
c = a/b
tp(a.."&/&"..b.."&=&"..round(c,3).."\\\\")
end
end
tex.print("\\bottomrule")
}
\end{longtable}
\end{document}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website