10

I would like to understand why the expression \the\count0\the\count1 returns 0 when they are written without spaces. Here's the code:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\begin{document}

    \count0=12
    \count1=34

    The value of \textbackslash count0 is `\the\count0'.

    The value of \textbackslash count1 is `\the\count1'.

    To form `1234' with count0 and count1 one does: `\the\count0 \the\count1'.

    But when the expression is put together one gets: `\the\count0\the\count1'.

    One way to get the desired output is to add \textbackslash relax between them: `\the\count0\relax\the\count1'.

\end{document}

Output:

enter image description here

One way to avoid the output 0 is to put \relax between the counters. Why do I get the 0 output and what is the difference in separating the expressions and adding the \relax command?

15

When TeX expands \the\count0, it sees that the thing to be printed is the contents of a \count register, therefore it looks for a 〈number〉 after \count (the number of the \count register to be printed). This “looking for” process consists in expanding all tokens from the input stream until finding an unexpandable token that doesn't fit in the grammar for a 〈number〉 given in the TeXbook p. 269. A 〈space token〉 always ends this process and becomes part of the 〈number〉 (it is gobbled by the 〈number〉 scanning process), but no more than one—see Grammar complements below. A \relax token also ends the process but doesn't become part of the 〈number〉, so TeX will use it for further processing of the input stream.

Note: in many cases, \relax does nothing, but in some cases, it may end particular processes of TeX (looking for \noalign or \omit at the beginning or end of an \halign [resp. \valign] row [resp. column]).

So, when you write \the\count0 \the\count1, the first \the applies to \count0 because the space following 0 ends a 〈number〉. \the\count0 (with ending space) always expands to the contents of count register 0 and adds no spurious space to the input or horizontal list.

\the\count1 prints... something that depends on what follows the 1. If more digits follow (possibly after expanding macros), the number of the register to be printed can be larger than 1. With a single quote following the 1 as in your example, the 〈number〉 is terminated and the contents of count register 1 is printed. When you can't be sure about what follows (macros not under your control), terminate the 〈number〉 with a space token; it will be gobbled. \relax is a popular alternative and always finishes a 〈number〉, but as pointed out in the above note, it then remains in the input stream, which may have undesirable consequences in some particular cases (see towards the end of this answer).

When you write \the\count0\the\count1', things are different for the first \the\count, because after 0, TeX is still expanding tokens looking for the next digits of the 〈number〉 that started with 0. Since \the is always expandable, it is expanded. \the\count1 expands to 34 in your example; as a consequence, this 34 becomes part of the first 〈number〉, which is 034, i.e. decimal 34 since the 〈number〉 didn't start with a single or double quote that would indicate octal or hexadecimal notation. Thus, in your example:

\the\count0\the\count1'

(with no space inside) prints the contents of count register 34 followed by an apostrophe, which you can verify by setting for instance \count34=77 (followed by a space token or by \relax) earlier in your document. Of course, in normal operation, you shouldn't write to \count registers without first making sure that they aren't already used for something else (see “scratch registers”).

Grammar complements

After unrolling the first grammar production rules for 〈number〉 (see TeXbook p. 269; these allow for optional plus, minus signs and spaces at the beginning of a 〈number〉, as well as coercion of an 〈internal dimen〉 or 〈internal glue〉 into a 〈number〉), you'll arrive at the production rules for a 〈normal integer〉:

〈normal integer〉 → 〈internal integer〉
     | 〈integer constant〉〈one optional space〉
     | '〈octal constant〉〈one optional space〉
     | "〈hexadecimal constant〉〈one optional space〉
     | `〈character token〉〈one optional space〉

where the ', " and ` are character tokens of category code 12 (“other”), 〈one optional space〉 is defined by:

〈one optional space〉 → 〈space token〉 | 〈empty〉

and 〈integer constant〉 matches any non-empty sequence of decimal digits with category code 12. The production rule

〈normal integer〉 → 〈integer constant〉〈one optional space〉

(one of those for 〈normal integer〉) is used for all 〈number〉s of your example, and the 〈space token〉 is the one we mentioned above, that becomes part of the 〈number〉 when present (but only one).

  • 1
    Yes, but with nuances: the expansion of a macro is indeed its “return value”, but contrary to return values in non-macro computer languages, it isn't well delimited and typed by default. When a macro is expanded, it can remove tokens from the input stream and insert new tokens. The inserted tokens may include delimiters such as braces or other tokens, but this is not mandated by the language. You really have to think of TeX as eating a stream of tokens progressively from one end. Macro expansion (mouth) is the first stage after tokenization (eyes); when the topmost token is unexpandable, it – frougon Aug 1 at 6:08
  • 1
    reaches the stomach and is executed by TeX (character box appended to the horizontal list, \par, \hbox, \vbox etc. command, assignment such as \def, \edef, addition, substraction, mult and div. on counters, etc.). Macros are expanded, this is their very purpose, but other commands are also expanded, such as \the. This can be triggered by a simple \expandafter (does one expansion step), by an \edef or \xdef (recursive expansion) or simply by being the topmost token in the input stream, and follows the same principle as for macros: this acts on the end of the input stream (...) – frougon Aug 1 at 6:17
  • 1
    (...) that is in TeX's mouth, can remove tokens from the input stream and/or insert new ones that will be processed later. Simple expansion of a macro is dumb: tokens are grabbed (removed from the input stream) according to the parameter text (which may include delimited arguments, though) and a whole pile of new tokens is inserted: the replacement text of the macro. Possible smartness comes from further processing of the inserted tokens. TeX primitives such as \the can do “smart things” on the first expansion step: \the behaves differently if the following token is \count, (...) – frougon Aug 1 at 6:44
  • 1
    (...) \dimen, \font, etc. \numexpr, \dimexpr and \glueexpr (three e-TeX expandable primitives) do interesting things in only one expansion step (this must be the case for all expandable TeX primitives, otherwise one would have to define what “intermediate states” are, and one would reach the next “intermediate state” in a single step by definition). (...) – frougon Aug 1 at 7:01
  • 1
    (...) \directlua even more, it is also expandable in a single step and can even perform arbitrary user-controlled assignments, something that can't happen in Knuth's TeX! – frougon Aug 1 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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