# Multicolumn within a newcommand error

I am new to writing packages and am coming across an error that I can't unravel, I would appreciate any help that you can provide. I am writing a few macros which are meant to be used within a tabular environment:

\newcommand{\topics}{\@ifstar{\topicsStar}{\topicsNoStar}}
\newcommand{\topicsNoStar}[1]{\multicolumn{2}{|p{2in}|}{test}}
\newcommand{\topicsStar}{&&}


When I call

\topicsNoStar{stuff}


the output is correct.

When I call

\topics{stuff}


I get the error message: !Misplaced \omit. \multispan ->\omit \@multispan

What is confusing me is the difference between the two calls - in my mind, they should be doing the same thing, but the output is not the same.

Now, I do plan to include @'s in the commands above in the final version, but, to debug, I wanted to be able to call them outside the style file. Also, the argument to NoStar is not used, it will be placed into the multicolumn once this is working correctly.

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  doncherry Aug 20 '12 at 18:03

This solution is built on the requirement suggested by Heiko in ! Misplaced \omit error.

etextools provides fully-expandable conditioning commands to allow \multicolumn to be considered as "the first element in the cell". This is not currently the case in your example, since \@ifstar is not fully expandable, while \FE@ifstar is a Fully Expandable version:

\documentclass{article}
\usepackage{etextools}% http://ctan.org/pkg/etextools
\makeatletter
\newcommand{\topics}[1]{%
\FE@ifstar{#1}
{test & test}% starred
{\multicolumn{2}{|p{2in}|}{#1}\@gobble}}% non-starred
\makeatother
\begin{document}
\begin{tabular}{|p{1in}p{1in}|}
\topics{stuff} \\
\topics* \\
\topics{some more stuff} \\
\topics* \\
\topics*
\end{tabular}
\end{document}

-

\multicolumn must be the first thing in a table cell and a command with a star variant defined in the usual way hides \multicolumn after some unexpandable token. Without packages you can do in the following way:

\documentclass{article}
\newcommand{\topics}[1]{%
\if*\detokenize{#1}%
&%
\else
\multicolumn{2}{|p{2in}|}{#1}%
\fi
}
\begin{document}
\begin{tabular}{|p{1in}p{1in}|}
\topics{stuff} \\
\topics* \\
\topics{some more stuff} \\
\topics* \\
\topics*
\end{tabular}
\end{document}

-