Skip to content

Commit

Permalink
code for #1632 (#1637)
Browse files Browse the repository at this point in the history
* code for #1632

* verb

* Update base/ltcounts.dtx

Co-authored-by: Yukai Chou <[email protected]>

* Update base/ltcounts.dtx

Co-authored-by: Yukai Chou <[email protected]>

* commas and brackets

* the

* updates from review

* safer test for  argument

* extra macro env

* commit test file for #1632

* guard star in counterwithin/out, from review #1632

* more words for ltnews #1632

---------

Co-authored-by: Yukai Chou <[email protected]>
  • Loading branch information
davidcarlisle and muzimuzhi authored Jan 26, 2025
1 parent 5d3a5d4 commit 8712c3a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 15 deletions.
6 changes: 6 additions & 0 deletions base/changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ not part of the distribution.
* ltcmd.dtx, usrguide.tex
New "c"-type argument

2025-01-22 David Carlisle <[email protected]>

* 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 <[email protected]>

* ltoutput.dtx (subsection{Floats}):
Expand Down
11 changes: 11 additions & 0 deletions base/doc/ltnews41.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 38 additions & 12 deletions base/ltcounts.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -56,14 +56,17 @@
%</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}
%
% \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.
Expand All @@ -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}
Expand Down Expand Up @@ -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}:
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
%
Expand Down Expand Up @@ -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}
Expand Down
21 changes: 18 additions & 3 deletions base/ltxref.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
%<latexrelease>\IncludeInRelease{2022/06/01}%
%<latexrelease> {\Ref}{Add starred version}%
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
36 changes: 36 additions & 0 deletions base/testfiles/github-1632.lvt
Original file line number Diff line number Diff line change
@@ -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}
22 changes: 22 additions & 0 deletions base/testfiles/github-1632.tlg
Original file line number Diff line number Diff line change
@@ -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 <return> for immediate help.
...
l. ...\counterwithin{equation}{*}
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
! LaTeX Error: No counter '*' defined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l. ...\counterwithout{*}{equation}
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
[1
] (github-1632.aux)

0 comments on commit 8712c3a

Please sign in to comment.