4

While experimenting with xsim I found that a basic functionality for me is missing: printing solutions which repeat the exercise's text (btw: styled with tcolorbox). I could emulate a similar behavior that looked rather suboptimal:

exercise

The problem here is that (a) the question is repeated in a box that says "solution" and has a wrong counter and (b) that I can't get the box of the exercise away (tried to \let\tcolorbox\relax, but that failed).

What I want to achieve is the following:

exercise wanted

So the question is: How can I either access the exercises text without formatting (note: I have some verbatim material in my real situation) or drop the tcolorbox around the exercise?

% arara: lualatex
\documentclass{scrbook}
\PreventPackageFromLoading{inputenc} % stop tcb from loading this

\usepackage[most]{tcolorbox}
\usepackage[clear-aux]{xsim}
\DeclareExerciseEnvironmentTemplate{tcb}
{%
    \tcolorbox[breakable,drop shadow,beforeafter skip = .5\baselineskip,title =
        \textbf{\GetExerciseName~\GetExerciseProperty{counter}}%
        \GetExercisePropertyT{subtitle}{ \textit{\PropertyValue}}%
        \IfInsideSolutionF{%
            \GetExercisePropertyT{points}{ % notice the space
                (%
                \PropertyValue
                \IfExerciseGoalSingularTF{points}
                    {\XSIMtranslate{point}}
                    {\XSIMtranslate{points}}%
                )%
            }%
        }%
    ]%
    \IfInsideSolutionT{%
        \GetExercisePropertyT{id}{\printexercise{ltxexercise}{\PropertyValue}}%
    }%
}{\endtcolorbox}
\DeclareExerciseType{ltxexercise}{
    exercise-env=ltxexercise,
    solution-env=ltxsolution,
    exercise-name=\XSIMtranslate{exercise},
    solution-name=\XSIMtranslate{solution},
    exercise-template=tcb,
    solution-template=tcb,
    within=chapter,
}

\begin{document}
\chapter{Exercises}
\begin{ltxexercise}
    Blub?
\end{ltxexercise}
\begin{ltxsolution}
    Under water
\end{ltxsolution}
\begin{ltxexercise}
    Quack?
\end{ltxexercise}
\begin{ltxsolution}
    At the surface
\end{ltxsolution}
\section{Solutions}
\printsolutions[headings=false]
\section{Intended result}
\begin{tcolorbox}[title=Solution for exercise 1]
    \tcbsubtitle{Exercise}
    Blub
    \tcbsubtitle{Solution}
    Under water
\end{tcolorbox}
\end{document}
0

1 Answer 1

5

This should work:

% arara: lualatex
\documentclass{scrbook}
\PreventPackageFromLoading{inputenc} % stop tcb from loading this

\usepackage[most]{tcolorbox}
\usepackage[clear-aux]{xsim}
\DeclareExerciseEnvironmentTemplate{tcb}
{%
    \tcolorbox[breakable,drop shadow,beforeafter skip = .5\baselineskip,title =
        \textbf{\IfInsideSolutionT{Solution for }%
          Exercise~\GetExerciseProperty{counter}}%
        \GetExercisePropertyT{subtitle}{ \textit{\PropertyValue}}%
        \IfInsideSolutionF{%
            \GetExercisePropertyT{points}{ % notice the space
                (%
                \PropertyValue
                \IfExerciseGoalSingularTF{points}
                    {\XSIMtranslate{point}}
                    {\XSIMtranslate{points}}%
                )%
            }%
        }%
    ]%
    \IfInsideSolutionT{%
      \tcbsubtitle{Exercise}
      \input{\jobname-\ExerciseType-\ExerciseID-exercise-body.tex}
      \tcbsubtitle{Solution}
    }
}{\endtcolorbox}
\DeclareExerciseType{ltxexercise}{
    exercise-env=ltxexercise,
    solution-env=ltxsolution,
    exercise-name=\XSIMtranslate{exercise},
    solution-name=\XSIMtranslate{solution},
    exercise-template=tcb,
    solution-template=tcb,
    within=chapter,
}

\begin{document}
\chapter{Exercises}
\begin{ltxexercise}
    Blub?
\end{ltxexercise}
\begin{ltxsolution}
    Under water
\end{ltxsolution}
\begin{ltxexercise}
    Quack?
\end{ltxexercise}
\begin{ltxsolution}
    At the surface
\end{ltxsolution}

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

\end{document}

enter image description here


As of xsim version 0.10 (sep. 2017) this feature is integrated into the package. The following declaration isn't needed anymore and you can directly use \GetExerciseBody{exercise}.

I'll add something like the following to the next version of xsim:

% #1: type
% #2: id
% #3: exercise|solution
\cs_new_protected:Npn \xsim_input:nnn #1#2#3
  {
    \group_begin:
      \__xsim_set_file_signature:nnn {#1} {#2} {#3}
      \file_input:n { \l__xsim_file_path_and_name_tl }
    \group_end:
  }
\cs_generate_variant:Nn \xsim_input:nnn {oo}

% #1: exercise|solution
\NewDocumentCommand \GetExerciseBody {m}
  { \xsim_input:oon { \ExerciseType } { \ExerciseID } {#1} }

Then in the template definition

\input{\jobname-\ExerciseType-\ExerciseID-exercise-body.tex}

can be replaced by

\GetExerciseBody{exercise}
7
  • Btw: How do you save the path internally (I do not like to hardcode my changed path)?
    – TeXnician
    Jul 17, 2017 at 14:51
  • @TeXnician it is an internal variable \l__xsim_file_path_tl. I can add some expl3 code which automatically adapts to the path option.
    – cgnieder
    Jul 17, 2017 at 15:29
  • @TeXnician see the updated answer
    – cgnieder
    Jul 17, 2017 at 15:40
  • Thanks. I hope I remember to remove the workaround after the update ;)
    – TeXnician
    Jul 17, 2017 at 15:43
  • @TeXnician easy: it will throw an error then :)
    – cgnieder
    Jul 17, 2017 at 15:44

You must log in to answer this question.

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