[Tex/LaTex] Why can’t caret and tilde be escaped with backslash alone

tex-core

I can escape some special characters with a backslash.

\documentclass{article}
\begin{document}
\# \$ \% \& \_ \{ \}
\end{document}

But why can't I escape ^ and ~ with a backslash alone? For example, the code below does not work.

\documentclass{article}
\begin{document}
\^
\end{document}

It causes the following error with texlive.

$ pdflatex foo.tex 
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./foo.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 2 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (./foo.aux)
! Missing \endcsname inserted.
<to be read again> 
                   \global 
l.4 \end
        {document}
? 
! Emergency stop.
<to be read again> 
                   \global 
l.4 \end
        {document}
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on foo.log.

In fact, I have to first escape with them a backslash and then add {} as a suffix to them to make it work.

\documentclass{article}
\begin{document}
\^{}
\~{}
\end{document}

I understand that {}, i.e. the empty parameter list, forces TeX to consider it as the end of a command and beginning of text again, but I want to understand why {} is necessary.

Two questions:

  1. Why can't these characters be escaped with \ alone?
  2. Is there any difference between \^{} and \^\? They seem to give the same output but I want to know if there is any difference between them that might product different results under any circumstances?

Best Answer

Although we commonly say that \ is the escape character, it's really the character that TeX uses to introduce a control sequence. There are two types of control sequences. Control words consist of \ plus a string of letters. Control symbols consist of \ plus a single non-letter.

But in neither case is \ acting as an escape per se. Each of the characters you think of as being "escaped" are really control symbols which all have actual definitions. Some simply expand to their corresponding character, but others do not. Here are their actual definitions:

*\show\#
> \#=\char"23.

*\show\$
> \$=\char"24.

*\show\%
> \%=\char"25.

*\show\&
> \&=\char"26.

*\show\{
> \{=macro:
->\delimiter "4266308 .

*\show\}
> \}=macro:
->\delimiter "5267309 .

*\show\^
> \^=macro:
#1->{\accent 94 #1}.

*\show\'
> \'=macro:
#1->{\accent 19 #1}.

*\show\~
> \~=macro:
#1->{\accent "7E #1}.

Notice that in the case of \^, \~, and \' the macro definitions take an argument. So they will take their next character as that argument. If you don't want that to happen, you need to explicitly stop it with the {}.

To answer your second question, \^{} is not identical to \^\ since the latter will insert a space but the former will not. So for example:

\^\ foo \^{}foo

will yield the following output:

output of fragment