1

Exercise 7.5 on page 40 of The TeXbook asks to make \ to category 12. The answer of this question is

{\catcode‘/=0 \catcode‘\\=13 /message{/string\}}

Why should assign the \ to 13 not 12?

The explaination of this answer is

(The “null control sequence” that you get when there are no tokens between \csname and \endcsname is not a solution to this exercise, because \string converts it to ‘\csname\endcsname’. There is, however, another solution: If TEX’s \escapechar parameter—which will be explained in one of the next dangerous bends—is negative or greater than 255, then ‘\string\’ works.)

  1. The “null control sequence” that you get when there are no tokens between \csname and \endcsname is not a solution to this exercise

What does this mean?

  1. If TEX’s \escapechar parameter—which will be explained in one of the next dangerous bends—is negative or greater than 255, then ‘\string\’ works.)

What does this mean?

Very difficult to understand them.

5
  • 1
    The question is about the effect of \string on active chars ...
    – Joseph Wright
    Nov 23, 2023 at 13:15
  • @JosephWright So can this way make \ to be category 12? I don't think so.
    – Y. zeng
    Nov 23, 2023 at 13:16
  • 2
    the result of \string\ is to make a character token with a catcode 12 \ it does not change the \catcode settings for new characters that are read. Just as \string~ makes a catcode12 token ~ it does not change \catcode`\~ which is still 13. Nov 23, 2023 at 13:19
  • as Joseph says he uses 13 there as the question is about active characters but you could use anything else that makes a token, so 11 or simply 12 would also work. Nov 23, 2023 at 13:22
  • Okay. How about the another two questions in the post?
    – Y. zeng
    Nov 23, 2023 at 13:23

1 Answer 1

2

The explanation is in the solution of the exercise. Knuth suggests to try

\message{\string~}
\message{\string\~}

to see that the result is, respectively ~ and \~. The fact that you get these characters in the output of \message, which does full expansion on the tokens it is fed with, means that these characters have “printable category code”, in this case precisely 12.

If you try \message{\string\} you won't go very far because \} becomes a single token and not a closing brace.

However, the example with ~ which is normally an active character, tells you that you can make the backslash active (in a group, so it will resume its standard behavior at group end) and “stringify” it:

{\catcode`/=0 \catcode`\\=\active /message{/string\}}

Of course if you want a “usable” backslash with category code 12, you need to define a macro for it:

{\catcode`/=0 \catcode`\\=\active /xdef/bschar{/string\}}

Alternatively (and you may want to investigate how this works)

\begingroup\lccode`~=`\\ \lowercase{\endgroup\edef\bschar{\string~}}

Another solution (the one hinted at by Knuth) is

{\escapechar=-1 \xdef\bschar{\string\\}}

All three ways end up in \bschar expanding to a backaslash with category code 12.

You might also do

\chardef\bschar=`\\

but this would make \bschar into an unexpandable token that prints a backslash (assuming the font has a backslash at slot 92).

Is it necessary to use category code 13? Not at all. All of the following will show \ on the console:

{\catcode`/=0 \catcode`\\=1 /message{/string\}}
{\catcode`/=0 \catcode`\\=2 /message{/string\}}
{\catcode`/=0 \catcode`\\=3 /message{/string\}}
{\catcode`/=0 \catcode`\\=4 /message{/string\}}
{\catcode`/=0 \catcode`\\=6 /message{/string\}}
{\catcode`/=0 \catcode`\\=7 /message{/string\}}
{\catcode`/=0 \catcode`\\=8 /message{/string\}}
{\catcode`/=0 \catcode`\\=11 /message{/string \}}
{\catcode`/=0 \catcode`\\=12 /message{/string\}}
{\catcode`/=0 \catcode`\\=13 /message{/string\}}

Note the (mandatory) space in the case of catcode 11. A space can be used also in the other cases, of course.

17
  • May you help me to explain the \active here? Does it equal to 13?
    – Y. zeng
    Nov 24, 2023 at 0:42
  • @Y.zeng A \chardef or \mathchardef token can be used in all situations in which the syntax requires a <number>. Plain TeX and LaTeX do \chardef\active=13.
    – egreg
    Nov 24, 2023 at 8:39
  • So, what is the meaning of \catcode`\\=\active?
    – Y. zeng
    Nov 24, 2023 at 8:40
  • @Y.zeng The same as \catcode`\\=13
    – egreg
    Nov 24, 2023 at 9:01
  • But you have assigned / to category 0, so I think you should change \catcode`\\=\active to /catcode`/\=/active, why not?
    – Y. zeng
    Nov 24, 2023 at 9:03

You must log in to answer this question.

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