I encountered a very strange problem today.
I want to fill tables with automatic generated data. Hence, I designed a macro, which takes four arguments (one being optional). Arguments No. 1 and 4 are allowed to be empty, therefore I put an \ifx#1\@empty test into my code.
This is my macro in the first version:
\newcommand{\admin}[4][]{%
#2&
\def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \texttt{\@rzid}\fi&
\def\@ldap{#1}\ifx\@ldap\@empty\relax
yes
\else
& no
\fi%
\\%
}
I was very surprised to find, that LaTeX didn't compile. This was the result:
! Extra \fi.
\\admin ...ldap @empty \relax yes \else & no \fi
\\
! Incomplete \ifx; all text was ignored after line 51.
< inserted text>
\fi
After two hours fiddling, I found out, that the ampersand causes all the trouble. This macro works without LaTeX throwing an error:
\newcommand{\admin}[4][]{%
#2&
\def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \texttt{\@rzid}\fi&
\def\@ldap{#1}\ifx\@ldap\@empty\relax
yes
\else
no
\fi%
\\%
}
The disadvantage is, that the word "No" is now standing in the wrong column. :-( My MWA (Minimal Work Around) is now as follows, but I'd like to know, what went wrong and how to avoid the additional if-clause I came up with.
\documentclass{article}
\makeatletter
%% This did not work due to the "&". Removing the ampersand will get
%% the macro working, but than the word "No" from the ifclause will be
%% printed in the wrong column.
% \newcommand{\admin}[4][]{%
% #2&
% \def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \texttt{\@rzid}\fi&
% \def\@ldap{#1}\ifx\@ldap\@empty\relax
% yes
% %% The error starts here. Comment out the next two lines, and the
% %% error vanishes. Remove the ampersand & and its also gone!
% \else
% & no
% %% This would work
% % \else
% % no
% \fi%
% \\%
% }
%% Now: this works, but you have to have an extra if-clause!
\newcommand{\admin}[4][]{%
#2&%
\def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \@rzid\fi&%
\def\@ldap{#1}\ifx\@ldap\@empty\relax
Yes
\fi
&
\newcommand{\admin}[4][]{%
#2&%
\def\@rzid{#4}\ifx\@rzid\@empty\relax ./. \else \@rzid\fi&%
\def\@ldap{#1}\ifx\@ldap\@empty\relax
Yes
\fi
&
\ifx\@ldap\@empty\relax\else
No
\fi%
\\%
\def\@ldap{#1}\ifx\@ldap\@empty\relax\else
No
\fi%
\\%
}
\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}
PS.: This is a minimal example. In my real document, the macro is way more elaborative, as I added a lot more info to the second-last and last column. Therefore, a suggestion, to use \multicolumn{2}{c}{Yes} or \multicolumn{2}{c}{NO}, spanning the last two columns to avoid the ampersand, is no option to me.


