10

Minimum working example (I will refer to the lines based on the first letter A-E):

\documentclass{article}
\newcommand{\inv}{^{-1}}
\begin{document}
$A^{-1}$
$B\inv$
$C'^{-1}$
% $D'_1^{-1}$ % fails
% $E'\inv$ % fails
\end{document}

I got an error Double superscript: You have written a double superscript incorrectly as a^b^c, or else you have written a prime with a superscript. Remember to include {and} when using multiple superscripts. Try a^{b ^ c} instead.

I realise this similar to this question, but that asks why line D fails.

My question: why does line C work but line E fail?

3
  • 1
    Try to enclose each element with a curly bracket. May 30 at 19:40
  • 1
    I was a few seconds faster than egreg :).
    – wipet
    May 30 at 19:58
  • This doesn't explain why (there are already two good explanations for that), but shows an alternate input that will work: $C^{\prime-1}$. May 30 at 20:08

2 Answers 2

9

When TeX finds ' (in math mode), it basically does the following:

  1. it emits ^\bgroup\prime;
  2. it looks at the following token (without expanding it);
  3. if the next token is another ' it emits \prime and returns to step 2;
  4. if the next token is ^, it gobbles it and absorbs what comes next as a macro argument (either a single token or a braced group) and considers it as part of the exponent that's being built and emits \egroup;
  5. otherwise it emits \egroup.

In your case, the next token is \inv so this falls into case 5. Thus

C'\inv

becomes

C^\bgroup\prime\egroup\inv

and then

C^\bgroup\prime\egroup^{-1}

Double superscript.

If you want to support this syntax you have to add to the lookup also \inv.

All considered, I'd use the syntax ^\inv instead.

Anyway, here's an expl3 implementation that supports \inv.

\documentclass{article}

\ExplSyntaxOn

\cs_new_protected:Nn \wang_prime:
  {
    % start a superscript
    \c_math_superscript_token
    \c_group_begin_token
    % we want a prime
    \prime
    \__wang_prime_next:
  }
\cs_new_protected:Nn \__wang_prime_next:
  {
    \peek_charcode_remove_ignore_spaces:NTF '
      { % another ' follows; add it and restart the recursion
        \prime
        \__wang_prime_next:
      }
      { % look for ^ or \inv
        \token_if_eq_meaning:NNTF \l_peek_token \c_math_superscript_token
          { % remove ^ and add the superscript
            \__wang_prime_remove_superscript:Nn
          }
          {
            \token_if_eq_meaning:NNTF \l_peek_token \inv
              {
                \__wang_prime_remove_inv:N
              }
              { % finish the business
                \c_group_end_token
              }
          }
      }
  }
\cs_new_protected:Nn \__wang_prime_remove_superscript:Nn { #2 \c_group_end_token }
\cs_new_protected:Nn \__wang_prime_remove_inv:N { -1 \c_group_end_token }
\char_set_active_eq:NN ' \wang_prime:
\ExplSyntaxOff

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

\begin{document}

$A^{-1}$

$B\inv$

$C'^{-1}$

$D''^{-1}$

$E'\inv$

\end{document}

Note that a'_{1}^{2} is not supported and cannot be.

enter image description here

4
  • Ah so you're suggesting I define \newcommand{\inv}{{-1}} and use the syntax ^\inv instead? Thank you for the detailed explanation. Where is this information documented? It's cool to have the top user answer my first question :D May 30 at 20:04
  • 1
    @BenjaminWang The code for ' is described in the TeXbook and is essentially the same in LaTeX.
    – egreg
    May 30 at 20:13
  • 1
    @BenjaminWang I added an implementation that supports \inv, but you should really consider not using it.
    – egreg
    May 30 at 20:33
  • thank you for the implementation :D I may or may not use it! May 31 at 11:15
9

The reason is in plain.tex file where the ' is defined at lines 735--739. Other formats (for example LaTeX) copy the same idea.

The ' is math-active character which runs ^\bgroup\prime followed by a macro which tests next token by \futurelet primitive. If the next token is ' or ^ then the next superscript is composed with the current superscript and the group (opened by \bgroup) isn't closed. But if the next token is another token then \egroup is put here and the next token is processed. This is the case of your example D (next token is _) or E (next token is \inv).

One nucleus cannot have more than one superscript, so you see the error "double superscript".

If you want to use constructs like $E'\inv$ or $E''\inv$, then you can re-define an internal macro of the format you are using. For example for Plain TeX or LaTeX:

{\catcode`\'=\active \catcode`\@=11
 \gdef'{^\bgroup\expandafter\prim@s}\gdef\pr@@@s#1{\expandafter\prim@s}}

for OpTeX:

{\_catcode`\'=\active \_gdef'{^\_bgroup\_ea\_primes}}
\def\_primesA #1{\_ea\_primes}

The idea is to add \expandafter before the \futurelet is processed. Note that the macro \prim@s (or \_primes) runs the \futurelet primitive.

3
  • Ah thank you this makes sense. Where can I find plain.tex? May 30 at 20:06
  • 1
    @BenjaminWang The plain.tex file is in each TeX distribution. For example, TeX live installs it to texmf-dist/plain/base/ directory. Or you can find it on your computer using a search tool, for example locate plain.tex at Unix-like systems. Of course, you can find it at CTAN site too: ctan.org/tex-archive/macros/plain/base
    – wipet
    May 31 at 4:32
  • thank you for the CTAN link, which is helpful for users who don't actually install LaTeX (e.g. by using Overleaf). May 31 at 11:13

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.