[Tex/LaTex] Repeating preamble in LaTeX tabular

tables

The TeX primitive \halign, on which every tabular etc. is built, allows repeated preambles. The easiest explanation of what it is comes by looking at the output of the code below.

\documentclass{article}
\usepackage{array}
\newcolumntype{\repeat}{<{\span\doublenextamp}}
\def\doublenextamp#1&{#1&&}
\begin{document}

First test.
\begin{tabular}{c\repeat lr}
    a   & b   & d     &e   & f     & apsdoi \\
    cde & def & erasd &arp & sefoi & wp     
\end{tabular}

\end{document}

This saves a lot of typing, and, for automatically generated tables, a lot of counting-myself-the-number-of-columns.

However, I could only manage to make it work in LaTeX when the repetition starts from the second column or more, not to repeat the whole preamble. That is because LaTeX (or rather the array package?) inserts a lot of stuff before anything that the user could put, but TeX wants the extra & (which marks the repetition) to come first.

Two questions:

  • Is there a packaged solution ?

  • Is there a custom solution with a clean user interface ?

Best Answer

Here's a solution that works by modifying the internals of the array package.

\documentclass{article}
\usepackage{array}
\makeatletter
\newcommand*\repeatall{%
        \def\@arstrut{&\unhbox\@arstrutbox}%
}
\newcommand*\repeatnone{%
        \def\@arstrut{\unhbox\@arstrutbox}%
}
\makeatother
\begin{document}
\repeatall
\begin{tabular}{l}
a&b&c\\
d&e&f\\
\end{tabular}
\end{document}

Maybe it'd be better to make that into an environment instead of switches.

\newenvironment{repeattabular}{%
        \repeatall
        \tabular
}{%
        \endtabular
}
Related Question