×

ShareLaTeX guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

L a T e X 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

Introduction

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.

Open an example in ShareLaTeX

Counter manipulation

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 manipulated:

\thesection
Prints 2, the value of the counter section at this point. To print the value of a counter just put \the right before the name of the counter.
\stepcounter{equation}
Increases by 1 the value of the counter equation . Other similar commands are \addtocounter and \refstepcounter , see the reference guide .

Open an example in ShareLaTeX

Creating new counters

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 L a T e X snippet the new environment example is defined, this environment has 3 counting-specific commands.

\newcounter{example}[section]
Creates a new counter called example that will be reset every time the section counter is increased. You can put any other counter instead of section or omit the parameter if you don't want your defined counter to be automatically reset.
\refstepcounter{example}
Increases the counter by 1 and makes it visible for the referencing mechanism.
\theexample
Prints the current value of the counter example .

For further information on user-defined environments see the article about defining new environments

Open an example in ShareLaTeX

Reference guide

Default counters in L a T e X

Usage Name
For document structure
  • part
  • chapter
  • section
  • subsection
  • subsubsection
  • paragraph
  • subparagraph
  • page
For floats
  • equation
  • figure
  • table
For footnotes
  • footnote
  • mpfootnote
For the enumerate environment
  • enumi
  • enumii
  • enumiii
  • enumiv

Counter manipulation commands

  • Increase the value of the counter by number
\addtocounter{CounterName}{number}
  • Incrementation (increase the value of the counter by 1)
\stepcounter{CounterName}
  • Incrementation linked with referencing mechanism
\refstepcounter{CounterName} 

It works like \stepcounter , but makes the counter visible to the referencing mechanism ( \ref{label} returns counter value)

  • Set the counter value explicitly
\setcounter{CounterName}{number}
  • Create a new counter (is automatically set to zero)
\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]
  • Return the value of the counter. Note, that it is not a formatted string, so it cannot be used in text.
\value{CounterName}
  • Display the value of the counter
\theCounterName

for example: \thechapter , \thesection , etc.

Open an example in ShareLaTeX

Further reading

For more information see: