consider the following MWE:





this works fine, the output is xX2 as expected.

When I now enable the mathtools package, I obtain the following error:

Package: graphics 2016/07/10 v1.0t Standard LaTeX Graphics (DPC,SPQR)

! Missing number, treated as zero.
<to be read again> 
l.33 \catcode`\^^
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

(At the beginning of the graphics package, which is used by mathtools, there is some catcode-voodoo which seems to conflict with my \catcode...)

possible solution 1

When I now move the two lines \catcode... and \let... after the \begin{document}, everything is fine again. I need them, however, to stay in the preamble, so this does not solve the problem for me.

possible solution 2

I tried wrapping the \catcode... and \let... inside an \AtBeginDocument, but then I get:

! Missing control sequence inserted.
<inserted text> 
l.11 \begin{document}

Please don't say `\def cs{...}', say `\def\cs{...}'.
I've inserted an inaccessible control sequence so that your
definition will be completed without mixing me up too badly.
You can recover graciously from this error, if you're
careful; see exercise 27.2 in The TeXbook.

! Undefined control sequence.
l.12    $x^


I had the, maybe stupid, idea to simplify the work with upright and italic sub- and superscripts. Until now, I had


for stuff like k\sub{b} and f_i. In combination with my question here I managed get equivalent output with k__b and f_i: when I double the underscore, I get upright subscripts like with my \sub command, and it's super fast to type/change. It also works with superscripts (^ and ^^), but only if I change the catcode/let the caret after the preamble, so I cannot bundle my code neatly inside a package.

  • Changing the category code of ^ in the preamble? Never do it. You can do it in \AtBeginDocument, but it requires \lowercase trickery. It would be better if you explain your aim in greater detail; for instance, do you need the active ^ only in math mode?
    – egreg
    Sep 13, 2016 at 12:24
  • @egreg: I added more information my question, please see above. Sep 13, 2016 at 12:36

1 Answer 1


What's the problem in changing the category code of ^ in the preamble? That some packages load files at begin document and these files might contain constructions such as


that are used to make byte 1 into a comment character or some `\^^M trickery.

You can set the catcode in the preamble, but delaying it with \AtBeginDocument (be sure to do it after loading all packages):


The \lowercase trick is explained elsewhere on the site, see The \lowercase trick. On the other hand, if the definition of ^ as an active character is only needed in math mode, it's better to make it only math active, see Underscore in textmode vs mathmode

\AtBeginDocument{\mathcode`\^="8000 }
  • wow, thank you! Concerning my use case with the ^^, it does not work with your math active solution. E.g. x^^a would lead to x!. Apparently the math active catcode does not alter the special meaning of ^^ as escape sequence, as would a simple \active? Sep 13, 2016 at 12:47
  • @ralfix Exactly so.
    – egreg
    Sep 13, 2016 at 12:55

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.