[Tex/LaTex] cloze multichoice question in the latex using the package ‘moodle’

compilation errorequationsmoodle;xml

I use both Moodle and latex to write all my quizzes. but, I use latex to generate formula rich questions in moodle. The latex package Moodle can do this. Then I import it as XML file to moodle. This works perfectly. Before import the quiz the latex compile the code and produce a PDF so can make sure everything ok. It works well for the close quiz question as well. But, when I have the all the question in the close quiz the 'multichoice' then when I import it as an xml then I get error message although latex compile it without any error.
appreciate any help

Latex code:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{moodle}
\begin{document}
\begin{quiz}{MRTS}
\begin{cloze}{MRTS Q1}\\
A production function is given as $ Q=15\sqrt{KL}$. Q=quantity of output, L = the units of labour, K = the units of capital. The marginal product of labour, $MP_L$ of this function is:
\begin{multi}[horizontal]
\item*$7.5K^{0.5}L^{-0.5}$
\item $7.5K^{-0.5}L^{0.5}$
\item $K^{0.5}L^{-0.5}$
\item $K^{-0.5}L^{0.5}$
\item $15K^{0.5}L^{-0.5}$
\item $15K^{-0.5}L^{0.5}$
\end{multi}
A production function is given as $ Q=15\sqrt{KL}$. Q=quantity of output, L = the units of labour, K = the units of capital. The marginal product of labour, $MP_K$ of this function is:? 
\begin{multi}[horizontal]
\item $7.5K^{0.5}L^{-0.5}$
\item *$7.5K^{-0.5}L^{0.5}$
\item $K^{0.5}L^{-0.5}$
\item $K^{-0.5}L^{0.5}$
\item $15K^{0.5}L^{-0.5}$
\item $15K^{-0.5}L^{0.5}$
\end{multi}
A production function is given as $ Q=15\sqrt{KL}$. Q=quantity of output, L = the units of labour, K = the units of capital.The marginal rate of technical substitution associated with this production function is?\\ Note: $MRTS=\displaystyle -\frac{\frac{\partial Q}{\partial L}}{\frac{\partial Q}{\partial K}}$
\begin{multi}[horizontal]
\item $\frac{K}{L}$
\item*$-\frac{K}{L}$
\item  $\sqrt{\frac{K}{L}}$
\item  $\sqrt{\frac{L}{K}}$
\item $-\sqrt{\frac{K}{L}}$
\item $-\sqrt{\frac{L}{K}}$
\end{multi}
\end{cloze}
\end{quiz}
\end{document}

Moodle output

Best Answer

Unfortunately, I suspect it is a bug/limitation of the Cloze type of questions. This MWE

%
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{moodle}

\begin{document}
\begin{quiz}{Test cloze} 
\begin{cloze}{Multiple questions}

    A $x$ is

    \begin{multi}[vertical]
        One two three
        \item*  one $u^1$
        \item  two
        \item three
    \end{multi}
\end{cloze}

\end{quiz}
\end{document}

works ok, and give the expected result in Moodle:

enter image description here

But if you add braces around the exponent:

\begin{document}
\begin{quiz}{Test cloze} 
\begin{cloze}{Multiple questions}

    A $x$ is

    \begin{multi}[vertical]
        One two three
        \item*  one $u^{1}$
        \item  two
        \item three
    \end{multi}
\end{cloze}

\end{quiz}
\end{document}

Moodle fails importing it with:

enter image description here

Now, the only difference between the two XML files generated is this one:

[romano:~/tmp] % diff quiz-hr-moodle\ \(copy\).xml quiz-hr-moodle.xml          
15c15
<     <text><![CDATA[<p></P><P>A \(x\) is </P><P>One two three {1:MULTICHOICE_V:=one \(u^{1}\)~two~three}</p>]]></text>
---
>     <text><![CDATA[<p></P><P>A \(x\) is </P><P>One two three {1:MULTICHOICE_V:=one \(u^1\)~two~three}</p>]]></text>

and so it seems that the parser of Moodle get confused by the closing brace in the formula. I really do not know how to escape it, and even if the problem is recognized in the Moodle doc, it seems that simply it will not work:

enter image description here

So basically my takeout is that you can't use formulas in answers in cloze questions. The manual says you can:

enter image description here

...but I didn't manage to get it going. Notice that if you go in the XML file and escape the { } with \{and \} then the XML file is accepted, but the formula is broken.

This does not happen in normal (no cloze) multi questions, fortunately.

So my stopgap solution would be to use Unicode formulas directly typed there, and switch to lualatex for the compilation.

Another stopgap solution is to use images for your formulas:

%
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{moodle}

\begin{document}
\begin{quiz}{Test cloze} 
    \begin{cloze}{one cloze}
    A $x$ is

    \begin{multi}[vertical]
        One two three
    \item*  one \includegraphics[height=4ex]{formula1.png}
        \item  two
        \item three
    \end{multi}
\end{cloze}
\end{quiz}
\end{document}

will render as:

Output with embedded graph

Not ideal (can be probably made better with a bit of transparency or whatever) but if you need it...

Related Question