[Tex/LaTex] Why does \textbackslash render as “n” in math mode

math-modesymbols

I've just rendered the following:

\documentclass[varwidth=true, border=2pt]{standalone}

\begin{document}$\textbackslash$
\end{document}

which seems to give the same as $n$. Do you know why?
As \textbackslash is for textmode and not for mathmode, I've expected TeX to fail in this situation. Instead, I get

LaTeX Warning: Command \textbackslash invalid in math mode

So why does \textasciitilde in math mode make LaTeX crash but \textbackslash not?

Best Answer

The definition of \textbackslash when the OT1 encoding is used is

\OMS-cmd \textbackslash \OMS\textbackslash

Both the first and the third token can't be typed by a user without some devious trick. However, the definition of \OMS-cmd is equivalent to

\def\OMS-cmd#1#2{%
  \ifx\protect\@typeset@protect
    \@inmathwarn #1%
    \expandafter\ifx\csname\cf@encoding\string#1\endcsname\relax
    ...<omitted irrelevant things>...
}

The macro \@inmathwarn is responsible for the warning

Command \textbackslash invalid in math mode

After that the macro \OMS\textbackslash is expanded. If we were in text mode, the command would do a font change in a group, selecting a font in the OMS encoding (math symbols), doing

\char"6E

and a backslash would appear, because that's what an OMS encoded font has at slot "6E (hexadecimal).

If we're in math mode, the same \char"6E instruction would be performed, but here the font change has no effect. So we get the character at position "6E in the font in math family 0, where an n is found. When \char"6E is found in math mode, TeX does as if it were \mathchar"006E and in family 0, slot "6E there's an n (it's the upright text font and the ASCII code of n is exactly "6E).

Things are different in the T1 encoding, because in this case the definition of \textbackslash is

\T1-cmd \textbackslash \T1\textbackslash

and the latter macro eventually expands to \char"5C. In a text font, T1 encoding, at slot "5C there's actually a backslash.

Here's the explanation. Now, some advice:

  • Never underestimate warnings and

  • never use \text... symbol commands in math mode (don't mistake them with \textrm, \textit or similar, which are obviously legal in math mode).