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.1j LaTeX Kernel (Counters)]
38 \documentclass{ltxdoc}
39 \GetFileInfo{ltcounts.dtx}
52 \MaintainedByLaTeXTeam{latex}
60 % \changes{v1.0c}{1994/03/29}
61 % {Create file from parts of ltmiscen and ltherest.}
62 % \changes{v1.1a}{1994/05/19}{Extracted file from ltcntlen.}
63 % \changes{v1.1d}{1996/04/23}{Documentation improvements}
65 % \section{Counters and Lengths}
66 % Commands for defining and using counters. This file defines:
68 % \DescribeMacro{\newcounter}
69 % To define a new counter.
71 % \DescribeMacro{\setcounter}
72 % To set the value of counters.
74 % \DescribeMacro{\addtocounter}
75 % Increase the counter |#1| by the number |#2|.
77 % \DescribeMacro{\stepcounter}
78 % Increase a counter by one.
80 % \DescribeMacro{\refstepcounter}
81 % Increase a counter by one, also setting the value used by |\label|.
84 % \DescribeMacro{\value}
85 % For accessing the value of the counter as a \TeX\ number (as opposed
86 % to |\the|\meta{counter} which expands to the \emph{printed}
87 % representation of \meta{counter})
89 % \DescribeMacro\arabic
90 % |\arabic|\marg{counter}: 1, 2, 3, \ldots
92 % \DescribeMacro\roman
93 % |\roman|\marg{counter}: i, ii, iii, \ldots
95 % \DescribeMacro\Roman
96 % |\Roman|\marg{counter}: I, II, III, \ldots
99 % |\alph|\marg{counter}: a, b, c, \ldots
101 % \DescribeMacro\Alph
102 % |\Alph|\marg{counter}: A, B, C, \ldots
104 % \DescribeMacro\fnsymbol
105 % |\fnsymbol|\marg{counter}: $*$, $\dagger$, $\ddagger$, \ldots
114 % \subsection{Environment Counter Macros}
116 % An environment foo has an associated counter defined by the
117 % following control sequences:
119 % \begin{tabular}{lp{.75\textwidth}}
120 % |\c@foo| & Contains the counter's numerical value.
121 % It is defined by |\newcount\foocounter|.\\
122 % |\thefoo| & Macro that expands to the printed value of |\foocounter|.
123 % For example, if sections are numbered within chapters,
124 % and section headings look like
126 % Section II-3. The Nature of Counters
128 % then |\thesection| might be defined by:\newline
129 % |\def\thesection|\newline\hspace*{2em}
130 % |{\@Roman{\c@chapter}-\@arabic{\c@section}}|\\
132 % |\p@foo| & Macro that expands to a printed `reference prefix' of
133 % counter foo. Any |\ref| to a value created by counter
134 % foo will produce the expansion of |\p@foo\thefoo| when
135 % the |\label| command is executed. See file \texttt{ltxref.dtx}
136 % for an extension of this mechanism.\\
137 % |\cl@foo| & List of counters to be reset when foo stepped.
139 % |\@elt{countera}\@elt{counterb}\@elt{counterc}|.
143 % |\thefoo| and |\p@foo| \emph{must} be defined in such a way that
144 % |\edef\bar{\thefoo}| or |\edef\bar{\p@foo}|
145 % defines |\bar| so that it will evaluate to the counter value at the
146 % time of the |\edef|, even after |\foocounter| and any other counters
147 % have been changed. This will happen if you use the standard commands
148 % |\@arabic|, |\@Roman|, etc.
151 % The following commands are used to define and modify counters.
153 % |\refstepcounter|\marg{foo}\\
154 % Same as |\stepcounter|, but it also defines
155 % |\@currentreference| so that a subsequent
156 % |\label|\marg{bar} command causes |\ref|\marg{bar} to
157 % generate the current value of counter \meta{foo}.
159 % |\@definecounter|\marg{foo}\\
160 % Initializes counter \marg{foo} (with empty reset list), defines
161 % |\p@foo| and |\thefoo| to be null. Also adds \meta{foo}
163 % the reset list of a dummy counter |@ckpt| used for taking
164 % checkpoints for the |\include| system.
166 % |\@addtoreset|\marg{foo}\marg{bar} :
167 % Adds counter \meta{foo} to the list of counters
168 % |\cl@bar| to be reset when counter \meta{bar} is stepped.
171 % \begin{macro}{\setcounter}
172 % \changes{v1.0d}{1994/04/09}
173 % {\cs{@nocnterr} now has counter name argument}
174 % \changes{v1.0e}{1994/04/17}
175 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
176 % |\setcounter|\marg{foo}\marg{val} :
177 % Globally sets |\foocounter| equal to \meta{val}.
179 \def\setcounter#1#2{%
182 {\global\csname c@#1\endcsname#2\relax}}
186 % \begin{macro}{\addtocounter}
187 % \changes{v1.0d}{1994/04/09}
188 % {\cs{@nocnterr} now has counter name argument}
189 % \changes{v1.0e}{1994/04/17}
190 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
191 % |\addtocounter|\marg{foo}\marg{val}
192 % Globally increments |\foocounter| by \meta{val}.
194 \def\addtocounter#1#2{%
197 {\global\advance\csname c@#1\endcsname #2\relax}}
201 % \begin{macro}{\newcounter}
202 % |\newcounter|\marg{newctr}\oarg{oldctr}
203 % Defines \meta{newctr} to be a counter, which is reset when counter
204 % \meta{oldctr} is stepped. If \meta{newctr} already defined
206 % `|c@newctr already defined|' error.
209 \expandafter\@ifdefinable \csname c@#1\endcsname
210 {\@definecounter{#1}}%
211 \@ifnextchar[{\@newctr{#1}}{}}
215 % \begin{macro}{\value}
217 % produces the value of counter \meta{ctr}, for use with
218 % a |\setcounter| or |\addtocounter| command.
220 \def\value#1{\csname c@#1\endcsname}
224 % \begin{macro}{\@newctr}
225 % \changes{v1.0d}{1994/04/09}
226 % {\cs{@nocnterr} now has counter name argument}
227 % \changes{v1.0e}{1994/04/17}
228 % {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
231 \@ifundefined{c@#2}{\@nocounterr{#2}}{\@addtoreset{#1}{#2}}}
235 % \begin{macro}{\stepcounter}
236 % \changes{LaTeX209}{1992/11/23}{Replaced \{\} in \cs{stepcounter} by
237 % \cs{begingroup} \cs{endgroup} to avoid adding an empty ord in
239 % \changes{v1.0d}{1994/04/09}
240 % {Use \cs{addtocounter} to have name checked}
241 % |\stepcounter|{foo}
242 % Globally increments counter |\c@FOO|
243 % and resets all subsidiary counters.
246 \addtocounter{#1}\@ne
249 \csname cl@#1\endcsname
254 % \begin{macro}{\@stpelt}
255 % \changes{v1.1h}{2015/01/10}{Reset all within counters in one go (latexrelease)}
257 % Rather than resetting the ``within'' counter to zero we set it to
258 % $-1$ and then run |\stepcounter| that moves it to $0$ and also
259 % initiates resetting the next level down.
262 %<latexrelease>\IncludeInRelease{2015/01/01}{\@stpelt}
263 %<latexrelease> {Reset nested counters}%
264 %<*2ekernel|latexrelease>
265 \def\@stpelt#1{\global\csname c@#1\endcsname \m@ne\stepcounter{#1}}%
266 %<latexrelease>\EndIncludeInRelease
267 %</2ekernel|latexrelease>
268 %<latexrelease>\IncludeInRelease{0000/00/00}{\@stpelt}
269 %<latexrelease> {Reset nested counters}%%
270 %<latexrelease>\def\@stpelt#1{\global\csname c@#1\endcsname \z@}%
271 %<latexrelease>\EndIncludeInRelease
276 % \begin{macro}{\cl@@ckpt}
278 \def\cl@@ckpt{\@elt{page}}
282 % \begin{macro}{\@definecounter}
283 % \changes{v1.1b}{1995/05/20}{Streamlined code}
284 % \changes{v1.1c}{1995/05/20}{And do it right}
287 \def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
289 \global\expandafter\let\csname cl@#1\endcsname\@empty
290 \@addtoreset{#1}{@ckpt}%
291 \global\expandafter\let\csname p@#1\endcsname\@empty
293 \gdef\csname the#1\expandafter\endcsname\expandafter
294 {\expandafter\@arabic\csname c@#1\endcsname}}
298 % \begin{macro}{\@addtoreset}
300 \def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}}
305 % Numbering commands for definitions of |\theCOUNTER| and |\list|
308 % All commands can now be used in text and math mode.
310 % \begin{macro}{\arabic}
311 % Representation of\meta{counter} as arabic numerals.
312 % Changed 29 Apr 86 to make it print the obvious thing
313 % it COUNTER not positive.
315 \def\arabic#1{\expandafter\@arabic\csname c@#1\endcsname}
319 % \begin{macro}{\roman}
320 % Representation of \meta{counter} as lower-case
323 \def\roman#1{\expandafter\@roman\csname c@#1\endcsname}
327 % \begin{macro}{\Roman}
328 % Representation of \meta{counter} as upper-case
331 \def\Roman#1{\expandafter\@Roman\csname c@#1\endcsname}
335 % \begin{macro}{\alph}
336 % Representation of \meta{counter} as a lower-case
337 % letter: 1 = a, 2 = b, etc.
339 \def\alph#1{\expandafter\@alph\csname c@#1\endcsname}
343 % \begin{macro}{\Alph}
344 % Representation of \meta{counter} as an upper-case
345 % letter: 1 = A, 2 = B, etc.
347 \def\Alph#1{\expandafter\@Alph\csname c@#1\endcsname}
351 % \begin{macro}{\fnsymbol}
352 % \changes{v1.1b}{1995/05/20}{Streamlined code}
353 % \changes{v1.1e}{1997/03/21}
354 % {Use \cs{mathsection} and \cs{mathparagraph}. latex/2445}
356 % Representation of \meta{COUNTER} as a footnote
357 % symbol: 1 = $*$, 2 = $\dagger$, etc.
359 \def\fnsymbol#1{\expandafter\@fnsymbol\csname c@#1\endcsname}
363 % \changes{v1.0f}{1994/05/13}{Removed \cs{@ialph}}
364 % \changes{v1.0f}{1994/05/13}{Removed \cs{@Ialph}}
366 % \begin{macro}{\@arabic}
367 % |\@arabic\FOOcounter|
368 % Representation of |\FOOcounter| as arabic numerals.
370 \def\@arabic#1{\number #1} %% changed 29 Apr 86
374 % \begin{macro}{\@roman}
375 % |\@roman\FOOcounter|
376 % Representation of |\FOOcounter| as lower-case
379 \def\@roman#1{\romannumeral #1}
383 % \begin{macro}{\@Roman}
384 % |\@Roman\FOOcounter|
385 % Representation of |\FOOcounter| as upper-case
388 % \changes{v1.1f}{1997/10/06}{Change \cs{@Roman} to be fully
389 % expandable, so that the result is written properly to files.}
391 \def\@Roman#1{\expandafter\@slowromancap\romannumeral #1@}
395 % \begin{macro}{\@slowromancap}
396 % \changes{v1.1f}{1997/10/06}{Macro added.}
397 % Fully expandable macro to change a roman number to uppercase.
399 \def\@slowromancap#1{\ifx @#1% then terminate
401 \if i#1I\else\if v#1V\else\if x#1X\else\if l#1L\else\if
402 c#1C\else\if d#1D\else \if m#1M\else#1\fi\fi\fi\fi\fi\fi\fi
403 \expandafter\@slowromancap
409 % \begin{macro}{\@alph}
410 % |\@alph\FOOcounter|
411 % Representation of |\FOOcounter| as a lower-case
412 % letter: 1 = a, 2 = b, etc.
415 \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or
416 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
417 y\or z\else\@ctrerr\fi}
421 % \begin{macro}{\@Alph}
422 % |\@Alph\FOOcounter|
423 % Representation of |\FOOcounter| as an upper-case
424 % letter: 1 = A, 2 = B, etc.
427 \ifcase#1\or A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or
428 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
429 Y\or Z\else\@ctrerr\fi}
433 % \begin{macro}{\@fnsymbol}
434 % \changes{v1.1b}{1995/05/20}{Allowing both text and math}
435 % Typesetting old fashioned footnote symbols.
436 % This can be done both in text or math mode now.
438 % \changes{v1.1h}{2015/01/10}{Unse \cs{TextOrMath} (latexrelease)}
439 % This macro is another example of an ever recurring problem in
440 % \TeX: Determining if something is text-mode or math-mode. It is
441 % imperative for the decision between text and math to be delayed
442 % until the actual typesetting is done as the code in question may
443 % go through an |\edef| or |\write| where an |\ifmmode| test would
444 % be executed prematurely. Hence in the implementation below,
445 % |\@fnsymbol| is not robust in itself but the parts doing the
446 % actual typesetting are.
448 % In the case of |\@fnsymbol| we make use of the robust command
449 % |\TextOrMath| which takes two arguments and typesets the first if
450 % in text-mode and the second if in math-mode. Note that in order
451 % for this command to make the correct decision, it must insert a
452 % |\relax| token if run under regular \TeX, which ruins any kerning
453 % between the preceding characters and whatever awaits
454 % typesetting. If you use e\TeX\ as engine for \LaTeX\ (as
455 % recommended) this unfortunate side effect is not present.
458 %<latexrelease>\IncludeInRelease{2015/01/01}{\@fnsymbol}{Use \TexOrMath}%
459 %<*2ekernel|latexrelease>
461 \ifcase#1\or \TextOrMath\textasteriskcentered *\or
462 \TextOrMath \textdagger \dagger\or
463 \TextOrMath \textdaggerdbl \ddagger \or
464 \TextOrMath \textsection \mathsection\or
465 \TextOrMath \textparagraph \mathparagraph\or
466 \TextOrMath \textbardbl \|\or
467 \TextOrMath {\textasteriskcentered\textasteriskcentered}{**}\or
468 \TextOrMath {\textdagger\textdagger}{\dagger\dagger}\or
469 \TextOrMath {\textdaggerdbl\textdaggerdbl}{\ddagger\ddagger}\else
472 %</2ekernel|latexrelease>
473 %<latexrelease>\EndIncludeInRelease
474 %<latexrelease>\IncludeInRelease{0000/00/00}{\@fnsymbol}{Use \TexOrMath}%
475 %<latexrelease>\def\@fnsymbol#1{\ensuremath{%
476 %<latexrelease> \ifcase#1\or *\or \dagger\or \ddagger\or \mathsection\or
477 %<latexrelease> \mathparagraph\or \|\or **\or \dagger\dagger
478 %<latexrelease> \or \ddagger\ddagger \else\@ctrerr\fi}}%
479 %<latexrelease>\EndIncludeInRelease
484 % \begin{macro}{\TextOrMath}
485 % \changes{v1.1h}{2015/01/11}{Add command to solve robustness
486 % issues (pr/3752) (latexrelease)}
487 % \changes{v1.1j}{2016/10/19}{Test directly for \cs{protected}}
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 |\protected| into |\relax|.
502 %<latexrelease>\IncludeInRelease{2015/01/01}{\TextOrMath}{\TextOrMath}%
503 %<*2ekernel|latexrelease>
504 \begingroup\expandafter\expandafter\expandafter\endgroup
505 \expandafter\ifx\csname protected\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