2

I am writing a book and I want the book to have Exercises at the end of each chapter and Solutions at the end. I'm using "xsim" to typeset the Exercises and Solutions.

I've started my MWE with an example file "xsim.texsx-576998", but in the MWE output (here), the instruction lines "In Exercise 1-2, ..." and "In Exercise 3-4, ..." don't show up at the place I want them to show up. I want them to show up like

In Exercise 1--2, solve for x.

1. x - 1 = 0
2. x^2 - 3x + 2 = 0

In Exercise 3--4, solve for y.

3. y^3 - 6 y^2 + 11 y - 6 = 0
4. y^4 - 10 y^3 + 35 y^2 - 50 y + 24 = 0

Questions

How can I make the Instructions appear between different exercises (see above)?

MWE

% TeXlive 2020
\documentclass[a4paper]{book}
\usepackage[margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{xsim}

% -----from xsim.texsx-576998-----
\DeclareExerciseEnvironmentTemplate{number-only}{%
    \par\noindent
    \textbf{\GetExerciseProperty{counter}}%
    \GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}

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

\renewcommand\printsolutions{%
    \def\currentchapter{}%
    \def\lastchapter{}%
    \ForEachUsedExerciseByType{%
        \let\lastchapter\currentchapter
        \edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
        \ifx\lastchapter\currentchapter\else
        \section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
        \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}
\chapter{ChapterOne}

\section{SectionOneOne}
\lipsum[1-1]

\noindent In Exercise 1--2, solve for \(x\).

\begin{exercise}
\(x - 1 = 0\)
\end{exercise}

\begin{solution}
\(x = 1\)
\end{solution}

\begin{exercise}
    \(x^2 - 3x + 2 = 0\)
\end{exercise}

\begin{solution}
    \(x = 1, 2\)
\end{solution}

\noindent In Exercise 3--4, solve for \(y\).

\begin{exercise}
    \(y^3 - 6 y^2 + 11 y - 6 = 0\)
\end{exercise}

\begin{solution}
    \(y = 1, 2, 3\)
\end{solution}

\begin{exercise}
    \(y^4 - 10 y^3 + 35 y^2 - 50 y + 24 = 0\)
\end{exercise}

\begin{solution}
    \(y = 1, 2, 3, 4\)
\end{solution}

\subsection*{Exercises}
\printsectionexercises

\chapter*{Solutions to the exercises}
\printsolutions

\end{document}

MWE Output enter image description here

1 Answer 1

0

Here is an idea which uses a new exercise property instruction that is printed in the exercise list before the exercise (if the property is given) and two new commands \This and \ExerciseRef which get the counter values of the current (\This) or a referenced exercise by refereincing the specified exercise ID:

\DeclareExerciseProperty{instruction}
\newcommand\ExerciseRef[1]{%
  \expanded{\noexpand\ExercisePropertyGet{exercise}{\GetExerciseIdForProperty{ID}{#1}}{counter-value}}%
}
\newcommand\This{\GetExerciseProperty{counter-value}}

This is now used as follows:

\begin{exercise}[instruction={In Exercises \This--\ExerciseRef{last:x}, solve for \(x\).}]
  \(x - 1 = 0\)
\end{exercise}
\begin{solution}
  \(x = 1\)
\end{solution}
\begin{exercise}
  \(3x - 12 = 0\)
\end{exercise}
\begin{solution}
  \(x = 4\)
\end{solution}
\begin{exercise}[ID=last:x]
  \(x^2 - 3x + 2 = 0\)
\end{exercise}
\begin{solution}
  \(x = 1, 2\)
\end{solution}

enter image description here


The complete code:

\documentclass[a4paper]{book}
\usepackage[margin=0.75in]{geometry}
\usepackage{lipsum}
\usepackage{xsim}

% -----from xsim.texsx-576998-----
\DeclareExerciseEnvironmentTemplate{number-only}{%
  \par\noindent
  \textbf{\GetExerciseProperty{counter}}%
  \GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}

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

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

\DeclareExerciseProperty{instruction}
\newcommand\ExerciseRef[1]{%
  \expanded{%
    \noexpand
    \ExercisePropertyGet{exercise}{\GetExerciseIdForProperty{ID}{#1}}{counter-value}%
  }%
}
\newcommand\This{\GetExerciseProperty{counter-value}}

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

\begin{document}

\chapter{ChapterOne}
\section{SectionOneOne}
\lipsum[1-1]

\begin{exercise}[instruction={In Exercises \This--\ExerciseRef{last:x}, solve for \(x\).}]
  \(x - 1 = 0\)
\end{exercise}
\begin{solution}
  \(x = 1\)
\end{solution}
\begin{exercise}
  \(3x - 12 = 0\)
\end{exercise}
\begin{solution}
  \(x = 4\)
\end{solution}
\begin{exercise}[ID=last:x]
  \(x^2 - 3x + 2 = 0\)
\end{exercise}
\begin{solution}
  \(x = 1, 2\)
\end{solution}

\begin{exercise}[instruction={In Exercises \This--\ExerciseRef{last:y}, solve for \(y\).}]
  \(y^3 - 6 y^2 + 11 y - 6 = 0\)
\end{exercise}
\begin{solution}
  \(y = 1, 2, 3\)
\end{solution}

\begin{exercise}[ID=last:y]
  \(y^4 - 10 y^3 + 35 y^2 - 50 y + 24 = 0\)
\end{exercise}
\begin{solution}
  \(y = 1, 2, 3, 4\)
\end{solution}

\subsection*{Exercises}
\printsectionexercises

\chapter*{Solutions to the exercises}
\printsolutions

\end{document}

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.