2

On page 45 of The TeXbook,

TEX has a standard way to refer to the invisible characters of ASCII: Code 0 can be typed as the sequence of three characters ^^@, code 1 can be typed ^^A, and so on up to code 31, which is ^^_ (see Appendix C).

The ^^@, ^^B and ^^C and so on all works but ^^A doesn't.

^^@\par
^^B\par
^^C\par
^^D\par
\bye

produces

enter image description here

but ^^A causes an error

! Missing $ inserted.
<inserted text> 
                $
<to be read again> 
                   ^^A
l.5 ^^A

Why can't use ^^A in TeX?

Y. zeng
  • 1,885
  • 1
    The input method does not change the catcode. So if you use the ^^ syntax to input a character, that will result in a math token, it still requires the math mode. This is the case with ^^A but neither with ^^@ not ^^B, ^^C or ^^D. – cabohah Nov 27 '23 at 08:12
  • @cabohah $^^A$ fails too. – Y. zeng Nov 27 '23 at 08:13
  • 1
    But with another error message. Try for example $x^^A{y}$. (Note: In plainTeX (not in LaTeX) ^^A has catcode 8 and therefore is a math subscript.) – cabohah Nov 27 '23 at 08:15
  • @cabohah How do you know it has catcode 8? – Y. zeng Nov 27 '23 at 08:18
  • You can find it in plain.tex line 17 or ask TeX using \showthe\catcode`\^^A. – cabohah Nov 27 '23 at 08:21
  • @cabohah how does that then match with this from plain.tex: \mathcode^^A="3223 % \downarrowor is\downarrowin plain TeX just the same as_`? – daleif Nov 27 '23 at 08:25
  • 3
    Please read, how \mathcode works. – cabohah Nov 27 '23 at 08:29

3 Answers3

10

The error message you get, indicates that ^^A results in a math token. In plain.TeX you can find:

\catcode`\_=8 \catcode`\^^A=8 % underline and downarrow are for subscripts

(Note: This part of plain.tex can also be found in Appendix B of “The TeXbook”, page 343.)

From this point ^^A is a math subscript. This corresponds with the terminal output of

\showthe\catcode`\^^A
\bye
8.
l.1 \showthe\catcode`\^^A

Note: The later

\mathcode`\^^A="3223 % \downarrow

in plain.tex is irrelevant for the catcode of ^^A. And because the catcode of ^^A is neither 11 (letter) nor 12 (other) the mathcode is also not relevant for the output. But it can become relevant, e.g., after changing the catcode of ^^A:

$x^^A{y}$

\catcode`^^A=12 $x^^A{y}$ \bye

enter image description here

For more information about \mathcode, how it works and the difference to \catcode see “The TeXbook” and “What is the differences between mathcode and catcode and how can I use mathcode?

cabohah
  • 11,455
  • 1
    If I remember correctly my TeXbook, the ascii code 1 was rendered as ↓ in some terminal DEK used at the time (and the same for the uparrow at ^^K) – Rmano Nov 27 '23 at 09:09
  • Thanks. How can I see the code in plain.tex as I don't know where it is? – Y. zeng Nov 27 '23 at 09:22
  • 1
    @Y.zeng kpsewhich plain.tex and then search for ^^A in that file. Or because you are reading The TeXbook, have a look into Appendix B. But as I have also shown, you can alternatively ask TeX for the catcode. – cabohah Nov 27 '23 at 09:37
7

You can refer to invisible ASCII characters, but the TeXbook doesn't say you can, or should, use them just for fun.

For instance, ^^@ (the null byte) has category code 9, so

Hell^^@ w^^@rld

would result in “Hell wrld”. And indeed you obtain three characters in output and not four.

To the contrary, ^^A (the 1 byte), has category code 8. Here's the table of category codes as set up in plain TeX:

enter image description here

Why the choice for ^^@? Because some operating systems used the null byte in order to fill their fixed length records. There should be no surprise in seeing category code 5 assigned to ^^M. Two cases remain, namely ^^A and ^^K.

The keyboard which Knuth used to work with could produce and when pressing CtrlA and CtrlK, respectively, and he liked these visual hints more than ^ and _. A consequence of this is that

$x^^A0$

has the same effect as $x_0$. And, of course, $^^A$ raises an error, because it would be like $_$.

You find the symbols on Knuth's keyboard in the table on page 369. This table also explains why some of these bytes are assigned a mathcode.

For instance CtrlD would produce on Knuth's screen and indeed we find

\mathcode`\^^D="225E % \land

in plain.tex.

Just for completeness, you can use ^^00 instead of ^^@. This convention is also described in chapter 8 and was added in TeX version 3.

egreg
  • 1,121,712
  • 1
    Thanks. 1. Why did you say "And indeed you obtain three characters in output and not four.", witch are 8 characters? 2. Why does hello1 ^^M hello2 \bye output hello1? 3. Does ^^ always find symbols from ASCII? – Y. zeng Nov 27 '23 at 10:10
  • @Y.zeng Look at the output: you get Theta, Lambda and Xi, which are the characters in slots 1, 2 and 3 in cmr10, but nothing for character 0 (which has a Gamma, by the way). For the second question, reread chapter 7: upon finding a character with catcode 5, TeX discards it and everything that may remain on the current line and replaces it with the \endlinechar. Question 3: what do you mean? – egreg Nov 27 '23 at 10:20
  • 1
  • What is Theta, Lambda, Xi and Gamma here? What I get is Hell wrld. 3. For example, in ^^t, t is 116th symbol of ASCII. As 116 is bigger than 64, 116-64=52. In ASCII table, 52th symbol is 4, so ^^t ouputs 4. Doesn't this state that ^^ always outputs symbols from ASCII?
  • – Y. zeng Nov 27 '23 at 10:37
  • 1
    @Y.zeng You've already read, that \char 127 after switching to font manfnt results in a symbol different from ASCII and that the same can be done using ^^? after setting up catcode of ^^? and the font. And I've already shown, that ^^A can result in output of a down arrow or a math subscript. So you should already know, that ^^ does not always output a symbol from ASCII. As already explained, ^^ is an input method. The output is more or less independent from the input, because the input can also result in, e.g., active character tokens. – cabohah Nov 27 '23 at 11:04