10

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?

1
  • You can define it in a newcommand
    – phollox
    Commented Jan 9, 2015 at 23:29

1 Answer 1

14

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.

10
  • Wow I was actually thinking of trying to do just that, but I had no idea of how to do it. Somehow I stumbled across your ensuremath campaign. Don't know what all that is about but I hope you got somewhere with it. I also thought about making a new command, but is there a way to make a new command that only applies when used in math mode (or should I ask this as a separate question)? Commented Jan 9, 2015 at 23:36
  • I should probably add that I realize that is what you just did, but it looks quite a bit more complicated than something basic like what I am thinking about. Commented Jan 9, 2015 at 23:38
  • Hey! I was just working on it! :(
    – yo'
    Commented Jan 9, 2015 at 23:40
  • 1
    @induktio It would be simpler if you just need one subscript prime not combined with other subscripts. But a double prime requires that approach. It's exactly the same process by which ' is turned into prime: change the backquotes that don't immediately follow \catcode or \mathcode into ', all _ into ^ and all \sp... into \p...: it will be the copy of the macros for '.
    – egreg
    Commented Jan 9, 2015 at 23:49
  • 1
    @phollox Simple: this one works, what you're proposing doesn't. ;-)
    – egreg
    Commented Jan 11, 2015 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .