9

I am trying to define tabular rows using a macro which shall perform an conditional definition of the tabular row according to a given parameter. Please consider the following MWE:

\documentclass{article}

\newcommand{\test}[3]{
    \ifnum\numexpr#1\relax=0
        #2 & %#3
    \else
        #2 & #3
    \fi
}

\begin{document}

    \begin{tabular}{ll}
        \test{0}{col1-1}{col1-2} \\
        \test{1}{col2-1}{col2-2} \\
    \end{tabular}

\end{document}

Removing the comment in the fifth line (#2 & %#3#2 & #3) leads to the error message Incomplete \ifnum; all text was ignored after line 14. Even after hours of searching the web I am not able to solve this issue.

What am I doing wrong?

Edit

@David Carlisle, @egreg, and @Francis: Thank you all very much for your answers. I tried all of your examples, and they all solve my issue. Although I think that the solution of @egreg is the formally accurate way to go, I consider the solution of @David Carlisle to be more elegant for my use case just for the sake of simplicity and brevity, while considering the answer of @Francis as a workaround. So I decided to mark the answer of @David Carlisle as the accepted one, without wanting to neglect the other answers.

Thank you very much for your help!

10

You can skip & inside the conditional, you just have to hide it a bit.

enter image description here

\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}
  • What about \cr (or \\)? – egreg Sep 2 '13 at 20:29
  • @egreg \cr you can use the same trick and \\ is Ok anyway as it's not magic – David Carlisle Sep 2 '13 at 20:31
8

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.

  • Are there more information about how this token works? Maybe in Tex by Topic? – Francis Sep 2 '13 at 20:19
4

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:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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