# Exsheets package: use the chapter name in chapter-hook option

I am typesetting a problems book with the exsheets package. I want the structure of solutions to repeat the structure of the problems set. Chapter-hook and section-hook options allow to add custom code to the list of solutions when the solutions of a new chapter/section are printed. Here is my MWE:

\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\SetupExSheets{
chapter-hook = \chapter{Solutions to the chapter \thechapter},
section-hook = \section{Solutions to the section \thesection},
}

\begin{document}
\tableofcontents

\part{Problems}
\chapter{Mechanics}
\section{Kinematics}
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}

\section{Dynamics}
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}

\chapter{Electricity}
\section{Electrostatics}
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}

\section{Electrodynamics}
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}

\part{Solutions}
\printsolutions

\end{document}


What I'd like to achieve is to have 1 Solutions to Mechanics instead of current 1 Solutions to the chapter 1, 2 Solutions to Electricity instead of 2 Solutions to the chapter 2 and so on. Any help is appreciated.

A solution via references of package nameref. The chapter/section number is used as label name.

\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\usepackage{nameref}
\SetupExSheets{
chapter-hook = \chapter{Solutions to \nameref{CHAP:\thechapter}},
section-hook = \section{Solutions to \nameref{SEC:\thesection}},
}
\newcommand*{\chaplabel}{\label{CHAP:\thechapter}}
\newcommand*{\seclabel}{\label{SEC:\thesection}}

\begin{document}
\tableofcontents

\part{Problems}
\chapter{Mechanics}\chaplabel
\section{Kinematics}\seclabel
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}

\section{Dynamics}\seclabel
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}

\chapter{Electricity}\chaplabel
\section{Electrostatics}\seclabel
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}

\section{Electrodynamics}\seclabel
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}

\part{Solutions}
\printsolutions

\end{document}


The label settings can be included in \chapter and \section:

• New command \mychapter that adds \chaplabel.
• Saving the old definition of \chapter (e.g. via package letltxmacro) and redefining \chapter to execute the saved version and appends \chaplabel.
• Depending on the needed argument types (star form, optional argument), the (re)definitions are more or less easy to more complicate.

Version, where \chaplabel and \seclabel are used in the <after code> argument of \titleformat, provided by package titlesec:

\documentclass{book}
\makeatletter
\@addtoreset{chapter}{part} % Reset \chapter numbering after each \part
\makeatother
\usepackage{exsheets}
\usepackage{nameref}
\SetupExSheets{
chapter-hook = \chapter{Solutions to \nameref{CHAP:\thechapter}},
section-hook = \section{Solutions to \nameref{SEC:\thesection}},
}
\newcommand*{\chaplabel}{\label{CHAP:\thechapter}}
\newcommand*{\seclabel}{\label{SEC:\thesection}}

\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{1em}%
{}[\chaplabel]
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}%
{}[\seclabel]

\usepackage{etoolbox}
\pretocmd\printsolutions{%
\let\chaplabel\relax
\let\seclabel\relax
}{}{\errmessage{Patching \noexpand\printsolutions failed.}}
\begin{document}
\tableofcontents

\part{Problems}
\chapter{Mechanics}
\section{Kinematics}
\begin{question}
A kinematics problem.
\begin{solution}
Solution to the kinematics problem.
\end{solution}
\end{question}

\section{Dynamics}
\begin{question}
A dynamics problem.
\begin{solution}
Solution to the dynamics problem.
\end{solution}
\end{question}

\chapter{Electricity}
\section{Electrostatics}
\begin{question}
An electrostatics problem.
\begin{solution}
Solution to the electrostatics problem.
\end{solution}
\end{question}

\section{Electrodynamics}
\begin{question}
An electrodynamics problem.
\begin{solution}
Solution to the electrodynamics problem.
\end{solution}
\end{question}

\part{Solutions}
\printsolutions
\end{document}


The trick to avoid the infinite recursion as in the comment is to disable \chaplabel and \seclabel before the chapters and sections of \printsolutions are processed.

Slight variation with a switch \ifwithlabels. Then the label setting can be disabled for unnumbered chapters, e.g.:

[...]
\newif\ifwithlabels
\newcommand*{\chaplabel}{\ifwithlabels\label{CHAP:\thechapter}\fi}
\newcommand*{\seclabel}{\ifwithlabels\label{SEC:\thesection}\fi}

\usepackage{titlesec}
\titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{1em}%
{}[\chaplabel]
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}%
{}[\seclabel]

\usepackage{etoolbox}
\pretocmd\printsolutions{%
\withlabelsfalse
}{}{\errmessage{Patching \noexpand\printsolutions failed.}}

\begin{document}
\tableofcontents

\withlabelstrue
\part{Problems}
[...]

• Thanks, the result is what I was looking for. But could you please elaborate on any way how to append \chaplabel automatically after every \chapter? I tried to do it using titlesec package since I am using it anyway: \titleformat{\chapter}{\normalfont\LARGE\bfseries}{\thechapter}{1em}{}[\chaplabel]. However, it gives an error: ! TeX capacity exceeded, sorry [input stack size=5000]. If I change [\chaplabel] to some text, it compiles without errors and the text appears after every chapter name (but unfortunately also after Contents). – pozitron57 Jun 30 '17 at 14:36