# Using xparse-defined command inside table

This is a followup to this question, I'm wondering why using my tline command below will not compile

\documentclass{article}
\usepackage{xparse,tabu}
\usepackage[table]{xcolor}

\NewDocumentEnvironment {trace} {} {%
\table
\tabu to \linewidth {|@{ }r@{::}l|l|X|}
A & B & C & D \\
}{%
\endtabu
\endtable
}

\newcommand{\commentcell}{\cellcolor[gray]{0.9}}
\newcommand{\linecell}{\cellcolor[gray]{0.75}}
\newcommand{\classcell}{\cellcolor[gray]{0.75}}
\newcommand{\methodcell}{\cellcolor[gray]{0.75}}
\newcommand{\codecell}{\cellcolor[gray]{0.75}}
\NewDocumentCommand \tline { o m m m m } {\linecell #2 & \classcell #3 & \methodcell #4 & \codecell #5 \\ %
\IfNoValueTF {#1}
{} % No comment was provided, skip creating a line
{\hline\multicolumn{4}{ |r| }{\commentcell #1} \\ }
\hline
}

\begin{document}

% This works
\begin{trace}
a & b & c & d
\end{trace}

% This fails with 'Missing Number'. Note that tabu definitely does not like the
% ending \\ caused by the \tline, but I know that I need to use \\ inside my
% tline command to cause a line break, so the question is how do I optionally
% create two rows inside tline without using \\, or is there a command tabu
% prefers to \\. I tried \newline with no luck
\begin{trace}
\tline{a}{b}{c}{d}
\end{trace}

\end{document}

• You can try with \DeclareExpandableDocumentCommand – egreg Jul 27 '13 at 7:28
• @egreg Would you make that an answer (with some detail)? – Joseph Wright Aug 27 '13 at 8:01
• I'm sorry with not being able to help, but I don't see what xparse has to do with this problem, which only depends on tabu. Unfortunately, the current version of tabu is unmaintained and the author has announced big changes to the next version (also backward incompatible ones). – egreg Aug 27 '13 at 9:12

You can't use \multicolumn inside a command defined with \NewDocumentCommand, but it can work if you use \DeclareExpandableDocumentCommand. However, tabu seems not to agree with what's done in environments defined with \NewDocumentEnvironment.

I repeated explicitly the environment just to show that the output is identical.

\documentclass{article}
\usepackage{xparse,tabu}
\usepackage[table]{xcolor}

\newenvironment{trace}
{%
\table
\tabu to \linewidth {|@{ }r@{::}l|l|X|}
A & B & C & D \\
}
{%
\endtabu
\endtable
}

\newcommand{\commentcell}{\cellcolor[gray]{0.9}}
\newcommand{\linecell}{\cellcolor[gray]{0.75}}
\newcommand{\classcell}{\cellcolor[gray]{0.75}}
\newcommand{\methodcell}{\cellcolor[gray]{0.75}}
\newcommand{\codecell}{\cellcolor[gray]{0.75}}
\DeclareExpandableDocumentCommand \tline { o m m m m } {%
\linecell #2 & \classcell #3 & \methodcell #4 & \codecell #5 \\ %
\IfNoValueTF {#1}
{}% No comment was provided, skip creating a line
{\hline\multicolumn{4}{ |r| }{\commentcell #1} \\ }
\hline
}

\begin{document}

% This works
\begin{trace}
a & b & c & d
\end{trace}

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

\begin{table}
\begin{tabu} to \linewidth {|@{ }r@{::}l|l|X|}
A & B & C & D \\
\linecell a & \classcell b & \methodcell c & \codecell d \\
\end{tabu}
\end{table}

\end{document}