Search code examples
rpackager-exams

How to code a question with multiple correct string answers using 'exams' package in R?


I want to create a question with multiple correct string answers in 'exams' package in R. For example, consider simple Excel-related question. Let's say the following Excel formula is the answer to a question and can be written as a string: "=A2*A3+B2*B3". However, this is not a unique way of writing the formula as there are multiple possible ways of writing the same formula in Excel. For example, the above formula can also be written as "=B2*B3+A2*A3". In latex format, we will leave the empty cell to write the answer with the expression: ~##ANSWER1##~. How can we code this (which accepts both answers as correct) using 'exams' package? I tried to write the code as follows but it does not work.

\begin{question}
Write the correct formula.
\begin{tabular}{ |l|l|l|l|}
   & A  & B & C              \\ 
1  &    &   &                \\
2  &    &   &                \\
3  &    &   & Enter formula here:  \\
4  &    &   & ~##ANSWER1##~  \\
5  &    &   &                \\
\end{tabular}
\end{question}

\begin{solution}

<<echo=FALSE, results=hide>>=
Answer1 <- c("=A2*A3+B2*B3")
Answer2 <- c("=B2*B3+A2*A3")
soln <- c(Answer1, Answer2)
@    
\begin{answerlist}
\item \Sexpr{soln}
\end{answerlist}

\end{solution}

\exname{multiple correct string answers}
\extype{cloze}
\exsolution{\Sexpr{soln}}
\exclozetype{string}

Solution

  • Following Achim's suggestions, I corrected the code below:

    \begin{question}
    Write the correct formula.
    \begin{tabular}{ |l|l|l|l|}
       & A  & B & C              \\ 
    1  &    &   &                \\
    2  &    &   &                \\
    3  &    &   & Enter formula here:  \\
    4  &    &   & ~##ANSWER1##~  \\
    5  &    &   &                \\
    \end{tabular}
    \end{question}
    
    \begin{solution}
    
    <<echo=FALSE, results=hide>>=
    Answer1 <- c("=A2*A3+B2*B3")
    Answer2 <- c("=B2*B3+A2*A3")
    soln <- "1:SHORTANSWER:%100%=A2*A3+B2*B3~%100%=B2*B3+A2*A3"
    @    
    \begin{answerlist}
    \item \Sexpr{soln}
    \end{answerlist}
    
    \end{solution}
    
    \exname{multiple correct string answers}
    \extype{cloze}
    \exsolution{\Sexpr{soln}}
    \exclozetype{verbatim}