ampersand "&" causes if-else-fi to fail
Note that in your actual MWA (I like that! :-D
) you are getting
which probably isn't the intended result. The reason behind it is the same as the reason why your first solution fails: cells are groups, and your \def\@ldap
is restricted to the fourth column. You should use \gdef
instead to have it working.
Personally I see no point in avoiding the double \if
at all costs but this would be a possible way
\newcommand{\admin}[4][]{%
#2&%
\def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \@rzid\fi &
\def\@ldap{#1}%
\ifx\@ldap\@empty\relax % I prefer \if\relax\detokenize{#1}\relax
\def\@tempa{\textcolor{green}{\ding{51}}&}%
\else
\def\@tempa{&\textcolor{red}{\ding{55}}}%
\fi
\@tempa
\\
}
Basically, the \if
just sets the definition of\@tempa
, which is expanded only after the \if
has been worked out.
Conditionals cannot straddle alignment cells, but you can remove the conditional before issuing &
; the technique uses \@firstoftwo
and \@secondoftwo
. See Why the \expandafter\@firstoftwo idiom?
\documentclass{article}
\makeatletter
\newcommand{\admin}[4][]{%
#2&%
\if\relax\detokenize{#4}\relax
./.%
\else
#4%
\fi&%
\if\relax\detokenize{#1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{Yes}{&No}%
\\%
}
\makeatother
\begin{document}
\begin{tabular}{@{}lllcc@{}}
\hline
\multicolumn{2}{@{}c}{Anwender}
& \multicolumn{1}{c}{RZ-ID}
& \multicolumn{2}{c@{}}{LDAP} \\
\multicolumn{1}{@{}c}{Vorname}
& \multicolumn{1}{c}{Nachname}
&
& \multicolumn{1}{c}{Yes}
& \multicolumn{1}{c@{}}{No} \\
\hline
\admin{First}{Lastname}{fl19}
\admin{Some}{Othername}{so97}
\admin[false]{Not}{Listed}{}
\hline
\end{tabular}
\end{document}
I also removed the two auxiliary macros, preferring a different safe test for emptiness.