1

I'm working on a textbook, and I'm trying to use xsim for questions/exercises and their answers.

What I want to do is simple:

  1. Place small question/exercise items throughout each chapter, approximately one per subsection, meaning anywhere from 10-20 per chapter.
  2. Print the questions/exercises in place, i.e. if the end of section 12.3.1 has two questions, those questions must apper at the end of section 12.3.1.
  3. Print the answer to all question/exercises in a special chapter at the end of the book.

EDIT: Here's mockup of what I want:

IN MAIN MATTER:

Chapter 14

14.1 Cats as pets

Text text text...

EXERCISES: 14.1
1. Draw a cat.
2. Bathe a cat.
3. Trace the history of cats through time.

Text text text...

14.2 Frogs as pets

Text text text...

EXERCISES: 14.2
1. Lick a frog.
2. Bathe a frog.
3. Feed a frog.

Text text text...

Chapter 21

21.1 The joys of fish

Text text text...

EXERCISES: 21.1
1. Catch a fish.
2. Draw a fish.

Text text text...

And in a single chapter at the end of the book:

SOLUTIONS TO EXERCISES

Chapter 14

14.1
1. Solution A
2. Solution B
3. Solution C

14.2
1. Solution X
2. Solution Y
3. Solution X


Chapter 15

15.1
1. Solution P
2. Solution Q

The problem is that in order place the answers in their own chapter at the end of the book, the questions must be placed in an xsim "collection" (\collectexercises{foo}...\collectexercisesstop{foo}), but putting them in a collection prevents them from being printed in-place. This makes sense if your goal is to print questions in a separate test document, but that is not what I need to do.

Any suggestions for doing what I want to do? Is xsim capable of this, or have I chosen the wrong tool?

1

You don't need collections in order to achieve what you want. You can use \ForEachUsedExerciseByType to build your own lists. This command maps over all exercises that have been used in a document. Within the argument of the command you can refer to the exercise type with #1 and to the exercise ID with #2. This allows you to check or call the properties of exercises, for example with \ExercisePropertyGet{<type>}{<ID>}{<property>}. (The available commands and properties are all listed in the manual.) With \XSIMprint{exercise|solution}{<type>}{<ID>} you can print the corresponding exercise or solution.

For the example below I define

