# Can't include a 'tabu' inside \newenvironment with cells in the postamble

I'm trying to include a tabu inside a new definition of an environment but it fails if the postamble (enddef) of the new environment includes tabu's contents, so cells. With tabular or tabular* works. The error is:

! Missing number, treated as zero. \tabu@0.H5


The MWE:

\documentclass{article}
\usepackage{tabu}
\newenvironment{testt}
{\begin{tabu} to \textwidth{cccc}
11 & 12 & 13 & 14 \\
21 & 22 & 23 & 24 \\}
{
41 & 42 & 43 & 44
\end{tabu}
}

\begin{document}

\begin{testt}
31 & 32 & 33 & 34 \\
\end{testt}
\end{document}


How to solve this?

• This seems to be a tabu bug. – egreg Apr 3 '13 at 16:51
• While not having a new version of tabu, what may be a workaround? – Emilio Lazo Apr 4 '13 at 23:52

Try the package environ. This package uses \BODY to markup the postion of the parameter, which avoid problems, such as mismatch of braces and what occured in this question.

See its document.

\documentclass{article}
\usepackage{tabu}
\usepackage{environ}
\NewEnviron{testt}
{\begin{tabu} to \textwidth{cccc}
11 & 12 & 13 & 14 \\
21 & 22 & 23 & 24 \\
\BODY
41 & 42 & 43 & 44
\end{tabu}
}

\begin{document}

\begin{testt}
31 & 32 & 33 & 34 \\
\end{testt}

\end{document}


The tabu package internally does

\tabu@collectbody#1#2\end


to collect up all of the content of the table before typesetting. That won't work correctly if you 'hide' any of the table. It does work if you only hide the \end{tabu} as for example

\documentclass{article}
\usepackage{tabu}
\newenvironment{testt}
{\begin{tabu} to \textwidth{cccc}
11 & 12 & 13 & 14 \\
21 & 22 & 23 & 24 \\}
{
\end{tabu}
}

\begin{document}

\begin{testt}
31 & 32 & 33 & 34 \\
41 & 42 & 43 & 44
\end{testt}
\end{document}


still grabs all of the table correctly.

You can see something is wrong if you use \showthe\currentgrouplevel inside the cells: all of the 'working' cases have group level 14, while the 'wrong' one is group level 8.

As egreg says, this is arguably a bug in tabu, or at least a 'feature' which should be documented. To advice on a work-around, I guess we need to know what effect you are after and can then suggest an alternative approach: I think that would be best as a new question.

You may try with the collect package. Nota bene: not having ever used neither collect nor tabu I am surely unable to expertly assess whether this is a viable and robust way.

\documentclass{article}
\usepackage{tabu}
\usepackage{collect}

\definecollection{rescuetabu}

\makeatletter
\newenvironment{testt}
{\@nameuse{collect*}{rescuetabu}
{\begin{tabu} to \textwidth{cccc}
11 & 12 & 13 & 14 \\
21 & 22 & 23 & 24 \\ }
{41 & 42 & 43 & 44 \\ \end{tabu}}{}{}}
{\@nameuse{endcollect*}}
\makeatother

\begin{document}

\texttt{normal tabu}

\begin{tabu} to \textwidth{cccc}
11 & 12 & 13 & 14 \\
21 & 22 & 23 & 24 \\
31 & 32 & 33 & 34 \\
\end{tabu}

\hrule\medskip
\texttt{rescued tabus}

\begin{testt}
31 & 32 & 33 & 34 \\
\end{testt}

\bigskip

\begin{testt}
51 & 52 & 53 & 54 \\
61 & 62 & 63 & 64 \\
\end{testt}
\hrule

\end{document}