8

' is defined in latex.ltx as follows

\def\active@math@prime{^\bgroup\prim@s}
{\catcode`\'=\active \global\let'\active@math@prime}
\def\prim@s{%
  \prime\futurelet\@let@token\pr@m@s}
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\def\pr@@@s#1{\prim@s}
\def\pr@@@t#1#2{#2\egroup}

I can see that the catcode of ' is changed to \active in a group. When the group ends, the catcode of ' falls back to 12 (other). So how could it possibly be that $f'$ means $f\active@math@prime$?

I also tested

\catcode`!=12
{\catcode`!=\active \global\let!=\active@math@prime}
$f!!!$

It does not give the same result as $f'''$. What happens to '?

12

The key is \mathcode set to hexadecimal 8000 (decimal 32768), which will set ! active, but only in math mode. Also, \pr@m@s needs redefinition to point to ! rather than '.

Ref: How can I make every occurrence of `+` and `-` be replaced by a macro, but only in math mode?

\documentclass{article}

\begin{document}
\makeatletter
\catcode`!=12
\mathcode\number`\!="8000 %
\def\pr@m@s{%
  \ifx!\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}

{\catcode`!=\active \global\let!=\active@math@prime}
$f!!!$
\end{document}

enter image description here

If you needed both ! and ' to function in the prime way, you could really edit \pr@m@s:

\documentclass{article}

\begin{document}
\makeatletter
\catcode`!=12
\mathcode\number`\!="8000 %
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\expandafter\expandafter\pr@@@s
  \else
  \ifx!\@let@token
    \expandafter\expandafter\expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi\fi}

{\catcode`!=\active \global\let!=\active@math@prime}
$f!'!^3$
\end{document}
  • 1
    OK so I didn't need to define ! nor did I need a back up of '. But that \mathcode is (probably) exactly what I am missing. – Symbol 1 Nov 20 at 21:32
  • 2
    A character with \mathcode set to "8000 is not really active: only when TeX is processing math codes it gets replaced by its definition as if it were active. If you try $\edef\test{'}\show\test$, you'll see that no expansion is done to ' even if TeX is in math mode. Also, only characters with category code 11 or 12 can be math active. – egreg Nov 20 at 21:46
  • @egreg I did think that maybe it is active in math so I tried $\def'{what?}$. And I got inaccessible. Now the rule is clear. – Symbol 1 Nov 20 at 21:52
  • Thanks @egreg, both for the edit and clarifying comment. – Steven B. Segletes Nov 21 at 1:36

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.