\begin{tabu} で文字列変数を書式文字列として使用する

\begin{tabu} で文字列変数を書式文字列として使用する

次のような問題があります。tabuパッケージを使用してテーブルを作成していますが、次のような書式文字列ではなく、\begin{tabu}{ccc}次のような書式文字列 (文字列変数として) を使用したいと考えています\begin{tabu}{\reptemp}

これが私のコードです:

\documentclass{article}
\usepackage{forloop,tabu}

\begin{document}

\def\reptemp{}      

\newcounter{ct}

    \forloop{ct}{1}{\value{ct} < 5}%
    {%
        \g@addto@macro\reptemp{c}
    }

\begin{tabu}{\reptemp}
\end{tabu}

\end{document}

そして、次のコンパイル エラーが発生します。

! Package array Error: Illegal pream-token (\reptemp): 'c' used.

この問題を解決する方法についてのヒントがあれば幸いです。

答え1

環境tabuは、プリアンブル引数の拡張を行いません。

\documentclass{article}
\usepackage{forloop,tabu}

\def\reptemp{}
\makeatletter
\newcounter{ct}

    \forloop{ct}{1}{\value{ct} < 5}%
    {%
        \g@addto@macro\reptemp{c}
    }
\makeatother

\newenvironment{xtabu}[1]
  {\begingroup\edef\x{\endgroup
   \unexpanded{\begin{tabu}}{\unexpanded\expandafter{#1}}}\x}
  {\end{tabu}}


\begin{document}
\begin{xtabu}{\reptemp}
1&2&3&4
\end{xtabu}
\end{document}

答え2

マクロを環境に渡す前に展開する必要があります。

\def\starttabu#1{\begin{tabu}{#1}}

\expandafter\starttabu\expandafter{\reptemp}

関連情報