problem with \ifthenelse + multicolumn
If you are an old timer and not using e* tools then you need to hide the conditionals from TeX's alignment scanner. This is tricky in general but if you know your test is always the first thing in a row (ie always in the first cell in a row) then you can do the tests in a \noalign
and come out with a suitably defined temporary macro.
This seems to work for your example
\documentclass{article}
\usepackage{multirow}
\usepackage{ifthen}
\newcommand{\essai}[1]{
\begin{tabular}{cc}
a&b \\ \hline
\noalign{%
\ifthenelse{\equal{#1}{coucou}}%
{\gdef\hmm{\multicolumn{2}{|c|}{oui - #1} \\ \hline}}%
{\gdef\hmm{non \\ \hline}}%
}\hmm
\end{tabular}
}
\begin{document}
\essai{coucou}
\essai{xoucou}
\end{document}
\multicolumn
must be the first thing TeX sees in a table cell, after expanding commands; unfortunately, the workings of \ifthenelse
leave something before \multicolumn
when the test is computed false.
Use a different test making command: in this case, \ifboolexpe
from etoolbox
(notice the final "e"), might seem promising.
Unfortunately, also the way etoolbox
implements the string equality test leads to unexpandable commands before TeX can see \multicolumn
, so a direct approach must be used:
\documentclass{article}
\makeatletter
\newcommand{\roystreqtest}[2]{%
\ifnum\pdfstrcmp{#1}{#2}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother
\newcommand{\essai}[1]{%
\begin{tabular}{cc}
a&b \\ \hline
\roystreqtest{#1}{coucou}%
{\multicolumn{2}{|c|}{oui - #1} \\ \hline}
{non \\ \hline}
\end{tabular}%
}
\begin{document}
\essai{coucou}
\essai{noncoucou}
\end{document}
If you have to compare strings that are not made only of ASCII printable characters, it's safer to say
\newcommand{\roystreqtest}[2]{%
\ifnum\pdfstrcmp{\detokenize{#1}}{\detokenize{#2}}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
If you're bold, you can use an already defined infrastructure: LaTeX3 provides an expandable equality test for strings: the definition of \roystreqtest
can be
\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \roystreqtest \str_if_eq:nnTF
\ExplSyntaxOff
and the rest would be the same.