How does \hline work?

Looking at \hline, I ran into a couple complex pieces:

\meaning\hline

macro:->\noalign {\ifnum 0=}\fi \hrule \@height \arrayrulewidth \futurelet
\reserved@a \@xhline


\meaning\@xhline

macro:->\ifx \reserved@a \hline \vskip \doublerulesep
\vskip -\arrayrulewidth \fi \ifnum 0={\fi }


It seems the \@xhline code is closing the \ifnum conditional. Could someone provide an explanation of what is going on with {\ifnum 0=}

• You're reporting badly the code. Anyway, those are very smart tricks for contributing a brace without TeX suspecting we're doing it. Nov 14, 2016 at 17:59
• @egreg I just copied and pasted from PDF. Maybe I should have used \show instead of \meaning and copied from the terminal. I will check everything when I get home. Nov 14, 2016 at 18:30
• the ifnum brace trick is explained in some detail in appendix D of the TeXBook Nov 14, 2016 at 19:00
• @egreg I double-checked the code with \show and it appears to be the same. What do you mean when you say that I am reporting the code badly? Nov 15, 2016 at 6:55
• @macmadness86 There were some backslashes, now removed. Nov 15, 2016 at 8:12

1 Answer

Here's the definition in the source file

% latex.ltx, line 5351:
\def\hline{%
\noalign{\ifnum0=}\fi\hrule \@height \arrayrulewidth \futurelet
\reserved@a\@xhline}
\def\@xhline{\ifx\reserved@a\hline
\vskip\doublerulesep
\vskip-\arrayrulewidth
\fi
\ifnum0={\fi}}


The responsibility of \hline is to add a horizontal rule and to look forward for another \hline. In order to do this, TeX is temporarily put outside the alignment, with the help of \noalign. The code

\noalign{\ifnum0=}\fi


opens the token list for \noalign, and the closing brace is not seen because it is part of a numeric test returning false. TeX expands tokens as it goes, performs the necessary assignments; in particular, \hrule is executed and then \futurelet is called to see whether another \hline command follows (for adding a small vertical space). Finally

\ifnum0={\fi}


is found, which closes the token list for \noalign and gets TeX back in doing the alignment.

Why not \bgroup and \egroup? They would work here, but it's preferable to have those explicit braces in case \hline gets expanded prematurely so the brace counter is fooled into thinking that they appear in a pair.

• So if I understand you, the test is whether 0 and  are equal? Why is the first \ifnum0=} not \ifnum0={ as in the TeXBook Appendix D p.385? Does it have to do with the reversed increase/decrease counter of alphabetic constants? Nov 15, 2016 at 6:44
• @macmadness86 In the first case we want a closing brace for ”balancing“ the opening one. Nov 15, 2016 at 8:16
• @macmadness86 it is comparing 0 with } (don't know how to get just backquote brace in the text) which is the character }, thereby swallowing up the } so that it doesn't close the group. And at the end it is the reverse. Nov 15, 2016 at 19:08
• @PietvanOostrum You have to escape the metacharacter grave accent with a backslash to type it literally. e.g.: }. Thanks for the helpful clarification. This is certainly a "dirty" TeX trick. Usually conditional operators have single token operands. You'd have to know the comment egreg will type below to understand it ;) Nov 16, 2016 at 12:44
• @macmadness86 Sorry, I didn't get your doubt: when TeX is looking for a number, for instance in the context of \ifnum, the combination <char> denotes an alphabetical constant, so the whole } is a number. Nov 16, 2016 at 12:46