# Using tabu/tabularx inside xparse NewDocumentEnvironment

My question seems very similar to this, but with tabu instead of tabular*. In fact, switching the options in that example causes the failure I see here.

\NewDocumentEnvironment {trace} {} {%
\begin{table}
\begin{tabu} to \textwidth {|@{ }r@{::}l|l|X|}
A & B & C & D \\
}{%
\end{tabu}
\end{table}
}

\begin{trace}
a & b & c & d \\
\end{trace}


This fails with ! Missing number, treated as zero. <to be read again> \tabu@0.H4 l.51 \end{trace}. Similarly for the tabularx environment, which also allows X. The below works just fine, but doesn't allow the X parameter I would like to use

\NewDocumentEnvironment {trace} {} {%
\begin{table}
\begin{tabular*}{\textwidth}{|@{ }r@{::}l|l|p{0.5\textwidth}|}
A & B & C & D \\
}{%
\end{tabular*}
\end{table}
}


Use the command-form within the environment definition in order to successfully compile:

\documentclass{article}
\usepackage{xparse,tabu,tabularx}% http://ctan.org/pkg/{xparse,tabu,tabularx}
\NewDocumentEnvironment {traceA} {} {%
\table
\tabularx{\linewidth}{|@{ }r@{::}l|l|X|}
A & B & C & D \\
}{%
\endtabularx
\endtable
}
\NewDocumentEnvironment {traceB} {} {%
\table
\tabu to \linewidth {|@{ }r@{::}l|l|X|}
A & B & C & D \\
}{%
\endtabu
\endtable
}

\begin{document}
\begin{traceA}
a & b & c & d
\end{traceA}

\begin{traceB}
a & b & c & d
\end{traceB}
\end{document}


The tabularx documentation (section 5 The Macros, p 4) eludes to this somewhat:

Now a trick to get the body of an environment into a token register, without doing any expansion. This does not do any real checking of nested environments, so if you should need to nest one tabularx inside another, the inner one must be surrounded by { }.

[...]

This mechanism of grabbing an environment body does have the disadvantage (shared with the AMS alignment environments) that you can not make extension environments by code such as

\newenvironment{foo}{\begin{tabularx}{XX}}{\end{tabularx}}


as the code is looking for a literal string \end{tabularx} to stop scanning. Since version 2.02, one may avoid this problem by using \tabularx and \endtabularx directly in the definition:

\newenvironment{foo}{\tabularx{XX}}{\endtabularx}


The scanner now looks for the end of the current environment (foo in this example.) There are some restrictions on this usage, the principal one being that \endtabularx is the first token of the "end code" of the environment.

My guess is tabu relies on similar restrictions.

• Great! Can I ask a quick followup: Now \NewDocumentCommand \tline { m m m m } {% #1 & #2 & #3 & #4 \} and \begin{trace} \tline{a}{b}{c}{d} \end{trace} fails with the same error Missing number – Hamy Jul 27 '13 at 0:20
• @Hamy: This works fine for tabularx, but not tabu. tabu doesn't like an empty line break at the end. Try \NewDocumentCommand \tline { m m m m } {% #1 & #2 & #3 & #4 } – Werner Jul 27 '13 at 0:41