summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/rbt-mathnotes/rbt-mathnotes-hw.cls
blob: 24e91f715e32743cb02848fdf6c2ba07fd5e0f8f (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
%% rbt-mathnotes-hw.cls
%% Copyright 2021 Rebecca B. Turner.
%
% 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 Rebecca B. Turner.
%
% This work consists of the files:
%     README.md
%     rbt-mathnotes.tex
%     rbt-mathnotes.sty
%     rbt-mathnotes.cls
%     rbt-mathnotes-util.sty
%     rbt-mathnotes-messages.sty
%     rbt-mathnotes-hw.cls
%     rbt-mathnotes-formula-sheet.cls
%     examples/cheat-sheet.tex
%     examples/multivar.tex
%     examples/topology-hw-1.tex
% and the derived files:
%     rbt-mathnotes.pdf
%     examples/cheat-sheet.pdf
%     examples/multivar.pdf
%     examples/topology-hw-1.pdf

\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}
\ProvidesExplClass{rbt-mathnotes-hw}{2021/11/29}{1.0.2}{Companion styles
  to rbt-mathnotes for typeset homework assignments.}

% \PassOptionsToPackage{noxcolor}{rbt-mathnotes}

\RequirePackage{kvoptions}
\DeclareDefaultOption
  {
    \PassOptionsToClass { \CurrentOption } { rbt-mathnotes }
  }
\ProcessKeyvalOptions*

\LoadClass { rbt-mathnotes }

