Debian Codegraph @

texmf-dist/source/latex-dev/base/ltcounts.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 1993-2026
4 % The LaTeX Project and any individual authors listed elsewhere
5 % in this file.
6 %
7 % This file is part of the LaTeX base system.
8 % -------------------------------------------
9 %
10 % It may be distributed and/or modified under the
11 % conditions of the LaTeX Project Public License, either version 1.3c
12 % of this license or (at your option) any later version.
13 % The latest version of this license is in
14 % https://www.latex-project.org/lppl.txt
15 % and version 1.3c or later is part of all distributions of LaTeX
16 % version 2008 or later.
17 %
18 % This file has the LPPL maintenance status "maintained".
19 %
20 % The list of all files belonging to the LaTeX base distribution is
21 % given in the file `manifest.txt'. See also `legal.txt' for additional
22 % information.
23 %
24 % The list of derived (unpacked) files belonging to the distribution
25 % and covered by LPPL is defined by the unpacking scripts (with
26 % extension .ins) which are part of the distribution.
27 %
28 % \fi
29 %
30 % \iffalse
31 %%% From File: ltcounts.dtx
32 %
33 %<*driver>
34 % \fi
35 \ProvidesFile{ltcounts.dtx}
36 [2026/03/10 v1.2c LaTeX Kernel (Counters)]
37 % \iffalse
38 \documentclass{ltxdoc}
39 \GetFileInfo{ltcounts.dtx}
40 \title{\filename}
41 \date{\filedate}
42 \author{%
43 Johannes Braams\and
44 David Carlisle\and
45 Alan Jeffrey\and
46 Leslie Lamport\and
47 Frank Mittelbach\and
48 Chris Rowley\and
49 Rainer Sch\"opf}
50
51 \begin{document}
52 \MaintainedByLaTeXTeam{latex}
53 \maketitle
54 \DocInput{\filename}
55 \end{document}
56 %</driver>
57 % \fi
58 %
59 % \providecommand\env[1]{\texttt{#1}}
60 %
61 % \changes{v1.0c}{1994/03/29}
62 % {Create file from parts of ltmiscen and ltherest.}
63 % \changes{v1.1a}{1994/05/19}{Extracted file from ltcntlen.}
64 % \changes{v1.1d}{1996/04/23}{Documentation improvements}
65 % \changes{v1.2c}{2025/12/31}{Added \cs{newcounteralias}}
66 % \section{Counters and Lengths}
67 % Commands for defining and using counters.
68 % This file defines the following commands. In each case \marg{counter} may be
69 % |*| denoting the current counter as set by a previous |\refstepcounter|.
70 %
71 % \DescribeMacro{\newcounter}
72 % To define a new counter.
73 %
74 % \DescribeMacro{\newcounteralias}
75 % To define an alias name for a counter.
76 %
77 % \DescribeMacro{\setcounter}
78 % To set the value of counters.
79 %
80 % \DescribeMacro{\addtocounter}
81 % Increase the \marg{counter} |#1| by the number |#2|.
82 %
83 % \DescribeMacro{\stepcounter}
84 % Increase the \marg{counter} by one.
85 %
86 % \DescribeMacro{\refstepcounter}
87 % Increase the \marg{counter} by one, also setting the value used by |\label|.
88 %
89 %
90 % \DescribeMacro{\value}
91 % For accessing the value of the counter as a \TeX\ number (as opposed
92 % to |\the|\meta{counter} which expands to the \emph{printed}
93 % representation of \meta{counter})
94 %
95 % \DescribeMacro\arabic
96 % |\arabic|\marg{counter}: 1, 2, 3, \ldots
97 %
98 % \DescribeMacro\roman
99 % |\roman|\marg{counter}: i, ii, iii, \ldots
100 %
101 % \DescribeMacro\Roman
102 % |\Roman|\marg{counter}: I, II, III, \ldots
103 %
104 % \DescribeMacro\alph
105 % |\alph|\marg{counter}: a, b, c, \ldots
106 %
107 % \DescribeMacro\Alph
108 % |\Alph|\marg{counter}: A, B, C, \ldots
109 %
110 % \DescribeMacro\fnsymbol
111 % |\fnsymbol|\marg{counter}: $*$, $\dagger$, $\ddagger$, \ldots
112 %
113 %
114 % \DescribeMacro\counterwithin
115 % |\counterwithin|\oarg{format}\marg{counter}\marg{within-counter}:
116 % Resets \meta{counter} whenever
117 % \meta{within-counter} is stepped. Also
118 % redefines |\the|\meta{counter} command to produce
119 % |\the|\meta{within-counter}.\meta{format}\marg{counter} with \cs{arabic} as
120 % the default for \meta{format}.
121 % Star form omits redefining the print representation.
122 %
123 % The |*| alias for the current counter may not be used in either argument.
124 %
125 % \DescribeMacro\counterwithout
126 % |\counterwithout|\oarg{format}\marg{counter}\marg{within-counter}:
127 % Removes \meta{counter} from the reset list of \meta{within-counter}.
128 % Also
129 % redefines |\the|\meta{counter} command to produce
130 % \meta{format}\marg{counter} with \cs{arabic} as
131 % the default for \meta{format}.
132 % Star form omits redefining the print representation.
133 %
134 % The |*| alias for the current counter may not be used in either argument.
135 %
136 % \MaybeStop{}
137 %
138 % \begin{macrocode}
139 %<*2ekernel>
140 % \end{macrocode}
141 %
142 % \subsection{Document command and environment counter macros}
143 %
144 % An environment \texttt{foo} has an associated counter defined by the
145 % following control sequences:
146 % \begin{center}
147 % \begin{tabular}{lp{.75\textwidth}}
148 % |\c@foo| & Contains the counter's numerical value.
149 % It is defined by |\newcount\foocounter|.\\
150 % |\thefoo| & Macro that expands to the printed value of |\foocounter|.
151 % For example, if sections are numbered within chapters,
152 % and section headings look like
153 %
154 % Section II-3. The Nature of Counters
155 %
156 % then |\thesection| might be defined by:\newline
157 % |\def\thesection|\newline\hspace*{2em}
158 % |{\@Roman{\c@chapter}-\@arabic{\c@section}}|\\
159 % |\theHfoo|& Macro that is used to create destination names. It should give a unique
160 % value across the document for every |\refstepcounter|.\\
161 % |\p@foo| & Macro that expands to a printed `reference prefix' of
162 % counter foo. Any |\ref| to a value created by counter
163 % foo will produce the expansion of |\p@foo\thefoo| when
164 % the |\label| command is executed. See file \texttt{ltxref.dtx}
165 % for an extension of this mechanism.\\
166 % |\cl@foo| & List of counters to be reset when foo stepped.
167 % Has format
168 % |\@elt{countera}\@elt{counterb}\@elt{counterc}|.\\
169 % |\alias@ctr@foo| & if foo is an alias counter created with |\newcounteralias| this
170 % command contains the name of the root counter.
171 % \end{tabular}
172 % \end{center}
173 % For some environments the counter is named slightly differently,
174 % e.g., the counters associated with the \texttt{enumerate}
175 % environment have the names \texttt{enumi}, \texttt{enumii},
176 % \texttt{enumiii}, and \texttt{enumiv} depending on the nesting
177 % level.
178 %
179 % The same internal macros are defined for counters used by commands
180 % such as \cs{chapter}, \cs{section}, etc.
181 %
182 % \textbf{NOTE:}\\
183 % |\thefoo| and |\p@foo| \emph{must} be defined in such a way that
184 % |\edef\bar{\thefoo}| or |\edef\bar{\p@foo}|
185 % defines |\bar| so that it will evaluate to the counter value at the
186 % time of the |\edef|, even after |\foocounter| and any other counters
187 % have been changed. This will happen if you use the standard commands
188 % |\@arabic|, |\@Roman|, etc.
189 %
190 %
191 % The following commands are used to define and modify counters.
192 %
193 % |\refstepcounter|\marg{foo}\\
194 % Same as |\stepcounter|, but it also defines
195 % |\@currentlabel|, |\@currentHref| and |\@currentcounter|
196 % and so that a subsequent
197 % |\label|\marg{bar} command causes |\ref|\marg{bar} to
198 % generate the current value of counter \meta{foo}.
199 %
200 % |\@definecounter|\marg{foo}\\
201 % Initializes counter \marg{foo} (with empty reset list), defines
202 % |\p@foo| and |\thefoo| to be null and |\theHfoo| to be
203 % |\number\value{foo}|. Also adds \meta{foo}
204 % to |\cl@@ckpt| --
205 % the reset list of a dummy counter |@ckpt| used for taking
206 % checkpoints for the |\include| system.
207 %
208 % |\@addtoreset|\marg{foo}\marg{bar} :
209 % Adds counter \meta{foo} to the list of counters
210 % |\cl@bar| to be reset when counter \meta{bar} is stepped.
211 %
212 % |\@removefromreset|\marg{foo}\marg{bar} :
213 % Removes counter \meta{foo} to the list of counters
214 % |\cl@bar| to be reset when counter \meta{bar} is stepped.
215 %
216 %
217 % \begin{macro}{\setcounter}
218 % \changes{v1.0d}{1994/04/09}
219 % {\cs{@nocnterr} now has counter name argument}
220 % \changes{v1.0e}{1994/04/17}
221 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
222 % |\setcounter|\marg{foo}\marg{val} :
223 % Globally sets |\foocounter| equal to \meta{val}.
224 % \begin{macrocode}
225 \def\setcounter#1#2{%
226 \@ifundefined{c@#1}%
227 {\@nocounterr{#1}}%
228 {\global\csname c@#1\endcsname#2\relax}}
229 % \end{macrocode}
230 % \end{macro}
231 %
232 % \begin{macro}{\addtocounter}
233 % \changes{v1.0d}{1994/04/09}
234 % {\cs{@nocnterr} now has counter name argument}
235 % \changes{v1.0e}{1994/04/17}
236 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
237 % |\addtocounter|\marg{foo}\marg{val}
238 % Globally increments |\foocounter| by \meta{val}.
239 % \begin{macrocode}
240 \def\addtocounter#1#2{%
241 \@ifundefined{c@#1}%
242 {\@nocounterr{#1}}%
243 {\global\advance\csname c@#1\endcsname #2\relax}}
244 % \end{macrocode}
245 % \end{macro}
246 %
247 % \begin{macro}{\newcounter}
248 % |\newcounter|\marg{newctr}\oarg{oldctr}
249 % Defines \meta{newctr} to be a counter, which is reset when counter
250 % \meta{oldctr} is stepped. If \meta{newctr} already defined
251 % produces
252 % `|c@newctr already defined|' error.
253 % \begin{macrocode}
254 \def\newcounter#1{%
255 \expandafter\@ifdefinable \csname c@#1\endcsname
256 {\@definecounter{#1}}%
257 \@ifnextchar[{\@newctr{#1}}{}}
258 % \end{macrocode}
259 % \end{macro}
260 %
261 % \begin{macro}{\newcounteralias}
262 % \changes{v1.2c}{2025/12/31}{New command \cs{newcounteralias}}
263 % |\newcounteralias|\marg{aliasctr}\marg{rootctr}
264 % Defines \meta{aliasctr} as an alias for the counter \meta{rootctr}.
265 % The root counter can itself be an alias counter (it is not
266 % recommended to chain counter names like this as keeping track of the
267 % representations can get tricky, but explicit tests to avoid
268 % that seems unnecessary).
269 % The various representation commands of the alias counter call the
270 % respective representations of the root. This means that if
271 % a representation of the root is redefined, e.g. to make
272 % \cs{theH}\meta{counter} unique then the alias follows.
273 % It is possible to redefine an alias representation,
274 % but this then breaks the connection to the root representation.
275 %
276 % Every alias counter FOO has an associated command \cs{alias@ctr@FOO} which points to
277 % the root. This allows to test if a counter is an alias counter and also allows to
278 % find the root.
279 %
280 % We do not rollback the definition to avoid errors if packages use it
281 % that do not rollback.
282 % \begin{macrocode}
283 \newcommand*{\newcounteralias}[2]{%
284 \@ifundefined{c@#2}{%
285 \@nocounterr{#2}%
286 }{%
287 \expandafter\@ifdefinable\csname c@#1\endcsname{%
288 \global\expandafter\let
289 \csname c@#1\expandafter\endcsname\csname c@#2\endcsname
290 \expandafter\edef\csname the#1\endcsname{\expandafter\noexpand\csname the#2\endcsname}%
291 \expandafter\edef\csname theH#1\endcsname{\expandafter\noexpand\csname theH#2\endcsname}%
292 \expandafter\edef\csname p@#1\endcsname{\expandafter\noexpand\csname p@#2\endcsname}%
293 \expandafter\edef\csname cl@#1\endcsname{\expandafter\noexpand\csname cl@#2\endcsname}%
294 \expandafter\def\csname alias@ctr@#1\endcsname{#2}%
295 }%
296 }%
297 }
298 % \end{macrocode}
299 % \end{macro}
300 %
301 % \begin{macro}{\value}
302 % |\value|\marg{ctr}
303 % produces the value of counter \meta{ctr}, for use with
304 % a |\setcounter| or |\addtocounter| command.
305 % \begin{macrocode}
306 \def\value#1{\csname c@#1\endcsname}
307 % \end{macrocode}
308 % \end{macro}
309 %
310 % \begin{macro}{\c@*}
311 % \changes{v1.2a}{2025/01/22}{\cs{c@*} added (gh/1632)}
312 % Make the current counter available as a \LaTeX\ counter
313 % with name |*|, so |\alph*| returns the current
314 % counter as a letter, |\stepcounter{*}| increments the current
315 % counter, etc.
316 % \begin{macrocode}
317 \protected\expandafter
318 \def\csname c@*\endcsname{\value\@currentcounter}
319 % \end{macrocode}
320 % \end{macro}
321 %
322 %\begin{macro}{\@newctr}
323 % \changes{v1.0d}{1994/04/09}
324 % {\cs{@nocnterr} now has counter name argument}
325 % \changes{v1.0e}{1994/04/17}
326 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
327 % \begin{macrocode}
328 \def\@newctr#1[#2]{%
329 \@ifundefined{c@#2}{\@nocounterr{#2}}{\@addtoreset{#1}{#2}}}
330 % \end{macrocode}
331 % \end{macro}
332 %
333 % \begin{macro}{\stepcounter}
334 % \changes{LaTeX209}{1992/11/23}{Replaced \{\} in \cs{stepcounter} by
335 % \cs{begingroup} \cs{endgroup} to avoid adding an empty ord in
336 % math mode}
337 % \changes{v1.0d}{1994/04/09}
338 % {Use \cs{addtocounter} to have name checked}
339 % |\stepcounter|{foo}
340 % Globally increments counter |\c@FOO|
341 % and resets all subsidiary counters.
342 % \begin{macrocode}
343 \def\stepcounter#1{%
344 \addtocounter{#1}\@ne
345 \begingroup
346 \let\@elt\@stpelt
347 \csname cl@#1\endcsname
348 \endgroup}
349 % \end{macrocode}
350 % \end{macro}
351 %
352 % \begin{macro}{\@stpelt}
353 % \changes{v1.1h}{2015/01/10}{Reset all within counters in one go (latexrelease)}
354 %
355 % Rather than resetting the ``within'' counter to zero we set it to
356 % $-1$ and then run |\stepcounter| that moves it to $0$ and also
357 % initiates resetting the next level down.
358 % \begin{macrocode}
359 %</2ekernel>
360 %<latexrelease>\IncludeInRelease{2015/01/01}{\@stpelt}
361 %<latexrelease> {Reset nested counters}%
362 %<*2ekernel|latexrelease>
363 \def\@stpelt#1{\global\csname c@#1\endcsname \m@ne\stepcounter{#1}}%
364 %<latexrelease>\EndIncludeInRelease
365 %</2ekernel|latexrelease>
366 %<latexrelease>\IncludeInRelease{0000/00/00}{\@stpelt}
367 %<latexrelease> {Reset nested counters}%%
368 %<latexrelease>\def\@stpelt#1{\global\csname c@#1\endcsname \z@}%
369 %<latexrelease>\EndIncludeInRelease
370 %<*2ekernel>
371 % \end{macrocode}
372 % \end{macro}
373 %
374 % \begin{macro}{\cl@@ckpt}
375 % \begin{macrocode}
376 \def\cl@@ckpt{\@elt{page}}
377 % \end{macrocode}
378 % \end{macro}
379 %
380 % \begin{macro}{\@definecounter}
381 % \changes{v1.1b}{1995/05/20}{Streamlined code}
382 % \changes{v1.1c}{1995/05/20}{And do it right}
383 % \changes{v1.1n}{2023/11/07}{Do not change \cs{the...} if already defined (gh/823)}
384 % \changes{v1.1o}{2024/09/20}{define theHfoo (used for internal links)}
385 % \changes{v1.1p}{2024/10/26}{Fully expand counter name in theHfoo commands (gh/1508)}
386 % \changes{v1.2c}{2025/12/31}{Add support for alias counter}
387 % Older definitions of \cs{@definecounter} called unconditionally \cs{newcount}.
388 % This had the effect that connection to an alias counter could break if a user
389 % did, e.g.,
390 % \begin{verbatim}
391 % \newcounter{theorem}
392 % \newcounteralias{lemma}{theorem}
393 % \newtheorem{theorem}{Theorem}
394 % \newtheorem{lemma}{Lemma}
395 % \end{verbatim}
396 % The command was therefore changed and now tests if the count already exist.
397 % If the counter to define is an already existing alias counter then we do not reset
398 % the representations and do not add it to \texttt{@ckpt}.
399 % \begin{macrocode}
400 %</2ekernel>
401 %<latexrelease>\IncludeInRelease{2026/06/01}{\@definecounter}
402 %<latexrelease> {add aliascounter}%
403 %<*2ekernel|latexrelease>
404 \def\@definecounter#1{%
405 \@ifundefined{c@#1}{\expandafter\newcount\csname c@#1\endcsname}{}%
406 \setcounter{#1}\z@
407 \@ifundefined{alias@ctr@#1}
408 {\global\expandafter\let\csname cl@#1\endcsname\@empty
409 \@addtoreset{#1}{@ckpt}%
410 \global\expandafter\let\csname p@#1\endcsname\@empty
411 \expandafter\xdef\csname theH#1\endcsname{%
412 \noexpand\the\noexpand\value{#1}}}{}%
413 % \end{macrocode}
414 % If \cs{the\#1} is undefined or \cs{relax} we define it with the
415 % standard definition for counters, otherwise we warn. This will
416 % catch, for example, that somebody defines a counter named
417 % ``index'' conflicting with the \env{theindex} environment.
418 % \begin{macrocode}
419 \expandafter
420 \ifx\csname the#1\endcsname\relax
421 \expandafter
422 \gdef\csname the#1\expandafter\endcsname\expandafter
423 {\expandafter\@arabic\csname c@#1\endcsname}%
424 \else
425 \@latex@warning{Command `\string\the#1' already
426 defined -- not changed}%
427 \fi}
428 %<latexrelease>\EndIncludeInRelease
429 %</2ekernel|latexrelease>
430 %<latexrelease>\IncludeInRelease{2024/11/01}{\@definecounter}
431 %<latexrelease> {provide theHfoo commands}%
432 %<latexrelease>\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
433 %<latexrelease> \setcounter{#1}\z@
434 %<latexrelease> \global\expandafter\let\csname cl@#1\endcsname\@empty
435 %<latexrelease> \@addtoreset{#1}{@ckpt}%
436 %<latexrelease> \global\expandafter\let\csname p@#1\endcsname\@empty
437 %<latexrelease> \expandafter\xdef\csname theH#1\endcsname{%
438 %<latexrelease> \noexpand\the\noexpand\value{#1}}%
439 % \end{macrocode}
440 % If \cs{the\#1} is undefined or \cs{relax} we define it with the
441 % standard definition for counters, otherwise we warn. This will
442 % catch, for example, that somebody defines a counter named
443 % ``index'' conflicting with the \env{theindex} environment.
444 % \begin{macrocode}
445 %<latexrelease> \expandafter
446 %<latexrelease> \ifx\csname the#1\endcsname\relax
447 %<latexrelease> \expandafter
448 %<latexrelease> \gdef\csname the#1\expandafter\endcsname\expandafter
449 %<latexrelease> {\expandafter\@arabic\csname c@#1\endcsname}%
450 %<latexrelease> \else
451 %<latexrelease> \@latex@warning{Command `\string\the#1' already
452 %<latexrelease> defined -- not changed}%
453 %<latexrelease> \fi}
454 %<latexrelease>\EndIncludeInRelease
455 %<latexrelease>\IncludeInRelease{0000/00/00}{\@definecounter}
456 %<latexrelease> {provide theHfoo commands}%%
457 %<latexrelease>\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
458 %<latexrelease> \setcounter{#1}\z@
459 %<latexrelease> \global\expandafter\let\csname cl@#1\endcsname\@empty
460 %<latexrelease> \@addtoreset{#1}{@ckpt}%
461 %<latexrelease> \global\expandafter\let\csname p@#1\endcsname\@empty
462 %<latexrelease> \expandafter
463 %<latexrelease> \ifx\csname the#1\endcsname\relax
464 %<latexrelease> \expandafter
465 %<latexrelease> \gdef\csname the#1\expandafter\endcsname\expandafter
466 %<latexrelease> {\expandafter\@arabic\csname c@#1\endcsname}%
467 %<latexrelease> \else
468 %<latexrelease> \@latex@warning{Command `\string\the#1' already
469 %<latexrelease> defined -- not changed}%
470 %<latexrelease> \fi}
471 %<latexrelease>\EndIncludeInRelease
472 %<*2ekernel>
473 % \end{macrocode}
474 % \end{macro}
475 % \begin{macro}{\@kernel@get@ctr@root}
476 % A helper command to retrieve the root counter for messages.
477 % \begin{macrocode}
478 \def\@kernel@get@ctr@root#1{%
479 \@ifundefined{alias@ctr@#1}{#1}
480 {\expandafter\@kernel@get@ctr@root\csname alias@ctr@#1\endcsname}}
481 % \end{macrocode}
482 % \end{macro}
483 %
484 % \begin{macro}{\@addtoreset}
485 % \changes{v1.1o}{2024/09/20}{add the parent theHfoo if a counter is reset}
486 % \changes{v1.1p}{2024/10/26}{Fully expand counter name in theHfoo commands (gh/1508)}
487 % If a counter is reset when a parent counter changes it no longer has an unique value
488 % across the document. As |\theH<counter>| should be unique
489 % this representation is changed to include also the
490 % representation of the parent. This is not 100\% guaranteed to work
491 % but has been used this way by hyperref for many years without causing problems.
492 % \changes{v1.2c}{2025/12/31}{Support alias counters}
493 % Changing \cs{cl@counter} and \cs{theHcounter} isn't trivial to do
494 % if the counter is an alias counter: e.g. only redefining \cs{theH}\meta{counter} from
495 % the alias counter breaks the connection to the theH-representation of the
496 % root counter. Redefining both \cs{theH}-representations could overwrite
497 % changes made by the user. If there is a chain of alias counters it gets even
498 % more complicated to keep everything in sync.
499 % So unlike the package \pkg{aliascnt} we avoid these complications and
500 % do not allow alias counters as arguments to \cs{@addtoreset} and \cs{@removefromreset}.
501 % \begin{macrocode}
502 %</2ekernel>
503 %<latexrelease>\IncludeInRelease{2026/06/01}{\@addtoreset}
504 %<latexrelease> {support alias counter}%
505 %<*2ekernel|latexrelease>
506 \def\@addtoreset#1#2{%
507 \@ifundefined{alias@ctr@#1}{%
508 \@ifundefined{alias@ctr@#2}
509 {\expandafter\@cons\csname cl@#2\endcsname {{#1}}%
510 \expandafter\xdef\csname theH#1\endcsname{%
511 \expandafter\noexpand\csname theH#2\endcsname.%
512 \noexpand\the\noexpand\value{#1}}}%
513 {\@latex@warning{Alias counters can not be used in a counter reset.\MessageBreak
514 Use '\@kernel@get@ctr@root{#2}' instead of '#2'.}}}
515 {\@latex@warning{Alias counters can not be used in a counter reset.\MessageBreak
516 Use '\@kernel@get@ctr@root{#1}' instead of '#1'.}}}
517 %<latexrelease>\EndIncludeInRelease
518 %</2ekernel|latexrelease>
519 %<latexrelease>\IncludeInRelease{2024/11/01}{\@addtoreset}
520 %<latexrelease> {provide theHfoo commands}%
521 %<latexrelease>\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}%
522 %<latexrelease> \expandafter\xdef\csname theH#1\endcsname{%
523 %<latexrelease> \expandafter\noexpand\csname theH#2\endcsname.%
524 %<latexrelease> \noexpand\the\noexpand\value{#1}}%
525 %<latexrelease>}
526 %<latexrelease>\EndIncludeInRelease
527 %<latexrelease>\IncludeInRelease{0000/00/00}{\@addtoreset}
528 %<latexrelease> {provide theHfoo commands}%%
529 %<latexrelease>\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}}
530 %<latexrelease>\EndIncludeInRelease
531 %<*2ekernel>
532 % \end{macrocode}
533 % \end{macro}
534 %
535 %
536 % \begin{macrocode}
537 %</2ekernel>
538 % \end{macrocode}
539 %
540 %
541 % \begin{macro}{\@removefromreset}
542 % \changes{v1.1k}{2018/03/08}{Interface added}
543 % \changes{v1.2c}{2025/12/31}{Support for alias counter}
544 % \begin{macrocode}
545 %<latexrelease>\IncludeInRelease{2026/06/01}{\@addtoreset}
546 %<latexrelease> {support alias counter}%
547 %<*2ekernel|latexrelease>
548 \def\@removefromreset#1#2{%
549 % \end{macrocode}
550 % Even through this is internal and the programmer should know what
551 % he/she is doing we test here if counter |#2| is defined. If not,
552 % the execution would run into a tight loop.
553 % As in |\@addtoreset| do not allow alias counters as arguments.
554 % \begin{macrocode}
555 \@ifundefined{c@#2}\relax
556 {\@ifundefined{alias@ctr@#1}{%
557 \@ifundefined{alias@ctr@#2}{%
558 \begingroup
559 \expandafter\let\csname c@#1\endcsname\@removefromreset
560 \def\@elt##1{%
561 \expandafter\ifx\csname c@##1\endcsname\@removefromreset
562 \else
563 \noexpand\@elt{##1}%
564 \fi}%
565 \expandafter\xdef\csname cl@#2\endcsname
566 {\csname cl@#2\endcsname}%
567 \endgroup}%
568 {\@latex@warning{Alias counters can not be removed from a reset.\MessageBreak
569 Use '\@kernel@get@ctr@root{#2}' instead of '#2'.}}}
570 {\@latex@warning{Alias counters can not be removed from a reset.\MessageBreak
571 Use '\@kernel@get@ctr@root{#1}' instead of '#1'.}}}}
572 %<latexrelease>\EndIncludeInRelease
573 %</2ekernel|latexrelease>
574 % \end{macrocode}
575 %
576 % \begin{macrocode}
577 %<latexrelease>\IncludeInRelease{2018-04-01}
578 %<latexrelease> {\@removefromreset}{Add interfaces}%
579 %<latexrelease>\def\@removefromreset#1#2{%
580 % \end{macrocode}
581 % Even through this is internal and the programmer should know what
582 % he/she is doing we test here if counter |#2| is defined. If not,
583 % the execution would run into a tight loop.
584 % \begin{macrocode}
585 %<latexrelease> \@ifundefined{c@#2}\relax
586 %<latexrelease> {\begingroup
587 %<latexrelease> \expandafter\let\csname c@#1\endcsname\@removefromreset
588 %<latexrelease> \def\@elt##1{%
589 %<latexrelease> \expandafter\ifx\csname c@##1\endcsname\@removefromreset
590 %<latexrelease> \else
591 %<latexrelease> \noexpand\@elt{##1}%
592 %<latexrelease> \fi}%
593 %<latexrelease> \expandafter\xdef\csname cl@#2\endcsname
594 %<latexrelease> {\csname cl@#2\endcsname}%
595 %<latexrelease> \endgroup}}
596 % \end{macrocode}
597 % \end{macro}
598 %
599 % \begin{macro}{\@ifbothcounters}
600 % \changes{v1.1k}{2018/03/08}{Interface added}
601 % \changes{v1.2a}{2025/01/22}{Guard against star alias.}
602 % Test if arg |#1| and |#2| are counters and if so execute |#3|.
603 % \begin{macrocode}
604 %<*2ekernel|latexrelease>
605 \begingroup\catcode`\*=11 \lowercase{\endgroup
606 \def\@ifbothcounters#1#2#3{%
607 % \end{macrocode}
608 % \begin{macrocode}
609 \begingroup\let\c@*\@undefined
610 % \end{macrocode}
611 % \begin{macrocode}
612 \@ifundefined{c@#1}{\@nocounterr{#1}}%
613 {% else counter is defined
614 \@ifundefined{c@#2}{\@nocounterr{#2}}%
615 {% else both counter and within are defined
616 #3}}%
617 % \end{macrocode}
618 % \begin{macrocode}
619 \endgroup}}
620 % \end{macrocode}
621 % \end{macro}
622 %
623 % \begin{macrocode}
624 %</2ekernel|latexrelease>
625 %<latexrelease>\EndIncludeInRelease
626 %<latexrelease>\IncludeInRelease{0000-00-00}
627 %<latexrelease> {\@removefromreset}{Add interfaces}%
628 %<latexrelease>\let \@removefromreset \undefined
629 %<latexrelease>\let \@ifbothcounters \undefined
630 %<latexrelease>\EndIncludeInRelease
631 %<*2ekernel>
632 % \end{macrocode}
633 %
634 %
635 %
636 %
637 % \begin{macro}{\counterwithin}
638 % New implementation using xparse and supporting an optional
639 % format argument.
640 % \changes{v1.1m}{2021/07/08}{New implementation for
641 % \cs{counterwithin}}
642 % \changes{v1.2b}{2025/02/22}{\cs{counterwithin} needs to expand
643 % its arguments (gh/1675)}
644 % \begin{macrocode}
645 %</2ekernel>
646 %<*2ekernel|latexrelease>
647 %<latexrelease>\IncludeInRelease{2025/06/01}%
648 %<latexrelease> {\counterwithin}{counter within}%
649 % \end{macrocode}
650 %
651 % \begin{macrocode}
652 \NewDocumentCommand \counterwithin {sO{\arabic}mm}{%
653 \@ifbothcounters{#3}{#4}{%
654 \@addtoreset{#3}{#4}%
655 \IfBooleanF #1%
656 {\expandafter
657 \xdef\csname the#3\expandafter\endcsname
658 {\expandafter\noexpand\csname the#4\endcsname
659 .\unexpanded{#2}{#3}}}%
660 }%
661 }
662 % \end{macrocode}
663 %
664 % \changes{v1.1k}{2018/03/08}{Interface added}
665 % \begin{macrocode}
666 %</2ekernel|latexrelease>
667 %<latexrelease>\EndIncludeInRelease
668 %<latexrelease>\IncludeInRelease{2021/11/15}%
669 %<latexrelease> {\counterwithin}{counter within}%
670 %<latexrelease>
671 %<latexrelease>\NewDocumentCommand \counterwithin {sO{\arabic}mm}{%
672 %<latexrelease> \@ifbothcounters{#3}{#4}{%
673 %<latexrelease> \@addtoreset{#3}{#4}%
674 %<latexrelease> \IfBooleanF #1%
675 %<latexrelease> {\expandafter
676 %<latexrelease> \gdef\csname the#3\expandafter\endcsname
677 %<latexrelease> \expandafter
678 %<latexrelease> {\csname the#4\endcsname .#2{#3}}}%
679 %<latexrelease> }%
680 %<latexrelease>}
681 %<latexrelease>\EndIncludeInRelease
682 % \end{macrocode}
683 %
684 % \begin{macrocode}
685 %<latexrelease>\IncludeInRelease{2018-04-01}
686 %<latexrelease> {\counterwithin}{counter within}%
687 %<latexrelease>
688 %<latexrelease>\def\counterwithin{\@ifstar\counterwithin@s\counterwithin@x}
689 %<latexrelease>\def\counterwithin@s#1#2{%
690 %<latexrelease> \@ifbothcounters{#1}{#2}{\@addtoreset{#1}{#2}}}
691 % \end{macrocode}
692 %
693 % \changes{v1.1l}{2018/04/11}{Correct default (issue/38)}
694 % \begin{macrocode}
695 %<latexrelease>\def\counterwithin@x#1#2{%
696 %<latexrelease> \@ifbothcounters{#1}{#2}%
697 %<latexrelease> {\@addtoreset{#1}{#2}%
698 %<latexrelease> \expandafter
699 %<latexrelease> \gdef\csname the#1\expandafter\endcsname\expandafter
700 %<latexrelease> {\csname the#2\expandafter\endcsname\expandafter
701 %<latexrelease> .\expandafter
702 %<latexrelease> \@arabic\csname c@#1\endcsname}}}
703 %<latexrelease>
704 %<latexrelease>\EndIncludeInRelease
705 %<latexrelease>\IncludeInRelease{0000-00-00}
706 %<latexrelease> {\counterwithin}{counter within}%
707 %<latexrelease>\let \counterwithin \undefined
708 %<latexrelease>\let \counterwithin@s \undefined
709 %<latexrelease>\let \counterwithin@x \undefined
710 %<latexrelease>\EndIncludeInRelease
711 %<*2ekernel>
712 % \end{macrocode}
713 % \end{macro}
714 %
715 %
716 %
717 %
718 % \begin{macro}{\counterwithout}
719 % New implementation using xparse and supporting an optional
720 % format argument.
721 % \changes{v1.1m}{2021/07/08}{New implementation for
722 % \cs{counterwithout}}
723 % \changes{v1.2b}{2025/03/01}{\cs{counterwithout} needs to expand
724 % its arguments (gh/1675)}
725 % \begin{macrocode}
726 %</2ekernel>
727 %<*2ekernel|latexrelease>
728 %<latexrelease>\IncludeInRelease{2025/06/01}%
729 %<latexrelease> {\counterwithout}{counter without}%
730 % \end{macrocode}
731 %
732 % \begin{macrocode}
733 \NewDocumentCommand \counterwithout {sO{\arabic}mm}{%
734 \@ifbothcounters{#3}{#4}{%
735 \@removefromreset{#3}{#4}%
736 \IfBooleanF #1%
737 {\expandafter
738 \xdef\csname the#3\endcsname {\unexpanded{#2}{#3}}}%
739 }%
740 }
741 % \end{macrocode}
742 %
743 % \changes{v1.1k}{2018/03/08}{Interface added}
744 % \begin{macrocode}
745 %</2ekernel|latexrelease>
746 %<latexrelease>\EndIncludeInRelease
747 % \end{macrocode}
748 %
749 % \begin{macrocode}
750 %<latexrelease>\IncludeInRelease{2021/11/15}%
751 %<latexrelease> {\counterwithout}{counter without}%
752 %<latexrelease>\NewDocumentCommand \counterwithout {sO{\arabic}mm}{%
753 %<latexrelease> \@ifbothcounters{#3}{#4}{%
754 %<latexrelease> \@removefromreset{#3}{#4}%
755 %<latexrelease> \IfBooleanF #1%
756 %<latexrelease> {\expandafter
757 %<latexrelease> \gdef\csname the#3\endcsname {#2{#3}}}%
758 %<latexrelease> }%
759 %<latexrelease>}
760 %<latexrelease>\EndIncludeInRelease
761 % \end{macrocode}
762 %
763 % \begin{macrocode}
764 %<latexrelease>\IncludeInRelease{2018-04-01}
765 %<latexrelease> {\counterwithout}{counter without}%
766 %<latexrelease>
767 %<latexrelease>\def\counterwithout {\@ifstar\counterwithout@s\counterwithout@x}
768 %<latexrelease>\def\counterwithout@s#1#2{%
769 %<latexrelease> \@ifbothcounters{#1}{#2}{\@removefromreset{#1}{#2}}}
770 %<latexrelease>\def\counterwithout@x#1#2{%
771 %<latexrelease> \@ifbothcounters{#1}{#2}%
772 %<latexrelease> {\@removefromreset{#1}{#2}%
773 %<latexrelease> \expandafter
774 %<latexrelease> \gdef\csname the#1\expandafter\endcsname\expandafter
775 %<latexrelease> {\expandafter
776 %<latexrelease> \@arabic\csname c@#1\endcsname}}}
777 %<latexrelease>\EndIncludeInRelease
778 %<latexrelease>
779 %<latexrelease>\IncludeInRelease{0000-00-00}
780 %<latexrelease> {\counterwithout}{counter without}%
781 %<latexrelease>\let \counterwithout \undefined
782 %<latexrelease>\let \counterwithout@s \undefined
783 %<latexrelease>\let \counterwithout@x \undefined
784 %<latexrelease>\EndIncludeInRelease
785 %<*2ekernel>
786 % \end{macrocode}
787 % \end{macro}
788 %
789 %
790 %
791 %
792 % Numbering commands for definitions of |\theCOUNTER| and |\list|
793 % arguments.
794 %
795 % All commands can now be used in text and math mode.
796 %
797 % \begin{macro}{\arabic}
798 % Representation of\meta{counter} as arabic numerals.
799 % Changed 29 Apr 86 to make it print the obvious thing
800 % it COUNTER not positive.
801 % \begin{macrocode}
802 \def\arabic#1{\expandafter\@arabic\csname c@#1\endcsname}
803 % \end{macrocode}
804 % \end{macro}
805 %
806 % \begin{macro}{\roman}
807 % Representation of \meta{counter} as lower-case
808 % Roman numerals.
809 % \begin{macrocode}
810 \def\roman#1{\expandafter\@roman\csname c@#1\endcsname}
811 % \end{macrocode}
812 % \end{macro}
813 %
814 % \begin{macro}{\Roman}
815 % Representation of \meta{counter} as upper-case
816 % Roman numerals.
817 % \begin{macrocode}
818 \def\Roman#1{\expandafter\@Roman\csname c@#1\endcsname}
819 % \end{macrocode}
820 % \end{macro}
821 %
822 % \begin{macro}{\alph}
823 % Representation of \meta{counter} as a lower-case
824 % letter: 1 = a, 2 = b, etc.
825 % \begin{macrocode}
826 \def\alph#1{\expandafter\@alph\csname c@#1\endcsname}
827 % \end{macrocode}
828 % \end{macro}
829 %
830 % \begin{macro}{\Alph}
831 % Representation of \meta{counter} as an upper-case
832 % letter: 1 = A, 2 = B, etc.
833 % \begin{macrocode}
834 \def\Alph#1{\expandafter\@Alph\csname c@#1\endcsname}
835 % \end{macrocode}
836 % \end{macro}
837 %
838 % \begin{macro}{\fnsymbol}
839 % \changes{v1.1b}{1995/05/20}{Streamlined code}
840 % \changes{v1.1e}{1997/03/21}
841 % {Use \cs{mathsection} and \cs{mathparagraph}. latex/2445}
842 %
843 % Representation of \meta{COUNTER} as a footnote
844 % symbol: 1 = $*$, 2 = $\dagger$, etc.
845 % \begin{macrocode}
846 \def\fnsymbol#1{\expandafter\@fnsymbol\csname c@#1\endcsname}
847 % \end{macrocode}
848 % \end{macro}
849 %
850 % \changes{v1.0f}{1994/05/13}{Removed \cs{@ialph}}
851 % \changes{v1.0f}{1994/05/13}{Removed \cs{@Ialph}}
852 %
853 % \begin{macro}{\@arabic}
854 % |\@arabic\FOOcounter|
855 % Representation of |\FOOcounter| as arabic numerals.
856 % \begin{macrocode}
857 \def\@arabic#1{\number #1} %% changed 29 Apr 86
858 % \end{macrocode}
859 % \end{macro}
860 %
861 % \begin{macro}{\@roman}
862 % |\@roman\FOOcounter|
863 % Representation of |\FOOcounter| as lower-case
864 % Roman numerals.
865 % \begin{macrocode}
866 \def\@roman#1{\romannumeral #1}
867 % \end{macrocode}
868 % \end{macro}
869 %
870 % \begin{macro}{\@Roman}
871 % |\@Roman\FOOcounter|
872 % Representation of |\FOOcounter| as upper-case
873 % Roman numerals.
874 %
875 % \changes{v1.1f}{1997/10/06}{Change \cs{@Roman} to be fully
876 % expandable, so that the result is written properly to files.}
877 % \begin{macrocode}
878 \def\@Roman#1{\expandafter\@slowromancap\romannumeral #1@}
879 % \end{macrocode}
880 % \end{macro}
881 %
882 % \begin{macro}{\@slowromancap}
883 % \changes{v1.1f}{1997/10/06}{Macro added.}
884 % Fully expandable macro to change a roman number to uppercase.
885 % \begin{macrocode}
886 \def\@slowromancap#1{\ifx @#1% then terminate
887 \else
888 \if i#1I\else\if v#1V\else\if x#1X\else\if l#1L\else\if
889 c#1C\else\if d#1D\else \if m#1M\else#1\fi\fi\fi\fi\fi\fi\fi
890 \expandafter\@slowromancap
891 \fi
892 }
893 % \end{macrocode}
894 % \end{macro}
895 %
896 % \begin{macro}{\@alph}
897 % |\@alph\FOOcounter|
898 % Representation of |\FOOcounter| as a lower-case
899 % letter: 1 = a, 2 = b, etc.
900 % \begin{macrocode}
901 \def\@alph#1{%
902 \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or
903 k\or l\or m\or n\or o\or p\or q\or r\or s\or t\or u\or v\or w\or x\or
904 y\or z\else\@ctrerr\fi}
905 % \end{macrocode}
906 % \end{macro}
907 %
908 % \begin{macro}{\@Alph}
909 % |\@Alph\FOOcounter|
910 % Representation of |\FOOcounter| as an upper-case
911 % letter: 1 = A, 2 = B, etc.
912 % \begin{macrocode}
913 \def\@Alph#1{%
914 \ifcase#1\or A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or
915 K\or L\or M\or N\or O\or P\or Q\or R\or S\or T\or U\or V\or W\or X\or
916 Y\or Z\else\@ctrerr\fi}
917 % \end{macrocode}
918 % \end{macro}
919 %
920 % \begin{macro}{\@fnsymbol}
921 % \changes{v1.1b}{1995/05/20}{Allowing both text and math}
922 % Typesetting old fashioned footnote symbols.
923 % This can be done both in text or math mode now.
924 %
925 % \changes{v1.1h}{2015/01/10}{Unse \cs{TextOrMath} (latexrelease)}
926 % This macro is another example of an ever recurring problem in
927 % \TeX: Determining if something is text-mode or math-mode. It is
928 % imperative for the decision between text and math to be delayed
929 % until the actual typesetting is done as the code in question may
930 % go through an |\edef| or |\write| where an |\ifmmode| test would
931 % be executed prematurely. Hence in the implementation below,
932 % |\@fnsymbol| is not robust in itself but the parts doing the
933 % actual typesetting are.
934 %
935 % In the case of |\@fnsymbol| we make use of the robust command
936 % |\TextOrMath| which takes two arguments and typesets the first if
937 % in text-mode and the second if in math-mode. Note that in order
938 % for this command to make the correct decision, it must insert a
939 % |\relax| token if run under regular \TeX, which ruins any kerning
940 % between the preceding characters and whatever awaits
941 % typesetting. If you use e\TeX\ as engine for \LaTeX\ (as
942 % recommended) this unfortunate side effect is not present.
943 % \begin{macrocode}
944 %</2ekernel>
945 %<latexrelease>\IncludeInRelease{2015/01/01}{\@fnsymbol}{Use \TextOrMath}%
946 %<*2ekernel|latexrelease>
947 \def\@fnsymbol#1{%
948 \ifcase#1\or \TextOrMath\textasteriskcentered *\or
949 \TextOrMath \textdagger \dagger\or
950 \TextOrMath \textdaggerdbl \ddagger \or
951 \TextOrMath \textsection \mathsection\or
952 \TextOrMath \textparagraph \mathparagraph\or
953 \TextOrMath \textbardbl \|\or
954 \TextOrMath {\textasteriskcentered\textasteriskcentered}{**}\or
955 \TextOrMath {\textdagger\textdagger}{\dagger\dagger}\or
956 \TextOrMath {\textdaggerdbl\textdaggerdbl}{\ddagger\ddagger}\else
957 \@ctrerr \fi
958 }%
959 %</2ekernel|latexrelease>
960 %<latexrelease>\EndIncludeInRelease
961 %<latexrelease>\IncludeInRelease{0000/00/00}{\@fnsymbol}{Use \TextOrMath}%
962 %<latexrelease>\def\@fnsymbol#1{\ensuremath{%
963 %<latexrelease> \ifcase#1\or *\or \dagger\or \ddagger\or \mathsection\or
964 %<latexrelease> \mathparagraph\or \|\or **\or \dagger\dagger
965 %<latexrelease> \or \ddagger\ddagger \else\@ctrerr\fi}}%
966 %<latexrelease>\EndIncludeInRelease
967 %<*2ekernel>
968 % \end{macrocode}
969 % \end{macro}
970 %
971 % \begin{macro}{\TextOrMath}
972 % \changes{v1.1h}{2015/01/11}{Add command to solve robustness
973 % issues (pr/3752) (latexrelease)}
974 % \changes{v1.1j}{2016/10/19}{Test directly for \cs{protected}}
975 % When using regular \TeX, we make this command robust so that it
976 % always selects the correct branch in an |\ifmmode| switch with
977 % the usual disadvantage of ruining kerning. For the application we
978 % use it for here that shouldn't matter. The alternative would be
979 % to mimic |\IeC| from \textsf{inputenc} but then it will have the
980 % disadvantage of choosing the wrong branch if appearing at the
981 % beginning of an alignment cell. However, users of e\TeX\ will be
982 % pleasantly surprised to get the best of both worlds and no bad
983 % side effects.
984 %
985 % First some code for checking if we are running e\TeX\ but making
986 % sure not to permanently turn |\protected| into |\relax|.
987 % \begin{macrocode}
988 %</2ekernel>
989 %<latexrelease>\IncludeInRelease{2015/01/01}{\TextOrMath}{\TextOrMath}%
990 %<*2ekernel|latexrelease>
991 \begingroup\expandafter\expandafter\expandafter\endgroup
992 \expandafter\ifx\csname protected\endcsname\relax
993 % \end{macrocode}
994 % In case of ordinary \TeX\ we define |\TextOrMath| as a robust
995 % command but make sure it always grabs its arguments. If we didn't do
996 % this it might very well gobble spaces in the input stream.
997 % \begin{macrocode}
998 \DeclareRobustCommand\TextOrMath{%
999 \ifmmode \expandafter\@secondoftwo
1000 \else \expandafter\@firstoftwo \fi}
1001 \protected@edef\TextOrMath#1#2{\TextOrMath{#1}{#2}}
1002 \else
1003 % \end{macrocode}
1004 % For e\TeX\ the situation is similar. The robust macro is a hidden
1005 % one so that we again avoid problems of gobbling spaces in the input.
1006 % \begin{macrocode}
1007 \protected\expandafter\def\csname TextOrMath\space\endcsname{%
1008 \ifmmode \expandafter\@secondoftwo
1009 \else \expandafter\@firstoftwo \fi}
1010 \edef\TextOrMath#1#2{%
1011 \expandafter\noexpand\csname TextOrMath\space\endcsname
1012 {#1}{#2}}
1013 \fi
1014 %</2ekernel|latexrelease>
1015 %<latexrelease>\EndIncludeInRelease
1016 %<latexrelease>\IncludeInRelease{0000/00/00}{\TextOrMath}{\TextOrMath}%
1017 %<latexrelease>\let\TextOrMath\@undefined
1018 %<latexrelease>\EndIncludeInRelease
1019 %<*2ekernel>
1020 % \end{macrocode}
1021 % \end{macro}
1022 %
1023 % \begin{macrocode}
1024 %</2ekernel>
1025 % \end{macrocode}
1026 %
1027 % \Finale
1028 %