Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The title already states the whole question. It has been answered by Joseph Wright at http://www.texdev.net/2009/11/17/tex-counts-and-latex-counters/, but I have not seen the according answer here at tex.stackexchange.com yet.

share|improve this question
In short: LaTeX counters are TeX count's with some additional wrapper layer for easier handling. –  Martin Scharrer Nov 16 '11 at 19:06
The @user syntax only works for users which already posted a comment to the same post. Joseph won't get notified about your comment above because of this. Spaces are also not allowed, so you should use @JosephWright. –  Martin Scharrer Nov 16 '11 at 19:07
I've been a bit bust, but should be able to post an answer here today (if no-one else does first). –  Joseph Wright Nov 17 '11 at 10:20
@MartinScharrer: Thanks & +1 for the info! –  Stephen Nov 17 '11 at 18:28

1 Answer 1

up vote 21 down vote accepted

At the LaTeX level, a counter is created using


This creates a counter initialised at zero which can then be set using

\setcounter{mycounter}{4} % Or whatever

or manipulated using \stepcounter and \addtocounter

\setcounter{mycounter}{0}    % Value is 0
\stepcounter{mycounter}      % Value is 1
\stepcounter{mycounter}      % Value is 2
\addtocounter{mycounter}{3}  % Value is 5

There are then some methods to get the counter value back out. LaTeX creates a \the... function for each counter, which will print the current value. In places where TeX expects a number, there is also the \value function:

\themycounter % Prints the current value
\ifnum\value{mycounter} > \value{myothercounter}%
  % Do stuff!

LaTeX's counters are set globally. That makes them good for tracking something that covers the entire document, but not as good for localised calculations.

A TeX count is created using


where the name is a name including a backslash. Setting a count is done very simply: there is no set function

\mycount 4\relax

Notice the \relax here. Without it, TeX will continue to look for the number in the next thing it finds. This can have some odd effects, and is best avoided. Altering the value can then be carried out using \advance

\mycount 0\relax         % Value is 0
\advance\mycount 1\relax % Value is 1
\advance\mycount 1\relax % value is 2
\advance\mycount 3\relax % Value is 5

A similar termination is brought about by having a space after the number

\mycount 4 % Comment used to show that there is a deliberate space

The value of a count register can be recovered using \the or \number, and the name itself can be used where TeX expects a number.

\the\mycount   % Prints the current value
\number\mycount % The same result
\ifnum\mycount > \myothercount
  % Do stuff!

The big difference is that TeX sets count registers locally. So to do a global assignment you have to do it deliberately

\global\mycount 3\relax

As LaTeX is built on TeX, you might guess that LaTeX's counters are an interface to TeX’s count registers, but it's not immediately obvious how this is done. The way it works is that LaTeX prefixes all of the counter names with c@, so that if I did


LaTeX would issue an error message: the counter is already defined. The other LaTeX functions then build on this, so that they manipulate the internal counters. This is all done globally and with some error checking. For example, the definition of \addtocounter is

    {\global\advance\csname c@#1\endcsname #2\relax}}

This checks the counter exists, and if it does globally advances it.

share|improve this answer
More-or-less the text of my blog entry reformatted. –  Joseph Wright Nov 17 '11 at 13:34
Thank you! Two things you might want to mention (if I understood that right): \count255 is often used as temporary count. and all \counts (even being local) take up one count register (format broken in comment): \documentclass{article} \begin{document} \begingroup \newcount\testa \testa 7\relax \the\testa\ is seven. \endgroup \begingroup \newcount\testb \testb 42\relax \the\testb\ is forty-two. \endgroup \the\testa\ and \the\testb\ are both 0, but they take up one \verb|\count| each (see \begin{verbatim} \testa=\count87 \testb=\count88 \end{verbatim} in the logfile). \end{document} –  Stephen Nov 17 '11 at 18:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.