1

I am trying to have a document with some exercise and solution. I know there is many packages to do that, and I chose xsim because it fits to many of my criteria.

Now, I would like to had a short solution after each exercise. I found one example given with the documentation of the package that had such shortsolution environment. But I do not know how to make them appear after each exercise.

I tried (without luck) to used a syntax close to the environment solution which can appear after each exercise using the comment \xsimsetup{solution/print = true}.

So here is my MWE:

% http://texwelt.de/wissen/fragen/23968/
\documentclass{article}
\usepackage{xsim}

% new property:
\DeclareExerciseProperty{shortsolution}

% new environment:
\NewDocumentEnvironment{shortsolution}{+b}
  {%
    \edef\ExerciseType{\csname g_xsim_exercise_type_tl\endcsname}%
    \edef\ExerciseID{\csname g_xsim_exercise_id_tl\endcsname}%
    \SetExerciseProperty{shortsolution}{#1}%
  }
  {}

% we'll use a description list for the list of short solutions:
\newcommand\printshortsolutions{%
  \begin{description}
    \ForEachUsedExerciseByType{%
      \GetExercisePropertyT{shortsolution}
        {\item[Short Solution ##3]####1}%
    }%
  \end{description}
}

\xsimsetup{ % This something I added and not working !
   shortsolution/print = true,
}

\begin{document}

\section{Problems}
% set shortsolution through option:
\begin{exercise}[subtitle=Pythagoras]
  This is the first problem.
\end{exercise}
\begin{shortsolution}
  This is a shortsolution to the first problem.
\end{shortsolution}
\begin{solution}
  This is the solution to the first problem.
\end{solution}

\begin{exercise}[subtitle=Another Problem]
  This is the second problem.
\end{exercise}
\begin{solution}
  This is the solution to the second problem.
\end{solution}

% set shortsolution with custom command:
\begin{exercise}[subtitle=Yet Another Problem]
  This is the third problem.
\end{exercise}
\begin{shortsolution}
  This is a shortsolution to the third problem.
\end{shortsolution}
\begin{solution}
  This is the solution to the third problem.
\end{solution}

\section{Shortsolutions}
\printshortsolutions

\section{Solutions}
\printsolutions[headings=false]

\end{document}

And as a bonus question, how to change the layout of the shortsolution environment displayed ?

2
  • If I understand you correctly an additional #1 should do in the environment definition, before or after \SetExerciseProperty{shortsolution}{#1}.
    – cgnieder
    Feb 6, 2022 at 21:48
  • @cgnieder, Yes you understood well. I think the difficulty here is to have this printing being optional.
    – R. N
    Feb 6, 2022 at 22:53

1 Answer 1

1
  1. If you want to typeset the shortsolution where the environment shortsolution is placed in the document just place another #1 in the definition, see the lines marked with <<<<

     \NewDocumentEnvironment{shortsolution}{+b}
       {%
         \edef\ExerciseType{\csname g_xsim_exercise_type_tl\endcsname}%
         \edef\ExerciseID{\csname g_xsim_exercise_id_tl\endcsname}%
         \SetExerciseProperty{shortsolution}{#1}%
         before% <<<<
         #1%     <<<<
         after%  <<<<
       }
       {}
    
  2. How you change the layout here is the same as everywhere else: \textbf{#1} would print it in bold, \textcolor{red}{#1} makes it red (given yuo load the xcolor package in the preamble), etc. The next example would place it in a tcolorbox:

     \usepackage{tcolorbox}
     \NewDocumentEnvironment{shortsolution}{+b}
       {%
         \edef\ExerciseType{\csname g_xsim_exercise_type_tl\endcsname}%
         \edef\ExerciseID{\csname g_xsim_exercise_id_tl\endcsname}%
         \SetExerciseProperty{shortsolution}{#1}%
         \begin{tcolorbox}
           #1
         \end{tcolorbox}
       }
       {}
    

    enter image description here

  3. Making this optional can be done by a simple switch

     \newif\ifshortsolprint
    

    and

     \ifshortsolprint
     \begin{tcolorbox}
       #1
     \end{tcolorbox}
     \fi
    

    which can be controlled via \shortsolprinttrue and \newif\shortsolprintfalse anywhere after the \newif\ifshortsolprint.

    Of course a key for \xsimsetup could also be defined but would require expl3:

     \ExplSyntaxOn
     \bool_new:N \l_xsim_shortsolution_bool
    
     \keys_define:nn {xsim}
       { shortsolution/print .bool_set:N = \l_xsim_shortsolution_bool }
    
     % new environment:
     \NewDocumentEnvironment{shortsolution}{+b}
       {
         \tl_set:NV \ExerciseType \g_xsim_exercise_type_tl
         \tl_set:NV \ExerciseID \g_xsim_exercise_id_tl
         \SetExerciseProperty {shortsolution} {#1}
         \bool_if:NT \l_xsim_shortsolution_bool
           { \begin{tcolorbox} #1 \end{tcolorbox} }
       }
       {}
     \ExplSyntaxOff
    

    This could now be controlled with

     \xsimsetup{ shortsolution/print = true }
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .