diff --git a/base/changes.txt b/base/changes.txt index 6ec5c7e8f..b8774f2af 100644 --- a/base/changes.txt +++ b/base/changes.txt @@ -17,6 +17,12 @@ not part of the distribution. * ltcmd.dtx, usrguide.tex New "c"-type argument +2025-01-22 David Carlisle + + * ltcounts.dtx, ltxref.dtx: + Allow * to denote the current counter (as used by \label) in counter + related commands, \alph*, \stepcounter*, etc. (gh/1632) + 2025-01-21 Frank Mittelbach * ltoutput.dtx (subsection{Floats}): diff --git a/base/doc/ltnews41.tex b/base/doc/ltnews41.tex index 1de376653..d3c5a78bb 100644 --- a/base/doc/ltnews41.tex +++ b/base/doc/ltnews41.tex @@ -244,6 +244,17 @@ \subsection{Socket and plug conditionals} % \githubissue{1577} + +\subsection{Accessing the current counter} +Counter commands such as \cs{alph}, \cs{stepcounter}, may now have +the argument \texttt{*} to denote the current counter (as used by +\cs{label}). This is compatible with the package \pkg{enumitem} use of +\verb|\alph*| in item labels but is generally available. +Not all commands accept \verb|*|, for example +\verb|\counterwithin| and \verb|\counterwithout| require counter names as before. +% +\githubissue{1632} + \subsection{Collecting environment bodies verbatim} The mechanisms in \pkg{ltcmd} (\enquote{\pkg{xparse}}) offer a powerful way to diff --git a/base/ltcounts.dtx b/base/ltcounts.dtx index 498d5d6db..da88a92b7 100644 --- a/base/ltcounts.dtx +++ b/base/ltcounts.dtx @@ -33,7 +33,7 @@ %<*driver> % \fi \ProvidesFile{ltcounts.dtx} - [2024/10/26 v1.1p LaTeX Kernel (Counters)] + [2025/01/22 v1.2a LaTeX Kernel (Counters)] % \iffalse \documentclass{ltxdoc} \GetFileInfo{ltcounts.dtx} @@ -56,6 +56,7 @@ % % \fi % +% \providecommand\env[1]{\texttt{#1}} % % \changes{v1.0c}{1994/03/29} % {Create file from parts of ltmiscen and ltherest.} @@ -63,7 +64,9 @@ % \changes{v1.1d}{1996/04/23}{Documentation improvements} % % \section{Counters and Lengths} -% Commands for defining and using counters. This file defines: +% 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. @@ -72,13 +75,13 @@ % To set the value of counters. % % \DescribeMacro{\addtocounter} -% Increase the counter |#1| by the number |#2|. +% Increase the \marg{counter} |#1| by the number |#2|. % % \DescribeMacro{\stepcounter} -% Increase a counter by one. +% Increase the \marg{counter} by one. % % \DescribeMacro{\refstepcounter} -% Increase a counter by one, also setting the value used by |\label|. +% Increase the \marg{counter} by one, also setting the value used by |\label|. % % % \DescribeMacro{\value} @@ -113,7 +116,8 @@ % |\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}: @@ -124,6 +128,8 @@ % 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} @@ -246,7 +252,19 @@ % \end{macrocode} % \end{macro} % -% \begin{macro}{\@newctr} +% \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} @@ -422,19 +440,27 @@ \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} +\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}}} + #3}}% +% \end{macrocode} +% \begin{macrocode} + \endgroup}} % \end{macrocode} % \end{macro} % @@ -621,7 +647,7 @@ % |\@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} diff --git a/base/ltxref.dtx b/base/ltxref.dtx index 7acd6250b..da2d9a6ae 100644 --- a/base/ltxref.dtx +++ b/base/ltxref.dtx @@ -33,7 +33,7 @@ %<*driver> % \fi \ProvidesFile{ltxref.dtx} - [2024/12/10 v1.1s LaTeX Kernel (Cross Referencing)] + [2025/01/22 v1.1t LaTeX Kernel (Cross Referencing)] % \iffalse \documentclass{ltxdoc} \GetFileInfo{ltxref.dtx} @@ -418,12 +418,21 @@ % % % \begin{macro}{\refstepcounter} +% \changes{v1.1t}{2025/01/22}{Macro added} +% Saved |*| for testing the argument of \cs{refstepcounter}. +% \begin{macrocode} +\def\ltx@star@counter{*} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\refstepcounter} % Step the counter and allow for labels to point to its current value. % \changes{v1.1n}{2020/05/05}{record the counter name in \cs{@currentcounter}} % \changes{v1.1o}{2020/08/23}{add default definition of \cs{@currentcounter}} % \changes{v1.1r}{2024/09/20}{set also \@currentHref} % \changes{v1.1r}{2024/09/20}{provide a kernel copy \@kernel@refstepcounter} % \changes{v1.1r}{2024/09/20}{add sockets} +% \changes{v1.1t}{2025/01/22}{Guard * from causing infinite loop} % \begin{macrocode} %\IncludeInRelease{2022/06/01}% % {\Ref}{Add starred version}% @@ -465,7 +474,10 @@ \def\refstepcounter#1{% \UseSocket{refstepcounter}{% \stepcounter{#1}% - \edef\@currentcounter{#1}% + \edef\reserved@a{#1}% + \ifx\reserved@a\ltx@star@counter\else + \let\@currentcounter\reserved@a + \fi \protected@edef\@currentlabel % \end{macrocode} % By generating the second csname first the |\p@...| command can @@ -501,7 +513,10 @@ \def\@kernel@refstepcounter#1{% \UseSocket{refstepcounter}{% \stepcounter{#1}% - \edef\@currentcounter{#1}% + \edef\reserved@a{#1}% + \ifx\reserved@a\ltx@star@counter\else + \let\@currentcounter\reserved@a + \fi \protected@edef\@currentlabel {\csname p@#1\expandafter\endcsname\csname the#1\endcsname}}}% % \end{macrocode} diff --git a/base/testfiles/github-1632.lvt b/base/testfiles/github-1632.lvt new file mode 100644 index 000000000..c05ad6552 --- /dev/null +++ b/base/testfiles/github-1632.lvt @@ -0,0 +1,36 @@ +\documentclass{article} +\input{test2e} + +\begin{document} + +\START +\section{aaa} +% this should not error +\arabic* + +\refstepcounter{*} + +\typeout{section counter: 2=\arabic*} + +\alph* + +\section{aaa} + +\subsection{bb} +\subsection{bbb} + +\addtocounter{*}{5} + +\typeout{subsection counter: roman 7 = \roman*} + +% this should not error +\begin{enumerate} +\item \Roman* +\end{enumerate} + +% these should both error +\counterwithin{equation}{*} + +\counterwithout{*}{equation} + +\end{document} diff --git a/base/testfiles/github-1632.tlg b/base/testfiles/github-1632.tlg new file mode 100644 index 000000000..bfb4d1dee --- /dev/null +++ b/base/testfiles/github-1632.tlg @@ -0,0 +1,22 @@ +This is a generated file for the LaTeX2e validation system. +Don't change this file in any respect. +section counter: 2=2 +subsection counter: roman 7 = vii +! LaTeX Error: No counter '*' defined. +See the LaTeX manual or LaTeX Companion for explanation. +Type H for immediate help. + ... +l. ...\counterwithin{equation}{*} +Your command was ignored. +Type I to replace it with another command, +or to continue without it. +! LaTeX Error: No counter '*' defined. +See the LaTeX manual or LaTeX Companion for explanation. +Type H for immediate help. + ... +l. ...\counterwithout{*}{equation} +Your command was ignored. +Type I to replace it with another command, +or to continue without it. +[1 +] (github-1632.aux)