\cs_set:Npn \mn__undefine_thm:n #1
  {
    \cs_undefine:c { #1 }
    \cs_undefine:c { end#1 }
    \cs_undefine:c { #1* }
    \cs_undefine:c { end#1* }
  }

\mn__undefine_thm:n { thm }
\mn__undefine_thm:n { lem }
\mn__undefine_thm:n { cor }
\mn__undefine_thm:n { prop }
\mn__undefine_thm:n { defn }
\mn__undefine_thm:n { notation }
\mn__undefine_thm:n { ex }

\cs_undefine:N \mn__undefine_thm:n

\theoremstyle { definition }
\newtheorem { thm } { Theorem }
\newtheorem { lem } { Lemma }
\newtheorem { cor } { Corollary }
\newtheorem { prop } { Proposition }
\newtheorem { defn } { Definition }
\newtheorem { notation } { Notation }
\newtheorem { ex } { Example }

% The displayed problem number. \g__mn_problems_int isn't incremented if a
% custom number is given.
\int_new:N \g__mn_problems_int
\int_set_eq:NN \g__mn_problems_int \c_one_int

% The internal problem number. \g__mn_all_problems_int is *always*
% incremented, so it can be used as a unique identifier in hyperref
% pdf bookmark names.
\int_new:N \g__mn_all_problems_int
\int_set_eq:NN \g__mn_all_problems_int \c_one_int

% Label prepended to problem numbers.
\tl_const:Nn \g__mn_problem_string_tl { Problem }

\skip_const:Nn \g__mn_problem_before_skip { 2em }

% Label appended to problem numbers; omitted from PDF bookmarks.
\tl_const:Nn \g__mn_problem_after_tl { . }

% A possible custom problem number. If \c_novalue_tl, use
% \g__mn_problems_int instead.
\tl_new:N \mn__problem_number_tl
\tl_set_eq:NN \mn__problem_number_tl \c_novalue_tl

\tl_const:Nn \mn__problem_number_default_tl
  {
    \int_to_arabic:n { \g__mn_problems_int }
  }

% Current problem number, either from \g__mn_problems_int or
% \mn__problem_number_tl.
\tl_new:N \mn__problem_number_current_tl

% Current problem display title, including "problem" label, number, and
% custom title.
% \tl_new:N \mn__problem_display_title_tl

\keys_define:nn { mn__main }
  {
    problem~string .value_required:n = true ,
    problem~string .tl_set:N = \g__mn_problem_string_tl ,
    problem~before~skip .skip_set:N = \g__mn_problem_before_skip
  }

\keys_define:nn { mn__problem }
  {
    number .value_required:n = true ,
    number .tl_set:N = \mn__problem_number_tl ,

    title .value_required:n = true ,
    title .tl_set:N = \mn__problem_title_tl ,
    title .initial:n = ,

    label .value_required:n = true ,
    label .tl_set:N = \mn__problem_label_tl ,

    % Treat unknown keys as the problem number; this lets us avoid having
    % 2 or more optional arguments.
    unknown .code:n =
      \tl_if_empty:nTF { #1 }
        {
          % No value; use it for the problem number.
          \tl_set_eq:NN \mn__problem_number_tl \l_keys_key_tl
        }
        {
          % Non-empty value; give an error.
          \msg_error:nnx
            { mathnotes }
            { no key in problem }
            { \tl_use:N \l_keys_key_tl }
        } ,
  }

\cs_set:Npn \mn__problem_title_pdf:
  {
    % A string like "Problem" or "Exercise" or "Question"
    \tl_use:N \g__mn_problem_string_tl
    % The problem number.
    \tl_if_empty:NF \mn__problem_number_current_tl
      {
        \ \tl_use:N \mn__problem_number_current_tl
      }
    \tl_if_empty:NF \mn__problem_title_tl
      {
        :~\tl_use:N \mn__problem_title_tl
      }
  }

\cs_set:Npn \mn__problem_title:
  {
    \mn__problem_title_pdf:
    % The text after the problem, nominally a period (".").
    \tl_use:N \g__mn_problem_after_tl
  }

% PDF anchor / bookmark name for hyperref.
\cs_set:Npn \mn__problem_anchor:
  {
    problem.\int_to_arabic:n { \g__mn_all_problems_int }
  }

\NewDocumentEnvironment { problem }
  {
    O{} % Problem number or options
  }
  {
    \keys_set:nn { mn__problem } { #1 }

    % Set the problem number
    \tl_set:Nn \mn__problem_number_current_tl { }
    \tl_if_eq:NNTF \mn__problem_number_tl \c_novalue_tl
      {
        % No number given
        \tl_set_eq:NN \mn__problem_number_current_tl \mn__problem_number_default_tl
      }
      {
        % Some number given, *maybe* empty
        \tl_set_eq:NN \mn__problem_number_current_tl \mn__problem_number_tl
      }

    \vspace { \skip_use:N \g__mn_problem_before_skip }

    \phantomsection
    \addcontentsline
      { toc }
      { chapter }
      { \mn__problem_title_pdf: }
    \begin{mdframed}
      [
        style = note ,
        startinnercode =
          \mn__note_title:n
            {
              \cs_gset:Npx \@currentlabel { \tl_use:N \mn__problem_number_current_tl }
              \cs_gset:Npx \@currentlabelname { \mn__problem_title: }
              \tl_if_empty:NF \mn__problem_label_tl
                {
                  \exp_after:wN \label { \tl_use:N \mn__problem_label_tl }
                }
              \mn__problem_title:
            }
          ,
      ]
  }
  {
    \end{mdframed}
    % If we didn't get a custom number, increment the counter.
    \tl_if_eq:NNT \mn__problem_number_tl \c_novalue_tl
      {
        \int_gincr:N \g__mn_problems_int
      }
    % But always increment the internal counter.
    \int_gincr:N \g__mn_all_problems_int
  }

\NewDocumentCommand \prob
  {
    O{} % Problem number or options
    m % Problem text
  }
  {
    \begin{problem}[#1]
      #2
    \end{problem}
  }

\cs_set:Npn \mn__add_par_arg_to_sectioning_cmd:N #1
  {
    \cs_set_eq:cN { mn__\cs_to_str:N #1 _old } #1
    \cs_set_eq:cc
      { mn__the\cs_to_str:N #1 _old }
      { the\cs_to_str:N #1 }
    \RenewDocumentCommand #1
      {
        s % ##1: Numbered / in ToC?
        d() % ##2: Number override.
        o % ##3: ToC title.
        o % ##4: Page header title.
        m % ##5: Title.
      }
      {
        \group_begin:
        \cs_set:Npx \mn__current_thesection_cmd
          { the\cs_to_str:N #1 }
        % Is there a number override?
        \IfValueTF { ##2 }
          {
            % If yes, *globally* change the number format.
            \cs_gset:cpn { \mn__current_thesection_cmd } { ##2 }
          }
          {
            % Otherwise, restore the number format from a previous override,
            % if applicable.
            \cs_gset_eq:cc
              { \mn__current_thesection_cmd }
              { mn__the\cs_to_str:N #1 _old  }
          }
        % Save the original command so we can use it as a single token.
        \cs_set_eq:Nc \mn__orig_sectioning_cmd { mn__\cs_to_str:N #1 _old }
        \exp_last_unbraced:Ne \mn__orig_sectioning_cmd
          {
            % Propagate the star.
            \IfBooleanT { ##1 } { * }
            % Propogate other arguments.
            \IfValueT { ##3 } { [##3] }
            \IfValueT { ##4 } { [##4] }
            { ##5 }
          }
        \group_end:
      }
  }

\cs_set:Npn \mn__add_par_arg_to_sectioning_cmds:n #1
  {
    \clist_map_function:nN { #1 } \mn__add_par_arg_to_sectioning_cmd:N
  }

\makeheadstyles { rbt-mathnotes-hw }
  {
    \chapterstyle { rbt-mathnotes }
    \headstyles { rbt-mathnotes }

    % Don't use chapter numbers in sections and beyond:
    \cs_set:Npn \thesection { \arabic{section} }

    % Add paren-arg to override numbers in sectioning commands.
    \mn__add_par_arg_to_sectioning_cmds:n
      {
        \chapter ,
        \section ,
        \subsection ,
        \subsubsection ,
        \paragraph ,
        \subparagraph ,
      }
  }

\headstyles { rbt-mathnotes-hw }

\setlength{\columnsep}{3em}

% No chapter numbers for figures.
\counterwithout{figure}{chapter}

% Put author name, title in headings
\makeevenfoot { headings } { } { } { }
\makeoddfoot  { headings } { } { } { }
\makeevenhead { headings } { } { } { \thepage }
\makeoddhead  { headings } { \@author,~\textit{ \@title } } { } { \thepage }