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

cgnieder
  • 66,645
Jakub Wagner
  • 215
  • 1
  • 6
  • 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

1 Answers1

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.

egreg
  • 1,121,712
  • 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
  • Let me rephrase that: I don't understand how that bit of your code is in any way different from plain \def*{\wagner@starstar}. – zwol Nov 29 '15 at 23:51
  • @zwol I don't want to make * globally active. It could be \begingroup\catcode`*=\active\gdef*{\wagner@starstar}\endgroup, of course, but, as I said, I prefer not using \gdef if I can avoid it. – egreg Nov 29 '15 at 23:55
  • @egreg For others coming across this, I think it would probably be beneficial if the Don't would be a bit more visible, in order to enforce the notion that doing it is not good practice. Just browsing over it now, it sort of reads like: Yes you can jump out of an airplane without a parachute. Don't. instead of Yes you can jump out of an airplane without a parachute, however !!!!!DON'T!!!!!** ;) – BMWurm Nov 30 '15 at 11:11
  • 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