Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

\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 \DeclareExpandableDocumentCommand for this.

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.

share|improve this answer
    
Yep, that fixes it. Thanks. –  Peter Grill May 1 '11 at 23:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.