Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 17 down vote accepted

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).

share|improve this answer

As mentioned in Ulrike's answer at Teletype \textbackslash in alltt environment, when OT1 encoding is employed, the glyph for \textbackslash is obtained from the symbol font (because there is no glyph for it in the regular font). If you run my MWE and zoom in on the column depicted in the figure (the '6 or ''E column), you will see that the symbol font (cmsy10) backslash character is in slot 110, which happens to be the same slot as the letter n in the italic font (cmti10). Presumably, in math mode, the font employed is redirected to the italic font, and so \char110, which renders as the \textbackslash in the symbol font, instead renders as an italic n in the math font.

This problem can be circumvented by using T1 encoding (\usepackage[T1]{fontenc}), which provides for a text backslash in the regular font. It will render properly, even in math mode.

\documentclass{article}
\usepackage{fullpage}
%\usepackage[T1]{fontenc}
\usepackage{fonttable}
\begin{document}
\fonttable{cmti10}
\fonttable{cmsy10}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.