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.
Asked
Active
Viewed 1.2k times
27
Werner
- 603,163
alex.jordan
- 2,179
1 Answers
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
-
-
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
booktabsmanual for a nice introduction into nice tables :) https://ctan.org/pkg/booktabs – Arne Oct 24 '22 at 18:08
arraypackage, and write the specifiers as{*{20}{c}}instead of{ccc…cc}. – Bernard Jul 21 '14 at 19:20arrayhere, although it is a good addition to any document. – Werner Jul 21 '14 at 19:21