[Tex/LaTex] setting \tabskip in a repeating \halign preamble

plain-textex-core

Is it somehow possible to set the \tabskip glue to zero for the first and last column in a repeated \halign?

Eg.

% tabskip=0pt
\halign{&#\hfil\tabskip1em plus1em minus.5em\cr
  Foo&Bar&Baz\cr
  \noalign{\smallskip\hrule}
}
\bye

There you can see that for the last column, there is a tabskip after it.

I would like to have a preamble which has zero tabskip for the first and last column. Is this somehow possible?

Best Answer

You can achieve what you wish by doubling the number of columns.

\halign{&#\tabskip0pt&#\hfil\tabskip1em plus1em minus.5em\cr
  Foo&&Bar&&Baz\cr
  \noalign{\smallskip\hrule}
}

If the alignment is large, and you don't want to type & each time, it is possible to hide the extra & in the preamble and place it just after every & with \aftergroup. However, it needs to be removed after each \cr, hence the setting of \everycr. This, in turn, requires us to add a weird extra \amp after the first \cr.

\def\amp{&}
\begingroup
  \def\gobbleamp\amp{} \everycr{\gobbleamp}
  \halign{&#\tabskip0pt\aftergroup\amp&#\hfil\tabskip1em plus1em minus.5em\cr\amp
    Foo&Bar&Baz\cr
    \noalign{\smallskip\hrule}
  }
\endgroup

Ha! I got some "eviler" method, with a nicer input.

\def\amp{&}
\halign{&#\tabskip0pt&\amp#\hfil\tabskip1emplus1emminus.5em\cr
  Foo&Bar&Baz\cr
  \noalign{\smallskip\hrule}
}

The \amp is adding a & just after each & actually typed by the user.

TeX calls the part of a cell's preamble before # the "u-part", and the part after # the "v-part". Here, the \amp is in the u-part of the preamble for even cells. When such a cell starts, TeX expands the tokens to check for the presence of \omit. Once it sees (in our case) that there is no \omit, TeX inserts the u-part, hence an \amp. It then proceeeds with typesetting. \amp is expanded to &, which ends the cell. At this point, the v-part is inserted (\hfil). Then TeX starts the following odd cell after the & that the preamble had inserted.

Earlier, I was trying to put \amp in the v-part of the preamble for odd cells, naively equivalent: trying to insert & before each user-provided & instead of after. I don't fully understand why it doesn't work.

Related Question