# redefine caret ^ while using mathtools package

consider the following MWE:

\documentclass{article}
%\usepackage{mathtools}

\newcommand\newCaret{X}

\catcode^=\active
\let^=\newCaret

\begin{document}
$x^2$
\end{document}


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.
X
l.33 \catcode\^^
A=\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:

\Gin@req@height=\dimen123
\Gin@req@width=\dimen124
)
! Missing control sequence inserted.
<inserted text>
\inaccessible
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^ 2$


background

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

\newcommand\sub[1]{_\text{#1}}
\let\supremum\sup
\renewcommand\sup[1]{^\text{#1}}


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? Sep 13, 2016 at 12:24

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

\catcode\^^A=\catcode\%


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):

\newcommand\NewCaret{X}
\AtBeginDocument{%
\begingroup\lccode\~=\^
\lowercase{\endgroup\let~}\NewCaret
\catcode\^=\active
}


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

\newcommand\NewCaret{X}
\begingroup\lccode\~=\^
\lowercase{\endgroup\let~}\NewCaret
\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. Sep 13, 2016 at 12:55