LaTeX/Counters
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 . [1]
Counter manipulation
[edit | edit source]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]
|
For example, if you want to enumerate the equations in each chapter independently, you can create something like an "equationschapter" counter that will be automatically reset at the begin of each section.
\newcounter{equationschapter}[section]
\section{First Section}
I present one equation:
\stepcounter{equationschapter} $a=b+c$ (Eq. \arabic{section}.\arabic{equationschapter})
\section{Second Section}
I present more equations:
\stepcounter{equationschapter} $a=c+d$ (Eq. \arabic{section}.\arabic{equationschapter})
\stepcounter{equationschapter} $d=e$ (Eq. \arabic{section}.\arabic{equationschapter})
|
To add to an existing counter another counter causing a reset when increased, use:
\counterwithin*{NameOfTheCounter}{NameOfTheOtherCounter}
|
If this doesn't work it might be because of an old LaTeX version, the following should work in that case:
\makeatletter
\@addtoreset{NameOfTheCounter}{NameOfTheOtherCounter}
\makeatother
|
To undo this effect one can use:
\counterwithout*{NameOfTheCounter}{NameOfTheOtherCounter}
|
or:
\makeatletter
\@removefromreset{NameOfTheCounter}{NameOfTheOtherCounter}
\makeatother
|
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
[edit | edit source]There are several ways to get access to a counter.
\theNameOfTheNewCounter
will print the formatted string related to the counter (note the "the" before the actual name of the counter).\value{NameOfTheNewCounter}
will return the counter value which can be used by other counters or for calculations. It is not a formatted string, so it cannot be used in text.\arabic{NameOfTheNewCounter}
will print the formatted counter using arabic numbers.
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 \newcount\mycounter
do abide by the backslash rule.
Counter style
[edit | edit source]The following internal LaTeX commands will convert numeric value of specified counter into printable string and insert string into document:
\arabic
- Numbers from to inclusive converted to strings «-2147483648», «-2147483647», …, «-1», «0», «1», …, «2147483646», «2147483647».
- Example: 1, 2, 3, …
\alph
- Numbers from 1 to 26 inclusive converted to strings «a», «b», …, «z». Other numbers (negative numbers, zero, 27, 28, …, ) converted to empty string.
- Example: a, b, c, …
\Alph
- Same as
\alph
, but upper case letters used. - Example: A, B, C, …
\roman
- Numbers from 1 to 4999 inclusive converted to strings «i» (1), «ii» (2), …, «mmmmcmxcix» (4999), where «i» — 1, «v» — 5, «x» — 10, «l» — 50, «c» — 100, «d» — 500, «m» — 1000. Numbers from 5000 to inclusive converted to strings «mmmmm» (5000), «mmmmmi» (5001), …. Other numbers (negative numbers, zero) converted to empty string.
- Example: i, ii, iii, …
\Roman
- Same as
\roman
, but upper case letters used. - Example: I, II, III, …
\fnsymbol
- Aimed at footnotes; prints a sequence of symbols.
Number Symbol(s) 1 ∗ 2 † 3 ‡ 4 § 5 ¶ 6 ∥ 7 ∗∗ 8 †† 9 ‡‡ Other numbers Empty string
- Example: ∗, †, ‡, …
LaTeX default counters
[edit | edit source]- part
- chapter
- section
- subsection
- subsubsection
- paragraph
- subparagraph
- page
- figure
- table
- footnote
- mpfootnote
For the enumerate environment:
- enumi
- enumii
- enumiii
- enumiv
For the eqnarray environment:
- equation
Book with parts, sections, but no chapters
[edit | edit source]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
[edit | edit source]See the List Structures chapter.
Custom sectioning
[edit | edit source]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
|