\the vs. \value: what is the difference

\thecounter typesets the result of the counter, be it in arabic, roman, etc, while \value{counter} provides an integer value in return. In many, but not all, instances, the difference will go unnoticed.

The rule is, if you are looking for a character, use \the...; if you are looking for an integer, use \value{...}.

Here's an example where it matters. By typesetting the page number in roman (via \pagenumbering{roman}), the option of \romannumeral\thepage is no longer an option, since it would be attempting \romannumeral i, whereas \romannumeral\value{page} works fine.

\documentclass{article}
\pagenumbering{roman}
\begin{document}
\romannumeral\value{page}

% \romannumeral\thepage will break
\end{document}

Here's another example...you cannot typeset \value

\documentclass{article}
\begin{document}

\newcounter{Q}
\setcounter{Q}{2}

%\value{Q} will break

\theQ
\end{document}

The difference can also affect comparisons, since \if compares tokens, not integers:

\documentclass{article}
\begin{document}

\newcounter{Q}
\setcounter{Q}{2}

\if\value{Q}2 T\else F\fi is false

\if\theQ2 T\else F\fi is true

\end{document}

The above example would show a match, were the \ifs changed to \ifnums, since \ifnum will interpret/convert the character 2 into an integer. However, even that can get you into trouble for more complex cases.

Here, I can combine \theQ with 3 to represent 23. The same does not apply for \value{Q}3.

\documentclass{article}
\begin{document}

\newcounter{Q}
\setcounter{Q}{2}

%\ifnum\value{Q}3=23 T\else F\fi will break

\ifnum\theQ3=23 T\else F\fi is true

\end{document}

Now this last one is quite unusual. That of using \value to try to provide the numerical part of a length specification. It works as expected if you merely specify \value{Q} pt. However, if you try 2\value{Q} pt, it takes the 2 as a multiplier and \value{Q} as a length specified in sp machine units! The trailing pt becomes an extraneous residual, not even part of the length.

This behavior occurs because, deep down in TeX, lengths are actually stored as integer counts in machine units. What value TeX uses as its minimal unit

\documentclass{article}
\def\q{\rule{2pt}{10pt}}
\begin{document}

\newcounter{Q}
\setcounter{Q}{2}

\q\hspace{\theQ pt}\q{} skips 2pt

\q\hspace{\value{Q} pt}\q{} skips 2pt

\q\hspace{1\theQ pt}\q{} skips 12pt

\setcounter{Q}{100000}

\q\hspace{2\value{Q} pt}\q{} skips 200000sp before reaching the ``pt''

\q\hspace{200000 sp}\q{} skips 200000sp for comparison
\end{document}

enter image description here


This is an interesting mathematical question. The number two hundred seventy-five can be represented in different ways:

  • 275 in decimal notation with “Western digits”,
  • ٢٧٥ in decimal notation with “Arabic digits”,
  • CCLXXV in Roman numerals,
  • 113 in hexadecimal digits,
  • 100010011 in binary digits,

and many others (it's duecentosettantacinque in Italian).

When you define a new counter in LaTeX, with \newcounter{foo}, also the command \thefoo is defined, which will expand to a representation of the value stored in the counter; the command is initialized to provide the decimal representation of the counter, but one can say

\renewcommand{\thefoo}{\roman{foo}}

and thereupon, calling \thefoo when the counter stores the number 275 will produce

cclxxv

(after macro expansion). The initial definition is essentially equivalent to

\newcommand{\thefoo}{\arabic{foo}}

A common case is the counter page: in the class book, the counter's representation uses \roman in the front matter and \arabic in the main matter and back matter.

However, the number stored in foo is indepedent of its representation: in some sense, the counter stores the abstract number, something not especially tied to a representation thereof. This abstract value is accessed to by \value{foo}.

Let's say you have an application where a counter is bound to contain only the values from 0 to 3 and you want to represent these values with non numerical objects; you can define, for instance,

\renewcommand{\thefoo}{%
  \ifcase\value{foo}%
     apple% case 0
  \or
     orange% case 1
  \or
     apricot% case 2
  \or
     strawberry% case 3
  \else
     bummer% wrong setting found
  \fi
}

The conditional \ifcase branches according to the number following it, in this case the abstract value stored in the counter.

You cannot say \value{foo} in the document in order to print the value, because the abstract number can only be represented in some way and you have to specify which one. Instead, when TeX is looking for a number (such as after \ifcase), then \value{foo} is the right code to use.

The objection “but we could use \arabic{foo} instead of \value{foo}” is dismissed. You really want the abstract number there, not some representation thereof.