summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/exsheets/cntformats.sty
blob: cb2cccd2db0409fe1d9f1ff031a5834f215b6987 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
% --------------------------------------------------------------------------
% the CNTFORMATS package
% 
%   A different way to read counters
%
% This package is part of the exsheets bundle
% --------------------------------------------------------------------------
% Clemens Niederberger
% Web:    https://bitbucket.org/cgnieder/exsheets/
% E-Mail: contact@mychemistry.eu
% --------------------------------------------------------------------------
% Copyright 2012-2013 Clemens Niederberger
% 
% 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 Clemens Niederberger.
% --------------------------------------------------------------------------
% The cntformats package is part of the exsheets bundle
% --------------------------------------------------------------------------
% If you have any ideas, questions, suggestions or bugs to report, please
% feel free to contact me.
% --------------------------------------------------------------------------
\def\@cntfmts@date{2013/10/11}
\def\@cntfmts@version{v0.6}

\ProvidesPackage{cntformats}[%
  \@cntfmts@date\space
  \@cntfmts@version\space
  A different way to read counters. (CN)]
\RequirePackage{etoolbox,cnltx-base}
% --------------------------------------------------------------------------
% message handling
\def\@cntfmts@error@message{%
  For details have a look at the `exsheets' manual.}

\cnltx@create@generic@message{@cntfmts}{cntformats}{Error}{\@cntfmts@error@message}
\cnltx@create@generic@message{@cntfmts}{cntformats}{Warning}{}
\cnltx@create@generic@message{@cntfmts}{cntformats}{WarningNoLine}{}
\cnltx@create@generic@message{@cntfmts}{cntformats}{Info}{}

\def\@cntfmts@err@already@defined#1{%
  \@cntfmts@error{The counter `#1' already exists!}}
\def\@cntfmts@err@unknown@counter#1{%
  \@cntfmts@error{The counter `#1' is undefined!}}
\def\@cntfmts@err@pattern@defined#1#2{%
  \@cntfmts@error{The pattern `#2' for module `#1' is already defined!}}
\def\@cntfmts@err@pattern@undefined#1#2{%
  \@cntfmts@error{The pattern `#2' for module `#1' is undefined!}}

% ----------------------------------------------------------------------------
% tokenlist analyzing and manipulating
%   if you happen to read this code and notice some obvious reasons why these
%   macros shoudn't be used like this, please send me your improved version
%   (seriously! But please explain why yours is better)
% \long\def\@cntfmts@ifintl#1#2{%
%   \def\@cntfmts@intl@tmpa##1#2{}%
%   \expandafter\if\expandafter\relax\expandafter
%   \detokenize\expandafter{\@cntfmts@intl@tmpa#1{}{}#2}\relax%
%     \expandafter\@secondoftwo
%   \else
%     \expandafter\@firstoftwo
%   \fi
% }
% \long\def\@cntfmts@ifintlcs#1{%
%   \expandafter\@cntfmts@ifintl\expandafter{#1}}

% \long\def\@cntfmts@replaceonceincs#1#2#3{%
%   \@cntfmts@ifintlcs#1{#2}%
%     {\@cntfmts@replace@once@incs#1{#2}{#3}}{}%
% }
% \long\def\@cntfmts@replace@once@incs#1#2#3{%
%   \def\@cntfmts@replo@tmpa##1#2##2\q@stop{%
%     \def#1{##1#3##2}}%
%   \expandafter\@cntfmts@replo@tmpa#1\q@stop
% }

% \long\def\@cntfmts@replaceallin#1#2#3{%
%   \def\@cntfmts@repla@tmpa{#1}%
%   \@cntfmts@replaceonceincs\@cntfmts@repla@tmpa{#2}{#3}%
%   \@cntfmts@ifintlcs\@cntfmts@repla@tmpa{#2}%
%     {\expandafter\@cntfmts@replaceallin\expandafter{\@cntfmts@repla@tmpa}{#2}{#3}}%
%     {\expandonce\@cntfmts@repla@tmpa}%
% }
% \long\def\@cntfmts@replaceallincs#1#2#3{%
%   \@cntfmts@replaceonceincs#1{#2}{#3}%
%   \@cntfmts@ifintlcs#1{#2}{\@cntfmts@replaceallincs#1{#2}{#3}}{}%
% }

% ----------------------------------------------------------------------------
% expansion tools
%   heavily inspired by expl3's \exp_args:N<spec> -- one might say: copied
% \long\def\@cntfmts@getnextbraced#1#2#3{#2\@cntfmts@firstofone{#3{#1}}}
% \long\def\@cntfmts@firstofone#1{#1}
% \long\def\@cntfmts@braced@unexpanded#1\@cntfmts@firstofone#2#3{%
%   #1\@cntfmts@firstofone{#2{#3}}}
% \long\def\@cntfmts@braced@expandedonce#1\@cntfmts@firstofone#2#3{%
%   \expandafter\@cntfmts@getnextbraced\expandafter{#3}{#1}{#2}}

% \long\def\@cntfmts@expand@first{%
%   \@cntfmts@braced@expandedonce
%   \@cntfmts@firstofone}

% \long\def\@cntfmts@expand@second{%
%   \@cntfmts@braced@unexpanded
%   \@cntfmts@braced@expandedonce
%   \@cntfmts@firstofone}

% \long\def\@cntfmts@expand@third{%
%   \@cntfmts@braced@unexpanded
%   \@cntfmts@braced@unexpanded
%   \@cntfmts@braced@expandedonce
%   \@cntfmts@firstofone}

% ----------------------------------------------------------------------------
% expandable commands with optional argument (and no mandatory)
%   this is probably not very robust. If you know what you're doing you're
%   probably able to define a better version yourself. If not: don't use this
%   one!
% \long\def\@cntfmts@isopt@#1[#2\q@marker#3#{%
%   \if\relax\detokenize{#1#2}\relax
%     \expandafter\@firstoftwo
%   \else
%     \expandafter\@secondoftwo
%   \fi}
% \long\def\@cntfmts@isopt#1#2#3{%
%   \@cntfmts@isopt@#1\q@marker[\q@marker{#2#1}{#2[{#3}]{#1}}}

% \long\def\@cntfmts@defwopt#1[#2]#3{%
%   \long\def#1##1{\@cntfmts@isopt{##1}{\csuse{\@cntfmts@strip@bs#1@opt}}{#2}}%
%   \long\csdef{\@cntfmts@strip@bs#1@opt}[##1]{#3}}

% \def\@cntfmts@strip@bs{\expandafter\@gobble\string}

% ----------------------------------------------------------------------------
% #1: module
% #2: counter
\def\@cntfmts@def@counter@read#1#2#3{%
  \cnltx@expandargs(no)\csdef
    {@#1@read@#3@counter}%
    {\csname @#1@read@#2@counter\endcsname}%
}

% create pattern for an existing counter:
% #1: module
% #2: counter
% #3: id
\newcommand*\@cntfmts@add@counter@pattern[3][cntfmts]{%
  \ifcsdef{c@#2}
    {}{\@cntfmts@err@unknown@counter{#2}}%
  \ifcsdef{@#1@#2@counter}
    {\@cntfmts@err@pattern@defined{#1}{#2}}
    {\csdef{@#1@#2@counter}{}}%
  \listcsadd{@#1@counter@ids}{#3}%
  \@cntfmts@def@counter@read{#1}{#2}{#3}%
}

\newcommand*\@cntfmts@add@counter@pattern@nostar[3][cntfmts]{%
  \@cntfmts@add@counter@pattern[#1]{#2}{#3}%
  \cnltx@expandargs(nno)\@cntfmts@create@read@counter{#1}{#2}{\csname c@#2\endcsname}%
}

\newrobustcmd*\AddCounterPattern{%
  \@ifstar
    {\@cntfmts@add@counter@pattern}
    {\@cntfmts@add@counter@pattern@nostar}%
}
\@onlypreamble\AddCounterPattern

% create pattern for a new counter
% #1: counter
% #2: id
\newcommand*\@cntfmts@new@counter@pattern[3][cntfmts]{%
  \ifcsdef{the#2}
    {\@cntfmts@err@counter@defined{#2}}{}%
  \newcounter{#2}%
  \listcsadd{@#1@counter@ids}{#3}%
  \@cntfmts@def@counter@read{#1}{#2}{#3}%
  \csdef{@#1@#2@counter}{}
}

\newcommand*\@cntfmts@new@counter@pattern@nostar[3][cntfmts]{%
  \@cntfmts@new@counter@pattern[#1]{#2}{#3}%
  \cnltx@expandargs(nno)\@cntfmts@create@read@counter{#1}{#2}{\csname c@#2\endcsname}%
}

\newrobustcmd*\NewCounterPattern{%
  \@ifstar
    {\@cntfmts@new@counter@pattern}
    {\@cntfmts@new@counter@pattern@nostar}%
}
\@onlypreamble\NewCounterPattern

% renew pattern:
\newcommand*\@cntfmts@renew@counter@pattern[3][cntfmts]{%
  \ifcsdef{the#2}
    {}{\@cntfmts@err@unknown@counter{#2}}%
  \ifcsdef{@#1@#2@counter}{}
    {\@cntfmts@err@pattern@undefined{#1}{#2}}%
  \ifinlistcs{@#1@counter@ids}{#3}
    {}{\listcsadd{@#1@counter@ids}{#3}}%
  \@cntfmts@def@counter@read{#1}{#2}{#3}%
}

\newcommand*\@cntfmts@renew@counter@pattern@nostar[3][cntfmts]{%
  \@cntfmts@renew@counter@pattern[#1]{#2}{#3}%
  \cnltx@expandargs(nno)\@cntfmts@create@read@counter{#1}{#2}{\csname c@#2\endcsname}%
}

\newrobustcmd\RenewCounterPattern{%
  \@ifstar
    {\@cntfmts@renew@counter@pattern}
    {\@cntfmts@renew@counter@pattern@nostar}%
}
\@onlypreamble\RenewCounterPattern

% ----------------------------------------------------------------------------
% assign a different internal command to <counter> than \c@<counter>
% can/must be used after a call from \AddCounterPattern* or
% \NewCounterPattern*
% #1: module
% #2: counter
% #3: internal command
\def\@cntfmts@create@read@counter#1#2#3{%
  \expandafter\newexpandablecmd
  \expandafter*\csname @#1@read@#2@counter\endcsname[1][]{%
    \ifblank{##1}
      {\@arabic{#3}}
      {\csuse{@cntfmts@counter@type@##1}{#3}}%
  }%
}

\newrobustcmd*\ReadCounterFrom[3][cntfmts]{%
  \@cntfmts@create@read@counter{#1}{#2}{#3}%
}

% ----------------------------------------------------------------------------
% pattern formats
% #1: key
% #2: number presentation command like \@alph
\def\@cntfmts@new@pattern@format#1#2{%
  \csdef{@cntfmts@counter@type@#1}{#2}%
}

\newrobustcmd*\NewPatternFormat[2]{%
  \@cntfmts@new@pattern@format{#1}{#2}%
}
\@onlypreamble\NewPatternFormat

% ----------------------------------------------------------------------------
% interpret and print pattern:
% #1: module
% #2: list
% #3: pattern
\def\@cntfmts@read@counter@pattern#1#2#3{%
  \def\@cntfmts@parsed@pattern{#3\@empty}%
  \forlistcsloop{\@cntfmts@replace@pattern{#1}}{#2}%
}
% #1: module
% #2: pattern-key
\def\@cntfmts@replace@pattern#1#2{%
  \cnltx@replace@all\@cntfmts@parsed@pattern
    {#2}{\csuse{@#1@read@#2@counter}}%
}

\newrobustcmd*\ReadCounterPattern[2][cntfmts]{%
  \@cntfmts@read@counter@pattern{#1}{@#1@counter@ids}{#2}\@cntfmts@parsed@pattern
}

\newrobustcmd*\ReadCounterPatternFrom[2][cntfmts]{%
  \cnltx@expandargs(nno)\@cntfmts@read@counter@pattern{#1}{@#1@counter@ids}{#2}%
  \@cntfmts@parsed@pattern
}

% #1: save pattern in
% #2: save interpretation in
% #3: pattern
% #4: module
\newcommand*\@cntfmts@save@counter@pattern[4]{%
  \@cntfmts@read@counter@pattern{#4}{@#4@counter@ids}{#3}%
  \let#2\@cntfmts@parsed@pattern
  \def#1{#3}%
}

\newcommand*\@cntfmts@esave@counter@pattern[4]{%
  \@cntfmts@read@counter@pattern{#4}{@#4@counter@ids}{#3}%
  \edef#2{\@cntfmts@parsed@pattern}%
  \def#1{#3}%
}

\newrobustcmd*\SaveCounterPattern[4][cntfmts]{%
  \@cntfmts@save@counter@pattern{#2}{#3}{#4}{#1}%
}

\newrobustcmd*\SaveCounterPatternFrom[4][cntfmts]{%
  \cnltx@expandargs(nno)\@cntfmts@save@counter@pattern{#2}{#3}{#4}{#1}%
}

\newrobustcmd*\eSaveCounterPattern[4][cntfmts]{%
  \@cntfmts@esave@counter@pattern{#2}{#3}{#4}{#1}%
}

\newrobustcmd*\eSaveCounterPatternFrom[4][cntfmts]{%
  \cnltx@expandargs(nno)\@cntfmts@esave@counter@pattern{#2}{#3}{#4}{#1}%
}

% ----------------------------------------------------------------------------
% predefined formats and pattern
\NewPatternFormat{a}{\@alph}
\NewPatternFormat{A}{\@Alph}
\NewPatternFormat{1}{\@arabic}
\NewPatternFormat{r}{\@roman}
\NewPatternFormat{R}{\@Roman}

\ifdef\c@paragraph
  {\AddCounterPattern{paragraph}{pg}}{}
\ifdef\c@subsubsection
  {\AddCounterPattern{subsubsection}{ssse}}{}
\ifdef\c@subsection
  {\AddCounterPattern{subsection}{sse}}{}
\ifdef\c@section
  {\AddCounterPattern{section}{se}}{}
\ifdef\c@chapter
  {\AddCounterPattern{chapter}{ch}}{}

\endinput

% HISTORY:
2012/09/30 v0.2beta - first version (as part of the `exsheets' bundle)
2012/11/08 v0.4     - stepped number with `exsheets' until now; next stepping
                      won't synchronize but will step to whatever deems
                      appropriate
2012/04/23 v0.5     - changed tests for heading commands to test explicitly for
                      the associated counters
                    - change test for counter in \@cntfmts@add@counter@pattern
                      from \the<ounter> to \c@<ounter>
                    - rename tokenlist test macros to use cntformats'
                      namespace
2013/10/11 v0.6     - use my `cnltx-base' package for the programming tools it
                      provides; this allows to get rid of the artefacts in the
                      expanded patterns