| 1 |
% https://tex.stackexchange.com/q/580636/ |
| 2 |
\documentclass{article} |
| 3 |
\usepackage[use-files]{xsim} |
| 4 |
|
| 5 |
\usepackage[colorlinks,allcolors=blue]{hyperref} |
| 6 |
|
| 7 |
\DeclareExerciseProperty{hints} |
| 8 |
\newcounter{hint} |
| 9 |
\renewcommand\theHhint{\ExerciseID.\arabic{hint}} |
| 10 |
\newcounter{hints} |
| 11 |
\newcounter{step} |
| 12 |
|
| 13 |
\NewDocumentEnvironment{hint}{}{% |
| 14 |
\stepcounter{hint}% |
| 15 |
\XSIMsetfilebegin{% |
| 16 |
\par |
| 17 |
\refstepcounter{hint}\label{hint:\ExerciseID:\arabic{hint}}% |
| 18 |
\noindent\textbf{Hint~\thehint\ to exercise \GetExerciseProperty{counter}}% |
| 19 |
\hfill Back to exercise \ref{ex:\ExerciseID}\par |
| 20 |
\noindent |
| 21 |
}% |
| 22 |
\XSIMsetfileend{\par\addvspace{\bigskipamount}}% |
| 23 |
\XSIMfilewritestart{\jobname-hint-\ExerciseID-\arabic{hint}.tex}% |
| 24 |
}{% |
| 25 |
\XSIMfilewritestop |
| 26 |
\stepcounter{hints}% |
| 27 |
\SetExpandedExerciseProperty{hints}{\arabic{hints}}% |
| 28 |
} |
| 29 |
|
| 30 |
\DeclareExerciseEnvironmentTemplate{hints}{% |
| 31 |
\setcounter{hint}{0}% |
| 32 |
\setcounter{hints}{0}% |
| 33 |
\par\noindent |
| 34 |
\textbf{\XSIMmixedcase{\GetExerciseName}\GetExerciseProperty{counter}:}\hfill |
| 35 |
\IfExercisePropertySetT{hints}{% |
| 36 |
\setcounter{step}{0}% |
| 37 |
\def\hintcomma{\def\hintcomma{, }}% |
| 38 |
Hint\ifnum\GetExerciseProperty{hints}=1\else s\fi: |
| 39 |
\loop\unless\ifnum\value{step}=\GetExerciseProperty{hints} |
| 40 |
\stepcounter{step}% |
| 41 |
\hintcomma\ref{hint:\ExerciseID:\arabic{step}}% |
| 42 |
\repeat |
| 43 |
\par\noindent |
| 44 |
}% |
| 45 |
}{\par\addvspace{\bigskipamount}} |
| 46 |
|
| 47 |
\xsimsetup{ |
| 48 |
exercise/within = section , |
| 49 |
exercise/begin-hook = \label{ex:\ExerciseID} , |
| 50 |
exercise/template = hints |
| 51 |
} |
| 52 |
|
| 53 |
\newcommand\printhints{% |
| 54 |
\ForEachUsedExerciseByType{% |
| 55 |
\setcounter{step}{0}% |
| 56 |
\setcounter{hint}{0}% |
| 57 |
\loop\unless\ifnum\value{step}=\GetExerciseProperty{hints} |
| 58 |
\stepcounter{step}% |
| 59 |
\input{\jobname-hint-\ExerciseID-\arabic{step}.tex}% |
| 60 |
\repeat |
| 61 |
}% |
| 62 |
} |
| 63 |
|
| 64 |
\begin{document} |
| 65 |
|
| 66 |
\section{Exercises} |
| 67 |
\begin{exercise} |
| 68 |
Description of the exercise. Run the following command |
| 69 |
\begin{verbatim} |
| 70 |
print("Hello world") |
| 71 |
Verbatim &^%$&\content |
| 72 |
\end{verbatim} |
| 73 |
\begin{hint} |
| 74 |
Run the example from the command line with the python command. |
| 75 |
\end{hint} |
| 76 |
\begin{hint} |
| 77 |
The solution is 42. |
| 78 |
\end{hint} |
| 79 |
\end{exercise} |
| 80 |
|
| 81 |
\begin{exercise} |
| 82 |
Another exercise |
| 83 |
\begin{hint} |
| 84 |
The first ten million years were the worst. Some \verb+&$+. |
| 85 |
\end{hint} |
| 86 |
\end{exercise} |
| 87 |
|
| 88 |
|
| 89 |
\section{Hints} |
| 90 |
\printhints |
| 91 |
|
| 92 |
\end{document} |