# Creating a solution label for an exercise using \declaretheoremstyle

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}

1. 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}).

1. To produce the header "Solution to Exercise xx" I need to remove the sol: part from the solution label to produce \ref{⟨the exrecise label⟩}, so in this example it would be, \ref{ex:ch1:every-prime}.

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}

\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}



output:

• I am writing a textbook in Lyx and deciding the package to use for exercises. I do not require the page numbers. I only need to print the answers at the end of the book. Can I use xsim? Please refer to the full question in tex.stackexchange.com/questions/631983/… Jan 29 at 22:44

An xsim attempt, based on the "Example 5: Crossreferencing between problems and answers" shown in package manual.

\documentclass{article}
\usepackage{amssymb}
\usepackage{xsim,lipsum,hyperref}

\DeclareExerciseEnvironmentTemplate{custom}
{%
\IfInsideSolutionTF
{\label{sol:\ExerciseID}}
{\label{ex:\ExerciseID}}%
\paragraph*
{%
\XSIMmixedcase{\GetExerciseName}%
\IfInsideSolutionTF
{
to \GetExerciseParameter{exercise-name}%
~\GetExerciseProperty{counter}%
~(From Page~\pageref{ex:\ExerciseID})
}
{%
~\GetExerciseProperty{counter}%
\GetExercisePropertyT{subtitle}{~(\PropertyValue)}
}%
}%
}
{%
\IfInsideSolutionF
{\par\leavevmode\hfill
Solution on page~\pageref{sol:\ExerciseID}~$\blacktriangleleft$}%
}
\xsimsetup{
exercise/template = custom ,
solution/template = custom ,
}

\begin{document}

\section{Exercises}
\begin{exercise}[subtitle=title]
\lipsum[1][2]
\end{exercise}
\begin{solution}
\lipsum[1][2]
\end{solution}

\begin{exercise}[subtitle=second title]
\lipsum[4]
\end{exercise}
\begin{solution}
\lipsum[4]
\end{solution}

\clearpage
\printsolutions

\end{document}


Update: Support no-solution exercises

To avoid possible incompatibilities, here is an xsim-only attempt.

A new command \IfSolutionEmptyTF is provided which uses xsim internal \xsim_attribute_get:nn. This might not be the most robust way.

I've tried xsim's \IfExistSolutionTF and it seems to not work as I expected. Also probably due to an xsim issue (see cgnieder/xsim#90), currently you have to provide an empty solution environment for each of no-solution exercises, like

\begin{solution} % no empty lines in env contents
\end{solution}


Full example

\documentclass{article}
\usepackage{amssymb}
\usepackage{xsim,lipsum,hyperref}

\ExplSyntaxOn
\cs_generate_variant:Nn \tl_if_empty:nTF {e}

\NewExpandableDocumentCommand{\IfSolutionEmptyTF}{}
{
\tl_if_empty:eTF
{
\exp_args:Ne \xsim_attribute_get:nn
{exercise!\ExerciseID} {solution-body}
}
}
\ExplSyntaxOff

\DeclareExerciseEnvironmentTemplate{custom-exercise}
{%
\label{ex:\ExerciseID}%
\paragraph*
{%
% typset "Exercise <num> (<subtitle>)"
\XSIMmixedcase{\GetExerciseName}%
~\GetExerciseProperty{counter}%
\GetExercisePropertyT{subtitle}{~(\PropertyValue)}
}%
}
{%
\par\leavevmode\hfill
% typeset "Solution on page <page>" or "(No solution)"
\ifcsname r@sol:\ExerciseID\endcsname
Solution on page~\pageref{sol:\ExerciseID}%
\else
(No Solution)%
\fi
~$\blacktriangleleft$%
}

\DeclareExerciseEnvironmentTemplate{custom-solution}
{%
\IfSolutionEmptyTF{}
{
\label{sol:\ExerciseID}
\paragraph*
{%
\XSIMmixedcase{\GetExerciseName}
to \GetExerciseParameter{exercise-name}%
~\GetExerciseProperty{counter}%
~(From Page~\pageref{ex:\ExerciseID})
}%
}
}{%
\par
}
\xsimsetup{
exercise/template = custom-exercise ,
solution/template = custom-solution ,
}

\begin{document}

\section{Exercises}
\begin{exercise}[subtitle=title]
\lipsum[1][2]
\end{exercise}
\begin{solution}
\lipsum[1][2]
\end{solution}

\begin{exercise}[subtitle=no solution]
\lipsum[3][1] aaa
\end{exercise}
% You still need to provide an empty solution environment.
\begin{solution}
\end{solution}

\begin{exercise}[subtitle=third title]
\lipsum[4][1-3]
\end{exercise}
\begin{solution}
\lipsum[4][4-6]
\end{solution}

\begin{exercise}[subtitle=fourth title]
\lipsum[4][1-3]
\end{exercise}
\begin{solution}
\lipsum[4][4-6]
\end{solution}

\clearpage
\printsolutions

\end{document}


• Thanks a lot for your answer. I prefer a way to achieve such a layout by typesetting the document in the LyX editor. I think it is too complicated to get it inside LyX with modules.
– tush
Nov 30, 2021 at 14:40
• I add my upvoted😃 Dec 6, 2021 at 14:28
• @tush The difficulty here is how to make a solution env aware of which exercise it is linked to. And I doubt LyX would preview such utilities successfully. Dec 7, 2021 at 0:56
• @muzimuzhiZ Thanks a lot for your answer! There is only one thing I would like to add to my book. Please see my addition to the question.
– tush
Dec 7, 2021 at 12:10
• @tush Answer updated. Dec 8, 2021 at 4:55

One approach would be to use the exsheets package which conveniently allows the placement of solutions at the end of a chapter with the \printsolutions[chapter] command. We can label each question (and solution) for page numbering and linking using the provided \CurrentQuestionID. I chose to label the questions Q:\CurrentQuestionID and the answers A:\CurrentQuestionID.

In order to style the questions and solutions similar to your example, we define a new command \problem which takes in the <subtitle><question text><solution> and passes it to exsheets's question and solution environment and handles the basic formatting.

Obviously the document needs to be compiled twice for the page references.

\documentclass{book}
\usepackage{wasysym}
\usepackage{exsheets}
\newcommand\problem[3]{
\begin{question}
\textbf{(#1)} \label{Q:\CurrentQuestionID} #2
\begin{flushright} Solution on page \pageref{A:\CurrentQuestionID} \LEFTarrow \end{flushright}
\end{question}
\begin{solution}[name = Solution to exercise]
\textbf{(From Page  \pageref{Q:\CurrentQuestionID})}
\label{A:\CurrentQuestionID} #3
\end{solution}
}

\begin{document}
\chapter{Prime Numbers}
\section{Dolor sit amet}

\problem{Euclid}{
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.
}{The proof is straight forward. First note that...}

\problem{Fermat}{
$a^n+b^n=c^n$}{The proof is too large to fit on this page}

\section{Solutions}
\printsolutions[chapter]

\end{document}


This is doable in LyX. Under Documents>Settings>LaTeX Preamble enter the preamble and while in that settings pane change documentclass to book (or something else with chapters. Then you just enter the \problem in LaTeX code in LyX as shown. The output is the same as above.

• Can I choose not to see the Latex code on your Lyx screen but to see equations in the usual Lyx UI way? Is Lyx fully compatible with xsim? Thank you! Jan 29 at 20:53
• I posted a new question related to the above in tex.stackexchange.com/questions/631983/… Jan 29 at 22:39

With exercise package:

\documentclass{book}
\usepackage{fontspec,amsthm,thmtools}
\usepackage{amsmath, amssymb}
\usepackage{chngcntr}
\usepackage{etoolbox}
\usepackage{mwe}% <--- for testing purpose only

\usepackage{xcolor}
\usepackage[pdfborder={0 0 0}]{hyperref}% you must load it before the exercise package
\usepackage{exercise}
%\counterwithin{Exercise}{chapter} This line if you want exercise 1.1, 1.2, etc. in chapter 1
\renewcounter{Exercise}[chapter]
\renewcommand{\ExerciseName}{Exercise}
\setlength{\ExerciseSkipBefore}{\baselineskip}
\setlength{\ExerciseSkipAfter}{0pt}
\AfterEndEnvironment{Exercise}{\hspace{\fill}
Solution on page \textcolor{red}{\pageref{\ExerciseLabel-Answer}} $\blacktriangleleft$\vspace{\baselineskip}\par}

\begin{document}
\chapter{Prime numbers}
Lorem ipsum
\section{Dolor sit amet}

\begin{Exercise}[title={Euclid},label={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}

\blindtext% <--- for testing purpose only

\begin{Exercise}[title={Another one},label={ex:ch1:second}]
Prove that ducks are the best.
\end{Exercise}
\blindtext% <--- for testing purpose only

\section{Solution to Exercises}
The proof is straight forward. First note that...
\blindtext% <--- for testing purpose only
The proof is straight forward. First note that...
\blindtext% <--- for testing purpose only

\chapter{Second chapter}
Lorem ipsum

\section{A section}

\begin{Exercise}[title={An exercise},label={ex:ch2:an-ex}]
Prove that something is true.
\end{Exercise}

\blindtext% <--- for testing purpose only

\begin{Exercise}[title={Another one again},label={ex:ch2:second}]
Prove that ducks are the best of all.
\end{Exercise}

\section{Solution to Exercises}
\blindtext% <--- for testing purpose only

The proof of the first.

\blindtext% <--- for testing purpose only

The proof of the second.
\end{document}


If the Section with the answers contains only the answers, you can also use answerdelayed and print all the answers together with \shipoutAnswer:

\documentclass{book}
\usepackage{fontspec,amsthm,thmtools}
\usepackage{amsmath, amssymb}
\usepackage{chngcntr}
\usepackage{etoolbox}
\usepackage{mwe}% <--- for testing purpose only

\usepackage{xcolor}
\usepackage[pdfborder={0 0 0}]{hyperref}% you must load it before the exercise package
%\counterwithin{Exercise}{chapter} This line if you want exercise 1.1, 1.2, etc. in chapter 1
\renewcounter{Exercise}[chapter]
\renewcommand{\ExerciseName}{Exercise}
\setlength{\ExerciseSkipBefore}{\baselineskip}
\setlength{\ExerciseSkipAfter}{0pt}
\AfterEndEnvironment{Exercise}{\hspace{\fill}
Solution on page \textcolor{red}{\pageref{\ExerciseLabel-Answer}} $\blacktriangleleft$}

\begin{document}
\chapter{Prime numbers}
Lorem ipsum
\section{Dolor sit amet}

\begin{Exercise}[title={Euclid},label={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}
The proof is straight forward. First note that...

\blindtext% <--- for testing purpose only

\begin{Exercise}[title={Another one},label={ex:ch1:second}]
Prove that ducks are the best.
\end{Exercise}
The proof is straight forward. First note that...
\blindtext% <--- for testing purpose only

\section{Solution to Exercises}

\chapter{Second chapter}
Lorem ipsum

\section{A section}

\begin{Exercise}[title={An exercise},label={ex:ch2:an-ex}]
Prove that something is true.
\end{Exercise}
The proof of the first.

\blindtext% <--- for testing purpose only

\begin{Exercise}[title={Another one again},label={ex:ch2:second}]
Prove that ducks are the best of all.
\end{Exercise}%