Counters and their “partners”

A counter created via \newcounter{<counter>} is stored internally as \c@<counter>, just like a reference made using \label{<label>} is stored internally using \r@<label>. What's the use of \cl@<counter> and \p@<counter>? The macros associated with their use include (from latex.ltx):

\def\@definecounter#1{\expandafter\newcount\csname c@#1\endcsname
\setcounter{#1}\z@
\global\expandafter\let\csname cl@#1\endcsname\@empty
\global\expandafter\let\csname p@#1\endcsname\@empty
\expandafter
\gdef\csname the#1\expandafter\endcsname\expandafter
{\expandafter\@arabic\csname c@#1\endcsname}}
%...
\def\refstepcounter#1{\stepcounter{#1}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
}
%...
\def\stepcounter#1{%
\begingroup
\let\@elt\@stpelt
\csname cl@#1\endcsname
\endgroup}
%...

-

The answer is given by the LaTeX source documentation:

\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 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}.

For instance,

\newcounter{subsubsection}[subsection]


calls

\@newctr{subsubsection}[subsection]


and this is defined as

\def\@newctr#1[#2]{%


So the counter subsection is automatically reset when \subsubsection is called, hence \cl@subsubsection contains \@elt{subsection}.

As the documentation says, the p@ feature can be used to "extend" the label of a counter. For instance, article.cls contains

\renewcommand\p@enumii{\theenumi}
\renewcommand\p@enumiii{\theenumi(\theenumii)}
\renewcommand\p@enumiv{\p@enumiii\theenumiii}


with the effect that

\documentclass{article}
\begin{document}
\begin{enumerate}
\item foo
\begin{enumerate}
\item bar
\begin{enumerate}
\item\label{itembaz} baz
\end{enumerate}

\end{enumerate}

\end{enumerate}

\ref{itembaz}

\end{document}


will result in

and not just "i".

-
@lockstep that was copied verbatim from article.cls ;-) –  Stephan Lehmke Sep 29 '12 at 7:35
Nobody ever claimed that class was perfect. ;-) –  lockstep Sep 29 '12 at 7:38
This mechanism is slightly broken by default in latex. See here: tex.ac.uk/cgi-bin/texfaq2html?label=labelformat Also, you must have had a bug in your grep commandline because I see that this feature is used in various babel language files (eg, hebrew.ldf) also in the ams classes (amsart et al) and many others. –  Lev Bishop Sep 29 '12 at 7:40
@LevBishop Thank you for pointing that out! As I said, I just grepped for " p@" (note the space), so it is more than likely I overlooked something. The string "p@", though, is so frequent (think of "strip@", "skip@" or simply \p@) that it is futile to grep everywhere for it :-) My conclusion was that I can't give examples. If you can, please do! (Anyway, I'll try to find something based on your hints and incorporate that in my answer). –  Stephan Lehmke Sep 29 '12 at 8:21
The \p@ mechanism is mainly used for nested lists, see article.cls \renewcommand\p@enumii{\theenumi} –  David Carlisle Sep 29 '12 at 8:44