[Tex/LaTex] How does “\char`” work

charactersparsingsymbols

In LaTeX, you can use \char` followed by a symbol to get that symbol. For example \char`b 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.

Best Answer

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

Related Question