Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ?

share|improve this question
    
For automatically generated tables, back to TeX is ideal and simple. Hacking the array package is like going from London to Paris via New York:) See tug.ctan.org/tex-archive/macros/plain/contrib/ruled-tables for some pointers. –  Yiannis Lazarides Mar 8 '11 at 0:22
add comment

2 Answers 2

up vote 4 down vote accepted

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
}
share|improve this answer
    
Thank you @TH. It's a bit sad that the interface cannot be the same for repeating the whole preamble and for repeating from another column. I'm guessing that \repeatnone is there to disable repeating the preamble in nested tabulars? –  Bruno Le Floch Mar 8 '11 at 9:16
    
@Bruno: I hadn't actually thought of that, but sure. I actually wrote it since every tabular that appears after \repeatall will have a repeating preamble. –  TH. Mar 8 '11 at 19:08
    
Although, not repeating the preamble only helps to detect bugs anyways. Btw, in environment form, \repeatall will only take effect until the end of the repeattabular. –  Bruno Le Floch Mar 8 '11 at 19:15
    
@Bruno: Yes, that was the point of writing the environment in the first place. –  TH. Mar 9 '11 at 0:24
add comment

This doesn't exactly answer your question, but it might be helpful to others who have more modest needs, so I'm including it here. You can use the *n{c} to get n columns of type c. You can use *n{lr} to get n lr columns. With the array package, you can do this with any defined column type:

\documentclass{article}
\usepackage{array}

\newcolumntype{L}{lr}

\begin{document}
\begin{tabular}{*3{L}}
    a   & b   & d     &e   & f     & apsdoi \\
    cde & def & erasd &arp & sefoi & wp    
\end{tabular}
\end{document}

Of course this doesn't dynamically allocate columns, but depending on how the auto-generated tables are created, you may know in advance the number of columns that need to be repeated.

share|improve this answer
    
that is no the same. The && construct allows an infinite number of repetitions of an arbitrary combination of column definitions. –  Philipp Mar 7 '11 at 21:25
    
I've updated the answer. But I think I didn't read Bruno's question carefully enough, since this doesn't really achieve what he wants either; it relies on knowing the number of columns. –  Alan Munn Mar 7 '11 at 21:30
    
thanks for your answer, but in my case, I don't know how many columns there will be (although I could easily count the number of & myself in the specific application). –  Bruno Le Floch Mar 8 '11 at 9:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.