1 % \iffalse meta-comment
4 % The LaTeX3 Project and any individual authors listed elsewhere
7 % This file is part of the LaTeX base system.
8 % -------------------------------------------
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 % http://www.latex-project.org/lppl.txt
15 % and version 1.3c or later is part of all distributions of LaTeX
16 % version 2005/12/01 or later.
18 % This file has the LPPL maintenance status "maintained".
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
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.
31 %%% From File: ltcounts.dtx
35 \ProvidesFile{ltcounts.dtx}
36 [2015/06/05 v1.1i LaTeX Kernel (Counters)]
38 \documentclass{ltxdoc}
39 \GetFileInfo{ltcounts.dtx}
52 \MaintainedByLaTeXTeam{latex}
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}
66 % \section{Counters and Lengths}
67 % Commands for defining and using counters. This file defines:
69 % \DescribeMacro{\newcounter}
70 % To define a new counter.
72 % \DescribeMacro{\setcounter}
73 % To set the value of counters.
75 % \DescribeMacro{\addtocounter}
76 % Increase the counter |#1| by the number |#2|.
78 % \DescribeMacro{\stepcounter}
79 % Increase a counter by one.
81 % \DescribeMacro{\refstepcounter}
82 % Increase a counter by one, also setting the value used by |\label|.
85 % \DescribeMacro{\value}
86 % For accessing the value of the counter as a \TeX\ number (as opposed
87 % to |\the|\meta{counter} which expands to the \emph{printed}
88 % representation of \meta{counter})
90 % \DescribeMacro\arabic
91 % |\arabic|\marg{counter}: 1, 2, 3, \ldots
93 % \DescribeMacro\roman
94 % |\roman|\marg{counter}: i, ii, iii, \ldots
96 % \DescribeMacro\Roman
97 % |\Roman|\marg{counter}: I, II, III, \ldots
100 % |\alph|\marg{counter}: a, b, c, \ldots
102 % \DescribeMacro\Alph
103 % |\Alph|\marg{counter}: A, B, C, \ldots
105 % \DescribeMacro\fnsymbol
106 % |\fnsymbol|\marg{counter}: $*$, $\dagger$, $\ddagger$, \ldots
115 % \subsection{Environment Counter Macros}
117 % An environment foo has an associated counter defined by the
118 % following control sequences:
120 % \begin{tabular}{lp{.75\textwidth}}
121 % |\c@foo| & Contains the counter's numerical value.
122 % It is defined by |\newcount\foocounter|.\\
123 % |\thefoo| & Macro that expands to the printed value of |\foocounter|.
124 % For example, if sections are numbered within chapters,
125 % and section headings look like
127 % Section II-3. The Nature of Counters
129 % then |\thesection| might be defined by:\newline
130 % |\def\thesection|\newline\hspace*{2em}
131 % |{\@Roman{\c@chapter}-\@arabic{\c@section}}|\\
133 % |\p@foo| & Macro that expands to a printed `reference prefix' of
134 % counter foo. Any |\ref| to a value created by counter
135 % foo will produce the expansion of |\p@foo\thefoo| when
136 % the |\label| command is executed. See file \texttt{ltxref.dtx}
137 % for an extension of this mechanism.\\
138 % |\cl@foo| & List of counters to be reset when foo stepped.
140 % |\@elt{countera}\@elt{counterb}\@elt{counterc}|.
144 % |\thefoo| and |\p@foo| \emph{must} be defined in such a way that
145 % |\edef\bar{\thefoo}| or |\edef\bar{\p@foo}|
146 % defines |\bar| so that it will evaluate to the counter value at the
147 % time of the |\edef|, even after |\foocounter| and any other counters
148 % have been changed. This will happen if you use the standard commands
149 % |\@arabic|, |\@Roman|, etc.
152 % The following commands are used to define and modify counters.
154 % |\refstepcounter|\marg{foo}\\
155 % Same as |\stepcounter|, but it also defines
156 % |\@currentreference| so that a subsequent
157 % |\label|\marg{bar} command causes |\ref|\marg{bar} to
158 % generate the current value of counter \meta{foo}.
160 % |\@definecounter|\marg{foo}\\
161 % Initializes counter \marg{foo} (with empty reset list), defines
162 % |\p@foo| and |\thefoo| to be null. Also adds \meta{foo}
164 % the reset list of a dummy counter |@ckpt| used for taking
165 % checkpoints for the |\include| system.
167 % |\@addtoreset|\marg{foo}\marg{bar} :
168 % Adds counter \meta{foo} to the list of counters
169 % |\cl@bar| to be reset when counter \meta{bar} is stepped.
172 % \begin{macro}{\setcounter}
173 % \changes{v1.0d}{1994/04/09}
174 % {\cs{@nocnterr} now has counter name argument}
175 % \changes{v1.0e}{1994/04/17}
176 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
177 % |\setcounter|\marg{foo}\marg{val} :
178 % Globally sets |\foocounter| equal to \meta{val}.
180 \def\setcounter#1#2{%
183 {\global\csname c@#1\endcsname#2\relax}}
187 % \begin{macro}{\addtocounter}
188 % \changes{v1.0d}{1994/04/09}
189 % {\cs{@nocnterr} now has counter name argument}
190 % \changes{v1.0e}{1994/04/17}
191 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
192 % |\addtocounter|\marg{foo}\marg{val}
193 % Globally increments |\foocounter| by \meta{val}.
195 \def\addtocounter#1#2{%
198 {\global\advance\csname c@#1\endcsname #2\relax}}
202 % \begin{macro}{\newcounter}
203 % |\newcounter|\marg{newctr}\oarg{oldctr}
204 % Defines \meta{newctr} to be a counter, which is reset when counter
205 % \meta{oldctr} is stepped. If \meta{newctr} already defined
207 % `|c@newctr already defined|' error.
210 \expandafter\@ifdefinable \csname c@#1\endcsname
211 {\@definecounter{#1}}%
212 \@ifnextchar[{\@newctr{#1}}{}}
216 % \begin{macro}{\value}
218 % produces the value of counter \meta{ctr}, for use with
219 % a |\setcounter| or |\addtocounter| command.
221 \def\value#1{\csname c@#1\endcsname}
225 % \begin{macro}{\@newctr}
226 % \changes{v1.0d}{1994/04/09}
227 % {\cs{@nocnterr} now has counter name argument}
228 % \changes{v1.0e}{1994/04/17}
229 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
232 \@ifundefined{c@#2}{\@nocounterr{#2}}{\@addtoreset{#1}{#2}}}
236 % \begin{macro}{\stepcounter}
237 % \changes{LaTeX209}{1992/11/23}{Replaced \{\} in \cs{stepcounter} by
238 % \cs{begingroup} \cs{endgroup} to avoid adding an empty ord in
240 % \changes{v1.0d}{1994/04/09}
241 % {Use \cs{addtocounter} to have name checked}
242 % |\stepcounter|{foo}
243 % Globally increments counter |\c@FOO|
244 % and resets all subsidiary counters.
247 \addtocounter{#1}\@ne
250 \csname cl@#1\endcsname
255 % \begin{macro}{\@stpelt}
256 % \changes{v1.1h}{2015/01/10}{Reset all within counters in one go (latexrelease)}
258 % Rather than resetting the ``within'' counter to zero we set it to
259 % $-1$ and then run |\stepcounter| that moves it to $0$ and also
260 % initiates resetting the next level down.
263 %<latexrelease>\IncludeInRelease{2015/01/01}{\@stpelt}
264 %<latexrelease> {Reset nested counters}%
265 %<*2ekernel|latexrelease>
266 \def\@stpelt#1{\global\csname c@#1\endcsname \m@ne\stepcounter{#1}}%
267 %<latexrelease>\EndIncludeInRelease
268 %</2ekernel|latexrelease>
269 %<latexrelease>\IncludeInRelease{0000/00/00}{\@stpelt}
270 %<latexrelease> {Reset nested counters}%%
271 %<latexrelease>\def\@stpelt#1{\global\csname c@#1\endcsname \z@}%
272 %<latexrelease>\EndIncludeInRelease
277 % \begin{macro}{\cl@@ckpt}
279 \def\cl@@ckpt{\@elt{page}}
283 % \begin{macro}{\@definecounter}
284 % \changes{v1.1b}{1995/05/20}{Streamlined code}
285 % \changes{v1.1c}{1995/05/20}{And do it right}
288 \def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
290 \global\expandafter\let\csname cl@#1\endcsname\@empty
291 \@addtoreset{#1}{@ckpt}%
292 \global\expandafter\let\csname p@#1\endcsname\@empty
294 \gdef\csname the#1\expandafter\endcsname\expandafter
295 {\expandafter\@arabic\csname c@#1\endcsname}}
299 % \begin{macro}{\@addtoreset}
301 \def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}}
306 % Numbering commands for definitions of |\theCOUNTER| and |\list|
309 % All commands can now be used in text and math mode.
311 % \begin{macro}{\arabic}
312 % Representation of\meta{counter} as arabic numerals.
313 % Changed 29 Apr 86 to make it print the obvious thing
314 % it COUNTER not positive.
316 \def\arabic#1{\expandafter\@arabic\csname c@#1\endcsname}
320 % \begin{macro}{\roman}
321 % Representation of \meta{counter} as lower-case
324 \def\roman#1{\expandafter\@roman\csname c@#1\endcsname}
328 % \begin{macro}{\Roman}
329 % Representation of \meta{counter} as upper-case
332 \def\Roman#1{\expandafter\@Roman\csname c@#1\endcsname}
336 % \begin{macro}{\alph}
337 % Representation of \meta{counter} as a lower-case
338 % letter: 1 = a, 2 = b, etc.
340 \def\alph#1{\expandafter\@alph\csname c@#1\endcsname}
344 % \begin{macro}{\Alph}
345 % Representation of \meta{counter} as an upper-case
346 % letter: 1 = A, 2 = B, etc.
348 \def\Alph#1{\expandafter\@Alph\csname c@#1\endcsname}
352 % \begin{macro}{\fnsymbol}
353 % \changes{v1.1b}{1995/05/20}{Streamlined code}
354 % \changes{v1.1e}{1997/03/21}
355 % {Use \cs{mathsection} and \cs{mathparagraph}. latex/2445}
357 % Representation of \meta{COUNTER} as a footnote
358 % symbol: 1 = $*$, 2 = $\dagger$, etc.
360 \def\fnsymbol#1{\expandafter\@fnsymbol\csname c@#1\endcsname}
364 % \changes{v1.0f}{1994/05/13}{Removed \cs{@ialph}}
365 % \changes{v1.0f}{1994/05/13}{Removed \cs{@Ialph}}
367 % \begin{macro}{\@arabic}
368 % |\@arabic\FOOcounter|
369 % Representation of |\FOOcounter| as arabic numerals.
371 \def\@arabic#1{\number #1} %% changed 29 Apr 86
375 % \begin{macro}{\@roman}
376 % |\@roman\FOOcounter|
377 % Representation of |\FOOcounter| as lower-case
380 \def\@roman#1{\romannumeral #1}
384 % \begin{macro}{\@Roman}
385 % |\@Roman\FOOcounter|
386 % Representation of |\FOOcounter| as upper-case
389 % \changes{v1.1f}{1997/10/06}{Change \cs{@Roman} to be fully
390 % expandable, so that the result is written properly to files.}
392 \def\@Roman#1{\expandafter\@slowromancap\romannumeral #1@}
396 % \begin{macro}{\@slowromancap}
397 % \changes{v1.1f}{1997/10/06}{Macro added.}
398 % Fully expandable macro to change a roman number to uppercase.
400 \def\@slowromancap#1{\ifx @#1% then terminate
402 \if i#1I\else\if v#1V\else\if x#1X\else\if l#1L\else\if
403 c#1C\else\if d#1D\else \if m#1M\else#1\fi\fi\fi\fi\fi\fi\fi
404 \expandafter\@slowromancap
410 % \begin{macro}{\@alph}
411 % |\@alph\FOOcounter|
412 % Representation of |\FOOcounter| as a lower-case
413 % letter: 1 = a, 2 = b, etc.
416 \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or
417 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
418 y\or z\else\@ctrerr\fi}
422 % \begin{macro}{\@Alph}
423 % |\@Alph\FOOcounter|
424 % Representation of |\FOOcounter| as an upper-case
425 % letter: 1 = A, 2 = B, etc.
428 \ifcase#1\or A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or
429 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
430 Y\or Z\else\@ctrerr\fi}
434 % \begin{macro}{\@fnsymbol}
435 % \changes{v1.1b}{1995/05/20}{Allowing both text and math}
436 % Typesetting old fashioned footnote symbols.
437 % This can be done both in text or math mode now.
439 % \changes{v1.1h}{2015/01/10}{Unse \cs{TextOrMath} (latexrelease)}
440 % This macro is another example of an ever recurring problem in
441 % \TeX: Determining if something is text-mode or math-mode. It is
442 % imperative for the decision between text and math to be delayed
443 % until the actual typesetting is done as the code in question may
444 % go through an |\edef| or |\write| where an |\ifmmode| test would
445 % be executed prematurely. Hence in the implementation below,
446 % |\@fnsymbol| is not robust in itself but the parts doing the
447 % actual typesetting are.
449 % In the case of |\@fnsymbol| we make use of the robust command
450 % |\TextOrMath| which takes two arguments and typesets the first if
451 % in text-mode and the second if in math-mode. Note that in order
452 % for this command to make the correct decision, it must insert a
453 % |\relax| token if run under regular \TeX, which ruins any kerning
454 % between the preceding characters and whatever awaits
455 % typesetting. If you use e\TeX\ as engine for \LaTeX\ (as
456 % recommended) this unfortunate side effect is not present.
459 %<latexrelease>\IncludeInRelease{2015/01/01}{\@fnsymbol}{Use \TexOrMath}%
460 %<*2ekernel|latexrelease>
462 \ifcase#1\or \TextOrMath\textasteriskcentered *\or
463 \TextOrMath \textdagger \dagger\or
464 \TextOrMath \textdaggerdbl \ddagger \or
465 \TextOrMath \textsection \mathsection\or
466 \TextOrMath \textparagraph \mathparagraph\or
467 \TextOrMath \textbardbl \|\or
468 \TextOrMath {\textasteriskcentered\textasteriskcentered}{**}\or
469 \TextOrMath {\textdagger\textdagger}{\dagger\dagger}\or
470 \TextOrMath {\textdaggerdbl\textdaggerdbl}{\ddagger\ddagger}\else
473 %</2ekernel|latexrelease>
474 %<latexrelease>\EndIncludeInRelease
475 %<latexrelease>\IncludeInRelease{0000/00/00}{\@fnsymbol}{Use \TexOrMath}%
476 %<latexrelease>\def\@fnsymbol#1{\ensuremath{%
477 %<latexrelease> \ifcase#1\or *\or \dagger\or \ddagger\or \mathsection\or
478 %<latexrelease> \mathparagraph\or \|\or **\or \dagger\dagger
479 %<latexrelease> \or \ddagger\ddagger \else\@ctrerr\fi}}%
480 %<latexrelease>\EndIncludeInRelease
485 % \begin{macro}{\TextOrMath}
486 % \changes{v1.1h}{2015/01/11}{Add command to solve robustness
487 % issues (pr/3752) (latexrelease)}
488 % When using regular \TeX, we make this command robust so that it
489 % always selects the correct branch in an |\ifmmode| switch with
490 % the usual disadvantage of ruining kerning. For the application we
491 % use it for here that shouldn't matter. The alternative would be
492 % to mimic |\IeC| from \textsf{inputenc} but then it wil have the
493 % disadvantage of choosing the wrong branch if appearing at the
494 % beginning of an alignment cell. However, users of e\TeX\ will be
495 % pleasantly surprised to get the best of both worlds and no bad
498 % First some code for checking if we are running e\TeX\ but making
499 % sure not to permanently turn |\eTeXversion| into |\relax|.
502 %<latexrelease>\IncludeInRelease{2015/01/01}{\TextOrMath}{\TextOrMath}%
503 %<*2ekernel|latexrelease>
504 \begingroup\expandafter\expandafter\expandafter\endgroup
505 \expandafter\ifx\csname eTeXversion\endcsname\relax
507 % In case of ordinary \TeX\ we define |\TextOrMath| as a robust
508 % command but make sure it always grabs its arguments. If we didn't do
509 % this it might very well gobble spaces in the input stream.
511 \DeclareRobustCommand\TextOrMath{%
512 \ifmmode \expandafter\@secondoftwo
513 \else \expandafter\@firstoftwo \fi}
514 \protected@edef\TextOrMath#1#2{\TextOrMath{#1}{#2}}
517 % For e\TeX\ the situation is similar. The robust macro is a hidden
518 % one so that we again avoid problems of gobbling spaces in the input.
520 \protected\expandafter\def\csname TextOrMath\space\endcsname{%
521 \ifmmode \expandafter\@secondoftwo
522 \else \expandafter\@firstoftwo \fi}
523 \edef\TextOrMath#1#2{%
524 \expandafter\noexpand\csname TextOrMath\space\endcsname
527 %</2ekernel|latexrelease>
528 %<latexrelease>\EndIncludeInRelease
529 %<latexrelease>\IncludeInRelease{0000/00/00}{\TextOrMath}{\TextOrMath}%
530 %<latexrelease>\let\TextOrMath\@undefined
531 %<latexrelease>\EndIncludeInRelease