[Tex/LaTex] Using ** instead of ^

superscripts

I am used to use ** instead of ^.

Is it possible to somehow define macro that would interpret ** as ^?

I know that I can write ** and in the end replace it with ^ but that macro would be much more comfortable.

I have been looking for this few months ago and I discovered that you can redefine sign for superscript, but I only found that I can redefine it with only one sign but not with two.

Thank you

Best Answer

You can do it. Don't.1

\documentclass{article}

\makeatletter
\begingroup\lccode`~=`* \lowercase{\endgroup\def~}{\wagner@starstar}
\newcommand{\wagner@starstar}{%
  \@ifnextchar*{\wagner@superscript}{\wagner@asterisk}%
}
\newcommand{\wagner@superscript}[1]{^}
\mathchardef\wagner@asterisk=\mathcode`*
\AtBeginDocument{\mathcode`*="8000 }
\makeatother

\begin{document}

$A*B$

$a**2$

$\begin{array}{*{2}{c}}
a & b\\
c & d
\end{array}$

\end{document}

enter image description here


1 It's true that several languages use ** for denoting exponentiation (but several others use ^). They might also have rules about the interpretation of 2**3**4, which TeX doesn't have and they probably accept 2 ** 12 and this will definitely give the wrong result with TeX. Not because of the spaces, which are basically ignored in math mode, but because 2^{12} is the correct input. You could also be prone to type things like 2**(3+5) that would lead to disaster.

When in Rome, do as the Romans do.

Related Question