[Tex/LaTex] Problem with itemize and ifthenelse in a tabular environment

itemizetables

I want to fill a cells of a table with only a \item command, and the \begin{itemize} should be automatically inserted.
The following code works :

\newenvironment{matable}[0]{%
\begin{tabular}{l|p{5cm}}
theme & sujets \\ \hline\hline }
{\end{tabular}}

\newcommand{\messujets}[1]{ & \begin{itemize} #1 \end{itemize}\\ \hline}

\begin{matable}
\messujets{\item subject 1 \item subject 2}
\end{matable}

With the command \messujets I start a new line in the table, and I fill the second cell with my \item.
But if the argument of \messujets happens to be empty, LaTeX complains about a missing \item.
To avoid this, I want to have an \ifthenelse test in the cell. My attempt is to replace:

\begin{itemize} #1 \end{itemize}

by

\ifthenelse{\equal{#1}{}}{\begin{itemize} #1 \end{itemize}}

So, it would be :

\newcommand{\messujets}[1]{ & \ifthenelse{\equal{#1}{}}{}{\begin{itemize} #1 \end{itemize}}\\ \hline}

But this produces an error use of \@item does not match its definition which I do not understand…

Any idea?

Best Answer

The reason for the problem is that the \equal argument is a so called moving argument. That means it will break if it contains in its argument commands that are fragile and not protected by \protect. And because of the optional argument \item is fragile.

In short using \ifthenelse here makes your life unnecessarily complicated as you would need to write something like

\messujets{\protect\item subject 1 \protect\item subject 2}

For that reason a better approach is to use a lowlevel solution where you test for the argument being empty in a way that nothing is getting expanded and breaks. There are a number of ways to do this, e.g.,

\ifx\foo#1\foo ...\else ... \fi

This will work as long as the argument #1doesn't start with \foo so by selecting some internal command for \foo you can make sure that this is the case.

Related Question