> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/formatting/10-counters.md).

# Counters

## Introduction to LaTeX counters

LaTeX-based typesetting is a specialized form of programming: you are using a typesetting language (LaTeX commands) to provide “instructions” which result in an output: the compiled LaTeX document in the form of a PDF file. As with any programming endeavour, it’s likely that you will need LaTeX to temporarily store integer values for later re-use in your document (code)—you might want to:

* typeset those stored integers in your document
* provide automatic numbering of certain elements of your document
* use them as variables to control conditional code (or loops)
* change and update their values by incrementing/decrementing using some fixed value
* reset them to completely different values

Such integer-processing activities are extremely common in “regular” programming languages such as Javascript, C, Lua or any other language of your choice where you typically declare (create a name for) a variable (to contain an integer) and then assign a value to it. The LaTeX typesetting language provides similar capabilities, albeit implemented in a quite different way, as we’ll explore below.

In essence, a LaTeX counter is the name of a “LaTeX variable” used to store integer values which can be used for the actives listed above, and much else besides. LaTeX itself uses numerous internal counters which provide numbering of pages, sections, tables, figures, bulleted/numbered lists etc. This article explains how to access and modify those counters and how to create and use new ones.

## LaTeX commands for working with counters

Here is a list of LaTeX’s counter commands linked to its description in the following sections.

Creating and incrementing counters:

