-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmhex.sty
168 lines (156 loc) · 5.06 KB
/
mhex.sty
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
% !TeX encoding = UTF-8
% For the most current version, see https://github.com/supermihi/latex
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\RequirePackage{expl3,xparse,l3keys2e}
\ProvidesExplPackage{mhex}{2014/10/22}{5}{Defines exercise and solution environments with the ability to switch solution output on or off.}
\RequirePackage{etoolbox}
\RequirePackage{xspace}
\RequirePackage[inline]{enumitem}
\RequirePackage{amsfonts,amssymb}
\setlist[enumerate,1]{label=\alph*)}
\setlist[enumerate,2]{label=\arabic*.}
\RequirePackage{multicol}
\RequirePackage{xcolor}
% multi-column lists for small subexercises
\SetEnumitemKey{columns}{
itemsep=1\itemsep,
parsep=1\parsep,
before=\begin{multicols}{\ifstrempty{#1}{2}{#1}},
after=\raggedcolumns\end{multicols}
}
% ======================================================================================== %
% ========================= exercise and solution environments =========================== %
% ======================================================================================== %
\cs_new:Nn \l_mhex_exheader:n {
\par\medskip
\noindent{\textbf{\textsf{#1}}}
\par
}
\keys_define:nn { mhexerciseenv } {
points .tl_set:N = \l_mhex_curexpoints,
points .value_required:,
title .tl_set:N = \l_mhex_curextitle,
title .value_required: ,
}
\newcounter{mhexercise}
\defcounter{mhexercise}{0}
\renewcommand\themhexercise{
\tl_if_empty:NTF \l_mhex_sheetno {} {\l_mhex_sheetno.}
\arabic{mhexercise}
}
\DeclareDocumentEnvironment
{exercise}
{o}
{
\keys_set:nn {mhexerciseenv} {points=,title=,}
\IfValueTF
{#1}
{\keys_set:nn {mhexerciseenv} {#1}}
{ }
\refstepcounter{mhexercise}
\l_mhex_exheader:n
{
\l_mhex_exercisetext{}~\themhexercise
\tl_if_empty:NTF \l_mhex_curextitle {} {~\textendash{}~\l_mhex_curextitle}
\tl_if_empty:NTF \l_mhex_curexpoints {} {~(\l_mhex_curexpoints{}~\l_mhex_pointstext)}
}
\label{mhexref\arabic{mhexercise}}
\nopagebreak[4]
}
{\par\smallskip}
\keys_define:nn { mhexsolutionenv } {
points .tl_set:N = \l_mhex_cursolpoints,
points .value_required:,
}
\DeclareDocumentEnvironment
{solution}
{o}
{
\keys_set:nn {mhexsolutionenv} {points=,}
\IfValueTF
{#1}
{\keys_set:nn {mhexsolutionenv} {#1}}
{ }
\bool_if:NTF
\l_mhex_solution
{
\l_mhex_exheader:n{
\l_mhex_solutiontext{}~
\ref{mhexref\arabic{mhexercise}}
\tl_if_empty:NTF
\l_mhex_cursolpoints
{}
{~(\l_mhex_cursolpoints{}~\l_mhex_pointstext)}
}
}
{
\par\vspace*{0pt}
\setbox\z@\vbox\bgroup
}
}
{
\bool_if:NTF
\l_mhex_solution
{}
\egroup
}
% ======================================================================================== %
% ======================= Options for \exerciseSetup ===================================== %
% ======================================================================================== %
\keys_define:nn { mhex } {
sheetnumber .tl_set:N = \l_mhex_sheetno,
solution .bool_set:N = \l_mhex_solution,
exam .bool_set:N = \l_mhex_exammode,
}
% defaults
\keys_set:nn { mhex } {
sheetnumber = 1,
solution = false,
exam = false,
}
\cs_new:Npn \exerciseSetup #1 {
\keys_set:nn { mhex } { #1 }
\bool_if:NTF
\l_mhex_exammode
{\tl_set:Nn \l_mhex_sheetno {}}
{}
}
% ======================================================================================== %
% ======== Bilingual Setup: Defining Captions in German and English ====================== %
% ======================================================================================== %
\cs_new:Nn \l_mhex_addcaption:Nnn {
% add a bilingual text element.
% First argument is the name, second the german, third the english text.
% The text element will be accessible as \l_mhex_#1text.
\gappto \captionsenglish { \tl_set:cn {l_mhex_#1text} {#3} }
\gappto \captionsgerman { \tl_set:cn {l_mhex_#1text} {#2} }
\gappto \captionsngerman { \tl_set:cn {l_mhex_#1text} {#2} }
}
\l_mhex_addcaption:Nnn {exercise} {Aufgabe} {Exercise}
\l_mhex_addcaption:Nnn {exercises} {Aufgaben} {Exercises}
\l_mhex_addcaption:Nnn {solution} {Lösung~zu~Aufgabe} {Solution~to~Exercise}
\l_mhex_addcaption:Nnn {points} {Punkte} {points}
\l_mhex_addcaption:Nnn {part} {Teil} {part}
\l_mhex_addcaption:Nnn {parts} {Teile} {parts}
\DeclareDocumentCommand
\mhexCaption
{ m m }
{ \AtBeginDocument{
\tl_set:cn {l_mhex_#1text} {#2}
}
}
\AtBeginDocument{
% renew the \mhexCaption command to immediately change captions after preamble
\RenewDocumentCommand
\mhexCaption
{ m m }
{\tl_set:cn {l_mhex_#1text} {#2}}
% ensure that babel or polyglossia are loaded
\@ifpackageloaded{babel}{}
{\@ifpackageloaded{polyglossia}{}{\PackageError{mhex}{babel~or~polyglossia~must~be~loaded!}}}
\@ifpackageloaded{cleveref}{
\crefname{mhexercise}{\l_mhex_exercisetext}{\l_mhex_exercisestext}
\crefname{enumi}{\l_mhex_parttext}{\l_mhex_partstext}
}
}
\ProcessKeysOptions{ mhex }