9

In some packages like tabularx, I find the following syntax in many places

{\ifnum0=`}\fi

or even

\ifnum0=`{\fi}

But I can't figure out why this can be useful for? Is ` a register of some kind? Or is it a TeX-hack to generate error on specific occasion ?

Manuel
  • 27,118
M'vy
  • 402
  • 1
    see http://tex.stackexchange.com/questions/50828/execute-non-expandable-code-inside-a-tabular-environment –  Jul 09 '14 at 12:46
  • Perhaps take a look at http://tex.stackexchange.com/questions/9897/showcase-of-brace-tricks-egroup-iffalse-fi-etc – Joseph Wright Jul 09 '14 at 12:52

1 Answers1

14

Ah the \ifnum funky brace groups beloved of TeX\halign programmers:-)

`

is part of the syntax for a number in TeX.

125

is a decimal

"7D

is hex and

`}

is the character code of the specified character (which is also 125 as it happens).

So....

{\ifnum0=`}\fi

the inner \ifnum is testing if 0=125 which is false so when expanded this is equivalent to { so starts a brace group. However if the tokens are not being expanded and TeX is just looking for matching {} pairs then it sees that as a matching pair so you can go

\def\foo{  {\ifnum0=`}\fi }

but

\def\foo{  {  }

is an error (or at least does not stop at that }.

Usually you can use implicit brace groups \bgroup and \egroup to use an unmatched { but some constructs demand an explicit { token and so this trick (explained by Knuth in the TeXBook comes in useful).

Usually if you find that an environment that uses & to separate alignment cells does not work in a nested alignment it is because the author forgot to use these groups in the definition.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
David Carlisle
  • 757,742