0

I am writing some notes which contains question of multiple choice options, each question has 4 options out of which 0,1,2,3, or 4 options may be correct. I am using tasks package for wring options. (I am not using any class because its a type of book and I am using tasks package because I want some options to print in two columns with horizontal labeling.)

The problem I am having is I want to have a correct/true choice option in there and want to display answers at end of each section. I am not able to define a correct/true choice options and not able to print them at the end. I have seen all the questions regarding this but they dont have what I am looking for.

Please help me

The code I have is

    \documentclass{article}
    \usepackage{endnotes}

    \usepackage{tasks}
    \NewTasksEnvironment[style=enumerate, counter-format=($\alph*$),label-width=4ex,item-indent =2em,label-offset=0em]{choices}[\choice](1)
    \NewTasks[style=enumerate, counter-format=($\alph*$),label-width=4ex,item-indent =2em,label-offset=0em]{choices2}[\choice](2)
    \NewTasks[style=enumerate, counter-format=($\alph*$),label-width=4ex,item-indent =2em,label-offset=0em]{choices4}[\choice](4)



    % Define two new counters for ease of using.
    \newcounter{questionnumber}
    \newcounter{choicenumber}[questionnumber]
    % Formatting the counters, this is where you change how the counters
    % appear.
    \renewcommand*\thequestionnumber{\arabic{questionnumber}.}
    \renewcommand*\thechoicenumber{\alph{choicenumber})}

    % Define the \question and \choice commands to be similar to what is 
    % given in the OP
    \newcommand*\question{\item}
    %\newcommand*\choice{\item}

    % Here's a bit of a hack: \endnotetext stores the \meaning of the its argument
    % in the endnotetext file, so the macros aren't expanded. I use \edef to fully
    % expand the current \thechoicenumber, and use \expandafter to stuff it into
    % the argument for \endnotetext. Suggestions for improvements are welcome!
    \newcommand*\entreplace[1]{\endnotetext[\value{questionnumber}]{#1}}
    \newcommand\truechoice[2]{\item #1 \edef\tempchoice{\thechoicenumber} \expandafter\def\expandafter\currentcount\expandafter{\tempchoice \ \ #2} \expandafter\entreplace\expandafter{\currentcount}}

    % Define a choices environment, just a list basically. 
    %\newenvironment{choices}{\begin{tasks}{\thechoicenumber}{\usecounter{choicenumber}}}{\end{tasks}}



    % These are to set up the endnotes. The first makes the endnote marks look 
    % like the question numbering (as opposed to being in superscript). The second
    % sets the endnotes heading to read "Answers". 
    \renewcommand*\makeenmark{\theenmark.~~}
    \renewcommand*\notesname{Answers:}

    \begin{document}

    \section{Section 1}
    \begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
    \question Some question
    \begin{choices}
    \truechoice{True choice}{}
    \choice False choice
    \choice Bad choice
    \choice Bad choice
    \end{choices}
    \end{list}

    % Print the "answers"
    \theendnotes

    \section{Section 2}
    \begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
    \question Some question
    \begin{choices2}
    \choice False choice
    \truechoice{True choice}{}
    \choice Bad choice
    \choice Bad choice
    \end{choices2}
    \end{list}

    % Print the "answers"
    \theendnotes

    \section{Section 3}
    \begin{list}{\thequestionnumber}{\usecounter{questionnumber}}
    \question Some question
    \begin{choices4}
    \choice False choice
    \choice Bad choice
    \truechoice{True choice}{}
    \choice Bad choice
    \end{choices4}
    \end{list}

    % Print the "answers"
    \theendnotes

    \end{document}

This is what I get

enter image description here

1

I guess this is what you meant by

If I do that for more than one sections it does not display different answers. Any thought on how I can fix that?

in a comment to my answer to your previous question “multiple choice questions with correct choice environment” which I didn't understand (because the answers formed a section for themselves…) and you didn't bother to enlighten me. I would have modified my answer there months ago…

Below is the modified version of said answer with the same proposed syntax.

It only requires a little modification, namely to check the section value of the corresponding exercise in the definition of \getanswers:

\ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section} ... \fi

The code below gives:

enter image description here

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

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

\DeclareExerciseProperty{answer}

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

\newcommand*\getanswers{%
  \section*{Answers}
  \def\betweenanswers{\def\betweenanswers{\hspace{2em}}}%
  \ForEachUsedExerciseByID{%
    \ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
      \betweenanswers##3\ExercisePropertyGet{##1}{##2}{answer}%
    \fi
  }%
}

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

\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 $-2$ and $-3$?
    \begin{choice}(4)
      \choice $-6$
      \choice $6$
      \choice $5$
      \choice \answer{$-5$}
    \end{choice}
  \end{exercise}
\end{questions}

\getanswers

\section{Other Problems}
\begin{questions}
  \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}
\end{questions}

\getanswers

\end{document}
| improve this answer | |
  • @cgiender. Sorry I did not saw your comment back then. I just realized that I had missed your comment. In here this is not working 'xsim' package not found. Thank you very much. – Kumarm Apr 28 at 15:06
  • @Kumarm then your tex distribution is quite outdated. xsim was first uploaded to CTAN and added to both MiKTeX and texlive in May 2017, i.e., nearly three years ago… – cgnieder Apr 28 at 15:27
  • @cgiender I have the latest one installed. I see through Tex live utility that it is installed in my system, but still gives me an error – Kumarm Apr 28 at 15:41
  • @Kumarm check your log file which version you are actually using (BTW you are misspelling my user name) – cgnieder Apr 28 at 15:44
  • Sorry for name, I have copied it now. I did get it manage to work it, I downloaded 'xsim' and kept it in the folder. Thanks for editing my code. Its works very well, al most as I wanted. The one last problem I have is some of the questions have 2 or more as correct options. When I do \answer{} for more than one option it override the previous one. I need it to keep both and display both options in the answers. – Kumarm Apr 28 at 15:45

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.