1

I have recently adopted xsim (the successor of exsheets, author Cle­mens Nieder­berger) and had the following question: How can I select SOLUTIONS by ID? I wanted to be able to choose where to print the solution (not necessarily below the question and not necessarily at the end), with questions and solutions stored in external files. I also wanted the solutions to appear in a colored frame.

I quickly found how to select questions by ID (see question 383207) and how to make nice frames with tcolorbox (author Thomas F. Sturm) (see question 380959). But it took me a while to figure out how to select solutions. My workaround below "recycles" the question's ID to refer to the corresponding solution (this works as long as the solution is stored immediately below the question): QUESTION: can I set a UNIQUE ID for each solution?

With my suggested workaround below I can insert questions by ID with the following \includeQuestion{Q001} and, likewise, insert solutions by ID with \includeSolution{Q001}, where Q001 is a question's ID (questions and solutions share an ID, something I am not entirely satisfied with).

My main use case is that I want to be able to omit certain solutions, when they are too similar to previous ones, and be able to insert solutions to other questions when I do not have a dedicated solution (some questions differ only by the value of some parameter and I may not have time to rewrite a model answer for all parameter values). Thus I can insert the Solution to Question 1 below Question 10, preceded by something like "The following solution is for the case a=1. I leave it to you to redo the calculations for a=2."

enter image description here

I also use points and tags, but I have removed them to keep this template focused on the question at hand.

\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{filecontents} % for self-contained example
\begin{filecontents}{QuestionBank.tex}
\begin{question}[ID=Q001]
First Question: Select one of the following solutions:
\begin{enumerate}
\item A
\item B
\end{enumerate}
\end{question}
\begin{answer}
Solution to First Question
\end{answer}
\begin{question}[ID=Q002]
Second Question: Select one of the following solutions:
\begin{enumerate}
\item C
\item D
\end{enumerate}
\end{question}
\begin{answer}
Solution to Second Question
\end{answer}
\end{filecontents}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{xsim}
\usepackage[most]{tcolorbox}

\newcommand*\includeQuestion[1]{%
\XSIMexpandcode{\printexercise{question}{\GetExerciseIdForProperty{ID}{#1}}}%
}

\newcommand*\includeSolution[1]{%
\XSIMexpandcode{\printsolution{question}{\GetExerciseIdForProperty{ID}{#1}}}%
}

\DeclareExerciseEnvironmentTemplate{tcb}
{%
    \tcolorbox[breakable,
        drop shadow,
        beforeafter skip = 1\baselineskip,
        fonttitle   = \bfseries,
        fontupper = \normalsize,
        valign = top,
        colframe = \IfInsideSolutionF{green!20!white}\IfInsideSolutionT{blue!20!white},
        colback = \IfInsideSolutionF{green!3!white}\IfInsideSolutionT{blue!3!white},
        coltext = black,
        coltitle = black,
        boxrule = 1pt,
        width = \linewidth,
        left = 2mm,
        title =
        \IfInsideSolutionT{\XSIMmixedcase{\XSIMtranslate{solution}}~to~}%
        \XSIMmixedcase{\XSIMtranslate{question}}~\GetExerciseProperty{counter}%
    ]%
}{\endtcolorbox}

\DeclareExerciseType{question}{
    exercise-env = question,
    solution-env = answer,
    exercise-name = question, % used with headings=true
    solution-name = solution,  % used with headings=true
    exercise-template = tcb,
    solution-template = tcb
}

\DeclareExerciseCollection{myCollection}



% comment/uncomment to print solutions
%\renewcommand*\includeSolution[1]{}

\begin{document}

\collectexercises{myCollection}
\input{QuestionBank.tex}
\collectexercisesstop{myCollection}

\section*{Instructions}
Answer all questions. Total number of questions: \numberofquestions.

\section*{Questions}
\includeQuestion{Q001}
\includeSolution{Q001}

\includeQuestion{Q002}
\includeSolution{Q002}


%\newpage % all solutions together at the end
%\printsolutions[headings=true]

\end{document}
8
  • CAUTION: I was experiencing a problem with some answers not being printed. It puzzled me for a while. The problem was that I had inserted the solution environment inside the question environment instead of immediately after it.
    – PatrickT
    Commented Mar 7, 2018 at 18:17
  • What is the question? Your definition of \includeSolution looks solid!
    – cgnieder
    Commented May 4, 2018 at 13:25
  • @clemens, thanks for your interest. My question was: can I refer to an answer by ID? So together with \includeSolution{Q001}, I'd have includeAnswer{A001}, as this would give me more control over which answers are included or not included. My workaround has been to define questions and answers as if they were two types of questions and to not use the answer environment associated with each. It's fine, but it's a little wasteful in that I create two "answer-type" environments I don't use. I wrote the question to sound if there was a simpler way to do this.
    – PatrickT
    Commented May 4, 2018 at 19:33
  • So you actually want different IDs for questions and answers instead of having the same? I don't get which advantage that would bring. You can include each question and each answer individually already.
    – cgnieder
    Commented Feb 25, 2020 at 17:33
  • @cgnieder, yes I would love different IDs for questions and answers. It's what I would need for what I'm doing. I have a workaround described above.
    – PatrickT
    Commented Feb 26, 2020 at 5:51

0

You must log in to answer this question.

Browse other questions tagged .