2

I have the following problem. I'm using the tabu package to create tables but instead of a format string like this: \begin{tabu}{ccc} I want to use a format string like this (as string variable): \begin{tabu}{\reptemp}.

Here is my code:

\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}

And get the following compiling error:

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

I hope you have a tip for my how I can solve this problem.

  • 1
    Welcome to TeX.SX. Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Claudio Fiandrino Apr 11 '13 at 8:40
2

The tabu environment doesn't do expansion on the preamble argument.

\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}
1

You need to expand the macro before passing it to the environment:

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

\expandafter\starttabu\expandafter{\reptemp}

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.