Test which macro is called in tabular

I want to test if a certain macro is called in a tabular.

The MWE illustrates that. But the result is always 'No'.

How can I achieve what I want, without changing macro \Cl (which is just a simplified example)?

\documentclass{article}
\usepackage{collcell}

\newcolumntype{P}{>{\collectcell\foo}c<{\endcollectcell}}

\def\foo#1{\ifx#1\Cl Yes\else No\fi}

\def\Cl{\textbf{Cl}}

\begin{document}

\begin{tabular}{cPc}
a & b & c\\
d & \Cl & d\\
\end{tabular}

\end{document}


The macro \foo never gets to see \Cl. The reason is that when an alignment cell is started, TeX expands tokens in order to look for \omit (a primitive), which is what \multicolumn is based on.

The actual argument that's passed to \foo in the second row is \protect\textbf•{Cl} (the bullet means a space in the name of the macro): \Cl gets expanded once to \textbf{Cl} and \textbf{Cl} is expanded to \protect\textbf•{Cl}; since here \protect is equivalent to \relax, the search stops, but it's too late for \foo to see the real thing.

Just protect your \Cl macro.

\documentclass{article}
\usepackage{collcell}
\usepackage{etoolbox}

\newcolumntype{P}{>{\collectcell\foo}c<{\endcollectcell}}

\newcommand\foo[1]{\ifx#1\Cl Yes\else No\fi}

% we don't want \Cl gets expanded when looking for \omit
\newrobustcmd\Cl{\textbf{Cl}}

\begin{document}

\begin{tabular}{cPc}
a & b & c\\
d & \Cl & d\\
\end{tabular}

\end{document}


• (+1) I've learnt something today! :o) – Bernard Mar 15 '18 at 10:38
• (+2) I implemented it and it works smoothly. Thanks a lot! – Jack Mar 15 '18 at 21:25