I am writing a book with exercises scattered throughout the text, for which solutions are presented at the end of each chapter.
I want the layout to be as follows:
An exercise
environment is introduced inside the text. A solution to this exercise would later appear in the "Solution to exercises" section at the end of the chapter.
In addition, at the end of the exercise
environment would be a page reference to the page where its solution is labeled.
A visual example (made artificially) is this:
And the last section of the chapter would be
Is it possible to do that using the amsthm
and thmtools
packages?
I would assume that the "Solution on page xx" string would be the value of the postfoothook
key of the \declaretheoremstyle
command of thmtools
, but I don't know how to additionally insert a label
into that value.
Maybe I am going too far, but I am also open for a way to do that using one of the exercise
packages (xsim
looks the most promising one). I just haven't stumbled upon this exact layout.
EDIT:
Since the final document is to be typeset in LyX, I think that a better way to achieve my goal is not to work with xsim
, since for referencing, LyX works with the basic building stones of LaTeX - label
, pageref
and their friends.
I think that an appropriate code to be inserted into postfoothook
and heading
would do the job, but this is where I am asking for help:
The exercises and solutions labels would be in the following format:
ex:ch1:ratio-of-odd-numbers
<->sol:ex:ch1:ratio-of-odd-numbers
ex:ch2:rolling-n-sided-die
<->sol:ex:ch2:rolling-n-sided-die
ex:ch2:the-hotel-problem
<->sol:ex:ch2:the-hotel-problem
What I need is to refer to sol:⟨the exrecise label⟩
inside postfoothook
in exercise, and to refer to ⟨the exrecise label⟩
in the heading of solution.
\begin{exercise}\label{ex:ch1:every-prime}
blah blah
\end{exercise}
for which a solution is in the format
\begin{solution}\label{sol:ex:ch1:every-prime}
Solution to blah blah.
\end{solution}
- To produce the ref "Solution on page xx" I need to concatenate
sol:
with⟨the exrecise label⟩
to produce
\pageref{sol:⟨the exrecise label⟩}
where, in this example, ⟨the exrecise label⟩
is ex:every-prime
(to create \pageref{sol:ex:ch1:every-prime}
).
- To produce the header "Solution to Exercise xx" I need to remove the
sol:
part from thesolution
label to produce\ref{⟨the exrecise label⟩}
, so in this example it would be,\ref{ex:ch1:every-prime}
.
Any help about that?
A MWE:
\documentclass{book}
\usepackage{fontspec,hyperref,amsthm,thmtools}
%%
\declaretheoremstyle[
postfoothook = \rightline{Solution on page \pageref{sol:⟨the exrecise label⟩}
]{exercise}
\declaretheorem[style=exercise]{exercise}
\declaretheoremstyle[
heading = Solution to Exercise \ref{⟨the solution label without "sol:"⟩},
numbered=no,
]{solution}
\declaretheorem[style=solution]{solution}
%%
\begin{document}
\chapter{Prime numbers}
Lorem ipsum
\section{Dolor sit amet}
Consectetur adipiscing elit.
\begin{exercise}[Euclid]{ex:ch1:every-prime}
Prove that for every prime $p$, there is a prime $p^\prime > p$. In particular, the list of primes, $2, 3, 5, 7, \ldots$ , is infinite.
\end{exercise}
\section{Solution to exercises}
\begin{solution}\label{sol:ex:ch1:every-prime}
The proof is straight forward. First note that...
\end{solution}
\end{document}
Edit 2
As a follow up to muzimuzhi Z answer, I would like also to add an "Exercise with no solution" to my book. This environment may be named exercise-nosol
and I want it to share the same counter with the exercise
environment defined in the accepted answer.
How to do that?
That is, how to define the two environments (exercise
with xsim
, exercise-nosol
with thmtool
) so that they share the same counter?
I defined it using thmtools
as follows:
\declaretheoremstyle[
postfoothook = \rightline{(No Solution) $\blacktriangleleft$}
]{exercise-nosol}
\declaretheorem[name=Exercise,numberwithin=chapter,style=exercise]{exercise}