# Is there no way in Tex to do this?: MCQ using tasks package with answers to print at the end for each section

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.

The code I have is

    \documentclass{article}
\usepackage{endnotes}

\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.

% 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
\renewcommand*\makeenmark{\theenmark.~~}

\begin{document}

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

\theendnotes

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

\theendnotes

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

\theendnotes

\end{document}


This is what I get

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:

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

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

\expanded{%
#1%
}

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

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

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}

\section{Other Problems}
\begin{questions}
\begin{exercise}
What is the sum of the sides of a polygon called?
\begin{choice}(2)
\choice Leg
\choice Area
\choice Volume
\end{choice}
\end{exercise}
\end{questions}


• @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