3

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?

  • 1
    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. – David Carlisle Jun 30 '18 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 '18 at 0:56
5

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}
| improve this answer | |

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.