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.

\usepackage{filecontents} % for self-contained example
First Question: Select one of the following solutions:
\item A
\item B
Solution to First Question
Second Question: Select one of the following solutions:
\item C
\item D
Solution to Second Question



        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 =

    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


% comment/uncomment to print solutions



Answer all questions. Total number of questions: \numberofquestions.



%\newpage % all solutions together at the end

  • 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
    Mar 7, 2018 at 18:17
  • What is the question? Your definition of \includeSolution looks solid!
    – cgnieder
    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
    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
    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
    Feb 26, 2020 at 5:51


You must log in to answer this question.

Browse other questions tagged .