# How do I create a Moodle calculated multiple choice question from a tex file?

To create a multiple choice question I have been using the environment

\begin{multi}
...
\end{multi}


What is the environment to use to generate a calculated multiple choice question, that is one with a variable in it?

• welcome on this TeX.SE So that we can help you, can you please post a "Minimal Working Example" (MWE) of your code so we can see packages and code you use? you can also try site:tex.stackexchange.com <search term(s)> to have a visual on specific terms
– JeT
Jul 8 '20 at 8:31

This github project demonstrates how you can use python code within your LaTeX file to generate something that pretty much mimics the effect of calculated questions: you generate a pool of questions out of a prototype.

To push things a bit more, in a development version of the moodle package, I introduced support for moodle tags. This way, when building a quiz in Moodle, you can ask to pick up randomly a question among those that share a specific tag.

I also observed that with LuaTeX's internal scripting capabilities, you can do the same as what can be done using python.

Here is an example code:

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{moodle} % development version 0.8
\begin{document}
\begin{quiz}[tags={calculated}]{Example Quiz}
\directlua{
function clozenum_print(pair,op,result)
tex.print("\\begin{numerical}$"..pair[1].." "..op.." "..pair[2].." =$".."\\item ",result,"\\end{numerical}")
end
function cloze_print(pair,points)
tex.print("\\begin{cloze}[points="..points.."]{Arithmetic Quiz ("..pair[1]..", "..pair[2]..")}Solve the following tasks!\\\\")
clozenum_print(pair,"+",pair[1]+pair[2])
clozenum_print(pair,"-",pair[1]-pair[2])
clozenum_print(pair,"*",pair[1]*pair[2])
if pair[1]/pair[2]==math.floor(pair[1]/pair[2]) then
clozenum_print(pair,":",math.floor(pair[1]/pair[2]))
end
tex.print("\\end{cloze}")
end
for x = 2,4 do
for y = 2,4 do
if x>y then
if x/y==math.floor(x/y) then points=4 else points=3 end
cloze_print({x,y},points)
end
end
end
}
\end{quiz}
\end{document}


the resulting PDF is rendered like this and the resulting XML file is

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was generated on 2020-11-10 by LuaLaTeX -->
<!-- running on Linux with the package moodle v0.8 -->

<quiz>

<question type="category">
<category>
<text>$course$/top/Example Quiz</text>
</category>
</question>

<question type="cloze">
<name format="html">
<text><![CDATA[Arithmetic Quiz (3, 2)]]></text>
</name>
<questiontext format="html">
<text><![CDATA[<p>Solve the following tasks!<BR/> $$3 + 2 =$${1:NUMERICAL:=5:0}$$3 - 2 =$${1:NUMERICAL:=1:0}</p>]]></text>
</questiontext>
<generalfeedback format="html"><text/></generalfeedback>
<penalty>0.10</penalty>
<hidden>0</hidden>
<tags>
<tag><text><![CDATA[calculated]]></text></tag>
</tags>
</question>
<question type="cloze">
<name format="html">
<text><![CDATA[Arithmetic Quiz (4, 2)]]></text>
</name>
<questiontext format="html">
<text><![CDATA[<p>Solve the following tasks!<BR/> $$4 + 2 =$${1:NUMERICAL:=6:0}$$4 - 2 =$${1:NUMERICAL:=2:0}$$4 : 2 =$${1:NUMERICAL:=2:0}</p>]]></text>
</questiontext>
<generalfeedback format="html"><text/></generalfeedback>
<penalty>0.10</penalty>
<hidden>0</hidden>
<tags>
<tag><text><![CDATA[calculated]]></text></tag>
</tags>
</question>
<question type="cloze">
<name format="html">
<text><![CDATA[Arithmetic Quiz (4, 3)]]></text>
</name>
<questiontext format="html">
<text><![CDATA[<p>Solve the following tasks!<BR/> $$4 + 3 =$${1:NUMERICAL:=7:0}$$4 - 3 =$${1:NUMERICAL:=1:0}</p>]]></text>
</questiontext>
<generalfeedback format="html"><text/></generalfeedback>
<penalty>0.10</penalty>
<hidden>0</hidden>
<tags>
<tag><text><![CDATA[calculated]]></text></tag>
</tags>
</question>

</quiz>


Once the questions are imported in the question bank of your Moodle course, you can add edit a quiz to populate it with questions taken randomly from the question bank

If you look at the manual (texdoc moodle in your TeXLive installation, or on CTAN), there is no such thing implemented.

The available questions are just

• cloze
• matching
• essay