[Tex/LaTex] Misplaced \omit and \multicolumn in \IfEqCase

conditionalsmacrosmulticolumntablesxstring

I have the following code with a command that creates rows for a tabular:

\documentclass[letterpaper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xstring}

\newcommand*{\condrow}[2]{
\IfEqCase{#1}{
 {l}{#2 & & \\}
 {r}{ & & #2 \\}
 {c}{ & #2 & \\}
 {m}{\multicolumn{3}{c}{#2} \\}
}[\PackageError{condrow}{Undefined option: #1}{}]
}

\begin{document}
\begin{tabular}{|c|c|c|}
 \condrow{l}{left}
 \condrow{r}{right}
 \condrow{c}{center}
 \multicolumn{3}{c}{manual multi} \\ % WORKS
 \condrow{m}{condrow multi} % FAILS
\end{tabular}
\end{document}

While using the \multicolumn directly in the tabular works, using \multicolumn through the command \condrow fails.
My guess is that \IfEqCase in \condrow prepends something to the beginning of the \multicolumn command, triggering the following error:

! Misplaced \omit.
\multispan ->\omit 
                   \@multispan 
l.20  \condrow{m}{condrow multi}
                                 % FAILS

Prepending \\ before \multicolumn in the command makes the error disappear, but inserts an empty row.
The problem Misplaced \omit. \multispan with \newcommand is similar. However, the answers do not solve my case. I have tried using the ifthen package for branching without success.

Any ideas how to fix this?

Best Answer

An expl3 solution:

\documentclass[letterpaper,10pt]{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\condrow}{ m m }
 {
  \str_case:nnF { #1 }
   {
    { l } { #2 &    &    \\ }
    { r } {    &    & #2 \\ }
    { c } {    & #2 &    \\ }
    { m } { \multicolumn{3}{c}{#2} \\ }
   }
   { \PackageError{condrow}{Undefined~option:~#1}{<explanation>} }
 }
\ExplSyntaxOff

\begin{document}
\begin{tabular}{|c|c|c|}
  \condrow{l}{left}
  \condrow{r}{right}
  \condrow{c}{center}
  \condrow{m}{condrow multi}
\end{tabular}
\end{document}

This has the advantage of being more easily extended to accept new options.

enter image description here