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.
<to be read again>
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.
^
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?