4

Suppose we have

\newcommand{\inv}{-1}

We want to be able to do $A^\inv$ instead of $A^{\inv}$ but that won't work — only the minus sign is superscripted.

Accidentally I stumbled on a possible solution which is to define the macro as

\newcommand{\inv}{\kern0pt\relax -1}

but since I'm clueless as to why this works or whether this is actually a good solution I decided to ask anyway.

\documentclass{article}
\newcommand{\inv}{\kern0pt\relax -1}
\begin{document}
$A^\inv$ $A^{-1}$.
\end{document}
Ernest A
  • 2,773

2 Answers2

5

Adding a pair of braces inside the definition of \inv should work.

 \newcommand{\inv}{{-1}}

Now, when you do A^\inv, it will expand to A^{-1} instead of A^-1.

2

I think that, for this cases, it's better to give the command “active” control over the output. So I would use \inv{A}, read as inverse of A. That way you have full control over the output.

\newcommand*\inv[1]{#1^{-1}}
$\inv{A}$

The difference between A^\inv and \inv{A} is that in the first case you have to adjust \inv to be taken correctly by ^, whereas in the second you give \inv full power.

I would consider the “passive” approach only if it benefits the readability (and doesn't harm maintenance) of the code. Consider the absurd example

a + b % preferred
\add{a}{b} % easily to manage, but far less readble

In this case, \inv{A} doesn't harm readability / ease-of-understanding in my opinion, so I would go for that.

Manuel
  • 27,118
  • In the past I used this approach and found that the formulas were hard to read, especially when A is a long expression or when used in conjunction with other superscripts such as transpose symbols. And then I noticed that TeX's \prime is defined as a "passive" superscript so I decided to give it a try. – Ernest A Jul 21 '16 at 11:54
  • The reason \prime exists is because it's easier to program ' that way, I don't think that you should take that as an example / justification. But yes, in case you find it difficult to read, then Peter's solution is the way to go ;) – Manuel Jul 21 '16 at 12:29