ampersand “&” causes if-else-fi to fail

Note that in your actual MWA (I like that! :-D) you are getting

enter image description here

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 [email protected] is restricted to the fourth column. You should use gdefinstead 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&#3&%
  [email protected]{#4}[email protected]@emptyrelax ./. else @rzidfi &
  [email protected]{#1}%
  [email protected]@emptyrelax % I prefer ifrelaxdetokenize{#1}relax
     [email protected]{textcolor{green}{ding{51}}&}%
  else
     [email protected]{&textcolor{red}{ding{55}}}%
  fi
  @tempa
  \
}

enter image description here

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 [email protected] idiom?

documentclass{article}

makeatletter

newcommand{admin}[4][]{%
  #2&#3&%
  ifrelaxdetokenize{#4}relax
    ./.%
  else
    #4%
  fi&%
  ifrelaxdetokenize{#1}relax
    [email protected]
  else
    [email protected]
  fi
  {Yes}{&No}%
  \%
}
makeatother

begin{document}
  begin{tabular}{@{}[email protected]{}}
    hline
    multicolumn{2}{@{}c}{Anwender}
    & multicolumn{1}{c}{RZ-ID}
    & multicolumn{2}{[email protected]{}}{LDAP} \
    multicolumn{1}{@{}c}{Vorname}
    & multicolumn{1}{c}{Nachname}
    &
    & multicolumn{1}{c}{Yes}
    & multicolumn{1}{[email protected]{}}{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.

enter image description here

Leave a Comment