1

When I use

\begin{tabular}{l*{20}{c}}

it works perfectly.

But when I use

\begin{tabular}{l|c*{4}|c*{8}|c*{8}}

It gives me error. How can I specify the alignment operator as multiples without explicitly stating each one.

1
  • 4
    It is *{number of columns}{column type}. If you want | then do *{4}{|c}|
    – user11232
    Mar 31, 2015 at 8:43

1 Answer 1

1

You can think of the notation

*{<num>}{<col-spec>}

as a very simple 'repeater' or loop, if you will. Its only effect is that any valid set of column specifications <col-spec> will be placed literally <num> times into the tabulars preamble at the spot it's used.

So something like \begin{tabular}{*{2}{c}} will become effectively identical to \begin{tabular}{cc} when the tabular is being created. Since vertical rules (created with |) are valid 'column' specifications, they can be repeated just like any other column type: {*{2}{c|}} will become {c|c|}.

However, note that this would not be symmetric, because we need one more rule than column in order to have rules left and right of all repeated columns. The extra rule can be added outside the repeated column specifications: {|*{2}{c|}} or {*{2}{|c}|} will both 'expand' to {|c|c|}

Here's a short example showing the usage:

\documentclass{article}

\begin{document}
\begin{tabular}{|c|c|}
 A & B
\end{tabular}

\begin{tabular}{|*{2}{c|}}
 A & B
\end{tabular}

\begin{tabular}{*{2}{|c}|}
 A & B
\end{tabular}
\end{document}

enter image description here

Aside: Personally I avoid vertical rules in tables whenever possible, but some people like them or are required to use them. Though, in some cases, I concede they can be useful. You can read about some of the reasons behind my preference if you like in the booktabs package documentation or at Why not use vertical lines ('|') in a tabular?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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