# How does “\char” work?

In LaTeX, you can use \char followed by a symbol to get that symbol. For example \charb or \char\b would just yield the letter b.

• Which symbols/letters does that work for? Two cases I've found that yield different symbols are \char{ and \char}. Is there any sort of reason why those two don't work the way you would think?
• Why does this happen in the first place? Just typing out b doesn't give you anything special, so why does it work in the context of a \char?
• Why is the backslash ignored in things like \char\_ when there's a valid command for \_?

I'm still new to LaTeX and it's hard to Google symbol-related stuff like this, so that's why I'm asking here. Thanks in advance.

• Welcome to TeX.SX! – Bobyandbob Nov 22 '17 at 9:31
• I recommend Donald Knuth's book "The TeXbook". It gives you complete information on low level TeX commands, and exercises to test your understanding. – Benjamin McKay Nov 22 '17 at 10:45
• The backtick is explained in the answer below, but note also that \char is not a latex command and shouldn't be used in documents, the latex version is \symbol which takes a standard latex brace syntax so \symbol{b} or \symbol{64} or whatever. – David Carlisle Nov 22 '17 at 17:03

It is a misunderstanding to attach the left quote to the \char command as you do by writing \char.

\char expects as argument a number. With the left quote you are converting the next character or single-character command to its character code (which is a number) if TeX is currently looking for a number.

So b and \b both give the number 62 in such a context.

You can use this syntax in all places where a number is expected:

\documentclass{article}

\begin{document}
\setlength\parskip{b pt}

aba

abc

\end{document}

The backslash is needed only for special chars. E.g. \%, \{, \}, \\ but it doesn't harm to add it always. In this context it gives the character "itself".

• Perhaps expand on the backslash rule here? It's a TeX, not a LaTeX, convention ... – Joseph Wright Nov 22 '17 at 9:58
• perhaps worth mentioning that perhaps surprisingly \char~ is ok, despite the character being active. Or \char: with babel-french. (or \number\numexpr~\relax) – user4686 Nov 22 '17 at 11:01
• The backslash is not actually needed for { and }, unless this would leave unbalanced braces (in a \def, for instance). – egreg Nov 22 '17 at 14:37
• @jfbu It works because it's not expanded at all, right? I don't know myself, I'm curious. Also why does \char^ works but \char^^a gives something else hahahhah nah, I'm joking. – Manuel Nov 22 '17 at 15:05
• @Manuel yes it is not expanded at all. If you use " for hexadecimal input or ' for octal input in a context where TeX expects a number, expansion happens (and of course expansion happens also without prefix), hence surprise with  when one has not read the TeXBook closely ;-). Edit to my comment above I should have given \number~ as example. btw, this swallows one space token if it comes next. – user4686 Nov 22 '17 at 17:54