# Printing solution of mcq using xsim

How to print the solution of multiple choice question in the following example ?

\documentclass{scrartcl}
\usepackage[clear-aux]{xsim}

\usepackage{enumitem,amssymb,fmtcount}
\newlist{choices}{itemize}{1}
\setlist[choices]{label=$\Box$}
\newcommand*\choice{\item}

\DeclareExerciseProperty{choices}
\DeclareExerciseProperty*{multiple}
\DeclareExerciseEnvironmentTemplate{mc}
{%
\UseExerciseTemplate{begin}{default}%
\IfExerciseBooleanPropertyTF{multiple}
{Select one or more correct answers}
{%
\GetExercisePropertyT{choices}
}%
\begin{choices}
}
{%
\end{choices}
\UseExerciseTemplate{end}{default}%
}

\DeclareExerciseType{mc}{
exercise-env = multiplechoice ,
solution-env = correctchoices ,
exercise-name = Question ,
solution-name = Solution ,
exercise-template = mc ,
solution-template = mc ,
counter = exercise
}

\xsimsetup{
exercise/name = Question ,
solution/name = Solution
}

\begin{document}

\section{Questions}
\begin{multiplechoice}[choices=1]
\choice one
\choice two
\choice three
\choice four
\end{multiplechoice}

\begin{exercise}
Answer this question on a separate sheet.
\end{exercise}

\begin{multiplechoice}[multiple]
\choice one
\choice two
\choice three
\choice four
\end{multiplechoice}

\begin{multiplechoice}[choices=2]
\choice one
\choice two
\choice three
\choice four
\end{multiplechoice}

\end{document}


I tried with

%preamble
exercise/print = true,
solution/print = true,

%document
\begin{correctchoices}
\choice two
\end{correctchoices}


But it is giving odd output.

Also, is it possible to mark the answer on the question itself while printing solution?

• As I wrote in my previous answer: Use the correct key (correctchoices/print=true in this case). – TeXnician Apr 15 '19 at 19:05
• even if I give 'correctchoices/print=false', it is still printing the solution. Also the solution is getting printed alongwith the line "Select one or more correct answers" which I don't need. The answer also includes a box. – cosmicraga Apr 16 '19 at 17:17
• Also, is it possible to mark the answer on the question itself while printing solution? – cosmicraga Apr 16 '19 at 17:17
• I am looking for something like this (done with exam class) in the following link tex.stackexchange.com/questions/76416/… ... However in this case, with xsim. – cosmicraga Apr 16 '19 at 17:28

Starting with version 0.17 (2020/02/21) xsim has the exercise property solution which tells xsim to use the same environment body for the exercise and the solution. This is very convenient here. Using \IfInsideSolutionTF then allows us to insert the checkmark where needed:

\documentclass{scrartcl}
\usepackage{xsim}[2020/02/21]

\usepackage{enumitem,fontawesome,fmtcount,multicol}
\newlist{choices}{itemize}{1}
\setlist[choices]{label=\faSquareO}

\newcommand*\correct{\IfInsideSolutionTF{\faCheckSquareO}{\faSquareO}}

\newcommand*\choice{\item}

\DeclareExerciseProperty{choices}
\DeclareExerciseProperty*{multiple}
\DeclareExerciseEnvironmentTemplate{mc}
{%
\UseExerciseTemplate{begin}{default}%
\IfExerciseBooleanPropertyTF{multiple}
{Select one or more correct answers}
{%
\GetExercisePropertyT{choices}
}%
\begin{choices}
}
{%
\end{choices}
\UseExerciseTemplate{end}{default}%
}

\DeclareExerciseType{mc}{
exercise-env = multiplechoice ,
solution-env = correctchoices ,
exercise-name = Question ,
solution-name = Solution ,
exercise-template = mc ,
solution-template = mc ,
counter = exercise
}

\xsimsetup{
exercise/name = Question ,
solution/name = Solution ,
}

\begin{document}

\begin{multicols}{2}
\section{Questions}
\begin{multiplechoice}[choices=1,solution]
\choice one
\choice[\correct] two
\choice three
\choice four
\end{multiplechoice}

\begin{exercise}
Answer this question on a separate sheet.
\end{exercise}

\begin{multiplechoice}[multiple,solution]
\choice[\correct] one
\choice two
\choice three
\choice[\correct] four
\end{multiplechoice}

\begin{multiplechoice}[choices=2,solution]
\choice one
\choice[\correct] two
\choice three
\choice[\correct] four
\end{multiplechoice}

\columnbreak