How \span influences number of &' in \halign?

Consider this preamble:

\tabskip=0pt plus1fil#&#&#\tabskip=0pt


The last template is #, so it should not matter whether a line is \BOX&\BOX&\cr or \BOX&\BOX\cr. But it turns out, it does matter, but only when there is a line with \span. I would like to understand why the & before \cr is important. For example:

\def\BOX{\vrule width50pt height8.5pt depth3.5pt}

\vbox{\offinterlineskip
\halign to 16em{\tabskip=0pt plus1fil#&#&#\tabskip=0pt\cr
\BOX&\BOX&\cr
}}

\vbox{\offinterlineskip
\halign to 16em{\tabskip=0pt plus1fil#&#&#\tabskip=0pt\cr
\BOX&\BOX\cr
}}


For conveniency, there is a ruler. We can easily see that the second column shifts by 25pt when & before \cr is removed:

% ruler:
\vskip5pt
\def\\#1{\hbox to 10pt{\hfil#1}}
\def\1{\vrule height 0pt depth 2pt}
\def\2{\vrule height 0pt depth 4pt}
\def\3{\vrule height 0pt depth 8pt}
\def\4{\\\1\\\1\\\1\\\1\\\2\\\1\\\1\\\1\\\1\\\3}
\hbox to160pt{\hrulefill} \nointerlineskip
\hbox{\3\4\\\1\\\1\\\1\\\1\\\2\\\1} \nointerlineskip
\font\7=cmr7 \font\5=cmr5
\hbox to360pt{\rlap{\70}\kern50pt\rlap{\550}\kern50pt\rlap{\7100}\kern50pt
\rlap{\5150}\hss}


• What about \tabskip glue? – egreg Jan 20 '20 at 8:49
• @egreg In the first halign we have such lines: g0 w50 g30 w50 g30 w0 g0 and g0 w50 g30 w50 g30 w0 g0 (here the second w50 is an overfull box with filler glue set to 80 - the result of \span). In the secord halign we have such lines: g0 w50 g55 w50 g55 and g0 w50 g55 w50 g55 w-50 g0 and I'm completely stymied where these results come from, especially that number of boxes and glue in first line and second line are different. – Igor Liferenko Jan 21 '20 at 2:35

Let's consider the case when & is not used. When the second line (with \span) is present, we get this for the first line:

g0 w50 g55 w50 g55


when it is not present, we get this for the first line:

g0 w50 g30 w50 g30


From this we can conclude that glue is set with influence from line with \span. That is, glue is calculated for the second line and after that this glue is used for the first line.

Here is the second line:

...\hbox(8.5+3.5)x160.00024, glue set 55.00012fil
....\glue(\tabskip) 0.0
....\hbox(8.5+3.5)x50.0
....\glue(\tabskip) 0.0 plus 1.0fil
....\hbox(8.5+3.5)x50.0, glue set 55.00012fill
......\rule(8.5+3.5)x0.4
....\glue(\tabskip) 0.0 plus 1.0fil
....\hbox(0.0+0.0)x-50.0
....\glue(\tabskip) 0.0


Let's use formula on p.245 of TeXbook to understand why widths are 50, 50 and -50:

w_1 is w_11 = 50. w_2 is max from w_12-(w_11+t_1) and w_22. w_12 is -infinity, so w_2 is w_22 = 50.

The thing that must be found out is why third column has width -50.

w_3 is max from

• w_13 - (w_1+t_1) - (w_2+t_2)
• w_23 - (w_2+t_2)
• w_33

Natural width of tabskip glue is zero, so t_k is zero. So, w_3 is max from

• w_13 - w_1 - w_2
• w_23 - w_2
• w_33

There is no span from 1 to 3, so w_13 is -infinity. So, w_3 is max from

• w_23 - w_2
• w_33

w_2 is 50, so w_3 is max from

• w_23 - 50
• w_33

The value "-50" happens only in the first of these expressions, so it must be the value of w_3. For this w_33 must be -infinity. And it follows that w_23 is zero.

To sum it up: if span is done for more columns than are in other rows, TeX considers the width of the missing columns to be -infinity and width of entries that span the missing column to be zero.

To conclude: if you use \span, make sure that at least one of the rows has the corresponding number of &`'s.