Verbatim inside questions

Version 1 (Alexis Bienvenüe, 09/24/2016 03:08 pm) → Version 2/3 (Alexis Bienvenüe, 09/24/2016 03:08 pm)

h1. Verbatim inside questions

h2. The problem

The @verbatim@ environment interferes with AMC LaTeX package, so that it is impossible to use it inside @question@ or @questionmult@ environments... As an example, the following code won't compile:
<pre>
\begin{question}{program}
What is the return value of the following R code?
\begin{verbatim}
sum(sapply(1:2^2,function(x) { x/2 }))
\end{verbatim}
\begin{choices}[o]
\wrongchoice{2.5}
\correctchoice{5}
\wrongchoice{NaN}
\end{choices}
\end{question}
</pre>

h2. Some solutions

h3. Escaping

For very small texts, it is possible to escape all characters that have a special meaning for LaTeX. However, this can become quickly tedious...
<pre>
\begin{question}{program}
What is the return value of the following R code?
\begin{center}
\texttt{sum(sapply(1:2\textasciicircum 2,function(x) \{ x/2 \}))}
\end{center}
\begin{choices}[o]
\wrongchoice{2.5}
\correctchoice{5}
\wrongchoice{NaN}
\end{choices}
\end{question}
</pre>

h3. UseVerb

You can also record some one-line verbatims with the *@fancyvrb@* @fancyvrb@ package (define your verbatim outside — before — @\onecopy@):
<pre>
\SaveVerb{theRcode}'sum(sapply(1:2^2,function(x) { x/2 }))'

...

\begin{question}{program}
What is the return value of the following R code?
\begin{center}
\UseVerb{theRcode}
\end{center}
\begin{choices}[o]
\wrongchoice{2.5}
\correctchoice{5}
\wrongchoice{NaN}
\end{choices}
\end{question}
</pre>

h3. Verbatim boxes

For larger verbatims, the *@verbatimbox@* @verbatimbox@ package can help you:
<pre>
\begin{myverbbox}{\Rcode}
sum(sapply(1:2^2,function(x) { x/2 }))
\end{myverbbox}

...

\begin{question}{program}
What is the return value of the following R code?
\begin{center}
\Rcode
\end{center}
\begin{choices}[o]
\wrongchoice{2.5}
\correctchoice{5}
\wrongchoice{NaN}
\end{choices}
\end{question}
</pre>