rr-exams

R/exams package: Using verbatim (code blocks with linebreaks) as answer within answer list


I tried to create a simple question using code blocks with {verbatim} environment and compile that to PDF. But the problem is that the line breaks are ignored and the output code block becomes single line.

\begin{question}
How can I retrieve only the \texttt{id} and \texttt{name} columns
from the \texttt{user} table in a SQL database?

\begin{answerlist}
\item \begin{verbatim}
SELECT id, name
FROM user;
\end{verbatim}
\item \begin{verbatim}
SELECT *
FROM user;
\end{verbatim}
\end{answerlist}
\end{question}

I assume the {answerlist} has some preprocessing which trims away the line breaks, is there any way to display code blocks as alternatives for schoice questions?

I also tried to use markdown ``` but that doesn't work either.

Inspecting the intermediate exercise1.tex from texdir I can see the line breaks are trimmed away:

%%%%%%%%%%%%%%%%%%
%% exercise1.tex
%%%%%%%%%%%%%%%%%%
\begin{question}
How can I retrieve only the \texttt{id} and \texttt{name} columns
from the \texttt{user} table in a SQL database?

\begin{answerlist}
  \item \begin{verbatim} SELECT id, name FROM user; \end{verbatim}
  \item \begin{verbatim} SELECT * FROM user; \end{verbatim}
\end{answerlist}
\end{question}

Solution

  • TL;DR You are correct: It is not possible to include code with line breaks within the answerlist because the linebreaks are trimmed.

    Background: The reason for this is that multiline expressions, notably verbatim code chunks, do not render well in some of the supported learning management systems.

    Alternatives: Either include code chunks before the answer list or turn them into images and include those.

    Examples: Both in R/Markdown format because I feel this is slightly easier to set up - but R/LaTeX would also be possible.

    First, this is an exercise, say sql1.Rmd which presents the code chunks first and then a short list of questions. Below is the output from exams2webquiz("sql1.Rmd") from exams2forms.

    Question
    ========
    How can I retrieve only the `id` and `name` columns from the `user` table in a
    SQL database?
    
    Code A:
    
    ```
    SELECT id, name
    FROM user;
    ```
    
    Code B:
    
    ```
    SELECT *
    FROM user;
    ```
    
    Answerlist
    ----------
    * Code A
    * Code B
    
    Meta-information
    ================
    exname: SQL select
    extype: schoice
    exsolution: 10
    

    Screenshot from exams2webquiz("sql1.Rmd")

    Second, this is an alternative exercise, say sql2.Rmd which renders the code chunks into PNG images and uses these in the list of questions. Again, the output from exams2webquiz("sql2.Rmd") is shown below.

    ```{r, include = FALSE}
    code <- list(
    
    "SELECT id, name
    FROM user;",
    
    "SELECT *
    FROM user;"
    
    )
    
    for(i in seq_along(code)) tex2image(
      c("\\begin{verbatim}", code[[i]], "\\end{verbatim}"),
      dir = ".", name = LETTERS[i])
    ```
    
    Question
    ========
    How can I retrieve only the `id` and `name` columns from the `user` table in a
    SQL database?
    
    Answerlist
    ----------
    * ![](A.png){width=25%}
    * ![](B.png){width=25%}
    
    Meta-information
    ================
    exname: SQL select
    extype: schoice
    exsolution: 10
    

    Screenshot from exams2webquiz("sql2.Rmd")

    Caveats: The rendering of text into code chunks takes much longer and you would likely have to tweak the arguments of tex2image(...) more, e.g., the width of the images so that the code is of equal size. Finally, the vertical alighnment of the code may look awkward depending on PDF vs. HTML output and require more tweaks.