TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to the TeXbook the two tokens following \if are expanded and compared? It seems then that \if\char98\char98 T \else F \fi should be the same as \if bb T \else F \fi which does return T. What is wrong with my interpretation?

share|improve this question
\char98 has an action comparable to the printer fishing out a "b" from the case and it's performed at a very later stage than macro expansion, that is, when everything has been decided about the paragraph shape and glyphs to use (among other things) and the printer has to produce the paragraph by putting letters together. – egreg Jul 8 '12 at 8:48

The \char primitive is not expandable, so its "expansion" is itself, and not "the character whose code is 98". Since the 98 afterwards is not consumed, the next token is 9, and that's different from the \char token, so you hit the false path.

One way to do this as you want is to \chardef\x=98; then \if\x\x will return true. Another way is to do \if ^^62^^62; as TeX by Topic tells me on p.44, the ^^ syntax is parsed way before expansions are made (it's actually an input syntax), and therefore ^^62 is one single character. Note (see egreg's comment) that ^^62 is in hexadecimal, while \char98 is decimal (0x62 = 98).

Maybe I should expand on that "input syntax" comment. There are two kinds of characters in TeX: input characters, which are what you write in a .tex file, and printable characters, which are what issue a typesetting directive to TeX. In the jargon of the TeXbook, an input character belongs to TeX's "eyes", and an output character to its "stomach". Between those two is the "mouth", where the macro language lives, and \if belongs to that language. By contrast, although \char is recognized by the eyes as being a token, its actual function is not known until the stomach, which is when absorbs its arguments and becomes a printable character.

A normal character like a is both input and printable under the usual catcodes, so \if aa works as expected, but to get an input character by charcode into \if you need to use a syntax that is recognized by the eyes, and for that, we have ^^.

share|improve this answer
Pay attention that ^^98 refers to the ASCII code 0x98 (hexadecimal), so writing ^^98\char98 will print two different characters. – egreg Jul 8 '12 at 8:45
@egreg: fixed, thanks. – Ryan Reich Jul 8 '12 at 8:50
Note that \if^^62^^62 is equivalent to \ifbb, an undefined control sequence. The conversion is done very early on. You need to put a space there. – Bruno Le Floch Jul 8 '12 at 9:54
@Bruno I should have realized that :) – Ryan Reich Jul 8 '12 at 16:31
@RyanReich A limitation of \if ^^xy^^XY is that one cannot pass xy or XY as arguments: \if ^^#1 would be interpreted exactly like \if c1 (and taking the false path). – egreg Jul 9 '12 at 9:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.