LaTeX/Counters

< LaTeX

Counters are an essential part of LaTeX: they allow you to control the numbering mechanism of everything (sections, lists, captions, etc.). To that end each counter stores an integer value in the range of long integer, i.e., from to .

Counter manipulation

In LaTeX it is fairly easy to create new counters and even counters that reset automatically when another counter is increased (think subsection in a section for example). With the command

\newcounter{NameOfTheNewCounter}

you create a new counter that is automatically set to zero. If you want the counter to be reset to zero every time another counter is increased, use:

\newcounter{NameOfTheNewCounter}[NameOfTheOtherCounter]

To increase the counter, either use

\stepcounter{NameOfTheNewCounter}

or

\refstepcounter{NameOfTheNewCounter} % used for labels and cross referencing

or

\addtocounter{NameOfTheNewCounter}{number}

here the number can also be negative. For automatic resetting you need to use \stepcounter.

To set the counter value explicitly, use

\setcounter{NameOfTheNewCounter}{number}

Counter access

There are several ways to get access to a counter.

Note that \arabic{NameOfTheNewCounter} may be used as a value too, but not the others.

Strangely enough, LaTeX counters are not introduced by a backslash in any case, even with the \the command. plainTeX equivalents \count and \newcounter\mycounter do abide by the backslash rule.

Counter style

Each counter also has a default format that dictates how it is displayed whenever LaTeX needs to print it. Such formats are specified using internal LaTeX commands:

Command Example
\arabic 1, 2, 3 ...
\alph a, b, c ...
\Alph A, B, C ...
\roman i, ii, iii ...
\Roman I, II, III ...
\fnsymbol Aimed at footnotes; prints a sequence of symbols.

LaTeX default counters

For the enumerate environment:

For the eqnarray environment:

Book with parts, sections, but no chapters

Here follows an example where we want to use parts and sections, but no chapters in the book class :

\renewcommand{\thesection}{\thepart .\arabic{section}}

\part{My Part}                                                                
\section{My Section}
\subsection{My Subsection}

Custom enumerate

See the List Structures chapter.

Custom sectioning

Here is an example for recreating something similar to a section and subsection counter that already exist in LaTeX:

\newcounter{mysection}
\newcounter{mysubsection}[mysection]
\addtocounter{mysection}{2} % set them to some other numbers than 0
\addtocounter{mysubsection}{10} % same
%
\arabic{mysection}.\arabic{mysubsection}
Blah blah

\stepcounter{mysection}
\arabic{mysection}.\arabic{mysubsection}
Blah blah

\stepcounter{mysubsection}
\arabic{mysection}.\arabic{mysubsection}
Blah blah

\addtocounter{mysubsection}{25}
\arabic{mysection}.\arabic{mysubsection}
Blah blah and more blah blah
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.