Normally, the catcode of dollar symbol ($) is 3 in text. But I found that

  • the catcode of $ is 12 in the header and footer, and
  • $ can still be used as math shift.



\def\@oddfoot{\the\catcode`$, $a + b = c$\hfill}
\def\@oddhead{\the\catcode`$, $a + b = c$\hfill}

\the\catcode`$, $a + b = c$\par
\the\catcode`$, $a + b = c$

My current understanding:

  • the catcode of $ in $a + b = c$ is get from the re-definition of \@oddfoot/head, this may explain the second part.
  • but I still confusing about the first part, as stated in the question title.
  • 3
    The $ still works as a math shift, because the catcode of $ at the time you defined \@oddfoot and \@oddhead was 3, the tokenization of it was done the moment it was defined and TeX doesn't change category codes of once tokenized text (unless you use \scantokens). – Skillmon Jul 6 at 12:29

First things first, the setting in force when you do the redefinition of \@oddhead and \@oddfoot is $3 (the subscript denotes the catcode). At the time of the definition TeX reads the characters \the\catcode`$, $a + b = c$\hfill and turns them into tokens:
\the \catcode `12$3,1210$3a1110+1210b1110=1210c11$3\hfill
(A very useful tool to get this list of tokens is expl3's \tl_analysis_show:N .)

This tokenisation occurs with the catcode setting in force at the time the definition was made, and TeX won't change this even when catcodes change. Quoting The TeXBook (Chapter 8, The Characters You Type; emphasis mine):

[...] it is best not to play with the category codes very often, because you must remember that characters never change their categories once they have become tokens.

or, with an example, from Appendix D:

[...] TeX stamps the category on each character when that character is first read from a file. Thus, if \verbatim were defined by a construction of the form \long\def\verbatim#1{⟨something⟩}, argument #1 would already be converted to a list of tokens when ⟨something⟩ starts; \catcode changes would not affect the argument.

This is what is usually called "frozen catcodes" and, as Skillmon said in the comment, the only way to change it is with ε-TeX's \scantokens.

Now to your document, with \@oddhead and \@oddfoot already tokenised with $3, when the document begins the catcode of $ is still 3, as you see in the first equation. Then you change $ to catcode 12 and write another equation, which is not in math-mode anymore because $ is not catcode 3. And now the document ends.

TeX knows that you wrote something, but it still didn't try breaking the paragraphs into pages: everything you wrote until now is in a \vbox waiting to be shipped out. When LaTeX sees the \end{document} it starts preparing a final (an only in this case) page with what you wrote. Part of this preparation is the inclusion of headers and footers, at which time it uses \@oddhead and \@oddfoot. At this time the catcode of $ is queried by your definition, and it is 12, simply because you changed it.

There is no special catcode setting for page headers (not usually, at least), but it doesn't try to enforce any specific setting, so the last one active ($12) will be used. However the equations will still be in math-mode because the tokenized \@oddhead and \@oddfoot have $3. The conclusion is: the catcode of $ isn't changed by LaTeX's output routine; you changed it!

You can experiment by changing the catcode back:

\the\catcode`$, $a + b = c$\par
\the\catcode`$, $a + b = c$

or making the change in a group:

\the\catcode`$, $a + b = c$\par
\the\catcode`$, $a + b = c$\par}

and you'll see the settings are back to normal.

As an exercise, try playing with \scantokens (with \catcode`$=12 before the \end{document}):

\def\@oddfoot{\the\catcode`$, \scantokens{$a + b = c$\hfill}}
\def\@oddhead{\scantokens{\the\catcode`$, $a + b = c$\hfill}}

(try to guess the output before running TeX)

  • Thanks for your patient and careful answer. As you said, "the last one active ($<sub>12</sub>) will be used" and "the catcode of $ isn't changed by LaTeX's output routine; you changed it!" I have confused myself when asking this question. – Johannes Z Jul 6 at 20:39
  • And an answer to your exercise: both produces output "12, (dollar symbol)a + b = c(dollar symbol)". The catcode of $ in both inline equations is re-scanned when \scantokens expands in the output routine, causing each of the four $ gets catcode 12. By the way, does it make any differences that \the\catcode`$ is within or not within \scantokens? – Johannes Z Jul 6 at 20:44
  • 1
    @JohannesZ might be worth reading a bit about \scantokens, as there are some pitfalls in its usage: tex.stackexchange.com/a/213713/117050 – Skillmon Jul 7 at 21:23
  • 1
    @JohannesZ In this situation, not really. I have to admit it's a pretty dull test. It was just for giving a taste of \scantokens (and, of course, it's pitfalls, as Skillmon mentioned). However, if you were to set the catcode of $ to 9 (ignore) before the end of the document, then you'd see a difference between the two (and why, to be on the safe side, it is best to escape the character: \catcode`\$=12). – Phelype Oleinik Jul 8 at 12:47

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.