Use macro to create array column specification

programming

How can I make a custom macro that creates an array with some number of columns, but I'm having trouble forcing the macro to expand inline. In test.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{test}
\RequirePackage{tikz}
\RequirePackage{array}

\newcommand{\generateColumns}[1]{%
    \foreach \n in {1,...,#1}{c}
}

\newcommand{\myCustomArray}[2]{%
    \begin{array}{\generateColumns{#1}}
       #2
    \end{array}
}

But \generateColumns{#1} is evidently passed to \begin{array} literally! How can I force expansion of a \newcommand macro in this context (or even better, change \generateColumns to be inline since I only use it once)?

Best Answer

This is already covered as an option by LaTeX (no need for tikz):

\newcommand{\myCustomArray}[2]{%
  \begin{array}{ *{#1}{c} }
    #2
  \end{array}%
}

The column specification *{<num>}{<spec>} replicates <spec> a total of <num> times.

Related Question