# Which spaces exactly are ignored by \halign?

Some spaces seem ignored by the \halign primitive. For instance,

\setbox0=\vbox{\halign{ # \cr \space b \cr}}
\showbox0


indicates that there is no glue before b, but that there are two spaces after. It seems that spaces in the preamble are lost as soon as it is read, but I can't be sure.

Which spaces (implicit spaces, funny spaces, macros expanding to spaces, other spaces) are lost where in \halign?

-

I don't know about implicit spaces, but after & or \cr, tokens are expanded (looking for \omit or \noalign) until an unexpandable token is found. Spaces are ignored in both cases.

-
but spaces are also ignored within the preamble? –  Bruno Le Floch Apr 20 '11 at 15:34
@Bruno: No. It looks like the space after the { is ignored, but the space following the # is certainly there. A preamble of  a # \cr inserts a space before and after the b. –  TH. Apr 20 '11 at 15:43
right. So spaces would only be ignored at the beginning of the u part of the template, perhaps. –  Bruno Le Floch Apr 20 '11 at 15:48
@Bruno: That would be consistent with ignoring spaces after &. –  TH. Apr 20 '11 at 15:50

When reading the "u"-part of the preamble for each column of an \halign, namely the part which will be inserted before the cell contents, TeX ignores any leading "spacer". In this context, a "spacer" is defined as any implicit or explicit character token with category code 10. See @<Scan the template \<u_j>...@>= in tex.web for those who understand Pascal code.

At the start of each cell, i.e., just after each & or \cr (or \crcr), TeX fully expands tokens and ignores "spacers". This is done by calling @<Get the next non-blank non-call token@> before any call to init_col.

A test to show that indeed arbitrary implicit space tokens are ignored:

% Define \sp as an implicit space token "q".
\lccode =q\def\\{\let\sp= }\lowercase{\\ }

% Store an \halign in a box. The \sp in the u-part
% preamble is completely ignored, directly when TeX
% stores the preamble.  To prove this, we redefine
% \sp globally, and see that it doesn't appear at
% the next line.
%
\setbox0=\vbox{\halign{
\sp x # \cr
\sp \space A\gdef\sp{!} \cr
\space B \cr
}}
% Show the box.  Leading spaces have all been ignored.
% Not trailing spaces.
\tracingall\showbox0

-