summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/xsim/examples/xsim.texsx-580636.tex
blob: a6f2be66513d0057aba6569c33c9716d4bd6cc4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
% https://tex.stackexchange.com/q/580636/
\documentclass{article}
\usepackage[use-files]{xsim}

\usepackage[colorlinks,allcolors=blue]{hyperref}

\DeclareExerciseProperty{hints}
\newcounter{hint}
\renewcommand\theHhint{\ExerciseID.\arabic{hint}}
\newcounter{hints}
\newcounter{step}

\NewDocumentEnvironment{hint}{}{%
  \stepcounter{hint}%
  \XSIMsetfilebegin{%
    \par
    \refstepcounter{hint}\label{hint:\ExerciseID:\arabic{hint}}%
    \noindent\textbf{Hint~\thehint\ to exercise \GetExerciseProperty{counter}}%
    \hfill Back to exercise \ref{ex:\ExerciseID}\par
    \noindent
  }%
  \XSIMsetfileend{\par\addvspace{\bigskipamount}}%
  \XSIMfilewritestart{\jobname-hint-\ExerciseID-\arabic{hint}.tex}%
}{%
  \XSIMfilewritestop
  \stepcounter{hints}%
  \SetExpandedExerciseProperty{hints}{\arabic{hints}}%
}

\DeclareExerciseEnvironmentTemplate{hints}{%
  \setcounter{hint}{0}%
  \setcounter{hints}{0}%
  \par\noindent
  \textbf{\XSIMmixedcase{\GetExerciseName}\GetExerciseProperty{counter}:}\hfill
  \IfExercisePropertySetT{hints}{%
    \setcounter{step}{0}%
    \def\hintcomma{\def\hintcomma{, }}%
    Hint\ifnum\GetExerciseProperty{hints}=1\else s\fi:
    \loop\unless\ifnum\value{step}=\GetExerciseProperty{hints}
      \stepcounter{step}%
      \hintcomma\ref{hint:\ExerciseID:\arabic{step}}%
    \repeat
    \par\noindent
  }%
}{\par\addvspace{\bigskipamount}}

\xsimsetup{
  exercise/within = section ,
  exercise/begin-hook = \label{ex:\ExerciseID} ,
  exercise/template = hints
}

\newcommand\printhints{%
  \ForEachUsedExerciseByType{%
    \setcounter{step}{0}%
    \setcounter{hint}{0}%
    \loop\unless\ifnum\value{step}=\GetExerciseProperty{hints}
      \stepcounter{step}%
      \input{\jobname-hint-\ExerciseID-\arabic{step}.tex}%
    \repeat
  }%
}

\begin{document}

\section{Exercises}
\begin{exercise}
  Description of the exercise. Run the following command
\begin{verbatim}
print("Hello world")
Verbatim &^%$&\content
\end{verbatim}
\begin{hint}
  Run the example from the command line with the python command.
\end{hint}
\begin{hint}
  The solution is 42.
\end{hint}
\end{exercise}

\begin{exercise}
  Another exercise
\begin{hint}
  The first ten million years were the worst. Some \verb+&$+.
\end{hint}
\end{exercise}


\section{Hints}
\printhints

\end{document}