Counters
Within 'counters', LaTeX keeps track of numberings, like automatic title numbering and numbering of equations.
#
InspectionThe current value of the subsection
-counter can be printed
in your document with \arabic{subsection}
. However, this will always print
the number in arabic numbers, but you might want to put it in roman numbers for example, or prepend
the current section number or add diferent formatting. This formatting is done by \thesubsection
.
See the following demo:
\section{AA}
Counters: \thesubsection,
\arabic{subsection}, \roman{subsection}
\subsection{BB}
Counters: \thesubsection,
\arabic{subsection}, \roman{subsection}
\subsection{CC}
Counters: \thesubsection,
\arabic{subsection}, \roman{subsection}
#
ManipulationYou can change the value of the counter with \setcounter
and \addtocounter
: first you pass the name of the counter,
followed by the new value or the amount to be added.
\section{AA}
\setcounter{section}{9}
\subsection{BB}
\addtocounter{subsection}{-4}
subsection: \arabic{subsection}
\subsection{CC}
Title numbering doesn't use \addtocounter{section}{1}
directly,
but \refstepcounter
. This increments the counter, but also resets
the subsection counter, meaning you don't get 1, 1.1, 1.2, 2, 2.3, but 1, 1.1, 1.2, 2, 2.1.
It also arranges that \label
and \ref
function properly.
Spare the formatting, you're now able to simulate \section
and \subsection
.
\refstepcounter{section}
\label{sec:AA}
\textbf{\thesection, AA}
\refstepcounter{subsection}
\textbf{\thesubsection, BB}
\refstepcounter{subsection}
\label{sec:CC}
\textbf{\thesubsection, CC}
\refstepcounter{section}
\textbf{\thesection, DD}
Zie Paragraaf \ref{sec:AA} en
Deelparagraaf \ref{sec:CC}.
\refstepcounter{subsection}
\textbf{\thesubsection, DD}
#
FormattingWith \meaning\thesubsection
you can see how the formatting works:
the command \thesubsection
evaluates to \thesection.\@arabic\c@subsection
,
which is equivalent to \thesection.\arabic{subsection}
.
If you now use
\renewcommand\thesubsection
{(\thesection)\alph{subsection}}
then 2.1 becomes (2)a and 5.3 becomes (5)c.
#
CounterwithinWe just saw how you can change a number's formatting using \renewcommand
.
You could use this for example to preprend the equation number with the current section
-number. That
results in
But after a \section
, the equation number just continues, so the next
equation would be (3.20)
instead of (3.1)
. As we have seen, \refstepcounter
can perform automatic number resets, like it does for subsection
-numbers. To enable the
same feature for equations, we need to specify \counterwithin*{equation}{section}
.
You can also use \counterwithin
(without the star), which
does everything automatically:
\counterwithin{equation}{section}
% is equivalent to:
\counterwithin*{equation}{section}
\renewcommand\theequation
{\thesection.\arabic{equation}}
To remove a counter from the reset list, you can use \counterwithout*
.
Without the star, the formatting itself will also be reset.
#
Available countersTo list the counters which are by default defined, add the following code into your document:
{\makeatletter
\def\@elt #1{#1; }
\cl@@ckpt}
It will print something like
page; equation; enumi; enumii; enumiii; enumiv; footnote; mpfootnote; part; section; subsection;subsubsection; paragraph; subparagraph; figure; table; parentequation