At the LaTeX level, a counter is created using
\newcounter{mycounter}
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!
\fi
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
\newcount\mycount
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!
\fi
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
\newcount\c@mycounter
\newcounter{mycounter}
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
\def\addtocounter#1#2{%
\@ifundefined{c@#1}%
{\@nocounterr{#1}}%
{\global\advance\csname c@#1\endcsname #2\relax}}
This checks the counter exists, and if it does globally advances it.
TeX has a fixed number (256 in classic TeX 32768 in etex and xetex and 65536 in luatex)
of registers which store integer values.
\newcount\c
allocates the name \c
to one of these registers, and classic TeX primitives like \advance
operate on them, note that addition here involves assignment back to the register so it is not an expandable operation.
LaTeX's \newcounter
can be viewed as a syntactic wrapper around \newcount
.
\newcounter{abc}
allocates the name \c@abc
to a primitive TeX register allocated with \newcount
. However as is often the case the extra layer of abstraction gives some useful features, notably that subsidiary macros are defined such as \theabc
which defines the print form, and the counter is placed on reset lists so that for example incrementing section
automatically sets subsection
to zero. A similar list is used to preserve counters for the \include
mechanism.
TeX primitive operations may be local or global
\c=2 { \advance\c 1 } \the\c
will produce 2 as the increment will be lost at the }
\c=2 { \global\advance\c 1 } \the\c
will produce 3 as the global assignment will be seen at all grouping levels.
LaTeX counter assignments are always global which reflects their top level use
as for example a figure
counter, the caption is typically inside a box which forms a group but you want the counter to have global document scope.
This means you often see primitive register use for local "scratch" arithmetic
and latex counters for top level structural document counters.
\numexpr
is an e-tex extension which gives (more or less) the same arithmetic operations as the \advance
, \multiply
etc primitives, but is classified as an expandable operation and does not automatically assign the value back to a register.
Because it is expandable it works in an \edef
as shown in your (corrected)
\edef\c{\the\numexpr 2 + 5 \relax}
\show\c
example.
Best Answer
As I indicated in my comment, you can dispense with the counters. Just use the
\def
as an argument. If you need a more complex calculation, you can use\numexpr
, as in my 2nd example.To follow up with the OP's comment about difficulty of mixing tokens and counters, I will post this MWE that works with that mix: