8

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

  • So what about your usage of ^ when you deal with limits? Do you use ** as well? As in, \sum_{i=0}**n? – Werner Nov 29 '15 at 18:57
  • 3
    Note that * is something that is used in command definitions, like \newcommand* or \renewcommand*, or even in array column specifications: *{<num>}{<col spec>}... – Werner Nov 29 '15 at 19:00
  • 1
    Depending on the editor you're using, shouldn't you be able to define a macro there that changes your ** input into ^ as you type, therefore eliminating the need to redefine any commands? – BMWurm Nov 30 '15 at 11:17
26

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.

| improve this answer | |
  • 2
    Making * math active doesn't interfere with other usages of the character as shown with the array environment. – egreg Nov 29 '15 at 19:12
  • Out of curiosity, why is the \lowercase trick necessary here? – zwol Nov 29 '15 at 22:26
  • 1
    @zwol I avoid \gdef whenever possible. – egreg Nov 29 '15 at 22:32
  • 1
    @BMWurm I've added some reasons. – egreg Nov 30 '15 at 12:49
  • 2
    I like both the tone and content of egreg 's answer. – JPi Nov 30 '15 at 13:05

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.