10

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)

  • 4
    Doesn't \begin{tabular}{l*{6}{n{2}{3}}} do the job, or did I misunderstood what you want to do? – Bernard Mar 17 '15 at 13:00
  • @Bernhard That's it :) Works like a charm. Can you make an answer out of this such that I can accept it? Thanks. – Jean-Paul Mar 17 '15 at 13:05
23

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 !{…}.

  • 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

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.