[Tex/LaTex] Subscript equivalent of ‘ for \prime

best practicesmath-mode

I am working through the TeXbook and I came across the section (pg. 130) where ' and \prime are discussed. Knuth mentions that TeX treats \prime as a large symbol that appears only in superscripts instead of making it a smaller symbol that has already been shifted up into the superscript position. Part of the reason is because some authors actually use \prime in the subscript position.

I know $y'_1+y''_2$ yields a typographical output equivalent to that of $y^\prime_1+y^{\prime\prime}_2$, but I am wondering if there is an equivalent to ' for putting the \prime into the subscript position.

For example, $h'$ is typographically equivalent to $h^\prime$, but is $h_\prime$ equivalent to any "quicker" expression (i.e., involving an apostrophe or something similar)? I tried $h_'$, but this produces an error; then I tried $h_{'}$, but I realized this is equivalent to typing $h_{{}^\prime}$.

Basically, is there a character X where $hX$ and $h_\prime$ are equivalent?

Best Answer

Just mimic what the kernel does for \prime:

\documentclass{article}

\makeatletter
\def\active@math@sprime{_\bgroup\sprim@s}
{\catcode`\`=\active \global\let`\active@math@sprime}
\def\sprim@s{%
  \prime\futurelet\@let@token\spr@m@s}
\def\spr@m@s{%
  \ifx`\@let@token
    \expandafter\spr@@@s
  \else
    \ifx_\@let@token
      \expandafter\expandafter\expandafter\spr@@@t
    \else
      \egroup
    \fi
  \fi}
\def\spr@@@s#1{\sprim@s}
\def\spr@@@t#1#2{#2\egroup}
\mathcode`\`="8000
\makeatother

\begin{document}

Normal use: `test'.

Math: $h`$ and $h``$ and also $h`_{1}$.

\end{document}

enter image description here

What does this do? First of all, a backquote in math mode is dealt with as if it were an active character, because of \mathcode`\`="8000; TeX will look for a definition of ` as active character, which is \active@math@sprime.

When it's found first in a possible sequence of backquotes, TeX expands \active@math@sprime, so doing _\bgroup\sprim@s. This starts a subscript, exploiting the fact that _\bgroup<tokens>\egroup is legal syntax. Now \sprim@s is expanded, which typesets \prime and does

\futurelet\@let@token\spr@m@s

This looks at the following token, stores it into \@let@token without removing it from the input stream and executes \spr@m@s.

This macro does some tests:

  1. if \@let@token is a backquote, the \else...\fi part is removed by \expandafter and \spr@@@s is executed;

  2. if \@let@token is _ the \else...\fi\fi part is removed by the triple \expandafter and \spr@@@t is executed;

  3. none of the above: \egroup is executed which will close the subscript.

Now let's look at \spr@m@s: it is defined just to remove the next token (which is a backquote) and does \sprim@s again. This is how $h``$ becomes $h_\bgroup\prime\prime\egroup$.

The macro \spr@@@t has two arguments: the first is _ which is simply gobbled and the second is the intended subscript, which is closed by \egroup that balances the initial \bgroup. This is how $h`_{1}$ becomes $h_\bgroup\prime 1\egroup.