% \iffalse meta-comment
%
% Copyright (C) 1993-2026
% The LaTeX Project and any individual authors listed elsewhere
% in this file.
%
% This file is part of the LaTeX base system.
% -------------------------------------------
%
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%    https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This file has the LPPL maintenance status "maintained".
%
% The list of all files belonging to the LaTeX base distribution is
% given in the file `manifest.txt'. See also `legal.txt' for additional
% information.
%
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
%
% \fi
%
% \iffalse
%%% From File: ltcounts.dtx
%
%<*driver>
% \fi
\ProvidesFile{ltcounts.dtx}
             [2026/03/10 v1.2c LaTeX Kernel (Counters)]
% \iffalse
\documentclass{ltxdoc}
\GetFileInfo{ltcounts.dtx}
\title{\filename}
\date{\filedate}
 \author{%
  Johannes Braams\and
  David Carlisle\and
  Alan Jeffrey\and
  Leslie Lamport\and
  Frank Mittelbach\and
  Chris Rowley\and
  Rainer Sch\"opf}

\begin{document}
 \MaintainedByLaTeXTeam{latex}
 \maketitle
 \DocInput{\filename}
\end{document}
%</driver>
% \fi
%
% \providecommand\env[1]{\texttt{#1}}
%
% \changes{v1.0c}{1994/03/29}
%     {Create file from parts of ltmiscen and ltherest.}
% \changes{v1.1a}{1994/05/19}{Extracted file from ltcntlen.}
% \changes{v1.1d}{1996/04/23}{Documentation improvements}
% \changes{v1.2c}{2025/12/31}{Added \cs{newcounteralias}}
% \section{Counters and Lengths}
% Commands for defining and using counters.
% This file defines the following commands. In each case \marg{counter} may be
% |*| denoting the current counter as set by a previous |\refstepcounter|.
%
% \DescribeMacro{\newcounter}
% To define a new counter.
%
% \DescribeMacro{\newcounteralias}
% To define an alias name for a counter.
% 
% \DescribeMacro{\setcounter}
% To set the value of counters.
%
% \DescribeMacro{\addtocounter}
% Increase the \marg{counter} |#1| by the number |#2|.
%
% \DescribeMacro{\stepcounter}
% Increase the \marg{counter} by one.
%
% \DescribeMacro{\refstepcounter}
% Increase the \marg{counter} by one, also setting the value used by |\label|.
%
%
% \DescribeMacro{\value}
% For accessing the value of the counter as a \TeX\ number (as opposed
% to |\the|\meta{counter} which expands to the \emph{printed}
% representation of \meta{counter})
%
% \DescribeMacro\arabic
% |\arabic|\marg{counter}: 1, 2, 3, \ldots
%
% \DescribeMacro\roman
% |\roman|\marg{counter}: i, ii, iii, \ldots
%
% \DescribeMacro\Roman
% |\Roman|\marg{counter}: I, II, III, \ldots
%
% \DescribeMacro\alph
% |\alph|\marg{counter}: a, b, c, \ldots
%
% \DescribeMacro\Alph
% |\Alph|\marg{counter}: A, B, C, \ldots
%
% \DescribeMacro\fnsymbol
% |\fnsymbol|\marg{counter}:  $*$, $\dagger$, $\ddagger$, \ldots
%
%
% \DescribeMacro\counterwithin
% |\counterwithin|\oarg{format}\marg{counter}\marg{within-counter}:
% Resets \meta{counter} whenever
% \meta{within-counter} is stepped. Also
% redefines |\the|\meta{counter} command to produce
% |\the|\meta{within-counter}.\meta{format}\marg{counter} with \cs{arabic} as
% the default for \meta{format}.
% Star form omits redefining the print representation.
%
% The |*| alias for the current counter may not be used in either argument.
%
% \DescribeMacro\counterwithout
% |\counterwithout|\oarg{format}\marg{counter}\marg{within-counter}:
% Removes \meta{counter} from the reset list of \meta{within-counter}.
% Also
% redefines |\the|\meta{counter} command to produce
% \meta{format}\marg{counter} with \cs{arabic} as
% the default for \meta{format}.
% Star form omits redefining the print representation.
%
% The |*| alias for the current counter may not be used in either argument.
%
% \MaybeStop{}
%
%    \begin{macrocode}
%<*2ekernel>
%    \end{macrocode}
%
% \subsection{Document command and environment counter macros}
%
%  An environment  \texttt{foo}   has an associated counter defined by the
%  following control sequences:
%  \begin{center}
%  \begin{tabular}{lp{.75\textwidth}}
%  |\c@foo|  &  Contains the counter's numerical value.
%                It is defined by  |\newcount\foocounter|.\\
%  |\thefoo| & Macro that expands to the printed value of |\foocounter|.
%              For example, if sections are numbered within chapters,
%              and section headings look like
%
%                  Section II-3.  The Nature of Counters
%
%              then |\thesection| might be defined by:\newline
%                 |\def\thesection|\newline\hspace*{2em}
%                    |{\@Roman{\c@chapter}-\@arabic{\c@section}}|\\
%  |\theHfoo|& Macro that is used to create destination names. It should give a unique
%              value across the document for every |\refstepcounter|.\\ 
%  |\p@foo|  & Macro that expands to a printed `reference prefix' of
%              counter foo.  Any |\ref| to a value created by counter
%              foo will produce the expansion of |\p@foo\thefoo|  when
%              the |\label| command is executed. See file \texttt{ltxref.dtx}
%              for an extension of this mechanism.\\
%  |\cl@foo| & List of counters to be reset when foo stepped.
%              Has format
%                  |\@elt{countera}\@elt{counterb}\@elt{counterc}|.\\
%  |\alias@ctr@foo| & if foo is an alias counter created with |\newcounteralias| this
%                  command contains the name of the root counter.                 
%  \end{tabular}
%  \end{center}
%  For some environments the counter is named slightly differently,
%  e.g., the counters associated with the \texttt{enumerate}
%  environment have the names \texttt{enumi}, \texttt{enumii},
%  \texttt{enumiii}, and \texttt{enumiv} depending on the nesting
%  level.
%
%  The same internal macros are defined for counters used by commands
%  such as \cs{chapter}, \cs{section}, etc.
%
% \textbf{NOTE:}\\
% |\thefoo| and |\p@foo| \emph{must} be defined in such a way that
% |\edef\bar{\thefoo}| or |\edef\bar{\p@foo}|
% defines |\bar| so that it will evaluate to the counter value at the
% time of the |\edef|, even after |\foocounter| and any other counters
% have been changed.  This will happen if you use the standard commands
% |\@arabic|, |\@Roman|, etc.
%
%
%  The following commands are used to define and modify counters.
%
%    |\refstepcounter|\marg{foo}\\
%         Same as |\stepcounter|, but it also defines
%         |\@currentlabel|, |\@currentHref| and |\@currentcounter|
%          and so that a subsequent
%         |\label|\marg{bar} command causes |\ref|\marg{bar} to
%          generate the current value of counter \meta{foo}.
%
%    |\@definecounter|\marg{foo}\\
%        Initializes counter \marg{foo} (with empty reset list), defines
%        |\p@foo| and |\thefoo| to be null and |\theHfoo| to be
%        |\number\value{foo}|. Also adds \meta{foo}
%        to |\cl@@ckpt| --
%          the reset list of a dummy counter |@ckpt| used for taking
%          checkpoints for the |\include| system.
%
%    |\@addtoreset|\marg{foo}\marg{bar} :
%          Adds counter \meta{foo} to the list of counters
%         |\cl@bar| to be reset  when counter \meta{bar} is stepped.
%
%    |\@removefromreset|\marg{foo}\marg{bar} :
%          Removes counter \meta{foo} to the list of counters
%         |\cl@bar| to be reset  when counter \meta{bar} is stepped.
%
%
% \begin{macro}{\setcounter}
% \changes{v1.0d}{1994/04/09}
%     {\cs{@nocnterr} now has counter name argument}
% \changes{v1.0e}{1994/04/17}
%     {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
%    |\setcounter|\marg{foo}\marg{val}  :
%  Globally sets |\foocounter| equal to \meta{val}.
%    \begin{macrocode}
\def\setcounter#1#2{%
  \@ifundefined{c@#1}%
    {\@nocounterr{#1}}%
    {\global\csname c@#1\endcsname#2\relax}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\addtocounter}
% \changes{v1.0d}{1994/04/09}
%     {\cs{@nocnterr} now has counter name argument}
% \changes{v1.0e}{1994/04/17}
%     {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
% |\addtocounter|\marg{foo}\marg{val}
% Globally increments |\foocounter| by \meta{val}.
%    \begin{macrocode}
\def\addtocounter#1#2{%
  \@ifundefined{c@#1}%
    {\@nocounterr{#1}}%
    {\global\advance\csname c@#1\endcsname #2\relax}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newcounter}
% |\newcounter|\marg{newctr}\oarg{oldctr}
%    Defines \meta{newctr} to be a counter, which is reset when counter
%       \meta{oldctr} is stepped.  If \meta{newctr} already defined
% produces
%           `|c@newctr  already defined|' error.
%    \begin{macrocode}
\def\newcounter#1{%
  \expandafter\@ifdefinable \csname c@#1\endcsname
    {\@definecounter{#1}}%
  \@ifnextchar[{\@newctr{#1}}{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newcounteralias}
% \changes{v1.2c}{2025/12/31}{New command \cs{newcounteralias}}
% |\newcounteralias|\marg{aliasctr}\marg{rootctr}
% Defines \meta{aliasctr} as an alias for the counter \meta{rootctr}.
% The root counter can itself be an alias counter (it is not 
% recommended to chain counter names like this as keeping track of the 
% representations can get tricky, but explicit tests to avoid
% that seems unnecessary).
% The various representation commands of the alias counter call the 
% respective representations of the root. This means that if 
% a representation of the root is redefined, e.g. to make
% \cs{theH}\meta{counter} unique then the alias follows. 
% It is possible to redefine an alias representation, 
% but this then breaks the connection to the root representation. 
% 
% Every alias counter FOO has an associated command \cs{alias@ctr@FOO} which points to
% the root. This allows to test if a counter is an alias counter and also allows to
% find the root. 
% 
% We do not rollback the definition to avoid errors if packages use it 
% that do not rollback.
%    \begin{macrocode}
\newcommand*{\newcounteralias}[2]{%
    \@ifundefined{c@#2}{%
      \@nocounterr{#2}%
    }{%
      \expandafter\@ifdefinable\csname c@#1\endcsname{%
       \global\expandafter\let
        \csname c@#1\expandafter\endcsname\csname c@#2\endcsname
       \expandafter\edef\csname the#1\endcsname{\expandafter\noexpand\csname the#2\endcsname}%        
       \expandafter\edef\csname theH#1\endcsname{\expandafter\noexpand\csname theH#2\endcsname}%        
       \expandafter\edef\csname p@#1\endcsname{\expandafter\noexpand\csname p@#2\endcsname}%        
       \expandafter\edef\csname cl@#1\endcsname{\expandafter\noexpand\csname cl@#2\endcsname}%
       \expandafter\def\csname alias@ctr@#1\endcsname{#2}%
      }%
    }%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\value}
%    |\value|\marg{ctr}
%           produces the value of counter \meta{ctr}, for use with
%           a |\setcounter| or |\addtocounter| command.
%    \begin{macrocode}
\def\value#1{\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\c@*}
% \changes{v1.2a}{2025/01/22}{\cs{c@*} added (gh/1632)}
% Make the current counter available as a \LaTeX\ counter
% with name |*|, so |\alph*| returns the current
% counter as a letter, |\stepcounter{*}| increments the current
% counter, etc.
%    \begin{macrocode}
\protected\expandafter
  \def\csname c@*\endcsname{\value\@currentcounter}
%    \end{macrocode}
% \end{macro}
%
%\begin{macro}{\@newctr}
% \changes{v1.0d}{1994/04/09}
%     {\cs{@nocnterr} now has counter name argument}
% \changes{v1.0e}{1994/04/17}
%     {Use \cs{@nocounterr} instead of \cs{@nocnterr}}
%    \begin{macrocode}
\def\@newctr#1[#2]{%
  \@ifundefined{c@#2}{\@nocounterr{#2}}{\@addtoreset{#1}{#2}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\stepcounter}
% \changes{LaTeX209}{1992/11/23}{Replaced \{\} in \cs{stepcounter} by
%       \cs{begingroup} \cs{endgroup} to avoid adding an empty ord in
%       math mode}
% \changes{v1.0d}{1994/04/09}
%     {Use \cs{addtocounter} to have name checked}
%    |\stepcounter|{foo}
%  Globally increments counter |\c@FOO|
%                             and resets all subsidiary counters.
%    \begin{macrocode}
\def\stepcounter#1{%
  \addtocounter{#1}\@ne
  \begingroup
    \let\@elt\@stpelt
    \csname cl@#1\endcsname
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@stpelt}
% \changes{v1.1h}{2015/01/10}{Reset all within counters in one go (latexrelease)}
%
%    Rather than resetting the ``within'' counter to zero we set it to
%    $-1$ and then run |\stepcounter| that moves it to $0$ and also
%    initiates resetting the next level down.
%    \begin{macrocode}
%</2ekernel>
%<latexrelease>\IncludeInRelease{2015/01/01}{\@stpelt}
%<latexrelease>                             {Reset nested counters}%
%<*2ekernel|latexrelease>
\def\@stpelt#1{\global\csname c@#1\endcsname \m@ne\stepcounter{#1}}%
%<latexrelease>\EndIncludeInRelease
%</2ekernel|latexrelease>
%<latexrelease>\IncludeInRelease{0000/00/00}{\@stpelt}
%<latexrelease>                             {Reset nested counters}%%
%<latexrelease>\def\@stpelt#1{\global\csname c@#1\endcsname \z@}%
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cl@@ckpt}
%    \begin{macrocode}
\def\cl@@ckpt{\@elt{page}}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\@definecounter}
%  \changes{v1.1b}{1995/05/20}{Streamlined code}
%  \changes{v1.1c}{1995/05/20}{And do it right}
%  \changes{v1.1n}{2023/11/07}{Do not change \cs{the...} if already defined (gh/823)}
%  \changes{v1.1o}{2024/09/20}{define theHfoo (used for internal links)}
%  \changes{v1.1p}{2024/10/26}{Fully expand counter name in theHfoo commands (gh/1508)}
%  \changes{v1.2c}{2025/12/31}{Add support for alias counter}
%  Older definitions of \cs{@definecounter} called unconditionally \cs{newcount}. 
%  This had the effect that connection to an alias counter could break if a user 
%  did, e.g.,
%  \begin{verbatim}
%  \newcounter{theorem}
%  \newcounteralias{lemma}{theorem}
%  \newtheorem{theorem}{Theorem}
%  \newtheorem{lemma}{Lemma}
%  \end{verbatim}
%  The command was therefore changed and now tests if the count already exist.
%  If the counter to define is an already existing alias counter then we do not reset
%  the representations and do not add it to \texttt{@ckpt}.
%    \begin{macrocode}
%</2ekernel>
%<latexrelease>\IncludeInRelease{2026/06/01}{\@definecounter}
%<latexrelease>                             {add aliascounter}%
%<*2ekernel|latexrelease>
\def\@definecounter#1{%
     \@ifundefined{c@#1}{\expandafter\newcount\csname c@#1\endcsname}{}%
     \setcounter{#1}\z@
     \@ifundefined{alias@ctr@#1}
      {\global\expandafter\let\csname cl@#1\endcsname\@empty
       \@addtoreset{#1}{@ckpt}%
       \global\expandafter\let\csname p@#1\endcsname\@empty
       \expandafter\xdef\csname theH#1\endcsname{%
         \noexpand\the\noexpand\value{#1}}}{}%
%    \end{macrocode}
%    If \cs{the\#1} is undefined or \cs{relax} we define it with the
%    standard definition for counters, otherwise we warn. This will
%    catch, for example, that somebody defines a counter named
%    ``index'' conflicting with the \env{theindex} environment.
%    \begin{macrocode}
     \expandafter
     \ifx\csname the#1\endcsname\relax
       \expandafter
       \gdef\csname the#1\expandafter\endcsname\expandafter
              {\expandafter\@arabic\csname c@#1\endcsname}%
     \else
       \@latex@warning{Command `\string\the#1' already
                       defined -- not changed}%
     \fi}
%<latexrelease>\EndIncludeInRelease
%</2ekernel|latexrelease>
%<latexrelease>\IncludeInRelease{2024/11/01}{\@definecounter}
%<latexrelease>                             {provide theHfoo commands}%
%<latexrelease>\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
%<latexrelease>     \setcounter{#1}\z@
%<latexrelease>     \global\expandafter\let\csname cl@#1\endcsname\@empty
%<latexrelease>     \@addtoreset{#1}{@ckpt}%
%<latexrelease>     \global\expandafter\let\csname p@#1\endcsname\@empty
%<latexrelease>     \expandafter\xdef\csname theH#1\endcsname{%
%<latexrelease>       \noexpand\the\noexpand\value{#1}}%
%    \end{macrocode}
%    If \cs{the\#1} is undefined or \cs{relax} we define it with the
%    standard definition for counters, otherwise we warn. This will
%    catch, for example, that somebody defines a counter named
%    ``index'' conflicting with the \env{theindex} environment.
%    \begin{macrocode}
%<latexrelease>     \expandafter
%<latexrelease>     \ifx\csname the#1\endcsname\relax
%<latexrelease>       \expandafter
%<latexrelease>       \gdef\csname the#1\expandafter\endcsname\expandafter
%<latexrelease>              {\expandafter\@arabic\csname c@#1\endcsname}%
%<latexrelease>     \else
%<latexrelease>       \@latex@warning{Command `\string\the#1' already
%<latexrelease>                       defined -- not changed}%
%<latexrelease>     \fi}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000/00/00}{\@definecounter}
%<latexrelease>                             {provide theHfoo commands}%%
%<latexrelease>\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
%<latexrelease>     \setcounter{#1}\z@
%<latexrelease>     \global\expandafter\let\csname cl@#1\endcsname\@empty
%<latexrelease>     \@addtoreset{#1}{@ckpt}%
%<latexrelease>     \global\expandafter\let\csname p@#1\endcsname\@empty
%<latexrelease>     \expandafter
%<latexrelease>     \ifx\csname the#1\endcsname\relax
%<latexrelease>       \expandafter
%<latexrelease>     \gdef\csname the#1\expandafter\endcsname\expandafter
%<latexrelease>          {\expandafter\@arabic\csname c@#1\endcsname}%
%<latexrelease>     \else
%<latexrelease>       \@latex@warning{Command `\string\the#1' already
%<latexrelease>                       defined -- not changed}%
%<latexrelease>     \fi}
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
% \begin{macro}{\@kernel@get@ctr@root}
% A helper command to retrieve the root counter for messages.
%    \begin{macrocode}
\def\@kernel@get@ctr@root#1{%
 \@ifundefined{alias@ctr@#1}{#1}
   {\expandafter\@kernel@get@ctr@root\csname alias@ctr@#1\endcsname}} 
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@addtoreset}
%  \changes{v1.1o}{2024/09/20}{add the parent theHfoo if a counter is reset}
%  \changes{v1.1p}{2024/10/26}{Fully expand counter name in theHfoo commands (gh/1508)}
%  If a counter is reset when a parent counter changes it no longer has an unique value
%  across the document. As |\theH<counter>| should be unique
%  this representation is changed to include also the
%  representation of the parent. This is not 100\% guaranteed to work
%  but has been used this way by hyperref for many years without causing problems.
%  \changes{v1.2c}{2025/12/31}{Support alias counters}
%  Changing \cs{cl@counter} and \cs{theHcounter} isn't trivial to do 
%  if the counter is an alias counter: e.g. only redefining \cs{theH}\meta{counter} from 
%  the alias counter breaks the connection to the theH-representation of the 
%  root counter. Redefining both \cs{theH}-representations could overwrite
%  changes made by the user. If there is a chain of alias counters it gets even 
%  more complicated to keep everything in sync. 
%  So unlike the package \pkg{aliascnt} we avoid these complications and 
%  do not allow alias counters as arguments to \cs{@addtoreset} and \cs{@removefromreset}.
%    \begin{macrocode}
%</2ekernel>
%<latexrelease>\IncludeInRelease{2026/06/01}{\@addtoreset}
%<latexrelease>                             {support alias counter}%
%<*2ekernel|latexrelease>
\def\@addtoreset#1#2{%
  \@ifundefined{alias@ctr@#1}{%
   \@ifundefined{alias@ctr@#2}
     {\expandafter\@cons\csname cl@#2\endcsname {{#1}}%
      \expandafter\xdef\csname theH#1\endcsname{%
       \expandafter\noexpand\csname theH#2\endcsname.%
        \noexpand\the\noexpand\value{#1}}}%
     {\@latex@warning{Alias counters can not be used in a counter reset.\MessageBreak
       Use '\@kernel@get@ctr@root{#2}' instead of '#2'.}}}
   {\@latex@warning{Alias counters can not be used in a counter reset.\MessageBreak
     Use '\@kernel@get@ctr@root{#1}' instead of '#1'.}}}
%<latexrelease>\EndIncludeInRelease
%</2ekernel|latexrelease>
%<latexrelease>\IncludeInRelease{2024/11/01}{\@addtoreset}
%<latexrelease>                             {provide theHfoo commands}%
%<latexrelease>\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}%
%<latexrelease>  \expandafter\xdef\csname theH#1\endcsname{%
%<latexrelease>    \expandafter\noexpand\csname theH#2\endcsname.%
%<latexrelease>    \noexpand\the\noexpand\value{#1}}%
%<latexrelease>}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000/00/00}{\@addtoreset}
%<latexrelease>                             {provide theHfoo commands}%%
%<latexrelease>\def\@addtoreset#1#2{\expandafter\@cons\csname cl@#2\endcsname {{#1}}}
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
%
%
%    \begin{macrocode}
%</2ekernel>
%    \end{macrocode}
%
%
% \begin{macro}{\@removefromreset}
%    \changes{v1.1k}{2018/03/08}{Interface added}
%    \changes{v1.2c}{2025/12/31}{Support for alias counter}
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{2026/06/01}{\@addtoreset}
%<latexrelease>                             {support alias counter}%
%<*2ekernel|latexrelease>
\def\@removefromreset#1#2{%
%    \end{macrocode}
%    Even through this is internal and the programmer should know what
%    he/she is doing we test here if counter |#2| is defined. If not,
%    the execution would run into a tight loop.
%    As in |\@addtoreset| do not allow alias counters as arguments.
%    \begin{macrocode}
  \@ifundefined{c@#2}\relax
  {\@ifundefined{alias@ctr@#1}{%
     \@ifundefined{alias@ctr@#2}{%
       \begingroup
         \expandafter\let\csname c@#1\endcsname\@removefromreset
         \def\@elt##1{%
           \expandafter\ifx\csname c@##1\endcsname\@removefromreset
           \else
             \noexpand\@elt{##1}%
           \fi}%
         \expandafter\xdef\csname cl@#2\endcsname
          {\csname cl@#2\endcsname}%
        \endgroup}%
       {\@latex@warning{Alias counters can not be removed from a reset.\MessageBreak
       Use '\@kernel@get@ctr@root{#2}' instead of '#2'.}}} 
     {\@latex@warning{Alias counters can not be removed from a reset.\MessageBreak
       Use '\@kernel@get@ctr@root{#1}' instead of '#1'.}}}}
%<latexrelease>\EndIncludeInRelease    
%</2ekernel|latexrelease>
%    \end{macrocode}
%
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{2018-04-01}
%<latexrelease>                 {\@removefromreset}{Add interfaces}%
%<latexrelease>\def\@removefromreset#1#2{%
%    \end{macrocode}
%    Even through this is internal and the programmer should know what
%    he/she is doing we test here if counter |#2| is defined. If not,
%    the execution would run into a tight loop.
%    \begin{macrocode}
%<latexrelease>  \@ifundefined{c@#2}\relax
%<latexrelease>  {\begingroup
%<latexrelease>     \expandafter\let\csname c@#1\endcsname\@removefromreset
%<latexrelease>     \def\@elt##1{%
%<latexrelease>       \expandafter\ifx\csname c@##1\endcsname\@removefromreset
%<latexrelease>       \else
%<latexrelease>         \noexpand\@elt{##1}%
%<latexrelease>       \fi}%
%<latexrelease>     \expandafter\xdef\csname cl@#2\endcsname
%<latexrelease>      {\csname cl@#2\endcsname}%
%<latexrelease>    \endgroup}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@ifbothcounters}
%    \changes{v1.1k}{2018/03/08}{Interface added}
%    \changes{v1.2a}{2025/01/22}{Guard against star alias.}
%    Test if arg |#1| and |#2| are counters and if so execute |#3|.
%    \begin{macrocode}
%<*2ekernel|latexrelease>
\begingroup\catcode`\*=11 \lowercase{\endgroup
\def\@ifbothcounters#1#2#3{%
%    \end{macrocode}
%    \begin{macrocode}
  \begingroup\let\c@*\@undefined
%    \end{macrocode}
%    \begin{macrocode}
  \@ifundefined{c@#1}{\@nocounterr{#1}}%
    {% else counter is defined
     \@ifundefined{c@#2}{\@nocounterr{#2}}%
       {% else both counter and within  are defined
         #3}}%
%    \end{macrocode}
%    \begin{macrocode}
   \endgroup}}
%    \end{macrocode}
%  \end{macro}
%
%    \begin{macrocode}
%</2ekernel|latexrelease>
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000-00-00}
%<latexrelease>                 {\@removefromreset}{Add interfaces}%
%<latexrelease>\let \@removefromreset \undefined
%<latexrelease>\let \@ifbothcounters  \undefined
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%
%
%
%
%  \begin{macro}{\counterwithin}
%     New implementation using xparse and supporting an optional
%    format argument.
%  \changes{v1.1m}{2021/07/08}{New implementation for
%           \cs{counterwithin}}
% \changes{v1.2b}{2025/02/22}{\cs{counterwithin} needs to expand
%                             its arguments (gh/1675)}
%    \begin{macrocode}
%</2ekernel>
%<*2ekernel|latexrelease>
%<latexrelease>\IncludeInRelease{2025/06/01}%
%<latexrelease>                 {\counterwithin}{counter within}%
%    \end{macrocode}
%
%    \begin{macrocode}
\NewDocumentCommand \counterwithin {sO{\arabic}mm}{%
  \@ifbothcounters{#3}{#4}{%
    \@addtoreset{#3}{#4}%
    \IfBooleanF #1%
       {\expandafter
         \xdef\csname the#3\expandafter\endcsname
            {\expandafter\noexpand\csname the#4\endcsname
             .\unexpanded{#2}{#3}}}%
  }%
}
%    \end{macrocode}
%
%    \changes{v1.1k}{2018/03/08}{Interface added}
%    \begin{macrocode}
%</2ekernel|latexrelease>
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{2021/11/15}%
%<latexrelease>                 {\counterwithin}{counter within}%
%<latexrelease>
%<latexrelease>\NewDocumentCommand \counterwithin {sO{\arabic}mm}{%
%<latexrelease>  \@ifbothcounters{#3}{#4}{%
%<latexrelease>    \@addtoreset{#3}{#4}%
%<latexrelease>    \IfBooleanF #1%
%<latexrelease>       {\expandafter
%<latexrelease>         \gdef\csname the#3\expandafter\endcsname
%<latexrelease>         \expandafter
%<latexrelease>             {\csname the#4\endcsname .#2{#3}}}%
%<latexrelease>  }%
%<latexrelease>}
%<latexrelease>\EndIncludeInRelease
%    \end{macrocode}
%
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{2018-04-01}
%<latexrelease>                 {\counterwithin}{counter within}%
%<latexrelease>
%<latexrelease>\def\counterwithin{\@ifstar\counterwithin@s\counterwithin@x}
%<latexrelease>\def\counterwithin@s#1#2{%
%<latexrelease>  \@ifbothcounters{#1}{#2}{\@addtoreset{#1}{#2}}}
%    \end{macrocode}
%
%    \changes{v1.1l}{2018/04/11}{Correct default (issue/38)}
%    \begin{macrocode}
%<latexrelease>\def\counterwithin@x#1#2{%
%<latexrelease>  \@ifbothcounters{#1}{#2}%
%<latexrelease>      {\@addtoreset{#1}{#2}%
%<latexrelease>       \expandafter
%<latexrelease>       \gdef\csname the#1\expandafter\endcsname\expandafter
%<latexrelease>            {\csname the#2\expandafter\endcsname\expandafter
%<latexrelease>             .\expandafter
%<latexrelease>             \@arabic\csname c@#1\endcsname}}}
%<latexrelease>
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000-00-00}
%<latexrelease>                 {\counterwithin}{counter within}%
%<latexrelease>\let \counterwithin    \undefined
%<latexrelease>\let \counterwithin@s  \undefined
%<latexrelease>\let \counterwithin@x  \undefined
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
%
%
%
%
%  \begin{macro}{\counterwithout}
%     New implementation using xparse and supporting an optional
%    format argument.
% \changes{v1.1m}{2021/07/08}{New implementation for
%                             \cs{counterwithout}}
% \changes{v1.2b}{2025/03/01}{\cs{counterwithout} needs to expand
%                             its arguments (gh/1675)}
%    \begin{macrocode}
%</2ekernel>
%<*2ekernel|latexrelease>
%<latexrelease>\IncludeInRelease{2025/06/01}%
%<latexrelease>                 {\counterwithout}{counter without}%
%    \end{macrocode}
%
%    \begin{macrocode}
\NewDocumentCommand \counterwithout {sO{\arabic}mm}{%
  \@ifbothcounters{#3}{#4}{%
    \@removefromreset{#3}{#4}%
    \IfBooleanF #1%
        {\expandafter
         \xdef\csname the#3\endcsname {\unexpanded{#2}{#3}}}%
  }%
}
%    \end{macrocode}
%
%    \changes{v1.1k}{2018/03/08}{Interface added}
%    \begin{macrocode}
%</2ekernel|latexrelease>
%<latexrelease>\EndIncludeInRelease
%    \end{macrocode}
%    
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{2021/11/15}%
%<latexrelease>                 {\counterwithout}{counter without}%
%<latexrelease>\NewDocumentCommand \counterwithout {sO{\arabic}mm}{%
%<latexrelease>  \@ifbothcounters{#3}{#4}{%
%<latexrelease>    \@removefromreset{#3}{#4}%
%<latexrelease>    \IfBooleanF #1%
%<latexrelease>        {\expandafter
%<latexrelease>         \gdef\csname the#3\endcsname {#2{#3}}}%
%<latexrelease>  }%
%<latexrelease>}
%<latexrelease>\EndIncludeInRelease
%    \end{macrocode}
%
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{2018-04-01}
%<latexrelease>                 {\counterwithout}{counter without}%
%<latexrelease>
%<latexrelease>\def\counterwithout {\@ifstar\counterwithout@s\counterwithout@x}
%<latexrelease>\def\counterwithout@s#1#2{%
%<latexrelease>  \@ifbothcounters{#1}{#2}{\@removefromreset{#1}{#2}}}
%<latexrelease>\def\counterwithout@x#1#2{%
%<latexrelease>  \@ifbothcounters{#1}{#2}%
%<latexrelease>      {\@removefromreset{#1}{#2}%
%<latexrelease>       \expandafter
%<latexrelease>       \gdef\csname the#1\expandafter\endcsname\expandafter
%<latexrelease>            {\expandafter
%<latexrelease>             \@arabic\csname c@#1\endcsname}}}
%<latexrelease>\EndIncludeInRelease
%<latexrelease>
%<latexrelease>\IncludeInRelease{0000-00-00}
%<latexrelease>                 {\counterwithout}{counter without}%
%<latexrelease>\let \counterwithout   \undefined
%<latexrelease>\let \counterwithout@s \undefined
%<latexrelease>\let \counterwithout@x \undefined
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
%
%
%
%
%    Numbering commands for definitions of |\theCOUNTER| and |\list|
%    arguments.
%
%    All commands can now be used in text and math mode.
%
%  \begin{macro}{\arabic}
%          Representation of\meta{counter} as arabic numerals.
%          Changed 29 Apr 86 to make it print the obvious thing
%          it COUNTER not positive.
%    \begin{macrocode}
\def\arabic#1{\expandafter\@arabic\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\roman}
%  Representation of \meta{counter} as lower-case
%                           Roman numerals.
%    \begin{macrocode}
\def\roman#1{\expandafter\@roman\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\Roman}
%  Representation of \meta{counter} as upper-case
%                           Roman numerals.
%    \begin{macrocode}
\def\Roman#1{\expandafter\@Roman\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\alph}
%  Representation of \meta{counter} as a lower-case
%                           letter: 1 = a, 2 = b, etc.
%    \begin{macrocode}
\def\alph#1{\expandafter\@alph\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\Alph}
%  Representation of \meta{counter} as an upper-case
%                           letter: 1 = A, 2 = B, etc.
%    \begin{macrocode}
\def\Alph#1{\expandafter\@Alph\csname c@#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\fnsymbol}
%  \changes{v1.1b}{1995/05/20}{Streamlined code}
% \changes{v1.1e}{1997/03/21}
%     {Use \cs{mathsection} and \cs{mathparagraph}. latex/2445}
%
%    Representation of \meta{COUNTER} as a footnote
%                           symbol: 1 = $*$, 2 = $\dagger$, etc.
%    \begin{macrocode}
\def\fnsymbol#1{\expandafter\@fnsymbol\csname c@#1\endcsname}
%    \end{macrocode}
%  \end{macro}
%
% \changes{v1.0f}{1994/05/13}{Removed \cs{@ialph}}
% \changes{v1.0f}{1994/05/13}{Removed \cs{@Ialph}}
%
%  \begin{macro}{\@arabic}
%     |\@arabic\FOOcounter|
%     Representation of |\FOOcounter| as arabic numerals.
%    \begin{macrocode}
\def\@arabic#1{\number #1}  %% changed 29 Apr 86
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@roman}
%   |\@roman\FOOcounter|
%    Representation of |\FOOcounter| as lower-case
%                           Roman numerals.
%    \begin{macrocode}
\def\@roman#1{\romannumeral #1}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@Roman}
%     |\@Roman\FOOcounter|
%     Representation of |\FOOcounter| as upper-case
%                    Roman numerals.
%
% \changes{v1.1f}{1997/10/06}{Change \cs{@Roman} to be fully
%    expandable, so that the result is written properly to files.}
%    \begin{macrocode}
\def\@Roman#1{\expandafter\@slowromancap\romannumeral #1@}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@slowromancap}
% \changes{v1.1f}{1997/10/06}{Macro added.}
%    Fully expandable macro to change a roman number to uppercase.
%    \begin{macrocode}
\def\@slowromancap#1{\ifx @#1% then terminate
     \else
       \if i#1I\else\if v#1V\else\if x#1X\else\if l#1L\else\if
       c#1C\else\if d#1D\else \if m#1M\else#1\fi\fi\fi\fi\fi\fi\fi
       \expandafter\@slowromancap
     \fi
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@alph}
%     |\@alph\FOOcounter|
%     Representation of |\FOOcounter| as a lower-case
%                    letter: 1 = a, 2 = b, etc.
%    \begin{macrocode}
\def\@alph#1{%
  \ifcase#1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or
   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
    y\or z\else\@ctrerr\fi}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@Alph}
%     |\@Alph\FOOcounter|
%       Representation of |\FOOcounter| as an upper-case
%                    letter: 1 = A, 2 = B, etc.
%    \begin{macrocode}
\def\@Alph#1{%
  \ifcase#1\or A\or B\or C\or D\or E\or F\or G\or H\or I\or J\or
   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
    Y\or Z\else\@ctrerr\fi}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@fnsymbol}
%  \changes{v1.1b}{1995/05/20}{Allowing both text and math}
%    Typesetting old fashioned footnote symbols.
%    This can be done both in text or math mode now.
%
%  \changes{v1.1h}{2015/01/10}{Unse \cs{TextOrMath} (latexrelease)}
%    This macro is another example of an ever recurring problem in
%    \TeX: Determining if something is text-mode or math-mode. It is
%    imperative for the decision between text and math to be delayed
%    until the actual typesetting is done as the code in question may
%    go through an |\edef| or |\write| where an |\ifmmode| test would
%    be executed prematurely. Hence in the implementation below,
%    |\@fnsymbol| is not robust in itself but the parts doing the
%    actual typesetting are.
%
%    In the case of |\@fnsymbol| we make use of the robust command
%    |\TextOrMath| which takes two arguments and typesets the first if
%    in text-mode and the second if in math-mode. Note that in order
%    for this command to make the correct decision, it must insert a
%    |\relax| token if run under regular \TeX, which ruins any kerning
%    between the preceding characters and whatever awaits
%    typesetting. If you use e\TeX\ as engine for \LaTeX\ (as
%    recommended) this unfortunate side effect is not present.
%    \begin{macrocode}
%</2ekernel>
%<latexrelease>\IncludeInRelease{2015/01/01}{\@fnsymbol}{Use \TextOrMath}%
%<*2ekernel|latexrelease>
\def\@fnsymbol#1{%
   \ifcase#1\or \TextOrMath\textasteriskcentered *\or
   \TextOrMath \textdagger \dagger\or
   \TextOrMath \textdaggerdbl \ddagger \or
   \TextOrMath \textsection  \mathsection\or
   \TextOrMath \textparagraph \mathparagraph\or
   \TextOrMath \textbardbl \|\or
   \TextOrMath {\textasteriskcentered\textasteriskcentered}{**}\or
   \TextOrMath {\textdagger\textdagger}{\dagger\dagger}\or
   \TextOrMath {\textdaggerdbl\textdaggerdbl}{\ddagger\ddagger}\else
   \@ctrerr \fi
}%
%</2ekernel|latexrelease>
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000/00/00}{\@fnsymbol}{Use \TextOrMath}%
%<latexrelease>\def\@fnsymbol#1{\ensuremath{%
%<latexrelease>   \ifcase#1\or *\or \dagger\or \ddagger\or \mathsection\or
%<latexrelease>     \mathparagraph\or \|\or **\or \dagger\dagger
%<latexrelease>     \or \ddagger\ddagger \else\@ctrerr\fi}}%
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\TextOrMath}
%    \changes{v1.1h}{2015/01/11}{Add command to solve robustness
%      issues (pr/3752) (latexrelease)}
%    \changes{v1.1j}{2016/10/19}{Test directly for \cs{protected}}
%    When using regular \TeX, we make this command robust so that it
%    always selects the correct branch in an |\ifmmode| switch with
%    the usual disadvantage of ruining kerning. For the application we
%    use it for here that shouldn't matter. The alternative would be
%    to mimic |\IeC| from \textsf{inputenc} but then it will have the
%    disadvantage of choosing the wrong branch if appearing at the
%    beginning of an alignment cell. However, users of e\TeX\ will be
%    pleasantly surprised to get the best of both worlds and no bad
%    side effects.
%
%    First some code for checking if we are running e\TeX\ but making
%    sure not to permanently turn |\protected| into |\relax|.
%    \begin{macrocode}
%</2ekernel>
%<latexrelease>\IncludeInRelease{2015/01/01}{\TextOrMath}{\TextOrMath}%
%<*2ekernel|latexrelease>
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname protected\endcsname\relax
%    \end{macrocode}
% In case of ordinary \TeX\ we define |\TextOrMath| as a robust
% command but make sure it always grabs its arguments. If we didn't do
% this it might very well gobble spaces in the input stream.
%    \begin{macrocode}
\DeclareRobustCommand\TextOrMath{%
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
\protected@edef\TextOrMath#1#2{\TextOrMath{#1}{#2}}
\else
%    \end{macrocode}
% For e\TeX\ the situation is similar. The robust macro is a hidden
% one so that we again avoid problems of gobbling spaces in the input.
%    \begin{macrocode}
\protected\expandafter\def\csname TextOrMath\space\endcsname{%
  \ifmmode  \expandafter\@secondoftwo
  \else     \expandafter\@firstoftwo  \fi}
\edef\TextOrMath#1#2{%
  \expandafter\noexpand\csname TextOrMath\space\endcsname
  {#1}{#2}}
\fi
%</2ekernel|latexrelease>
%<latexrelease>\EndIncludeInRelease
%<latexrelease>\IncludeInRelease{0000/00/00}{\TextOrMath}{\TextOrMath}%
%<latexrelease>\let\TextOrMath\@undefined
%<latexrelease>\EndIncludeInRelease
%<*2ekernel>
%    \end{macrocode}
%  \end{macro}
%
%    \begin{macrocode}
%</2ekernel>
%    \end{macrocode}
%
% \Finale
%
