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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9561032652854919, "perplexity": 1854.9460574747748}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-50/segments/1606141194634.29/warc/CC-MAIN-20201127221446-20201128011446-00121.warc.gz"}