# Latex3: multicolumn at the beginning of a token list produces an error

I'm not sure if using token list is a good approach to generating table data. Anyway, here is a weird result. It works well in all other cases except when the first item in token list is multicolumn.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

%Table A
\tl_new:N\g_tabledata_a_tl
\tl_gput_right:Nn\g_tabledata_a_tl{\multicolumn{2}{c}{MyTable} \\}
\tl_gput_right:Nn\g_tabledata_a_tl{My & Table \\}
\NewDocumentCommand{\UseTabledataA}{}{\tl_use:N\g_tabledata_a_tl}

%Table B
\tl_new:N\g_tabledata_b_tl
\tl_gput_right:Nn\g_tabledata_b_tl{My & Table \\}
\tl_gput_right:Nn\g_tabledata_b_tl{\multicolumn{2}{c}{MyTable} \\}
\NewDocumentCommand{\UseTabledataB}{}{\tl_use:N\g_tabledata_b_tl}

\ExplSyntaxOff

\begin{document}
%% Table A. This doesn't work.
\begin{tabular}{cc}
\UseTabledataA
\end{tabular}

\bigskip

%% Table B. This has no problem.
\begin{tabular}{cc}
\UseTabledataB
\end{tabular}

\bigskip

% For reference, this also has no problem
\begin{tabular}{cc}
\multicolumn{2}{c}{MyTable} \\
My & Table \\
\end{tabular}
\end{document}


The error message is:

Misplaced \omit.\multispan ->\omit\@multispan \UseTabledataA

I know multicolumn has to be the first thing in the cell. Maybe this has something to do with that?

• on your edit, it is best not to edit solutions into the question it disturbs the question/answer format of the site, but specifically your comment seems wrong, in all cases if the token list is a vald table body, exposing it via NewExpandableDocumentCommand will work. Of coure it is possible to construct token lists that are not valid table bodies but that would be specific to whatever tokens you have in this list and not really addressed by this question. Jun 30, 2018 at 16:28
• I've rolled back the edit to avoid having the answer you've got in your question. Otherwise, it looks as if the question is what to do when that solution doesn't work, and the answer you've got then makes no sense.
– cfr
Jul 1, 2018 at 0:56

The cell content has to expand to see the \omit to form a spanning cell so you need

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

%Table A
\tl_new:N\g_tabledata_a_tl
\tl_gput_right:Nn\g_tabledata_a_tl{\multicolumn{2}{c}{MyTable} \\}
\tl_gput_right:Nn\g_tabledata_a_tl{My & Table \\}
\NewExpandableDocumentCommand{\UseTabledataA}{}{\tl_use:N\g_tabledata_a_tl}

%Table B
\tl_new:N\g_tabledata_b_tl
\tl_gput_right:Nn\g_tabledata_b_tl{My & Table \\}
\tl_gput_right:Nn\g_tabledata_b_tl{\multicolumn{2}{c}{MyTable} \\}
\NewDocumentCommand{\UseTabledataB}{}{\tl_use:N\g_tabledata_b_tl}

\ExplSyntaxOff

\begin{document}
%% Table A. This doesn't work.
\begin{tabular}{cc}
\UseTabledataA
\end{tabular}

\bigskip

%% Table B. This has no problem.
\begin{tabular}{cc}
\UseTabledataB
\end{tabular}

\bigskip

% For reference, this also has no problem
\begin{tabular}{cc}
\multicolumn{2}{c}{MyTable} \\
My & Table \\
\end{tabular}
\end{document}