[Tex/LaTex] Versatile question/answer package

answersexercises

I need to produce exercise sheets, and I am looking for a package providing exercise/answer environments. I tried for now exercise and exsheets packages, and I can almost do what I want but there is always a restriction.

I would like to produce a list of exercises such that :

  • An exercise can possibly be just one line "Exercise 1. Blah blah blah?"
  • An exercise can possibly be longer, with multiple questions. In that case the first line would just be "Exercise X. Title" and then there would be one line per question, with proper numeration.
  • I should be able to give the solution for each exercise or question, and choose to display the solution or not.
  • When there are multiple questions, I should be able to give an answer for each question (and not only for the whole exercise). The answer should appear after each question.
  • The exercises and answers are written directly in the same source file.
  • It is possible to customize the way the answer are written (e.g. to add a fbox or color)
  • Ideally, also being able to add some symbol to specify the difficulty, but this is not essential.

What would be, according to you, the best package to do that, or the one requiring as few tricks as possible?

Best Answer

All of the mentioned points should be doable using the xsim package (which is the successor of exsheets). I do not have enough knowledge of other exercise packages to be able to judge if they allow similar thingsā€¦

Here is a suggestion about how you could use xsim to achieve what you want. Hopefully the code is self-explanatory.

\documentclass{article}
\usepackage{xsim,tcolorbox,needspace}

% declare a boolean property:
\DeclareExerciseProperty*{short}
% declare a tag like property:
\DeclareExerciseTagging{level}

% declare a template which typesets exercises differently according to given
% properties:
\DeclareExerciseEnvironmentTemplate{exercise}
  {%
    \renewcommand*\theenumi{\theexercise.\arabic{enumi}}%
    \par\addvspace{\baselineskip}
    \Needspace*{2\baselineskip}
    \noindent
    \GetExercisePropertyT{level}{\marginpar{\sffamily Level: #1}}%
    \textbf{\XSIMmixedcase{\GetExerciseName}~\GetExerciseProperty{counter}.} %
    \GetExercisePropertyT{subtitle}{\textit{#1}}%
    \IfExerciseBooleanPropertyF{short}{\par\noindent}%
  }
  {}

% declare a tcolorbox template for the solutions:
\DeclareExerciseEnvironmentTemplate{solution}
  {\tcolorbox[colback=yellow,colframe=red]}
  {\endtcolorbox}

% declare a user command for short answers:
\NewDocumentCommand\answer{m}{%
  \IfSolutionPrintT{%
    \UseExerciseTemplate{begin}{solution}%
      #1%
    \UseExerciseTemplate{end}{solution}%
  }{}%
}

% setup exercises and solutions:
\SetExerciseParameters{exercise}{
  exercise-template = exercise ,
  solution-template = solution
}

% remove this option to hide the answers:
% \xsimsetup{solution/print=true}

\usepackage{lipsum}

\begin{document}

\begin{exercise}[level=hard,short]
  Just a short exercise
\end{exercise}
\begin{solution}
  The somewhat longer solution to the short exercise. \lipsum[1]
\end{solution}

\begin{exercise}[subtitle=This one has a title,level=easy]
  Answer the following questions.
  \begin{enumerate}
    \item question \answer{answer}
    \item question \answer{answer}
  \end{enumerate}
\end{exercise}

\begin{exercise}
  A long exercise. \lipsum[4]\answer{The answer}
\end{exercise}

\begin{exercise}[level=medium,short]
  Another short exercise.\answer{The answer}
\end{exercise}

\end{document}

This gives

enter image description here

or

enter image description here

depending on wether solution/print=true has been set or not.

Related Question