summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/xsim/examples/xsim.texsx-576998.tex
blob: ee2947bbb533530e124678fe41a616977c153ddf (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
% https://tex.stackexchange.com/q/576998
\documentclass[twocolumn]{book}
\usepackage{xsim}

\DeclareExerciseEnvironmentTemplate{number-only}{%
  \par\noindent
  \textbf{\GetExerciseProperty{counter}}%
  \GetExercisePropertyT{subtitle}{ \textit{#1}} %
}{\par}

\newcommand\printsectionexercises{%
  \ForEachUsedExerciseByType{%
    \ifnum\ExercisePropertyGet{##1}{##2}{chapter-value}=\value{chapter}
      \ifnum\ExercisePropertyGet{##1}{##2}{section-value}=\value{section}
        \XSIMprint{exercise}{##1}{##2}%
      \fi
    \fi
  }%
}

\renewcommand\printsolutions{%
  \def\currentchapter{}%
  \def\currentsection{}%
  \def\lastchapter{}%
  \def\lastsection{}%
  \ForEachUsedExerciseByType{%
    \let\lastchapter\currentchapter
    \let\lastsection\currentsection
    \edef\currentchapter{\ExercisePropertyGet{##1}{##2}{chapter-value}}%
    \edef\currentsection{\ExercisePropertyGet{##1}{##2}{section-value}}%
    \ifx\lastchapter\currentchapter\else
      \section*{Chapter \ExercisePropertyGet{##1}{##2}{chapter}}
    \fi
    \ifx\lastsection\currentsection\else
      \subsection*{Exercises \ExercisePropertyGet{##1}{##2}{section}}
    \fi
    \XSIMprint{solution}{##1}{##2}%
  }%
}

\xsimsetup{
  exercise/template = number-only ,
  solution/template = number-only ,
  exercise/within = section ,
  exercise/the-counter = \arabic{exercise}. ,
  exercise/print = false
}

\begin{document}

\setcounter{chapter}{13}
\chapter{}
\section{Cats as pets}
Text text text\dots
\begin{exercise}
  Draw a cat.
\end{exercise}
\begin{solution}
  Drawing a cat.
\end{solution}

Text text text\dots
\begin{exercise}
  Bathe a cat.
\end{exercise}
\begin{solution}
  Bathing a cat.
\end{solution}

Text text text\dots
\begin{exercise}
  Trace the history of cats through time.
\end{exercise}
\begin{solution}
  Tracing the history of cats through time.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\section{Frogs as pets}
Text text text\dots
\begin{exercise}
  Lick a frog.
\end{exercise}
\begin{solution}
  Licking a frog.
\end{solution}

Text text text\dots
\begin{exercise}
  Bathe a frog.
\end{exercise}
\begin{solution}
  Bathing a frog.
\end{solution}

Text text text\dots
\begin{exercise}
  Feed a frog.
\end{exercise}
\begin{solution}
  Feeding a frog.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\setcounter{chapter}{20}
\chapter{}

\section{The joys of fish}
Text text text\dots
\begin{exercise}
  Catch a fish.
\end{exercise}
\begin{solution}
  Catching a fish.
\end{solution}

Text text text\dots
\begin{exercise}
  Draw a fish.
\end{exercise}
\begin{solution}
  Drawing a fish.
\end{solution}

\subsection*{Exercises \thesection}
\printsectionexercises

\chapter*{Solutions to the exercises}
\printsolutions

\end{document}