\newcommand\printsectionexercises{%
  \ForEachUsedExerciseByType{%
    \ifnum\ExercisePropertyGet{##1}{##2}{chapter-value}=\value{chapter}
      \ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
        \XSIMprint{exercise}{##1}{##2}%
      \fi
    \fi
  }%
}

which checks the section value against the current one and prints the corresponding exercise and

\renewcommand\printsolutions{%
  \def\currentchapter{}%
  \def\currentsection{}%
  \def\lastchapter{}%
  \def\lastsection{}%
  \ForEachUsedExerciseByType{%
    \let\lastchapter\currentchapter
    \let\lastsection\currentsection
    \edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
    \edef\currentsection{\ExercisePropertyGet{##1}{##2}{section-value}}%
    \ifx\lastchapter\currentchapter\else
      \section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
    \fi
    \ifx\lastsection\currentsection\else
      \subsection*{Exercises \ExercisePropertyGet{##1}{##2}{section}}
    \fi
    \XSIMprint{solution}{##1}{##2}%
  }%
}

whih checks the chapter and section values and if they have changed insert a suiting heading and prints the solution.

Together with the package setup

\xsimsetup{
  exercise/template = number-only ,
  solution/template = number-only ,
  exercise/within = section ,
  exercise/the-counter = \arabic{exercise}. ,
  exercise/print = false
}

and the custom exercise template

\DeclareExerciseEnvironmentTemplate{number-only}{%
  \par\noindent
  \textbf{\GetExerciseProperty{counter}}%
  \GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}

The exercises then are placed somewhere in a section and at the end of the section we use

\subsection*{Exercises \thesection}
\printsectionexercises

and later in the book

\chapter*{Solutions to the exercises}
\printsolutions

The complete example below gives

enter image description here

enter image description here

enter image description here


The complete code:

\documentclass[twocolumn]{book}
\usepackage{xsim}

\DeclareExerciseEnvironmentTemplate{number-only}{%
  \par\noindent
  \textbf{\GetExerciseProperty{counter}}%
  \GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}

\newcommand\printsectionexercises{%
  \ForEachUsedExerciseByType{%
    \ifnum\ExercisePropertyGet{##1}{##2}{chapter-value}=\value{chapter}
      \ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
        \XSIMprint{exercise}{##1}{##2}%
      \fi
    \fi
  }%
}

\renewcommand\printsolutions{%
  \def\currentchapter{}%
  \def\currentsection{}%
  \def\lastchapter{}%
  \def\lastsection{}%
  \ForEachUsedExerciseByType{%
    \let\lastchapter\currentchapter
    \let\lastsection\currentsection
    \edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
    \edef\currentsection{\ExercisePropertyGet{##1}{##2}{section-value}}%
    \ifx\lastchapter\currentchapter\else
      \section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
    \fi
    \ifx\lastsection\currentsection\else
      \subsection*{Exercises \ExercisePropertyGet{##1}{##2}{section}}
    \fi
    \XSIMprint{solution}{##1}{##2}%
  }%
}

\xsimsetup{
  exercise/template = number-only ,
  solution/template = number-only ,
  exercise/within = section ,
  exercise/the-counter = \arabic{exercise}. ,
  exercise/print = false
}

\begin{document}

\setcounter{chapter}{13}
\chapter{}
\section{Cats as pets}
Text text text\dots
\begin{exercise}
  Draw a cat.
\end{exercise}
\begin{solution}
  Drawing a cat.
\end{solution}

Text text text\dots
\begin{exercise}
  Bathe a cat.
\end{exercise}
\begin{solution}
  Bathing a cat.
\end{solution}

Text text text\dots
\begin{exercise}
  Trace the history of cats through time.
\end{exercise}
\begin{solution}
  Tracing the history of cats through time.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\section{Frogs as pets}
Text text text\dots
\begin{exercise}
  Lick a frog.
\end{exercise}
\begin{solution}
  Licking a frog.
\end{solution}

Text text text\dots
\begin{exercise}
  Bathe a frog.
\end{exercise}
\begin{solution}
  Bathing a frog.
\end{solution}

Text text text\dots
\begin{exercise}
  Feed a frog.
\end{exercise}
\begin{solution}
  Feeding a frog.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\setcounter{chapter}{20}
\chapter{}

\section{The joys of fish}
Text text text\dots
\begin{exercise}
  Catch a fish.
\end{exercise}
\begin{solution}
  Catching a fish.
\end{solution}

Text text text\dots
\begin{exercise}
  Draw a fish.
\end{exercise}
\begin{solution}
  Drawing a fish.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\chapter*{Solutions to the exercises}
\printsolutions

\end{document}
5
  • This is very close to what I'm trying to do. The only issue is how the solutions to the exercises in each chapter are organized and printed. In each chapter, I have 5-10 "bundles" of 3-5 exercises each, with each bundle at the end of a section. In the chapter with the solutions, what I'm going for (sorry if this wan't clear above) is to also organize the solutions by chapter and then bundle, e.g. Solutions to exercises in Chapter 14 (this is a section) // Exercise 14.1 (this is a subsection) // Solution 1... \n Solution 2... \n Solution 3... (ordinary text). Can this be done? – Gil Williams Dec 31 '20 at 9:53
  • Regarding my comment above, doing this would seem to require organizing the "bundles" of questions into something higher-level, I suspect, to achieve this in the body text: Exercises: 14.1 // 1. First exercise here. // 2. Second exercise here. // 3. Third exercise here.. – Gil Williams Dec 31 '20 at 10:39
  • @GilWilliams see my edited answer – cgnieder Dec 31 '20 at 15:21
  • Works perfectly! My heartfelt thanks. – Gil Williams Dec 31 '20 at 15:44
  • @GilWilliams I'll add this to the examples in the manual for the next update of xsim :) – cgnieder Dec 31 '20 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.