27

Suppose I have a table with 20 columns that should all be centered. Is there a way to declare the alignment without literally counting out 20 c's? Something like c{20} might be the syntax. I feel like I saw this once, but I can't find a reference anywhere.

Werner
  • 603,163
alex.jordan
  • 2,179

1 Answers1

30

Replication of column specifications is done using a *{<num>}{<col spec>} interface, which should repeat <col spec> a total of <num> times:

\begin{tabular}{*{20}{c}}
  % tabular content
\end{tabular}

will produce a table of 20 columns, each one centred. To add vertical column rules (which you should never do), you can use

\begin{tabular}{|*{20}{c|}}
  % tabular content
\end{tabular}

Note how the replication only includes a single right-side vertical rule with a single left-side rule at the beginning. One could also have used *{20}{|c}|.

Werner
  • 603,163
  • The second form behaves slightly more oddly with \multicolumn? – cfr Jul 22 '14 at 02:23
  • 1
    @cfr: Yes; this is because of the insertion of the rules. I forget which (left/right), but they are associated with that particular column. – Werner Jul 22 '14 at 02:30
  • yet, be careful in using |. See the booktabs manual for a nice introduction into nice tables :) https://ctan.org/pkg/booktabs – Arne Oct 24 '22 at 18:08