3

I am writing some notes in which I need to write some objective type questions. I also need to have one or two options correct in some questions. How can I define a correct choice environment and print all the answers in the end.

I have looked some of the answers on the stackexchange, however they do not suite my needs. Thanks in advance.

    \documentclass{article}
    \usepackage{tasks}
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice}[\choice](1)
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice2}[\choice](2)
    \NewTasks[style=enumerate,counter-format=($tsk[a]$),label-width=4ex]{choice4}[\choice](4)


    \begin{document}

    \begin{enumerate}
    \item What is the product of $-2$ and $3$?
    \begin{choice}
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
    \item What is the sum of the sides of a polygon called?
    \begin{choice2}
      \choice Leg
      \choice Perimeter
      \choice Area
      \choice Volume
    \end{choice2}
    \item What is the product of $-2$ and $3$?
    \begin{choice4}
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice4}
    \end{enumerate}
    \end{document} 
1
  • There are packages (examdesign) and document classes (exam) which specialize in this sort of thing. Alternatively, one can store information using macros (see \csname) or in the aux file for later retrieval. Jul 2 '19 at 16:02
2

Here is an idea (which also made it as an example into xsim's manual):

The proposed syntax is

\section{Problems}
\begin{questions}
  \begin{exercise}
    What is the product of $-2$ and $3$?
    \begin{choice}(4)
      \choice \answer{$-6$}
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
  \end{exercise}
    ...
\end{questions}

\section{Answers}
\getanswers

It can be obtained with the packages xsim and tasks and the following preamble setup:

\usepackage[no-files]{xsim}% the `no-files` option is not necessary
\usepackage{tasks}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]}
  {}

\DeclareExerciseProperty{answer}

\newcommand*\answer[1]{%
  \expanded{%
    \SetExerciseProperty{answer}
      { (\noexpand\textit{\alph{task}}) \unexpanded{#1}}}%
  #1%
}

\newcommand*\getanswers{%
  \def\betweenanswers{\def\betweenanswers{\hspace{2em}}}%
  \ForEachUsedExerciseByID{%
    \betweenanswers##3\ExercisePropertyGet{##1}{##2}{answer}%
  }%
}

\xsimsetup{
  exercise/template = item,
  exercise/the-counter = \arabic{exercise}.
}

\NewTasksEnvironment[
  label = (\textit{\alph*}) ,
  label-width = 14pt
]{choice}[\choice]

\newenvironment{questions}
   {\itemize}
   {\enditemize}

and gives the following result:

enter image description here

The complete code:

\documentclass{article}
\usepackage[no-files]{xsim}
\usepackage{tasks}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]}
  {}

\DeclareExerciseProperty{answer}

\newcommand*\answer[1]{%
  \XSIMexpandcode{%
    \SetExerciseProperty{answer}
      { (\noexpand\textit{\alph{task}}) \unexpanded{#1}}}%
  #1%
}

\newcommand*\getanswers{%
  \def\betweenanswers{\def\betweenanswers{\hspace{2em}}}%
  \ForEachUsedExerciseByID{%
    \betweenanswers##3\ExercisePropertyGet{##1}{##2}{answer}%
  }%
}

\xsimsetup{
  exercise/template = item,
  exercise/the-counter = \arabic{exercise}.
}

\NewTasksEnvironment[
  label = (\textit{\alph*}) ,
  label-width = 14pt
]{choice}[\choice]

\newenvironment{questions}
   {\itemize}
   {\enditemize}

\begin{document}

\section{Problems}
\begin{questions}
  \begin{exercise}
    What is the product of $-2$ and $3$?
    \begin{choice}(4)
      \choice \answer{$-6$}
      \choice $6$
      \choice $5$
      \choice $-5$
    \end{choice}
  \end{exercise}
  \begin{exercise}
    What is the sum of the sides of a polygon called?
    \begin{choice}(2)
      \choice Leg
      \choice \answer{Perimeter}
      \choice Area
      \choice Volume
    \end{choice}
  \end{exercise}
  \begin{exercise}
    What is the sum of $-2$ and $-3$?
    \begin{choice}(4)
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice \answer{$-5$}
    \end{choice}
  \end{exercise}
\end{questions}

\section{Answers}
\getanswers

\end{document}
2
  • thank you so much for answering this. Actually it still has two problems. 1) I do not want to print what the answer is, I just want to print the correct option. 2) If I do that for more than one sections it does not display different answers. Any thought on how I can fix that?
    – Kumarm
    Nov 14 '19 at 16:13
  • 1) remove \unexpanded{#1} from the definition of \answer. 2) I don't understand what you mean
    – cgnieder
    Feb 20 '20 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.