12

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.

3
  • Welcome to TeX.SX!
    – Bobyandbob
    Nov 22, 2017 at 9:31
  • 1
    I recommend Donald Knuth's book "The TeXbook". It gives you complete information on low level TeX commands, and exercises to test your understanding. Nov 22, 2017 at 10:45
  • 1
    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. Nov 22, 2017 at 17:03

1 Answer 1

16

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

9
  • 2
    Perhaps expand on the backslash rule here? It's a TeX, not a LaTeX, convention ...
    – Joseph Wright
    Nov 22, 2017 at 9:58
  • 2
    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, 2017 at 11:01
  • 2
    The backslash is not actually needed for { and }, unless this would leave unbalanced braces (in a \def, for instance).
    – egreg
    Nov 22, 2017 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, 2017 at 15:05
  • 1
    @Erwann \int_eval:n {`A} if you want the character number. Or \char_value_catcode:n {`A} if you want the category code. You need to escape special characters. But \int_eval:n is not needed where an integer denotation is expected.
    – egreg
    Jan 2, 2022 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .