Verbatim inside questions

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

1 1
h1. Verbatim inside questions
2 1
3 1
h2. The problem
4 1
5 1
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:
6 1
<pre>
7 1
\begin{question}{program}
8 1
  What is the return value of the following R code?
9 1
  \begin{verbatim}
10 1
    sum(sapply(1:2^2,function(x) { x/2 }))
11 1
  \end{verbatim}
12 1
  \begin{choices}[o]
13 1
    \wrongchoice{2.5}
14 1
    \correctchoice{5}
15 1
    \wrongchoice{NaN}
16 1
  \end{choices}
17 1
\end{question}
18 1
</pre>
19 1
20 1
h2. Some solutions
21 1
22 1
h3. Escaping
23 1
24 1
For very small texts, it is possible to escape all characters that have a special meaning for LaTeX. However, this can become quickly tedious...
25 1
<pre>
26 1
\begin{question}{program}
27 1
  What is the return value of the following R code?
28 1
  \begin{center}
29 1
    \texttt{sum(sapply(1:2\textasciicircum 2,function(x) \{ x/2 \}))}
30 1
  \end{center}
31 1
  \begin{choices}[o]
32 1
    \wrongchoice{2.5}
33 1
    \correctchoice{5}
34 1
    \wrongchoice{NaN}
35 1
  \end{choices}
36 1
\end{question}
37 1
</pre>
38 1
39 1
h3. UseVerb
40 1
41 1
You can also record some one-line verbatims with the @fancyvrb@ package (define your verbatim outside — before — @\onecopy@):
42 1
<pre>
43 1
\SaveVerb{theRcode}'sum(sapply(1:2^2,function(x) { x/2 }))'
44 1
45 1
...
46 1
47 1
\begin{question}{program}
48 1
  What is the return value of the following R code?
49 1
  \begin{center}
50 1
    \UseVerb{theRcode}
51 1
  \end{center}
52 1
  \begin{choices}[o]
53 1
    \wrongchoice{2.5}
54 1
    \correctchoice{5}
55 1
    \wrongchoice{NaN}
56 1
  \end{choices}
57 1
\end{question}
58 1
</pre>
59 1
60 1
h3. Verbatim boxes
61 1
62 1
For larger verbatims, the @verbatimbox@ package can help you:
63 1
<pre>
64 1
\begin{myverbbox}{\Rcode}
65 1
sum(sapply(1:2^2,function(x) { x/2 }))
66 1
\end{myverbbox}
67 1
68 1
69 1
...
70 1
71 1
\begin{question}{program}
72 1
  What is the return value of the following R code?
73 1
  \begin{center}
74 1
    \Rcode
75 1
  \end{center}
76 1
  \begin{choices}[o]
77 1
    \wrongchoice{2.5}
78 1
    \correctchoice{5}
79 1
    \wrongchoice{NaN}
80 1
  \end{choices}
81 1
\end{question}
82 1
</pre>