* [`\newcounter{somecounter}[anothercounter]`](#newcountersomecounteranothercounter)
* [`\setcounter{somecounter}{number}`](#setcountersomecounternumber)
* [`\addtocounter{somecounter}{number}`](#addtocountersomecounternumber)
* [`\stepcounter{somecounter}`](#stepcountersomecounter)
* [`\refstepcounter{somecounter}`](#refstepcountersomecounter)

Accessing and printing counter values:

* [`\arabic{somecounter}`](#arabicsomecounter)
* [`\roman{somecounter}`](#romansomecounter)
* [`\Roman{somecounter}`](#romanoutput)
* [`\alph{somecounter}`](#alphsomecounter)
* [`\Alph{somecounter}`](#alphoutput)
* [`\fnsymbol{somecounter}`](#fnsymbolsomecounter)
* [`\value{somecounter}`](#valuesomecounter)

Other counter commands:

* [`\counterwithin{somecounter}{anothercounter}`](#counterwithinsomecounteranothercounter)
* [`\counterwithout{somecounter}{anothercounter}`](#counterwithoutsomecounteranothercounter)

### Creating and incrementing counters

#### \newcounter{somecounter}\[anothercounter]

Used to define a new counter. The second argument, enclosed in `[]`, is optional but when used it defines `*somecounter*` as a new counter which is reset when `*anothercounter*` is stepped. `\newcounter` is often used in its simpler form `\newcounter{*somecounter*}` which creates `*somecounter*` and initializes it to contain 0.

* **Note**: For each counter created by the `\newcounter` command, it *also* defines a command that can be used to typeset the counter’s value. For example, `\newcounter{*mycounter*}` defines a command called `\the*mycounter*`. An example is shown in the following LaTeX code fragment which you can open in Overleaf, as a fully-formed document, using the link provided:

```latex
\newcounter{mycounter}
\setcounter{mycounter}{42}
You can now write \verb|\themycounter| to obtain \themycounter.
```

[Open this LaTeX fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bmycounter%7D%0A%5Csetcounter%7Bmycounter%7D%7B42%7D%0AYou+can+now+write+%5Cverb%7C%5Cthemycounter%7C+to+obtain+%5Cthemycounter.%0A%5Cend%7Bdocument%7D)

This example produces $$\text{You can now write }\verb|\themycounter|\text{ to obtain 42.}$$

* **Note**: If `*mycounter*` has already been defined LaTeX will issue the error `c@mycounter already defined`, as demonstrated by the following example you can open in Overleaf.

```latex
\newcounter{mycounter}
...lots of code... but we forget and write \verb|\newcounter{mycounter}| again
\newcounter{mycounter}
```

[Open this ***error-generating code*** in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bmycounter%7D%0A...lots+of+code...+but+we+forget+and+write+%5Cverb%7C%5Cnewcounter%7Bmycounter%7D%7C+again%0A%5Cnewcounter%7Bmycounter%7D%0A%5Cend%7Bdocument%7D)

#### \setcounter{somecounter}{number}

Sets `*somecounter*` to contain the value `*number*`.

* **Note**: `*number*` can be positive or negative.

An example is shown in the following LaTeX code fragment which you can open in Overleaf, as a fully-formed document, using the link provided:

```latex
\noindent Create a new counter \texttt{myvar} and assign it the value \texttt{42}.
\vspace{10pt}

\newcounter{myvar}
\setcounter{myvar}{42}

\noindent Writing \verb|\themymar| typesets \texttt{\themyvar}.
\vspace{10pt}

\noindent Next, we'll change \texttt{myvar} to \texttt{-42}, writing \verb|\setcounter{myvar}{-42}|
\setcounter{myvar}{-42}. Now, writing \verb|\themyvar| outputs \texttt{\themyvar}.
```

[Open this LaTeX fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Create+a+new+counter+%5Ctexttt%7Bmyvar%7D+and+assign+it+the+value+%5Ctexttt%7B42%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnewcounter%7Bmyvar%7D%0A%5Csetcounter%7Bmyvar%7D%7B42%7D+%0A%0A%5Cnoindent+Writing+%5Cverb%7C%5Cthemymar%7C+typesets+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+Next%2C+we%27ll+change+%5Ctexttt%7Bmyvar%7D+to+%5Ctexttt%7B-42%7D%2C+writing+%5Cverb%7C%5Csetcounter%7Bmyvar%7D%7B-42%7D%7C%0A%5Csetcounter%7Bmyvar%7D%7B-42%7D.+Now%2C+writing+%5Cverb%7C%5Cthemyvar%7C+outputs+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cend%7Bdocument%7D)

#### \addtocounter{somecounter}{number}

Increases the counter `*somecounter*` by amount `*number*`.

* **Note**: `*number*` can be positive, to increase the counter value, or negative to decrease it. For example:

```latex
\addtocounter{somecounter}{-1} % decreases somecounter by 1
```

An example is shown in the following LaTeX code fragment which you can open in Overleaf, as a fully-formed document, using the link provided:

```latex
\noindent Create a new counter \texttt{myvar} and assign it the value \texttt{42}.
\vspace{10pt}

\newcounter{myvar}
\setcounter{myvar}{42}

\noindent Writing \verb|\themymar| typesets \texttt{\themyvar}.
\vspace{10pt}

\noindent Next, we’ll change \texttt{myvar} to \texttt{142} by writing \verb|\addtocounter{myvar}{100}|\addtocounter{myvar}{100}. Now, writing \verb|\themyvar| outputs \texttt{\themyvar}.
```

[Open this LaTeX fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+addtocounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Create+a+new+counter+%5Ctexttt%7Bmyvar%7D+and+assign+it+the+value+%5Ctexttt%7B42%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnewcounter%7Bmyvar%7D%0A%5Csetcounter%7Bmyvar%7D%7B42%7D+%0A%0A%5Cnoindent+Writing+%5Cverb%7C%5Cthemymar%7C+typesets+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+Next%2C+we%E2%80%99ll+change+%5Ctexttt%7Bmyvar%7D+to+%5Ctexttt%7B142%7D+by+writing+%5Cverb%7C%5Caddtocounter%7Bmyvar%7D%7B100%7D%7C%5Caddtocounter%7Bmyvar%7D%7B100%7D.+Now%2C+writing+%5Cverb%7C%5Cthemyvar%7C+outputs+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cend%7Bdocument%7D)

#### \stepcounter{somecounter}

Increments `*somecounter*` by 1. An example is shown in the following LaTeX code fragment which you can open in Overleaf, as a fully-formed document, using the link provided:

```latex
If we create a new counter by writing \verb|\newcounter{mycounter}|
\newcounter{mycounter} then the counter \texttt{mycounter} is created and
initialized to zero, as you can see if you write \verb|\themycounter|:
\texttt{mycounter} currently stores: \themycounter.

If we now write \verb|\stepcounter{mycounter}|\stepcounter{mycounter} then
\verb|\themycounter| now contains the value \themycounter.
```

[Open this LaTeX fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+stepcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIf+we+create+a+new+counter+by+writing+%5Cverb%7C%5Cnewcounter%7Bmycounter%7D%7C%0A%5Cnewcounter%7Bmycounter%7D+then+the+counter+%5Ctexttt%7Bmycounter%7D+is+created+and+%0Ainitialized+to+zero%2C+as+you+can+see+if+you+write+%5Cverb%7C%5Cthemycounter%7C%3A%0A%5Ctexttt%7Bmycounter%7D+currently+stores%3A+%5Cthemycounter.+%0A%0AIf+we+now+write+%5Cverb%7C%5Cstepcounter%7Bmycounter%7D%7C%5Cstepcounter%7Bmycounter%7D+then+%0A%5Cverb%7C%5Cthemycounter%7C+now+contains+the+value+%5Cthemycounter.%0A%5Cend%7Bdocument%7D)

#### \refstepcounter{somecounter}

Increases `*somecounter*` by 1 and makes it visible for the referencing mechanism, also setting the value so that you can use `\label` afterwards.

**Example using \refstepcounter**

The following example uses `\refstepcounter` to create a new counter for an `example` environment and uses `\label` and `\ref` to illustrate referencing using the `example` counter variable.

```latex
\documentclass{article}

\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}

\begin{document}
\section{Three examples}
\begin{example}
Create a label in this first example \verb|\label{ex:1}|\label{ex:1}. This is the first example. The \texttt{example} counter will be reset at the start of each new each document \verb|\section|.
\end{example}

\begin{example}
And here's another numbered example. Create a second \verb|\label{ex:2}|\label{ex:2} to later reference this one. In Example \ref{ex:1} we read... something.
\end{example}

\begin{example}
And here's another numbered example: use \verb|\theexample| to typeset the number currently assigned to the \texttt{example} counter: it is  \theexample.
\end{example}

\section{Another section}
We've just started a new section meaning that the  \texttt{example} counter has been set to \theexample.
We'll reference examples from the previous section (Examples \ref{ex:1} and \ref{ex:2}).  This is a dummy section with no purpose whatsoever but to contain text. The \texttt{section} counter for this section can be typeset using \verb|\thesection|: it is  currently assigned the value of \thesection.

\begin{example}
This is the first example in this section: the \texttt{example} counter has been stepped and now set to \theexample.
\end{example}
\end{document}
```

[Open this `\refstepcounter` example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=An+example+using+refstepcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%0A%5Cnewcounter%7Bexample%7D%5Bsection%5D%0A%5Cnewenvironment%7Bexample%7D%5B1%5D%5B%5D%7B%5Crefstepcounter%7Bexample%7D%5Cpar%5Cmedskip%0A+++%5Cnoindent%5Ctextbf%7BExample%7E%5Ctheexample.+%231%7D+%5Crmfamily%7D%7B%5Cmedskip%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BThree+examples%7D%0A%5Cbegin%7Bexample%7D%0ACreate+a+label+in+this+first+example+%5Cverb%7C%5Clabel%7Bex%3A1%7D%7C%5Clabel%7Bex%3A1%7D.+This+is+the+first+example.+The+%5Ctexttt%7Bexample%7D+counter+will+be+reset+at+the+start+of+each+new+each+document+%5Cverb%7C%5Csection%7C.%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7Bexample%7D%0AAnd+here%27s+another+numbered+example.+Create+a+second+%5Cverb%7C%5Clabel%7Bex%3A2%7D%7C%5Clabel%7Bex%3A2%7D+to+later+reference+this+one.+In+Example+%5Cref%7Bex%3A1%7D+we+read...+something.+%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7Bexample%7D%0AAnd+here%27s+another+numbered+example%3A+use+%5Cverb%7C%5Ctheexample%7C+to+typeset+the+number+currently+assigned+to+the+%5Ctexttt%7Bexample%7D+counter%3A+it+is++%5Ctheexample.%0A%5Cend%7Bexample%7D%0A%0A%5Csection%7BAnother+section%7D%0AWe%27ve+just+started+a+new+section+meaning+that+the++%5Ctexttt%7Bexample%7D+counter+has+been+set+to+%5Ctheexample.++%0AWe%27ll+reference+examples+from+the+previous+section+%28Examples+%5Cref%7Bex%3A1%7D+and+%5Cref%7Bex%3A2%7D%29.++This+is+a+dummy+section+with+no+purpose+whatsoever+but+to+contain+text.+The+%5Ctexttt%7Bsection%7D+counter+for+this+section+can+be+typeset+using+%5Cverb%7C%5Cthesection%7C%3A+it+is++currently+assigned+the+value+of+%5Cthesection.%0A%0A%5Cbegin%7Bexample%7D%0AThis+is+the+first+example+in+this+section%3A+the+%5Ctexttt%7Bexample%7D+counter+has+been+stepped+and+now+set+to+%5Ctheexample.+%0A%5Cend%7Bexample%7D%0A%5Cend%7Bdocument%7D)

**Code summary**

In this example, 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 to 0 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 counter to be automatically reset.
* `\refstepcounter{example}`: Increases the `example` counter by 1 and makes it visible for the referencing mechanism, so that you can use `\label` afterwards.
* `\theexample`: Prints the current value of the counter `example`.

For further information on user-defined environments see [the article about defining new environments](/latex/commands/02-environments.md#defining-a-new-environment)

### Accessing and printing counter values

The following discussions are based on the LaTeX defaults prior to any customization applied by packages loaded by the user.

#### \arabic{somecounter}

Output the representation of `*somecounter*` as Arabic numerals: 1, 2, 3...

#### \roman{somecounter}

Output the representation of `*somecounter*` as a lower-case Roman numeral: i, ii, iii...

* **Note**: If `*somecounter*` $$\leq$$ `0` no output is produced. In addition, converting *extremely* large integer values to lower-case Roman numerals can take a long time to complete—and produces a lengthy sequence of `m` characters (representing the number `1000`).
* **TIP**: If you are interested in the algorithm TeX uses to convert Arabic numbers to Roman numerals you can read about it this article on [Algorithms for Roman Numerals](https://www.hanshq.net/roman-numerals.html#tex).

#### \Roman{somecounter}

Output the representation of `*somecounter*` as an upper-case Roman numeral: I, II, III...

* **Note**: If `*somecounter*` $$\leq$$ `0` no output is produced. In addition, converting *extremely* large integer values to upper-case Roman numerals can take a long time to complete—and produces a lengthy sequence of `M` characters (representing the number `1000`).
* **TIP**: If you are interested in the algorithm TeX uses to convert Arabic numbers to Roman numerals you can read about it this article on [Algorithms for Roman Numerals](https://www.hanshq.net/roman-numerals.html#tex).

#### \alph{somecounter}

Output the representation of `*somecounter*` as a lower-case letter where a=1, b=2, c=3...

* **Note**: `*somecounter*` must be in the range `1` $$\leq$$ `*somecounter*` $$\leq$$ `26`, otherwise it will trigger the error `! LaTeX Error: Counter too large.`

#### \Alph{somecounter}

Output the representation of `*somecounter*` as an upper-case letter where A=1, B=2, C=3...

* **Note**: `*somecounter*` must be in the range `1` $$\leq$$ `*somecounter*` $$\leq$$ `26`, otherwise it will trigger the error `! LaTeX Error: Counter too large.`

#### \fnsymbol{somecounter}

Output the representation of `*somecounter*` as a footnote symbol where 1 =∗, 2 =†...

* **Note**: `*somecounter*` must be in the range `1` $$\leq$$ `*somecounter*` $$\leq$$ `9`, otherwise it will trigger the error `! LaTeX Error: Counter too large.`

|                          |                                  |
| ------------------------ | -------------------------------- |
| Value of `*somecounter*` | Character typeset by `\fnsymbol` |
| 1                        | \*                               |
| 2                        | †                                |
| 3                        | ‡                                |
| 4                        | §                                |
| 5                        | ¶                                |
| 6                        | ∥                                |
| 7                        | ∗∗                               |
| 8                        | ††                               |
| 9                        | ‡‡                               |

#### Example of printing counter values

The following example demonstrates use of the commands `\arabic{*somecounter*}`, `\roman{*somecounter*}`, `\Roman{*somecounter*}`, `\alph{*somecounter*}`, `\Alph{*somecounter*}`, and `\fnsymbol{*somecounter*}`.

```latex
\newcounter{somecounter}
\setcounter{somecounter}{9}
\begin{itemize}
    \item \verb|\arabic{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as \arabic{somecounter}
    \item \verb|\roman{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as  \roman{somecounter}
    \item \verb|\Roman{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as  \Roman{somecounter}
    \item \verb|\alph{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as  \alph{somecounter}
   \item \verb|\Alph{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as  \Alph{somecounter}
    \item \verb|\fnsymbol{somecounter}| typesets the \texttt{somecounter} value of  \thesomecounter{} as  \fnsymbol{somecounter}
\end{itemize}
```

[Open this LaTeX fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Converting+counter+values\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bsomecounter%7D%0A%5Csetcounter%7Bsomecounter%7D%7B9%7D%0A%5Cbegin%7Bitemize%7D%0A++++%5Citem+%5Cverb%7C%5Carabic%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as+%5Carabic%7Bsomecounter%7D+%0A++++%5Citem+%5Cverb%7C%5Croman%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Croman%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5CRoman%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5CRoman%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5Calph%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Calph%7Bsomecounter%7D%0A+++%5Citem+%5Cverb%7C%5CAlph%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5CAlph%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5Cfnsymbol%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Cfnsymbol%7Bsomecounter%7D%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

This example produces the following Output:

![Example of converting counters in LaTeX](/files/ZZBDa76QapneOaKcTIfo)

#### \value{somecounter}

This purpose of command, as described in the LaTeX source code, is “For accessing the value of the counter as a TeX number”: i.e., you use `\value{somecounter}` for situations where LaTeX is expecting to process a numeric value.

**(Optional) background notes on the \value command**

It’s tempting to think the command `\value{*somecounter*}` will *directly* typeset the value of `*somecounter*`, but this is not the case. It is intended for use within other commands to access the value of the counter as a TeX number, as opposed to the command `\thesomecounter` which produces the printed representation of `*somecounter*`.

Within the LaTeX source code, `\value` is defined as:

```latex
\def\value#1{\csname c@#1\endcsname}
```

Consequently, `\value{*somecounter*}` creates an instance of the LaTeX-internal control sequence `\c@somecounter` which contains the value of the counter `*somecounter*`.

**Example**

The following example shows one basic use-case of `\value`.

```latex
\noindent Start by declaring two new counters:
\begin{verbatim}
\newcounter{first}
\newcounter{second}
\end{verbatim}

\newcounter{first}
\newcounter{second}

\noindent Sometime later in your code you set their values:
\begin{verbatim}
\setcounter{first}{100}
\setcounter{second}{50}
\end{verbatim}

\setcounter{first}{100}
\setcounter{second}{50}
\noindent Then you write more \LaTeX{} code...\vspace{10pt}

\noindent At some point we might want to add the value of counter \texttt{second} to counter \texttt{first}. One way to do that is using \verb|\value| to obtain the \textit{current} value stored in the \texttt{second} counter:

\begin{verbatim}
\addtocounter{first}{\value{second}}
\end{verbatim}

\addtocounter{first}{\value{second}}\noindent The value of \texttt{first} can be output by \verb|\thefirst|: \thefirst. We can also write \verb|\the\value{first}| which also typesets \the\value{first}. However,  writing \verb|\value{first}| will generate the error \texttt{Missing number, treated as zero} because \LaTeX{} then tries to execute the internal command \verb|\c@first|. Uncomment the following line to generate the error:
\begin{verbatim}
%\value{first}
\end{verbatim}
%\value{first}
```

[Open this code fragment in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+the+value+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Start+by+declaring+two+new+counters%3A%0A%5Cbegin%7Bverbatim%7D%0A%5Cnewcounter%7Bfirst%7D%0A%5Cnewcounter%7Bsecond%7D++++%0A%5Cend%7Bverbatim%7D%0A%0A%5Cnewcounter%7Bfirst%7D%0A%5Cnewcounter%7Bsecond%7D%0A%0A%5Cnoindent+Sometime+later+in+your+code+you+set+their+values%3A%0A%5Cbegin%7Bverbatim%7D%0A%5Csetcounter%7Bfirst%7D%7B100%7D%0A%5Csetcounter%7Bsecond%7D%7B50%7D++++%0A%5Cend%7Bverbatim%7D%0A%0A%5Csetcounter%7Bfirst%7D%7B100%7D%0A%5Csetcounter%7Bsecond%7D%7B50%7D++%0A%5Cnoindent+Then+you+write+more+%5CLaTeX%7B%7D+code...%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+At+some+point+we+might+want+to+add+the+value+of+counter+%5Ctexttt%7Bsecond%7D+to+counter+%5Ctexttt%7Bfirst%7D.+One+way+to+do+that+is+using+%5Cverb%7C%5Cvalue%7C+to+obtain+the+%5Ctextit%7Bcurrent%7D+value+stored+in+the+%5Ctexttt%7Bsecond%7D+counter%3A%0A%0A%5Cbegin%7Bverbatim%7D%0A%5Caddtocounter%7Bfirst%7D%7B%5Cvalue%7Bsecond%7D%7D%0A%5Cend%7Bverbatim%7D%0A%0A%5Caddtocounter%7Bfirst%7D%7B%5Cvalue%7Bsecond%7D%7D%5Cnoindent+The+value+of+%5Ctexttt%7Bfirst%7D+can+be+output+by+%5Cverb%7C%5Cthefirst%7C%3A+%5Cthefirst.+We+can+also+write+%5Cverb%7C%5Cthe%5Cvalue%7Bfirst%7D%7C+which+also+typesets+%5Cthe%5Cvalue%7Bfirst%7D.+However%2C++writing+%5Cverb%7C%5Cvalue%7Bfirst%7D%7C+will+generate+the+error+%5Ctexttt%7BMissing+number%2C+treated+as+zero%7D+because+%5CLaTeX%7B%7D+then+tries+to+execute+the+internal+command+%5Cverb%7C%5Cc%40first%7C.+Uncomment+the+following+line+to+generate+the+error%3A%0A%5Cbegin%7Bverbatim%7D%0A%25%5Cvalue%7Bfirst%7D%0A%5Cend%7Bverbatim%7D%0A%25%5Cvalue%7Bfirst%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Example using the \value command](/files/5lBFFnnvsnJMJBP5p1BF)

### Other counter commands

Here we describe the commands `\counterwithin` and `\counterwithout` which originated in the [`chngcntr` package](https://ctan.org/pkg/chngcntr) but have now been integrated into LaTeX itself—see [LaTeX News, April 2018](https://www.latex-project.org/news/latex2e-news/ltnews28.pdf). The `chngcntr` [package documentation](https://mirror.ox.ac.uk/sites/ctan.org/macros/latex/contrib/chngcntr/chngcntr.pdf) makes the following comments which help to understand the purpose of these commands:

On `\counterwithin`:

> It is sometimes desirable to change a counter that has been defined by `\newcounter{<ctr>}` to act as though it had been defined as `\newcounter{<ctr>}[<within>]`. The package provides the command `\counterwithin{<ctr>}{<within>}` that accomplishes this.

On `\counterwithout`:

> Likewise, the command `\counterwithout{<ctr>}{<within>}` changes a counter that has been created by `\newcounter{<ctr>}[<within>]` to act as though it had been created by `\newcounter{<ctr>}`.

In essence, these two commands provide a method to link (`\counterwithin`) or unlink (`\counterwithout`) two counters after they have been defined.

**\counterwithin{somecounter}{anothercounter}**

Resets `*somecounter*` to 0 whenever `*anothercounter*` is stepped, a mechanism which links the two counters. In addition to creating a link, this command redefines what is typeset by `\the*somecounter*`: instead of outputting the value of counter `*somecounter*` it produces `<value of anothercounter>**.**<value of somecounter>`. The starred form (`\counterwithin*{somecounter}{anothercounter}`) does not redefine the print-format of `*somecounter*`, as the following example demonstrates—select the link below the code to open it in Overleaf:

```latex
\documentclass{article}
\begin{document}
\section{A short example}
\newcounter{example}
\counterwithin{example}{section}
\newcounter{exampletwo}
\counterwithin*{exampletwo}{section}
\setcounter{example}{100}
\setcounter{exampletwo}{100}
\begin{itemize}
\item \verb|\theexample| typesets \theexample
\item \verb|\theexampletwo| typesets \theexampletwo
\end{itemize}
\end{document}
```

[Open this `\counterwithin` example in Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=Example+of+starred+version+of+counterwithin\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BA+short+example%7D%0A%5Cnewcounter%7Bexample%7D%0A%5Ccounterwithin%7Bexample%7D%7Bsection%7D%0A%5Cnewcounter%7Bexampletwo%7D%0A%5Ccounterwithin%2A%7Bexampletwo%7D%7Bsection%7D%0A%5Csetcounter%7Bexample%7D%7B100%7D%0A%5Csetcounter%7Bexampletwo%7D%7B100%7D%0A%5Cbegin%7Bitemize%7D%0A%5Citem+%5Cverb%7C%5Ctheexample%7C+typesets+%5Ctheexample%0A%5Citem+%5Cverb%7C%5Ctheexampletwo%7C+typesets+%5Ctheexampletwo%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Using the \counterwithin command](/files/VUMoTSce9NmLXRtGCdOo)

You can use `\counterwithin` with standard counters provided by LaTeX, such as:

* `section`: used by `\section` command
* `equation` used by the `equation` environment

or with counters you have created.

**Example of \counterwithin and \counterwithin\***

The following code provides a more complete example of using `\counterwithin` and `\counterwithin*`; select the link below the code to open it in Overleaf:

```latex
\documentclass{article}
\counterwithin{equation}{section}
\newcounter{example}
\counterwithin*{example}{section}
\newenvironment{example}[1][]{%
\stepcounter{example}%
\par\vspace{5pt}\noindent
\fbox{\textbf{Example~\thesection.\theexample}}%
\hrulefill\par\vspace{10pt}\noindent\rmfamily}%
{\par\noindent\hrulefill\vrule width10pt height2pt depth2pt\par}
\begin{document}
\section{First equation}
Some introductory text...
\begin{example}
\begin{equation}
    f(x)=\frac{x}{1+x^2}
\end{equation}
\end{example}

\subsection{More detail}
\begin{example}
Here we discuss
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
... but don't say anything
\end{example}

\subsubsection{Even more detail}
\begin{example}
\begin{equation}
    f(x)=\frac{x^2}{1+x^3}
\end{equation}
\begin{equation}
    f(x+\delta x)=\frac{(x+\delta x)^2}{1+(x+\delta x)^3}
\end{equation}
\end{example}

\section{Third equation}
\begin{example}
The following function...
\begin{equation}
    f_1(x)=\frac{x+1}{x-1}
\end{equation}
..is a function
\begin{equation}
    f_2(x)=\frac{x^2}{1+x^3}
\end{equation}
\begin{equation}
    f_3(x)=\frac{3+ x^2}{1-x^3}
\end{equation}
\end{example}
\end{document}
```

[Open this `\counterwithin` example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+using+counterwithin\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Ccounterwithin%7Bequation%7D%7Bsection%7D%0A%5Cnewcounter%7Bexample%7D%0A%5Ccounterwithin%2A%7Bexample%7D%7Bsection%7D%0A%5Cnewenvironment%7Bexample%7D%5B1%5D%5B%5D%7B%25%0A%5Cstepcounter%7Bexample%7D%25%0A%5Cpar%5Cvspace%7B5pt%7D%5Cnoindent%0A%5Cfbox%7B%5Ctextbf%7BExample%7E%5Cthesection.%5Ctheexample%7D%7D%25%0A%5Chrulefill%5Cpar%5Cvspace%7B10pt%7D%5Cnoindent%5Crmfamily%7D%25%0A%7B%5Cpar%5Cnoindent%5Chrulefill%5Cvrule+width10pt+height2pt+depth2pt%5Cpar%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BFirst+equation%7D%0ASome+introductory+text...%0A%5Cbegin%7Bexample%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%7D%7B1%2Bx%5E2%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%0A%5Csubsection%7BMore+detail%7D%0A%5Cbegin%7Bexample%7D%0AHere+we+discuss%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%2B1%7D%7Bx-1%7D%0A%5Cend%7Bequation%7D%0A...+but+don%27t+say+anything%0A%5Cend%7Bexample%7D%0A%0A%5Csubsubsection%7BEven+more+detail%7D%0A%5Cbegin%7Bexample%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%5E2%7D%7B1%2Bx%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%2B%5Cdelta+x%29%3D%5Cfrac%7B%28x%2B%5Cdelta+x%29%5E2%7D%7B1%2B%28x%2B%5Cdelta+x%29%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%0A%5Csection%7BThird+equation%7D%0A%5Cbegin%7Bexample%7D%0AThe+following+function...%0A%5Cbegin%7Bequation%7D%0A++++f_1%28x%29%3D%5Cfrac%7Bx%2B1%7D%7Bx-1%7D%0A%5Cend%7Bequation%7D%0A..is+a+function%0A%5Cbegin%7Bequation%7D%0A++++f_2%28x%29%3D%5Cfrac%7Bx%5E2%7D%7B1%2Bx%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cbegin%7Bequation%7D%0A++++f_3%28x%29%3D%5Cfrac%7B3%2B+x%5E2%7D%7B1-x%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Using the \counterwithin command](/files/jeig4wGTSAmPHv93YVn3)

**Code summary**

* `\newcounter{example}`: This creates a new counter for use in our environment, also called `example`
* `\counterwithin*{example}{section}`: This links our `example` counter with the `section` counter used within the `\section` command: each time we issue a `\section` command the `example` counter is reset to zero. Note that we used the starred version `\counterwithin*` to avoid redefinition of `\theexample`
* Next, we create a new environment also called `example`. Within that environment we use `\stepcounter{example}` to increment the environment’s counter. Note too we write `\textbf{Example~\thesection.\theexample}` to output **Example** `<section number>.<example number>`

```latex
\newenvironment{example}[1][]{%
\stepcounter{example}%
\par\vspace{5pt}\noindent
\fbox{\textbf{Example~\thesection.\theexample}}%
\hrulefill\par\vspace{10pt}\noindent\rmfamily}%
{\par\noindent\hrulefill\vrule width10pt height2pt depth2pt}
```

**\counterwithout{somecounter}{anothercounter}**

`\counterwithout{*somecounter*}{*anothercounter*}` removes the link between `*somecounter*` and `*anothercounter*` so that they are independent. For any pair of counters, you can switch between using `\counterwithout` and `\counterwithin`, as the following example shows for the `example` and `section` counters—you can open this example in Overleaf using the link provided below the code.

```latex
\section{First equation}
\begin{example}
\begin{equation}
    f(x)=\frac{x}{1+x^2}
\end{equation}
\end{example}

\subsection{Second equation}
\begin{example}
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
\end{example}
\vspace{6pt}\noindent Note how the \texttt{example} counter is reset at the start Section \ref{sec:n0}.
\section{Third equation}
\label{sec:n0}
\begin{example}
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
\end{example}
\vspace{6pt}\noindent Here, we wrote \verb|\counterwithout{example}{section}| so that \texttt{example} is no longer reset at the start of a section. In Sections \ref{sec:n1} and \ref{sec:n2} the \texttt{example} counter keeps increasing. \counterwithout{example}{section}
\section{Fourth equation}
\label{sec:n1}
\begin{example}
\begin{equation}
    f(x)=\frac{x^2+x^3}{1+x^3}
\end{equation}
\end{example}
\section{Fifth equation}
\label{sec:n2}
\begin{example}
\begin{equation}
    f(x,k)=\frac{x^2-x^k}{1+x^3}
\end{equation}
\end{example}
```

[Open this `\counterwithout` example in Overleaf](<https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+using+counterwithout\&snip=\documentclass{article}&#xA;\counterwithin{equation}{section}&#xA;\newcounter{example}&#xA;\counterwithin*{example}{section}&#xA;\newenvironment{example}\[1]\[]{%&#xA;\stepcounter{example}%&#xA;\par\vspace{3pt}\noindent&#xA;\fbox{\textbf{Example~\thesection.\theexample}}%&#xA;\hrulefill\par\vspace{3pt}\noindent\rmfamily}%&#xA;{\par\noindent\hrulefill\vrule+width10pt+height2pt+depth2pt\par}&#xA;\begin{document}&#xA;\section{First+equation}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x}{1+x^2}&#xA;\end{equation}&#xA;\end{example}&#xA;&#xA;\subsection{Second+equation}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x+1}{x-1}&#xA;\end{equation}&#xA;\end{example}&#xA;\vspace{6pt}\noindent+Note+how+the+\texttt{example}+counter+is+reset+at+the+start+Section+\ref{sec:n0}.+&#xA;\section{Third+equation}&#xA;\label{sec:n0}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x+1}{x-1}&#xA;\end{equation}&#xA;\end{example}&#xA;\vspace{6pt}\noindent+Here,+we+wrote+\verb|\counterwithout{example}{section}|+so+that+\texttt{example}+is+no+longer+reset+at+the+start+of+a+section.+In+Sections+\ref{sec:n1}+and+\ref{sec:n2}+the+\texttt{example}+counter+keeps+increasing.+\counterwithout{example}{section}&#xA;\section{Fourth+equation}&#xA;\label{sec:n1}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x^2+x^3}{1+x^3}&#xA;\end{equation}&#xA;\end{example}&#xA;\section{Fifth+equation}&#xA;\label{sec:n2}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x,k)=\frac{x^2-x^k}{1+x^3}&#xA;\end{equation}&#xA;\end{example}&#xA;\end{document}>)

This example produces the following output:

![Using the counterwithout command](/files/IcydTNbNEvtbtJbZMsMp)

## Example

### enumerate

The `enumerate` list environment uses four *counter* variables which keep track of the current label value for each level:

|           |                                  |
| --------- | -------------------------------- |
| **Level** | **`enumerate`** counter variable |
| Level 1   | `enumi`                          |
| Level 2   | `enumii`                         |
| Level 3   | `enumiii`                        |
| Level 4   | `enumiv`                         |

These counters can be changed using the `\setcounter` command. See the example below—which is not necessarily the best solution (see the [Overleaf lists](/latex/latex-basics/04-lists.md) article for more information):

```latex
This example shows one way to change the numbering of a list; here, changing the value of the \texttt{enumi} counter to start the list numbering at 4 (it is incremented by the \verb|\item| command):

\begin{enumerate}
\setcounter{enumi}{3}
\item Something.
\item Something else.
\item Another element.
\item The last item in the list.
\end{enumerate}
```

[Open this example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Modifying+an+enumerate+counter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BIntroduction%7D%0AThis+example+shows+one+way+to+change+the+numbering+of+a+list%3B+here%2C+changing+the+value+of+the+%5Ctexttt%7Benumi%7D+counter+to+start+the+list+numbering+at+4+%28it+is+incremented+by+the+%5Cverb%7C%5Citem%7C+command%29%3A%0A%0A%5Cbegin%7Benumerate%7D%0A%5Csetcounter%7Benumi%7D%7B3%7D%0A%5Citem+Something.%0A%5Citem+Something+else.%0A%5Citem+Another+element.%0A%5Citem+The+last+item+in+the+list.%0A%5Cend%7Benumerate%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Modifying an enumerate counter](/files/otr31dQZWxL9Wbbao0vX)

## Default counters in LaTeX

| Usage                           | Name                                                                                                                  |
| ------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| For document structure          | <p>- part<br>- chapter<br>- section<br>- subsection<br>- subsubsection<br>- paragraph<br>- subparagraph<br>- page</p> |
| For floats                      | <p>- equation<br>- figure<br>- table</p>                                                                              |
| For footnotes                   | <p>- footnote<br>- mpfootnote</p>                                                                                     |
| For the `enumerate` environment | <p>- enumi<br>- enumii<br>- enumiii<br>- enumiv</p>                                                                   |

## Further reading

For more information see:

* [Environments](/latex/commands/02-environments.md)
* [Commands](/latex/commands/01-commands.md)
* [Lists](/latex/latex-basics/04-lists.md)
* [Page numbering](/latex/formatting/03-page-numbering.md)
* [Sections and chapters](/latex/document-structure/01-sections-and-chapters.md)
* [Mathematical expressions](/latex/mathematics/01-mathematical-expressions.md)
* [Inserting Images](/latex/more-topics/27-inserting-images.md)
* [Tables](/latex/figures-and-tables/01-tables.md)
* [Typing exams in LaTeX](/latex/field-specific/09-typesetting-exams-in-latex.md)
* [Writing your own package](/latex/class-files/03-writing-your-own-package.md)
* [Writing your own class](/latex/class-files/04-writing-your-own-class.md)
* [The not so short introduction to LaTeX2ε](http://www.ctan.org/tex-archive/info/lshort/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/formatting/10-counters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
