summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/exercisepoints/exercisepoints.sty
blob: 42a37546727a240f1f54106195946735c469b624 (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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
% !TEX encoding = UTF-8 Unicode
%
%% exercisepoints.sty
%% A LaTeX package to count exercises and points.
%%
%% Copyright (c) 2017-2019 Henning Kerstan.
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Henning Kerstan.
%
% This work consists of the files `exercisepoints.sty' and `exercisepoints.tex'.
%


% package identification
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{exercisepoints}[%
  2019/01/03 v1.2.3 %
  count exercises and points%
]


% switches for custom exercise layout
\newif\if@exercisepoints@usecustomlayout%
\newif\if@exercisepoints@exercise@layoutdefined%
\newif\if@exercisepoints@subexercise@layoutdefined%

\DeclareOption{customlayout}{%
  \@exercisepoints@usecustomlayouttrue%
}

% this package does not have any other options
\DeclareOption*{
  \PackageWarning{exercisepoints}{Unknown option '\CurrentOption'}
}
\ProcessOptions\relax%


% avoid nesting (i.e. putting exercises in exercises)
\newif\if@exercisepoints@inexercise%
\global\@exercisepoints@inexercisefalse%
\newif\if@exercisepoints@insubexercise%
\global\@exercisepoints@insubexercisefalse%


% counters to store number of exercises
\newcounter{exercisepoints@exercisecount}
\newcounter{exercisepoints@subexercisecount}[exercisepoints@exercisecount]


% lengths to store points
\newlength{\exercisepoints@currentexercisepoints}
\newlength{\exercisepoints@currentsubexercisepoints}
\newlength{\exercisepoints@totalpoints}
\newlength{\exercisepoints@bonuspoints}


% macros to get total points and total number of exercises (whole document)
\providecommand{\totalpoints}{0}
\providecommand{\numberofexercises}{0}


% macros to get info about current exercise(-part)
% (to be used in AtBeginExercise or AtEndExercise hooks)
\providecommand{\currentexercisetitle}{}
\providecommand{\currentsubexercisetitle}{}

\providecommand{\currentexercisepoints}{%
  \if@exercisepoints@inexercise\else%
    \PackageError{exercisepoints}{'\string\currentexercisepoints' can only be used in exercise or subexercise environment}%
  \fi%
  %
  \ifcsname exercisepoints@points@\currentexercisenumber\endcsname%
    \@nameuse{exercisepoints@points@\currentexercisenumber}%
  \else%
    \textbf{??}% TODO:issue package warning in such a way that it can be used also in section commands
  \fi%
}

\providecommand{\currentsubexercisepoints}{%
  \if@exercisepoints@insubexercise\else%
    \PackageError{exercisepoints}{'\string\currentsubexercisepoints' can only be used in subexercise environment}%
  \fi%
  %
  \ifcsname exercisepoints@points@\currentexercisenumber.\currentsubexercisenumber\endcsname%
    \@nameuse{exercisepoints@points@\currentexercisenumber.\currentsubexercisenumber}%
  \else%
    \textbf{??}% TODO:issue package warning in such a way that it can be used also in section commands
  \fi%
}

\providecommand{\currentexercisenumber}{%
  \theexercisepoints@exercisecount%
}

\providecommand{\currentsubexercisenumber}{%
  \theexercisepoints@subexercisecount%
}


% hooks to modify the exercise typesetting
\providecommand{\AtBeginExercise}[1]{
  \renewcommand{\exercisepoints@begin}{#1}%
  \@exercisepoints@exercise@layoutdefinedtrue%
}

\providecommand{\AtEndExercise}[1]{
  \renewcommand{\exercisepoints@end}{#1}
}

% the same for subexercises
\providecommand{\AtBeginSubexercise}[1]{
  \renewcommand{\exercisepoints@sub@begin}{#1}
  \@exercisepoints@subexercise@layoutdefinedtrue%
}

\providecommand{\AtEndSubexercise}[1]{
  \renewcommand{\exercisepoints@sub@end}{#1}
}


% storage for the hooks 
\newcommand{\exercisepoints@begin}{}
\newcommand{\exercisepoints@end}{}
\newcommand{\exercisepoints@sub@begin}{}
\newcommand{\exercisepoints@sub@end}{}


% a very simple default exercise style
\if@exercisepoints@usecustomlayout\else%
  \RequirePackage{ifthen}%
  \AtBeginExercise{%
    ~\smallskip\\\sffamily%
    \ifthenelse{\equal{\currentexercisetitle}{}}%
      {\textbf{Exercise~\currentexercisenumber}} % empty title
      {%
        \textbf{Exercise~\currentexercisenumber:} %
    \currentexercisetitle%
      }% non-empty title
    \,~\hfill\textbf{(\currentexercisepoints~Points)}%
    \smallskip\\\noindent\normalfont%
  }
  \AtEndExercise{~\smallskip\\}
  %
  \AtBeginSubexercise{%
    ~\smallskip\\\sffamily%
    \ifthenelse{\equal{\currentsubexercisetitle}{}}%
      {\emph{Exercise~\currentexercisenumber.\currentsubexercisenumber}} % empty title
      {%
        \emph{Exercise~\currentexercisenumber.\currentsubexercisenumber:} %
    \emph{\currentsubexercisetitle}%
      }% non-empty title
    \,~\hfill\emph{(\currentsubexercisepoints~Points)}%
    \smallskip\\\noindent\normalfont%
  }
  \AtEndSubexercise{~\\}
\fi


% points commands sets points (additive within an exercise environment)
\newcommand{\points}[1]{%
  \if@exercisepoints@inexercise%
    \addtolength{\exercisepoints@currentexercisepoints}{#1 pt}%
    \global\exercisepoints@currentexercisepoints=\exercisepoints@currentexercisepoints%
    \addtolength{\exercisepoints@totalpoints}{#1 pt}%
    \global\exercisepoints@totalpoints=\exercisepoints@totalpoints%
    \if@exercisepoints@insubexercise%
      \addtolength{\exercisepoints@currentsubexercisepoints}{#1 pt}%
      \global\exercisepoints@currentsubexercisepoints=\exercisepoints@currentsubexercisepoints%
    \fi%
  \else%
    \PackageError{exercisepoints}{\string\points{...} can only be used within exercise or subexercise environment.}%
  \fi%
}

% itempoints units
\newcommand{\exercisepoints@itempointsunit@singular}{}
\newcommand{\exercisepoints@itempointsunit@plural}{}
\newcommand{\setitempointsunit}[2]{% singular, plural
  \renewcommand{\exercisepoints@itempointsunit@singular}{#1}%
  \renewcommand{\exercisepoints@itempointsunit@plural}{#2}%
}

% itempoints calls points and displays points flush right for use in
% enumerate environments
\RequirePackage{ifthen}
\newcommand{\itempoints}[1]{%
  \points{#1}%
  % check if exactly 1 points
  \ifthenelse{\equal{#1}{1}}{% not robust, so issuing 1.0 will not result in correct (singular) form!
    \ifthenelse{\equal{\exercisepoints@itempointsunit@singular}{}}{%
      \,~\hfill(#1)%
    }{%
      \,~\hfill(#1~\exercisepoints@itempointsunit@singular)%
    }%
  }{%
    \ifthenelse{\equal{\exercisepoints@itempointsunit@plural}{}}{%
      \,~\hfill(#1)%
    }{%
      \,~\hfill(#1~\exercisepoints@itempointsunit@plural)%
    }%
  }%
}




% getpoints retrieves points for a specific exercise number (starting at 0)
\newcommand{\getpoints}[1]{%
  \ifthenelse{#1 < \numberofexercises}%
  {\exercisepoints@readfromaux{points@#1}}%
  {\textbf{??}}%
}


% aux storage function (key-value store using #1 as key and #2 as value)
\newcommand{\exercisepoints@storetoaux}[2]{% 
  \immediate\write\@auxout{%
    \string\global\string\long\string\@namedef{exercisepoints@#1}{#2}%
  }%
}


% aux retrieval
\newcommand{\exercisepoints@readfromaux}[1]{%
  \ifcsname exercisepoints@#1\endcsname%
    \@nameuse{exercisepoints@#1}%
  \else%
    \textbf{??}%
    \PackageWarning{exercisepoints}{%
      Key 'exercisepoints@#1' not found in aux file. Maybe you need to recompile?%
    }%
  \fi%
}


% exercise environment; optional parameter is title of the respective exercise
\newenvironment{exercise}[1][]{%
  \if@exercisepoints@inexercise%
    \PackageError{exercisepoints}{You cannot nest exercise environments}%
  \fi
  %
  \if@exercisepoints@exercise@layoutdefined\else%
    \PackageError{exercisepoints}{Option `customlayout' requires you to define an exercise layout using at least `\string\AtBeginExercise{...}'}
  \fi 
  %
  \global\@exercisepoints@inexercisetrue%
  \setlength{\exercisepoints@currentexercisepoints}{0pt}%
  \global\exercisepoints@currentexercisepoints=\exercisepoints@currentexercisepoints%
  \renewcommand{\currentexercisetitle}{#1}%
  \exercisepoints@begin%
}{%
  \exercisepoints@end%
  \exercisepoints@storetoaux{points@\theexercisepoints@exercisecount}{\strip@pt\exercisepoints@currentexercisepoints}%
  \stepcounter{exercisepoints@exercisecount}%
  \global\@exercisepoints@inexercisefalse%
}


% exercise environment; optional parameter is title of the respective exercise
\newenvironment{subexercise}[1][]{%
  \if@exercisepoints@insubexercise%
    \PackageError{exercisepoints}{You cannot nest subexercise environments}%
  \fi%
  %
  \if@exercisepoints@subexercise@layoutdefined\else%
    \PackageError{exercisepoints}{Option `customlayout' requires you to define a subexercise layout using at least `\string\AtBeginSubexercise{...}'}
  \fi%
  \global\@exercisepoints@insubexercisetrue%
  \setlength{\exercisepoints@currentsubexercisepoints}{0pt}%
  \global\exercisepoints@currentsubexercisepoints=\exercisepoints@currentsubexercisepoints%
  \renewcommand{\currentsubexercisetitle}{#1}%
  \exercisepoints@sub@begin%
}{%
  \exercisepoints@sub@end%
  \exercisepoints@storetoaux{points@\theexercisepoints@exercisecount.\theexercisepoints@subexercisecount}{\strip@pt\exercisepoints@currentsubexercisepoints}%
  \stepcounter{exercisepoints@subexercisecount}%
  \global\@exercisepoints@insubexercisefalse%
}


\newcommand{\bonuspoints}[1]{%
  \setlength{\exercisepoints@bonuspoints}{#1 pt}%
  \global\exercisepoints@bonuspoints=\exercisepoints@bonuspoints%
}

\newcommand{\getbonuspoints}{%
  \exercisepoints@readfromaux{points@bonus}%
}

\newcommand{\totalpointswithbonus}{%
  \readfromaux{totalpointswithbonus}%
}


% read values from aux file at begin
\AtBeginDocument{
  \setlength{\exercisepoints@totalpoints}{0pt}% reset total points length
  %
  % set numberofexercises to 0 if not found in aux
  \ifcsname exercisepoints@numberofexercises\endcsname%
    \renewcommand{\numberofexercises}{%
      \exercisepoints@readfromaux{numberofexercises}%
    }%
  \fi%
  %
  \renewcommand{\totalpoints}{\exercisepoints@readfromaux{points@total}}%
  \renewcommand{\totalpointswithbonus}{\exercisepoints@readfromaux{points@totalwithbonus}}%
}


% store values in aux file at end
\AtEndDocument{
  \exercisepoints@storetoaux{numberofexercises}{\theexercisepoints@exercisecount}%
  \exercisepoints@storetoaux{points@total}{\strip@pt\exercisepoints@totalpoints}%
  \exercisepoints@storetoaux{points@bonus}{\strip@pt\exercisepoints@bonuspoints}%
  \newlength{\exercisepoints@totalpointswithbonus}%
  \setlength{\exercisepoints@totalpointswithbonus}{\exercisepoints@totalpoints}%
  \addtolength{\exercisepoints@totalpointswithbonus}{\exercisepoints@bonuspoints}%
  \exercisepoints@storetoaux{points@totalwithbonus}{\strip@pt\exercisepoints@totalpointswithbonus}%
}