# How is \char processed in math mode?

The following example produces ${\rm\char"5}$
\end


Why does it not produce the following instead? The TeXbook p. 289 explains how \char〈8-bit number〉 can form a 〈character〉, and that a 〈character〉 is a particular case of a 〈math symbol〉. What a 〈math symbol〉 found in math mode does is explained on page 291, but let's get back to page 289 about 〈character〉:

When TeX is in math mode or display math mode, a 〈character〉 command takes on added significance: It specifies a number between 0 and 32767 = 215 − 1. This is done by replacing the character number by its \mathcode value. If the \mathcode value turns out to be 32768 = "8000 however, the 〈character〉 is replaced by an active character token having the original character code (0 to 255); TeX forgets the original 〈character〉 and expands this active character according to the rules of Chapter 20.

So, what matters when finding \char〈8-bit number〉 in math mode is the \mathcode of the specified character. In your case, the character is number 5, i.e. \^^E. Line 59 of plain.tex reads:

\mathcode\^^E="023A % \lnot


Therefore \char"5 in math mode is a 〈math symbol〉 with \mathcode "023A. It specifies class 0 (ordinary, cf. TeXbook p. 154), family 2 and position "3A in the corresponding font.

The font in question is thus \textfont2, because your \rm does nothing here, AFAICT:

\rm:
macro:->\fam \z@ \tenrm


\fam has an effect on symbols of class 7 (variable), but here the symbol has class 0; besides, \tenrm affects the text font and has no effect in math mode, AFAIK (except of course if you temporarily leave math mode using, for instance, an \hbox command inside the dollar signs).

You may query what this font is with:

\tracingonline=1
\showthe\textfont2


which outputs:

> \tensy .
l.3 \showthe\textfont2


and confirms what can be seen with \tracingoutput=1:

Completed box being shipped out 
\vbox(667.20255+0.0)x469.75499

(...)

.\vbox(643.20255+0.0)x469.75499, glue set 633.20255fill
..\glue(\topskip) 5.69446
..\hbox(4.30554+0.0)x469.75499, glue set 443.0883fil
...\hbox(0.0+0.0)x20.0
...\mathon
...\tensy :
...\mathoff

(...)



: indeed has ASCII code "3A. So, the character you get in the output is found in position "3A of font \tensy. The TeXbook p. 350 (plain.tex) declares:

\font\tensy=cmsy10


You can verify with texdoc encguide (page 33 here) that character "3A of font cmsy10 is indeed the \lnot we see on your screenshot.

Note: this is not directly related to the question, but since its crux is the \mathcode and the paragraph I quoted from the TeXbook explains what happens with the special case of a \mathcode equal to "8000, I'll seize the opportunity and point our dear readers to this answer of egreg where the "8000 special case is put to good use (cf. function \__hs_cls_activate_comma: in his answer).

• Behavior of \char is also described on top of p.155 in TeXbook. – Igor Liferenko Aug 2 '19 at 2:32
• an interesting case arises when mathcode for a <character> has not been defined - then mathcode is taken to be equal to charcode – Igor Liferenko Aug 2 '19 at 3:02
• Incidentally, is it possible to "undefine" a mathcode if it has already been defined? For example, how to change $\mathcode"5=0 \char"5$ so that П would be printed, instead of Г. – Igor Liferenko Aug 2 '19 at 3:29
• I don't think it is possible to undefine a \mathcode, and the top of page 155 says that INITEX starts in a state where the \mathcode for every possible character code (256 values in Knuth's TeX) is defined. And beware, one doesn't have \mathcode x = x for digits and letters in this case: it's x +"7000 for the 10 digits and x +"7100 for the 52 letters. This makes in-math digits in roman type by default, and in-math letters in italic type by default (and can be overriden with \fam). To get your П, simply point to position 5 of \textfont0 which is by default cmr10. – frougon Aug 2 '19 at 6:37
• You can do that with: \mathcode"5="0005 $\char"5$ \end. You could set up many \mathcode entries like that in a file you \input or in a custom format. Note that this treats the character as a mathematical entity (a 〈math symbol〉). If it's really text inside a formula, $\hbox{\char"5}$ seems more appropriate and needs no particular font or \mathcode setup (of course, \char"5 also works in horizontal mode without needing dollar signs nor \hbox: it causes a character box to be appended to the horizontal list). – frougon Aug 2 '19 at 7:22