13

This question already has an answer here:

In my readings I've come across that the TeX preprocessor replaces ^^+ with k. The explanation given is the following:

For instance, the sequence ^^+ is replaced by k because the ASCII codes of k and + differ by 64. Since this replacement takes place before tokens are formed, writing \vs^^+ip 5cm has the same effect as \vskip 5cm.

I'm not a mathematician but I've looked at the ASCII table, and while I can see the ASCII codes of k with ASCII code of 107 and + with ASCII code of 43 differ by 64.

Yet I don't see what the mathematical relationship that he is describing is. What is going on here?

marked as duplicate by ShreevatsaR, Stefan Pinnow, CarLaTeX, Andrew Swann, Troy Jan 8 '18 at 8:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    There is no math behind it (beside substracting 64). It is simply a special input method. It is not really useful to input k in this way, but ^^M e.g. is ASCII 13=CR and ^^J is LF etc and it is quite useful to have a way to add them explictly in the code e.g. in error messages. – Ulrike Fischer Jan 7 '18 at 14:49
  • 1
    although the question is about another character value (^^^M), some of the explanation might be useful: tex.stackexchange.com/q/388563/579 (this is one of the questions listed under "related:; it is hard to search for this particilar type of "code" question.) – barbara beeton Jan 7 '18 at 14:56
  • 1
    this covers it quite extensively: tex.stackexchange.com/q/62725/7674 – Robert Jan 7 '18 at 19:10
13

You want to know what happens with ^^<char>, where <char> stands for a printable ASCII character (that is, between 32 and 126). The rule is simple: if <code> is the ASCII code for <char>, then there are two cases:

  1. if <code> > 63, then ^^<char> represents the ASCII character with code <code> - 64

  2. if <code> ≤ 63, then ^^<char> represents the ASCII character with code <code> + 64

However, if <char> is among 0123456789abcdef and also the following character is in the same range, then ^^<char><char> represents the character with ASCII code resulting from interpreting the two characters as hexadecimal digits.

Quoting the TeXbook (page 45):

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). If the character following ^^ has an internal code between 64 and 127, TeX subtracts 64 from the code; if the code is between 0 and 63, TeX adds 64. Hence code 127 can be typed ^^?, and the dangerous bend sign can be obtained by saying {\manual^^?}. However, you must change the category code of character 127 before using it, since this character ordinarily has category 15 (invalid); say, e.g., \catcode`\^^?=12.

[...]

There's also a special convention in which ^^ is followed by two “lowercase hexadecimal digits,” 09 or af. With this convention, all 256 characters are obtainable in a uniform way, from ^^00 to ^^ff. Character 127 is ^^7f.

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