1

I am creating an assignment for students that would contain some several chapters with theory, exercise and solution. I want to create multiple choice questions using exercise package of latex. I believe this package does not directly support MCQs. I have come across a few pieces of the code that I have combined.

My aim is to create MCQs with one or more than one correct answers such that all answers along with explanations and analysis should appear at the end of the chapter.

If someone suggest other packages/methods, it would be fine too.

\documentclass[a4paper]{amsbook}
\usepackage[margin=1in]{geometry}
\usepackage{mathptmx}
\usepackage[cmintegrals, cmbraces]{newtxmath}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed]{exercise}
\usepackage{environ}

\newlength{\choiceslen}
\newif\ifshowcorrect
\newcounter{choices}
\newcommand{\choicefinal}[1]{%
\ifnum\value{choices}>0 \hfill\fi\egroup
\hspace{0pt}%
\hbox to\choiceslen
\bgroup
\stepcounter{choices}%
\ifcase#1\relax
(\Alph{choices})%
\else
\ifshowcorrect
\expandafter\bfseries
\fi
{(\Alph{choices})}%
\fi\space
}
\newcommand{\choicetemp}[1]{%
\stepcounter{choices}%
\space(\Alph{choices})\cr
}

\NewEnviron{choices}{%
\setcounter{choices}{0}%
\let\choice\choicetemp
\shorthandchoice% <---------- Add \choice shorthand
\settowidth{\choiceslen}{\vbox{\halign{##\hfil\cr\BODY\crcr}}}
\ifdim\choiceslen>.5\textwidth
\setlength{\choiceslen}{\textwidth}%
\else
\ifdim\choiceslen>.25\textwidth
\setlength{\choiceslen}{.5\textwidth}%
\else
\setlength{\choiceslen}{.25\textwidth}%
\fi
\fi
\let\choice\choicefinal
\shorthandchoice% <---------- Add \choice shorthand
\setcounter{choices}{0}%

\begin{flushleft}
\bgroup\BODY\hfill\egroup
\end{flushleft}
}

\newcommand{\shorthandchoice}{%
\let\oldchoice\choice
\renewcommand{\choice}{\oldchoice{0}}
\def\correctchoice{\oldchoice{1}}
}
\parindent 0pt
\linespread{1.1}

\begin{document}
\begin{Exercise}[label={mylabel}]
What is the value of 2+5? Which fruit do you like?(To see the answer click here: \refAnswer{\ExerciseLabel})
\showcorrecttrue
\begin{choices}
\correctchoice apple
\choice banana
\correctchoice orange
\choice pear
\end{choices}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
7 
\end{Answer}

\begin{ExerciseList}
\Exercise What is the value of $\pi$  \showcorrecttrue
\begin{choices}
\correctchoice apple
\choice banana
\correctchoice orange
\choice pear
\end{choices}
\Answer Approximate value is 22/7.
\Exercise What is the value of $e$
\Answer Approximate value is 2.71...
\end{ExerciseList}
\clearpage
\begin{center}
\textbf{\textit{    Answers.}}
\end{center}
\newpage % I've added a new page only to clearly show the hyperlink. Of course, you don't need it in your document.
\shipoutAnswer
\end{document}
2

I'm not sure this is what you want to achieve, but you could use tasks package.

\documentclass[a4paper]{amsbook}
\usepackage[margin=1in]{geometry}
\usepackage{mathptmx}
\usepackage[cmintegrals, cmbraces]{newtxmath}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed, lastexercise]{exercise}
\usepackage{tasks}
\settasks{
    counter-format = {(tsk[A])},
    label-width=1.4em
    }

\begin{document}
\chapter{Exercises}
\begin{Exercise}[label={mylabel}]
What is the value of 2+5? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2
\task 5
\task\label{correctchoice}7
\task 3
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correctchoice} 7
\end{Answer}
\begin{Exercise}[label={mysecondlabel}]
Which fruit do you like? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task\label{apple}apple
\task banana
\task\label{orange}orange
\task pear
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{apple} apple or \ref{orange} orange 
\end{Answer}
\begin{Exercise}[label={mythirdlabel}]
What is the value of $\pi$? (To see the answer click here: \refAnswer{\ExerciseLabel})
\begin{tasks}(4)
\task 2.15
\task 1.31
\task 4.3
\task\label{correct}3.14
\end{tasks}
\end{Exercise}
\begin{Answer}[ref=\ExerciseLabel]
\ref{correct} 3.14
\end{Answer}
\chapter{Answers}
\shipoutAnswer
\end{document}

enter image description here

enter image description here

  • Thanks CarLaTeX, I think that should work. I shall check this for options that would fill half of the textwidth or so. Thank you. – Parag Mar 3 at 9:18
  • @Parag You're welcome. Let me know if you have any problem. – CarLaTeX Mar 3 at 9:23
  • CarLaTeX Hi, Is there a possibility to auto-label exercise? – Parag Apr 19 at 10:12
  • @Parag I don't think there is, at least not with simple instruction. It's better if you ask another question. – CarLaTeX Apr 19 at 10:55
  • Thanks for the quick response. I thought something like this might be feasible. tex.stackexchange.com/questions/285211/… – Parag Apr 19 at 11:09

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.