9

Is there any use for the unescaped ^ in (La)TeX, when not in math mode? If not, I couldn't see why it's necessary to escape it.

  • Why do you think you shouldn't escape it? – user31729 Jul 3 '15 at 9:15
  • 1
    Related: The ^^ notation in various engines – Heiko Oberdiek Jul 3 '15 at 9:59
  • Christian, I would thing writing \^{o} instead of just ^{o} would be odd if the naked ^ served no purpose. By contrast, I could always see why I needed to escape $. – ezequiel-garzon Jul 3 '15 at 10:06
14

Yes, there is a very basic one: when input lines are being read, if TeX finds two consecutive ^ characters, it interprets the input in a very peculiar way:

  1. if the ^^ combination is followed by two hexadecimal lowercase digits (0123456789abcdef), then TeX transforms the four bytes into the byte corresponding to the hexadecimal number;

  2. otherwise TeX computes the character code of the following character and transforms the three bytes into the character having ASCII code differing by 64 from the computed one, adding or subtracting 64 so that the result is in the range 0–127.

Thus ^^20 is completely equivalent to typing a space, while ^^M is the same as typing the usual end line character.

XeLaTeX and LuaTeX add to this mechanism. Since Unicode characters can be specified with 21 bits, up to six ^ characters are allowed followed by the same number of hexadecimal lowercase digits.

So, for example,

^^^^0021

represents five tokens in standard TeX ^^^ (category code 30, see the rule above, second item), followed by ^ 0 0 2 1; in LuaTeX or XeTeX it represents just one character having character code "21 (hexadecimal, 33 decimal).

TeXnical note. The convention actually is about two (or more for Unicode engines) identical characters having category code 7. Usually only ^ is given category code 7.

  • Thank you for such a thorough answer. I tried to look for an answer before posting this! – ezequiel-garzon Jul 3 '15 at 9:52
  • I'm still thinking... why require two ^ for this purpose, instead of just one? – ezequiel-garzon Jul 3 '15 at 10:00
  • 5
    @ezequiel-garzon because one ^ is almost always a mis-placed math expression but conversely two superscripts would be an error in math mode so the syntax is safe to use for another use. – David Carlisle Jul 3 '15 at 10:09
10

In addition to egreg's answer the other reason is same reason \sin gives an error, or \hat etc.

By far the most common reason for encountering a ^ in text mode is that the author has gone

blah blah and 1 + x^2 = 2, blah ...

but forgotten to mark the math mode region.

Making math mode commands an error in text mode is an explicit design decision that permeates the TeX language. A ^ is almost never needed in normal text, plain TeX and latex 2.09 didn't even bother to define a command to access it, latex2e added \textasciicircumflex for consistency but as you can tell by the length it was assumed to be just for setting up encoding files, not something anyone would ever really want in a document.

  • Thanks for your explanation! It's true that ^ by itself rarely appears in writing, but I found it puzzling that you needed to type, say, \^{o} instead of just ^{o}. – ezequiel-garzon Jul 3 '15 at 9:56
  • 1
    @ezequiel-garzon oh so you would expect ^ to be a command taking an argument rather that just typesetting itself? that would be very unlike TeX:-) There are setups for latex (and I think by default in context) that make ^ a normal character out of math mode (similar to ' which only has special behaviour in math) but always in text mode it typesets itself like ^ not acting as an accent command. – David Carlisle Jul 3 '15 at 10:08
  • @ezequiel-garzon: Why do you find \^ odd? It is a command and commands normally start with a backslash. See also \! \d \; \" ... – Ulrike Fischer Jul 3 '15 at 10:18
  • Ulrike, maybe I should have stuck to ^ as a character for clarity. Before learning what I learned here today, I found it odd that the character ^, though unusual (less unusual than @ before the Internet went mainstream), could not be used in text, assuming that ^ by itself had no role. It is clear why need to escape $ or ~ (using the latter as a command), for instance. – ezequiel-garzon Jul 3 '15 at 11:03
  • 1
    @ezequiel-garzon The @-sign was already used before the internet, on receipts (thingy @ $ 1.99) – Marcel Korpel Jul 3 '15 at 16:49

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.