Defining tabular rows with macros using `\ifnum` leads to `Incomplete \ifnum`
You can skip &
inside the conditional, you just have to hide it a bit.
\documentclass{article}
\newcommand{\test}[3]{%
\ifnum\numexpr#1\relax=0
AA#2 \uppercase{&} BB#3
\else
CC#2 \uppercase{&} DD#3
\fi
}
\begin{document}
\begin{tabular}{l|l}
\test{0}{col1-1}{col1-2} \\
\test{1}{col2-1}{col2-2} \\
\end{tabular}
\end{document}
You can't start a conditional in a table cell and end it in another, because TeX inserts an inaccessible token that signals the end of a cell and which is not allowed in text skipped in a conditional.
The usual trick is to do all the conditional and then execute one of the two codes:
\documentclass{article}
\makeatletter
\newcommand{\test}[3]{%
\ifnum\numexpr#1\relax=0
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#2 & }
{#2 & #3}
}
\makeatother
\begin{document}
\begin{tabular}{ll}
\test{0}{col1-1}{col1-2} \\
\test{1}{col2-1}{col2-2} \\
\end{tabular}
\end{document}
See What do \@firstoftwo and \@secondoftwo do? for more information.
In tabular environment &
cannot be skipped, so when your condition is true LaTeX will do the following: it scans #2 & #3
, then when it encounter \else
the expansion stops, however, when the second &
is met the tabular environment starts expansion again, so the output becomes #2 & #3 & #3
. It can be fix by building new control sequence, for example:
\documentclass{article}
\newcommand{\testa}[2]{#1 & #2}
\newcommand{\testb}[2]{#1 & #2}
\newcommand{\test}[3]{
\ifnum\numexpr#1\relax=0
\testa{#2}{#3}
\else
\testb{#2}{#3}
\fi
}
\begin{document}
\begin{tabular}{ll}
\test{0}{A}{B} \\
\test{1}{B}{A} \\
\end{tabular}
\end{document}
gives you: