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.

  • 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. – Benjamin McKay Nov 22 '17 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. – David Carlisle Nov 22 '17 at 17:03

1 Answers1

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

Ulrike Fischer
  • 327,261
  • 2
    Perhaps expand on the backslash rule here? It's a TeX, not a LaTeX, convention ... – Joseph Wright Nov 22 '17 at 09: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) –  Nov 22 '17 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 '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. –  Nov 22 '17 at 17:54
  • Is there a symbol to int function in latex3? \char_value_catcode:n{\char``A} @egreg – Erwann Jan 02 '22 at 19:04
  • 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 02 '22 at 21:02
  • @egreg And from int to char? – Erwann Jan 03 '22 at 09:59
  • @Erwann please don't use comments under my answer for a conversation with someone else. I get unneeded pings from this. Go to the chat or open a chat room with egreg or ask a proper question. – Ulrike Fischer Jan 03 '22 at 10:02