3

For fun, I want to change the backslash's catcode from 0 to 12. However, I can't seem to make it work. Here's my current MWE, which gives an error "improper alphabetic constant":

\catcode`\textbackslash=12

I know that \textbackslash is not the right one, however, I can't seem to find a command for a program backslash. Could someone please help? By the way, I have the forward slash's catcode set to 0 just in case the problem is that I need to change the character for commands.

1 Answer 1

13

\textbackslash is just for typesetting. As the name says, it's a text command, so it just prints the character \ from the current font, and isn't of any use for anything other than that.

You are looking for:

\catcode`\\=12

The syntax for the \catcode primitive is \catcode<number>=<catcode>, and it sets the catcode of the character whose ASCII code is <number> to <catcode>. This means that you could also use:

\catcode 92=12

However it's a mouthful to remember all the ASCII codes, and your code becomes quite a lot less readable. To improve on that TeX allows you to specify a number using an “alphabetic constant”. To do that, the <number> should start with a `, followed by the character token you want to make a number of. Valid alphabetic constants are `a, `*, etc.

The only problem is that a catcode-0 character (here the backslash) doesn't produce a token, so neither:

\catcode`\=12
\catcode`\ =12

do what you want (the first one changes the catcode of =, and the second the catcode of ). To specify these characters, TeX allows you to escape the character with a backslash, so to specify the alphabeitc constant `\ you prefix it with another backslash: `\\, so:

\catcode`\\=12
5
  • So, the ` allows you to change `\` from newline to a backslash? I didn't know that! +1 and accept
    – Someone
    May 11, 2020 at 15:42
  • Also, I knew that \textbackslash was used for typesetting. However, it was the best I could come up with.
    – Someone
    May 11, 2020 at 15:43
  • 1
    @Someone Yeah, that's a primitive TeX syntax. Any time TeX is looking for a number, if such number starts with a `, the next token can be either a character, or a character preceded by a backslash. In the case of \\ you need the preceding backslash because a single backslash doesn't make a token. Note that this works: \catcode`|=0 \catcode`\\=12 |catcode`\=3 |show\ because after the second \catcode, the backslash is just a character, so you don't have to prefix a backslash anymore. May 11, 2020 at 16:41
  • Does it mean that I could type `\\ and have it be a backslash? Or would it be a "open single quote and newline"?
    – Someone
    May 12, 2020 at 18:17
  • 1
    @Someone The latter. The special `\\ syntax only expands to 92 (the ASCII code of \) when TeX is looking for a number. Everywhere else it will mean open quote and \\. Compare the output of (\number`\\)(`\\). May 12, 2020 at 18:30

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.