Note that the ##3
argument is of the form \thechapter.\theexercise
, so we can extract the number of the chapter and check if it is equal to the argument of `\printhints.
Edit
It seems that you can get the value of number of the chapter that the exercise been written in with \ExercisePropertyGet
, so a simpler solution would be
\documentclass[a4paper,openany]{book}
\usepackage{xsim}
\xsimsetup{
exercise/within = chapter,
}
% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcommand\printhints[1]{%
\begin{description}
\ForEachUsedExerciseByType{%
\GetExercisePropertyT{hint}
{%
\ifnum \ExercisePropertyGet{##1}{##2}{chapter-value}=#1
\item[Hint to~##3]####1
\fi
}%
}%
\end{description}
}
\begin{document}
\chapter{Algebra}
\begin{exercise}[subtitle={Real numbers}]
Explain why the real numbers form a field.
\end{exercise}
\begin{solution}
Since addition and multiplication are defined and have the usual properties.
\end{solution}
\begin{exercise}
Explain what is a prime number.
\hint{a natural number greater than 1}
\end{exercise}
\begin{solution}
It is not a product of two smaller natural numbers.
\end{solution}
\chapter{Geometry}
\begin{exercise}[subtitle={Pythagoras' theorem}]
Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
\hint{Draw the altitude from point C, and call H its intersection with the side AB.}
\end{exercise}
\begin{solution}
The proof is easy.
\end{solution}
\begin{exercise}[subtitle={Thales's theorem}]
If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
\end{exercise}
\begin{solution}
Since the sum of the angles in a triangle is equal to $180$\ldots
\end{solution}
\chapter*{Hints and solutions}
\section*{Hints to exercises from chapter 1}
\printhints{1}
\section*{Solutions to exercises from chapter 1}
\printsolutions[headings=false,chapter=1]
\section*{Hints to exercises from chapter 2}
\printhints{2}
\section*{Solutions to exercises from chapter 2}
\printsolutions[headings=false,chapter=2]
\end{document}
With the help of expl3 you can easily generalize \printhints
to get a comma separated list of chapter numbers, instead of one number.
\documentclass[a4paper,openany]{book}
\usepackage{xsim}
\xsimsetup{
exercise/within = chapter,
}
% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\ExplSyntaxOn
\NewDocumentCommand \printhints { m } {
\seq_set_split:Nnn \l_a_seq { , } { #1 }
\begin{description}
\ForEachUsedExerciseByType{
\GetExercisePropertyT{hint}
{
\seq_set_split:Nnn \l_b_seq { . } { ##3 }
\seq_get_left:NN \l_b_seq \l_a_tl
\seq_if_in:NVT \l_a_seq { \l_a_tl }
{
\item[Hint to~##3]####1
}
}
}
\end{description}
}
\ExplSyntaxOff
\begin{document}
\chapter{Algebra}
\begin{exercise}[subtitle={Real numbers}]
Explain why the real numbers form a field.
\end{exercise}
\begin{solution}
Since addition and multiplication are defined and have the usual properties.
\end{solution}
\begin{exercise}
Explain what is a prime number.
\hint{a natural number greater than 1}
\end{exercise}
\begin{solution}
It is not a product of two smaller natural numbers.
\end{solution}
\chapter{Geometry}
\begin{exercise}[subtitle={Pythagoras' theorem}]
Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
\hint{Draw the altitude from point C, and call H its intersection with the side AB.}
\end{exercise}
\begin{solution}
The proof is easy.
\end{solution}
\begin{exercise}[subtitle={Thales's theorem}]
If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
\end{exercise}
\begin{solution}
Since the sum of the angles in a triangle is equal to $180$\ldots
\end{solution}
\chapter*{Hints and solutions}
\section*{Hints to exercises from chapter 1}
\printhints{1}
\section*{Solutions to exercises from chapter 1}
\printsolutions[headings=false,chapter=1]
\section*{Hints to exercises from chapter 2}
\printhints{2}
\section*{Solutions to exercises from chapter 2}
\printsolutions[headings=false,chapter=2]
\section*{Hints to exercises from chapters 1 and 2}
\printhints{1,2}
\end{document}
Another option is that the nth call of \printhints
would print the hints of the nth chapter
\documentclass[a4paper,openany]{book}
\usepackage{xsim}
\xsimsetup{
exercise/within = chapter,
}
% Add hints for the exercises
\DeclareExerciseProperty{hint}
\newcommand\hint[1]{\SetExerciseProperty{hint}{#1}}
\newcounter{hintchapcount}
\newcommand\printhints{%
\stepcounter{hintchapcount}
\begin{description}
\ForEachUsedExerciseByType{%
\GetExercisePropertyT{hint}
{%
\ifnum \ExercisePropertyGet{##1}{##2}{chapter-value}=\value{hintchapcount}
\item[Hint to~##3]####1
\fi
}%
}%
\end{description}
}
\begin{document}
\chapter{Algebra}
\begin{exercise}[subtitle={Real numbers}]
Explain why the real numbers form a field.
\end{exercise}
\begin{solution}
Since addition and multiplication are defined and have the usual properties.
\end{solution}
\begin{exercise}
Explain what is a prime number.
\hint{a natural number greater than 1}
\end{exercise}
\begin{solution}
It is not a product of two smaller natural numbers.
\end{solution}
\chapter{Geometry}
\begin{exercise}[subtitle={Pythagoras' theorem}]
Prove that the area of the square whose side is the hypotenuse is equal to the sum of the areas of the squares on the other two sides.
\hint{Draw the altitude from point C, and call H its intersection with the side AB.}
\end{exercise}
\begin{solution}
The proof is easy.
\end{solution}
\begin{exercise}[subtitle={Thales's theorem}]
If A, B, and C are distinct points on a circle where the line AC is a diameter, the angle ABC is a right angle.
\end{exercise}
\begin{solution}
Since the sum of the angles in a triangle is equal to $180$\ldots
\end{solution}
\chapter*{Hints and solutions}
\section*{Hints to exercises from chapter 1}
\printhints
\section*{Solutions to exercises from chapter 1}
\printsolutions[headings=false,chapter=1]
\section*{Hints to exercises from chapter 2}
\printhints
\section*{Solutions to exercises from chapter 2}
\printsolutions[headings=false,chapter=2]
\end{document}