LaTeX uses internal counters that provide numbering of pages, sections, tables, figures, etc. This article explains how to access and modify those counters and how to create new ones.
Contents |
A counter can be easily set to any arbitrary value with \setcounter
. See the example below:
\section{Introduction} This document will present several counting examples, how to reset and access them. For instance, if you want to change the numbers in a list. \begin{enumerate} \setcounter{enumi}{3} \item Something. \item Something else. \item Another element. \item The last item in the list. \end{enumerate}
In this example \setcounter{enumi}{3}
sets the value of the item counter in the list to 3. This is the general syntax to manually set the value of any counter. See the reference guide for a complete list of counters.
All commands changing a counter's state in this section are changing it globally.
Counters in a document can be incremented, reset, accessed and referenced. Let's see an example:
\section{Another section} This is a dummy section with no purpose whatsoever but to contain text. This section has assigned the number \thesection. \stepcounter{equation} \begin{equation} \label{1stequation} \int_{0}^{\infty} \frac{x}{\sin(x)} \end{equation}
In this example, two counters are used:
\thesection
section
at this point. For further methods to print a counter take a look on how to print counters.
\stepcounter{equation}
equation
. Other similar commands are \addtocounter
and \refstepcounter
, see the reference guide.
Further commands to manipulate counters include:
\counterwithin<*>{<ctr1>}{<ctr2>}
<ctr2>
to the counters that reset <ctr1>
when they're incremented. If you don't provide the *
, \the<ctr1>
will be redefined to \the<ctr2>.\arabic{<ctr1>}
. This macro is included in the LaTeX format since April 2018, if you're using an older version, you'll have to use the chngctr
package.
\counterwithout<*>{<ctr1>}{<ctr2>}
<ctr2>
from the counters that reset <ctr1>
when they're incremented. If you don't provide the *
, \the<ctr1>
will be redefined to \arabic{<ctr1>}
. This macro is included in the LaTeX format since April 2018, if you're using an older version, you'll have to use the chngctr
package.
\addtocounter{<ctr>}{<num>}
<num>
to the value of the counter <ctr>
.
\setcounter{<ctr>}{<num>}
<ctr>
's value to <num>
.
\refstepcounter{<ctr>}
\stepcounter
but you can use LaTeX's referencing system to add a \label
and later \ref
the counter. The printed reference will be the current expansion of \the<ctr>
.
The basic syntax to create a new counter is by \newcounter
. Below an example that defines a numbered environment called example:
\documentclass{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \newcounter{example}[section] \newenvironment{example}[1][]{\refstepcounter{example}\par\medskip \textbf{Example~\theexample. #1} \rmfamily}{\medskip} \begin{document} This document will present... \begin{example} This is the first example. The counter will be reset at each section. \end{example} Below is a second example \begin{example} And here's another numbered example. \end{example} \section{Another section} This is a dummy section with no purpose whatsoever but to contain text. This section has assigned the number \thesection. \stepcounter{equation} \begin{equation} \label{1stequation} \int_{0}^{\infty} \frac{x}{\sin(x)} \end{equation} \begin{example} This is the first example in this section. \end{example} \end{document}
In this LaTeX snippet the new environment example
is defined, this environment has 3 counting-specific commands.
\newcounter{example}[section]
section
or omit the parameter if you don't want your defined counter to be automatically reset.
\refstepcounter{example}
\label
afterwards.
\theexample
For further information on user-defined environments see the article about defining new environments
You can print the current value of a counter in different ways:
\theCounterName
2.1
for the first subsection in the second section.
\arabic
2
.
\value
\setcounter{section}{\value{subsection}}
).
\alph
b
.
\Alph
B
.
\roman
ii
.
\Roman
II
.
\fnsymbol
†
.
\theCounterName
is the macro responsible to print CounterName
's value in a formatted manner. For new counters created by \newcounter
it gets initialized as an Arabic number. You can change this by using \renewcommand
. For example if you want to change the way a subsection counter is printed to include the current section in italics and the current subsection in uppercase Roman numbers, you could do the following:
\renewcommand\thesubsection{\textit{\thesection}.\Roman{subsection}} \section{Example} \subsection{Example}\label{sec:example:ssec:example} This is the subsection \ref{sec:example:ssec:example}.
Default counters in LaTeX
Usage | Name |
---|---|
For document structure |
|
For floats |
|
For footnotes |
|
For the enumerate environment |
|
Counter manipulation commands
\addtocounter{CounterName}{number}
\stepcounter{CounterName}
\refstepcounter{CounterName}
It works like \stepcounter
, but makes the counter visible to the referencing mechanism (\ref{label}
returns counter value)
\setcounter{CounterName}{number}
\newcounter{NewCounterName}
If you want the NewCounterName counter to be reset to zero every time that another OtherCounterName counter is increased, use:
\newcounter{NewCounterName}[OtherCounterName]
\setcounter{section}{\value{subsection}}
.
\value{CounterName}
\theCounterName
for example: \thechapter
, \thesection
, etc. Note that this might result in more than just the counter, for example with the standard definitions of the article
class \thesubsection
will print Section.Subsection (e.g. 2.1
).
For more information see: