14

The following example has two normal LaTeX \newcommand macros and two xparse \NewDocumentCommand for specifying multicolumn text. The only difference between each pair of commands is one has the alignment character as part of the macro and one does not. However, the xparse version that requires the alignment character to be outside of the macro does NOT work and results in Misplaced \omit. error.

\documentclass{article}
\usepackage{xparse}

\newcommand{\MulticolumnA}[3]{&\multicolumn{#1}{#2}{#3}}
\newcommand{\MulticolumnB}[3]{ \multicolumn{#1}{#2}{#3}}

\NewDocumentCommand{\MulticolumnX}{m m m}{&\multicolumn{#1}{#2}{#3}}
\NewDocumentCommand{\MulticolumnY}{m m m}{ \multicolumn{#1}{#2}{#3}}

\begin{document}
\[
\begin{tabular}{lllllll}
    {a} \MulticolumnA{4}{l}{A-abcdefghijkli}\\
    {a}&\MulticolumnB{4}{l}{B-abcdefghijkli}\\
    {a} \MulticolumnX{4}{l}{X-abcdefghijkli}\\
    %{a}&\MulticolumnY{4}{l}{Y-abcdefghijkli}\\
    {col1}&col2&{col3}&{col4}&{col5}\\
\end{tabular}
\]
\end{document}

Above compiles as is, but does not compile if the one line is uncommented.

1 Answer 1

18

\NewDocumentCommand creates a robust command, via \protected. The documentation about e-TeX says, on page 14, that protected macros are not expanded when looking ahead in an alignment for \noalign or \omit. So, with \MulticolumnY after the &, the search for an \omit ends and when TeX expands \MulticolumnY it finds a misplaced \omit (from \multicolumn). You have to use \NewExpandableDocumentCommand for this.

\NewExpandableDocumentCommand has been added in the 2017/03/11 release of xparse. Previously only \DeclareExpandableDocumentCommand was available.

With \MulticolumnX this doesn't happen, because the command is not expanded when TeX is looking for \omit; it finds &, so it finishes the table entry and starts a new one, but now it can see directly \multicolumn and is happy.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .