13

I'm using package numprint to create a table which automatically formats its columns. However, the table has many columns.

Right now, my table definition looks like this:

\begin{tabular}{l n{2}{3} n{2}{3} n{2}{3} n{2}{3} n{2}{3} n{2}{3} .. etc ..} ..

Using package pgffor I would like to automate the number of column definitions (because they might differ). I thought of doing this using a \foreach as follows:

\begin{tabular}{l \foreach \k in {1,...,6}{ n{2}{3} }}

But it doesn't work. It gives an error that extra alignment tab has been changed to .., which implies that it can't interpret the output of the for each loop.

What is the correct way of doing this? (automatically generating the column types)

Jean-Paul
  • 421

1 Answers1

32

No pgffor package is required for this; just write your table preamble as:

\begin{tabular}{l *{6}{n{2}{3}}} 

The general syntax is:

*{n}{column(s) pattern}

where n is the number of repetitions, and the pattern can be any number of column specifiers, @{some code}, !{some code}, >{…}, <{…}.

Some part of this syntax depends on the array package – in particular >{…}, <{…} and !{…}.

Bernard
  • 271,350
  • Note that, unless I'm mistaken (which I may be at the moment), < and > are provided by the array package. – Sean Allred Mar 17 '15 at 13:34
  • @Sean Alfred: You're probably right (not sure, and perhaps it's also the case for !{…}). I'll add it. – Bernard Mar 17 '15 at 13:36
  • (note to future self): The "column pattern" can be something as simple as c, l, r. Thus, to center six columns, it's simply \begin{tabular}{*{6}{c}}. – PatrickT Jul 31 '21 at 08:45
  • @PatrickT: Yes, but it ca,n be more complex – e.g. *{2}{rcl} would yield somethi,g more or less likeeqnarray` with two alignment columns. – Bernard Jul 31 '21 at 09:02