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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
%% l3ctr2e LaTeX2e counters in LaTeX3 ways
%%
%% Copyright (C) 2012-2013 by Simon Sigurdhsson <sigurdhsson@gmail.com>
%%
%% 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 Simon Sigurdhsson.
%%
%% This work consists of the file l3ctr2e.tex and the derived file
%% l3ctr2e.sty.
\documentclass{skdoc}
\usepackage{hologo,booktabs,varioref,cleveref,hyperref}
\ExplSyntaxOn
\cs_set_protected_nopar:Npn\ExplHack{
\char_set_catcode_letter:n{ 58 }
\char_set_catcode_letter:n{ 95 }
}
\ExplSyntaxOff
\ExplHack
% Declare the target files
\SelfPreambleTo{\mypreamble}
\DeclareFile[key=package,preamble=\mypreamble]{l3ctr2e.sty}
% This is where the documentation begins
\begin{document}
% Change & version info
\version{0.1d}
\changes{0.1}{Initial version}
\changes{0.1c}{Moved attachment of global parent from \cs{__ctr_define:n} to \cs{ctr_new:n}}
% Metadata
\package[ctan=l3ctr2e,vcs=https://github.com/urdh/l3ctr2e]{l3ctr2e}
\title{The \textbf{\thepackage} package}
\author{Simon Sigurdhsson}
\email{sigurdhsson@gmail.com}
% First page
\maketitle
\begin{abstract}
A reimplementation of some of the \LaTeXe\ counter functionality
in \LaTeX3 code. It should be backwards-compatible with \LaTeXe\
counters.
\end{abstract}
\tableofcontents
\section{Documentation}
This package is intended to complement the \LaTeXe\ counter macros
with \LaTeX3 counterparts. The new macros use the same internal
counters and macros as the \LaTeXe\ ones, and as such they should be
fully backwards-compatible. The package is not terribly complicated
but could be useful in adapting (or rewriting) \LaTeXe\ packages or
document styles to \LaTeX3 standards.
\Warning{The package is not backwards-compatible as it is now: the
resetting of child counters is not implemented in a backwards-%
compatible way.}
\subsection{Options}
\Option{replace}\WithValues{true,false}\AndDefault{false}
There is only one option, \opt{replace}, which will make the package
replace the original \LaTeXe\ public API macros with new, \LaTeX3-%
based ones (with the same API). Its default value is \texttt{false}.
\subsection{Public API}
The public \LaTeX3 API has macros corresponding to the macros of the
\LaTeXe\ API according to \vref{tab:macros}. In addition to the
direct replacements there are also a number of new macros which are
missing in the original \LaTeXe\ API.
\begin{table}[tbp]
\centering
\caption{\LaTeX3 counterparts of the \LaTeXe\ counter macros}
\label{tab:macros}
\begin{tabular}{ll}
\toprule
\textbf{\LaTeXe} & \textbf{\LaTeX3} \\
\midrule
\cs*{newcounter} & \cs*{ctr_new:n} \\
\cs*{stepcounter} & \cs*{ctr_gincr:n} \\
\cs*{refstepcounter} & \cs*{ctr_ref_gincr:n} \\
\cs*{addtocounter} & \cs*{ctr_gadd:nn} \\
\cs*{setcounter} & \cs*{ctr_gset:nn} \\
\cs*{value} & \cs*{ctr_use:n} \\
\cs*{arabic} & \cs*{ctr_use_arabic:n} \\
\cs*{alph} & \cs*{ctr_use_alph:n} \\
\cs*{Alph} & \cs*{ctr_use_Alph:n} \\
\cs*{roman} & \cs*{ctr_use_roman:n} \\
\cs*{Roman} & \cs*{ctr_use_Roman:n} \\
\cs*{fnsymbol} & \cs*{ctr_use_fn:n} \\
\bottomrule
\end{tabular}
\end{table}
\subsubsection{Creating counters}
\DescribeMacro\ctr_new:n{<counter name>}
\DescribeMacro\ctr_new:nn{<counter name>}{<parent counter>}
This macro creates a new counter, and is the direct counterpart of the
\cs{newcounter} \LaTeXe\ macro. It has two variants, one of which also
sets a parent counter (\emph{i.e.} a counter which will reset the new
counter when incremented), corresponding to using \cs{newcounter} with
an optional argument.
\subsubsection{Setting counters}
\DescribeMacro\ctr_gzero:n{<counter name>}
This macro has no \LaTeXe\ counterpart, but could be useful (and is
used internally by other \thepkg\ macros). It sets the counter value
to zero.
\DescribeMacro\ctr_gincr:n{<counter name>}
This macro increments the counter by one, and is the \LaTeX3
equivalent of \cs{stepcounter}.
\DescribeMacro\ctr_ref_gincr:n{<counter name>}
In addition to incementing the counter by one, this macro sets the
internal \LaTeXe\ macro \cs{@currentlabel}, so that references and
labels work properly. It is the \LaTeX3 variant of
\cs{refstepcounter}.
\DescribeMacro\ctr_gadd:nn{<counter name>}{<value>}
The \LaTeX3 counterpart of \cs{addtocounter}. Simply adds \meta{value}
to the counter.
\DescribeMacro\ctr_gset:nn{<counter name>}{<new value>}
Corresponds to \cs{setcounter}. Sets the counter to \meta{new value}.
\subsubsection{Displaying counters}
\LaTeXe\ provides a couple of different ways to express counters in
typeset text. These macros correspond to (and extend) the repertoire
of counter value macros provided by \LaTeXe\ using functionality from
the \pkg{l3int} package.
\DescribeMacro\ctr_use:n{<counter name>}
This is simply \cs{value}. It will simply use the value of the counter
(using \cs{int_use:c}) and as such could be used in a \pkg{l3int}
context whenever an integer value of the counter is needed.
\DescribeMacro\ctr_use_arabic:n{<counter name>}
This macro typesets the counter value as arabic numbers, and
corresponds to the \cs{arabic} macro.
\DescribeMacro\ctr_use_alph:n{<counter name>}
\DescribeMacro\ctr_use_Alph:n{<counter name>}
Typesets the counter value as lower-case or upper-case alphabetic
characters --- the corresponding \LaTeXe\ macros are \cs{alph} and
\cs{Alph}.
\DescribeMacro\ctr_use_roman:n{<counter name>}
\DescribeMacro\ctr_use_Roman:n{<counter name>}
Typesets the counter value as lower-case or upper-case roman numerals.
Corresponds to \cs{roman} and \cs{Roman}.
\DescribeMacro\ctr_use_fn:n{<counter name>}
Typesets the counter value as footnote symbols, and is intended for
use in footnote marks only. Corresponds to \cs{fnsymbol}.
\DescribeMacro\ctr_use_binary:n{<counter name>}
\DescribeMacro\ctr_use_octal:n{<counter name>}
\DescribeMacro\ctr_use_hex:n{<counter name>}
Typesets the counter value as binary, octal or hexadecimal numbers.
These macros have no \LaTeXe\ counterparts.
\DescribeMacro\ctr_use_custom:nn{<counter name>}{<item>,\meta{item},...}
Typesets the counter value according to the comma-separated list
provided (containing a list of items). Values larger than the number
of items in the list will cause an error.
\section{Known issues}
A list of current issues is available in the Github repository of this
package\footnote{\url{https://github.com/urdh/l3ctr2e/issues}}, but as
of the release of \theversion, there are no known issues.
%\begin{description}
% \item[\#6] ???
%\end{description}
If you discover any bugs in this package, please report them to the issue
tracker in the \thepackage\ Github repository.
\Implementation
\section{Implementation}
Start by including \pkg{expl3} and well as declaring the class.
\begin{MacroCode}{package}
\RequirePackage{expl3,l3keys2e,xparse}
\ProvidesExplClass{l3ctr2e}%
{2013/05/25}{0.1d}{LaTeX2e counters in LaTeX3 ways}
\end{MacroCode}
\subsection{Options}
\begin{MacroCode}{package}
\keys_define:nn{l3ctr2e}{
\end{MacroCode}
\begin{option}{replace}
\begin{MacroCode}{package}
replace .bool_set:N = \g__ctr_replace_latexe_bool,
replace .default:n = true,
replace .initial:n = false
\end{MacroCode}
\end{option}
\begin{MacroCode}{package}
}
\ProcessKeysPackageOptions{l3ctr2e}
\end{MacroCode}
\subsection{Messages}
One message is declared for future use.
\begin{MacroCode}{package}
\msg_new:nnn{l3ctr2e}{no-counter}{No~counter~`#1'~defined!}
\end{MacroCode}
\subsection{Private macros}
\begin{macro}{\__ctr_exists:nT}[1]
{Counter name}
\begin{MacroCode}{package}
\prg_new_conditional:Npnn\__ctr_exists:n#1{T}{
\int_if_exist:cTF{c@#1}{
\prg_return_true:
}{
\msg_error:nnn{l3ctr2e}{no-counter}{#1}
\prg_return_false:
}
}
\end{MacroCode}
\end{macro}
\begin{macro*}{\g__ctr_children__ctr_global_tl}
\begin{MacroCode}{package}
\tl_new:N\g__ctr_children__ctr_global_tl
\end{MacroCode}
\end{macro*}
\begin{macro}{\__ctr_define:n}[1]
{Counter name}
This does not yet fully correspond to \cs{@definecounter}.
\begin{MacroCode}{package}
\cs_new:Npn\__ctr_define:n#1{
\int_new:c{c@#1} % actually \newcount, incorrect! FIX!
\ctr_gzero:n{#1}
\tl_new:c{g__ctr_children_#1_tl}
\cs_gset:cpn{cl@#1}{}
\cs_gset:cpn{p@#1}{}
\cs_gset:cpn{the#1}{\ctr_use_arabic:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\__ctr_add_child:nn}[2]
{Parent counter name}
{Counter name}
This probably fully corresponds to \cs{@addtoreset}.
\begin{MacroCode}{package}
\cs_new:Npn\__ctr_add_child:nn#1#2{
\tl_gput_right:cn{g__ctr_children_#1_tl}{{#2}}
}
\end{MacroCode}
\end{macro}
\subsection{Public API}
\subsubsection{Creating counters}
\begin{macro}{\ctr_new:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_new:n#1{
\__ctr_define:n{#1}
\__ctr_add_child:nn{_ctr_global}{#1}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_new:nn}[2]
{Counter name}
{Parent counter}
\changes{0.1c}{Changed \cs{ctr_new:n} into \cs{__ctr_define:n}}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_new:nn#1#2{
\__ctr_define:n{#1}
\__ctr_add_child:nn{#2}{#1}
}
\end{MacroCode}
\end{macro}
\subsubsection{Setting counters}
\begin{macro}{\ctr_gzero:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gzero:n#1{
\__ctr_exists:nT{#1}{\int_gzero:c{c@#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_gset:nn}[2]
{Counter name}
{New value}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gset:nn#1#2{
\__ctr_exists:nT{#1}{\int_gset:cn{c@#1}{#2}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_gadd:nn}[2]
{Counter name}
{Value to add}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gadd:nn#1#2{
\__ctr_exists:nT{#1}{\int_gadd:cn{c@#1}{#2}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_gincr:n}[1]
{Counter name}
\changes{0.1c}{Now also resets grandchildren and so on}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_gincr:n#1{
\ctr_gadd:nn{#1}{\c_one}
\tl_if_exist:cT{g__ctr_children_#1_tl}{
\tl_map_inline:cn{g__ctr_children_#1_tl}{
\ctr_gincr:n{##1} % To reset children of the child
\ctr_gzero:n{##1}
}
}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_ref_gincr:n}[1]
{Counter name}
\changes{0.1a}{Fixed erroneous use of non-existent \cs{ctr_gincr:c}}
\changes{0.1c}{Redeclaration of \cs{@currentlabel} is now local and unprotected}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_ref_gincr:n#1{
\ctr_gincr:n{#1}
\cs_set:Npx\@currentlabel{
\use:c{p@#1}
\use:c{the#1}
}
}
\end{MacroCode}
\end{macro}
\subsubsection{Displaying counters}
\begin{macro}{\ctr_value:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_value:n#1{
\use:c{c@#1}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use:n#1{
\int_use:c{c@#1}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_arabic:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_arabic:n#1{
\int_to_arabic:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_alph:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_alph:n#1{
\int_to_alph:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_Alph:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_Alph:n#1{
\int_to_Alph:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_binary:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_binary:n#1{
\int_to_binary:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_hex:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_hex:n#1{
\int_to_hexadecimal:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_octal:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_octal:n#1{
\int_to_octal:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_roman:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_roman:n#1{
\int_to_roman:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_Roman:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_Roman:n#1{
\int_to_Roman:n{\ctr_use:n{#1}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_fn:n}[1]
{Counter name}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_fn:n#1{
\ensuremath{\ctr_use_custom:nn{#1}{
*, \dagger, \ddagger,
\mathsection,\mathparagraph,\|,
**, \dagger\dagger,\ddagger\ddagger
}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\ctr_use_custom:nn}[2]
{Counter name}
{Comma-separated list of items}
\begin{MacroCode}{package}
\cs_new:Npn\ctr_use_custom:nn#1#2{
\clist_set:Nn\l_tmpa_clist{#2}
\tl_clear:N\l_tmpa_tl
\int_zero:N\l_tmpa_int
\clist_map_inline:Nn\l_tmpa_clist{
\int_incr:N\l_tmpa_int
\tl_put_right:Nx\l_tmpa_tl{{\int_use:N\l_tmpa_int}{##1}}
}
\exp_args:Nnnx\int_to_symbols:nnn{\ctr_use:n{#1}}
{\clist_count:N\l_tmpa_clist}
{\tl_use:N\l_tmpa_tl}
}
\end{MacroCode}
\end{macro}
\subsection{Replacing the \LaTeXe\ API}
\begin{MacroCode}{package}
\bool_if:NT\g__ctr_replace_latexe_bool{
\end{MacroCode}
\begin{macro}{\newcounter}[2]
{Counter name}
{Parent counter}
\begin{MacroCode}{package}
\RenewDocumentCommand\newcounter{mo}{
\IfNoValueTF{#2}{\ctr_new:n{#1}}
{\ctr_new:nn{#1}{#2}}
}
\end{MacroCode}
\end{macro}
\begin{macro}{\stepcounter}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\stepcounter{m}{\ctr_gincr:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\refstepcounter}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\refstepcounter{m}{\ctr_ref_gincr:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\addtocounter}[2]
{Counter name}
{Value to add}
\begin{MacroCode}{package}
\RenewDocumentCommand\addtocounter{mm}{\ctr_gadd:nn{#1}{#2}}
\end{MacroCode}
\end{macro}
\begin{macro}{\setcounter}[2]
{Counter name}
{New value}
\begin{MacroCode}{package}
\RenewDocumentCommand\setcounter{mm}{\ctr_gset:nn{#1}{#2}}
\end{MacroCode}
\end{macro}
\begin{macro}{\value}[1]
{Counter name}
\changes{0.1d}{Return a command sequence, not a number}
\begin{MacroCode}{package}
\RenewDocumentCommand\value{m}{\ctr_value:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\arabic}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\arabic{m}{\ctr_use_arabic:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\alph}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\alph{m}{\ctr_use_alph:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\Alph}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\Alph{m}{\ctr_use_Alph:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\roman}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\roman{m}{\ctr_use_roman:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\Roman}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\Roman{m}{\ctr_use_Roman:n{#1}}
\end{MacroCode}
\end{macro}
\begin{macro}{\fnsymbol}[1]
{Counter name}
\begin{MacroCode}{package}
\RenewDocumentCommand\fnsymbol{m}{\ctr_use_fn:n{#1}}
\end{MacroCode}
\end{macro}
\begin{MacroCode}{package}
}
\end{MacroCode}
\begin{MacroCode}{package}
\endinput
\end{MacroCode}
\Finale
\section{Installation}
The easiest way to install this package is using the package
manager provided by your \LaTeX\ installation if such a program
is available. Failing that, provided you have obtained the package
source (\file{l3ctr2e.tex} and \file{Makefile}) from either CTAN
or Github, running \texttt{make install} inside the source directory
works well. This will extract the documentation and code from
\file{l3ctr2e.tex}, install all files into the TDS tree at
\texttt{TEXMFHOME} and run \texttt{mktexlsr}.
If you want to extract code and documentation without installing
the package, run \texttt{make all} instead. If you insist on not
using \texttt{make}, remember that packages distributed using
\pkg{skdoc} must be extracted using \texttt{pdflatex}, \emph{not}
\texttt{tex} or \texttt{latex}.
\PrintChanges
\PrintIndex
\end{document}
|