LaTeX/Counters

LaTeX logo.svg

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Importing Graphics
  14. Floats, Figures and Captions
  15. Footnotes and Margin Notes
  16. Hyperlinks
  17. Labels and Cross-referencing

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Texts

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Letters
  2. Presentations
  3. Teacher's Corner
  4. Curriculum Vitae

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

Counters are an essential part of LaTeX: they allow you to control the numbering mechanism of everything (sections, lists, captions, etc.).

Counter manipulationEdit

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 accessEdit

There are several ways to get access to a counter.

  • \theNameOfTheNewCounter will print the formatted string related to 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 \newcounter\mycounter do abide by the backslash rule.

Counter styleEdit

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 countersEdit

  • part
  • chapter
  • section
  • subsection
  • subsubsection
  • paragraph
  • subparagraph
  • page
  • equation
  • figure
  • table
  • footnote
  • mpfootnote

For the enumerate environment:

  • enumi
  • enumii
  • enumiii
  • enumiv

Book with parts, sections, but no chaptersEdit

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 enumerateEdit

See the List Structures chapter.

Custom sectioningEdit

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


Previous: Lengths Index Next: Boxes
Last modified on 30 September 2013, at 13:34