1

On page 11 of the book The TeXbook, there is a question:

How many different control sequences of length 2 (including the escape character) are possible? How many of length 3?

The answer is:

There are 256 of length 2; most of these are undefined when TEX begins. (TEX allows any character to be an escape, but it does not distinguish between control sequences that start with different escape characters.) If we assume that there are 52 letters, there are exactly 522 possible control sequences of length 3 (one for each pair of letters, from AA to zz). But Chapter 7 explains how to use \catcode to change any character into a “letter”; therefore it’s possible to use any of 256^2 potential control sequences of length 3.

How is this 256 calculated and how is the 256^2? I can't understand the answer.

7
  • @campa How did you know TeX understands 8 bit ASCII?
    – Y. zeng
    Oct 28, 2023 at 11:59
  • 2
    it does not have to be ASCII but everything about characters in classic tex is based on 0-255 character numbers. input and fonts Oct 28, 2023 at 12:24
  • for luatex or xetex of course none of this applies and there are far far more allowable control sequences Oct 28, 2023 at 12:26
  • 3
    A friendly piece of advice: a wise man (@DavidCarlisle :-)) told me once that the TeXbook is written so that first you have to understand TeX and the read the TeXbook to understand what it was telling you. Jokes aside, if you stop at every sentence you don't understand immediately, it'll take you ages. Sometimes it's better to keep reading and going back when needed.
    – campa
    Oct 28, 2023 at 12:33
  • 3
    @Y.zeng technically ASCII is a 7 bit encoding with just 128 characters, tex is 8 bit with 256, but the textbook on many things does not spell out details in early chapters. The fact that character codes are 0+255 is explicit everywhere in tex syntax Oct 28, 2023 at 13:10

1 Answer 1

5

The fact that there are 256 2-letter sequences, and 256×256 three letter sequences is just a consequence that in classic TeX (and pdfTeX) a character code is a number betweeen 0 and 255. So there are 256 names of length 1 and 256×256 names of length 2 (each preceded by an escape character).

The 255 upper limit of character codes is explicit everywhere in tex syntax such as \char or \mathchar.

If you try \char256 in tex or pdftex You will get an error

! Bad character code (256).
l.3 \char256
            
? h
A character number must be between 0 and 255.
I changed this one to zero.

?

In xetex or luatex, 255 above needs to be replaced by the largest Unicode value which is hex 10FFFF = decimal 1114111 so there are 1114112 codes 0-1114111 and 114112×114112=1241245548544 three character sequences including the escape character.

with luatex you get

! Bad character code (1114112).
l.7 \char"110000
              
? h
A character code must be between 0 and 1114111.
I changed this one to zero.
? 

from \char"110000

6
  • Thanks. May you tell me, 1) why the maximum hex is 10FFFF? 2) Why you add quota between \char and 110000? In your first example, you use \char256, but in your second example, you use \char"110000.
    – Y. zeng
    Oct 29, 2023 at 11:05
  • @Y.zeng a double quote " is tex syntax for hexadecimal, so \char256 could be written \char"FF and \char"110000 could be written \char1114112 it's the same thing. But especially for larger numbers hex is easier, as it is easier to see the significance of hex 110000 than decimal 1114112 even though they are the same number. Unicode is an international standard and the fact that it defines an encoding from 0 to hex 10FFFF is just a fact of the definition, like asking why the English alphabet has 26 letters. It just is that way. Oct 29, 2023 at 11:11
  • I thought about this question for a while. Unicode is so limited, how should the characters be expressed if they exceed unicode? I used to think that unicode was infinite.
    – Y. zeng
    Oct 29, 2023 at 11:15
  • @Y.zeng limited?? 1114112 is a big number, and that is just base characters these can be combined with combining characters to add accents, or affect the skin colour on emoji or use complicated text combinations for Indic scripts. Only a very small fraction of that range is allocated so far. Oct 29, 2023 at 11:21
  • Okay. It turns out we have so limited characters in the world.
    – Y. zeng
    Oct 29, 2023 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .