At the start of an alignment cell, TeX is looking for an \omit
or \noalign
, rather than being in math mode. The way to deal with this is to insert a \relax
.
\documentclass{article}
\newcommand{\signal}[1]{%
\relax
\ifmmode
1#1
\else
2$#1$
\fi
}
\begin{document}
\begin{displaymath}
\begin{array}{cc}
\signal{a} \signal{b} & \signal{c} \signal{d} \\
\end{array}
\end{displaymath}
\end{document}
Now, the issue with that is that it will mess up kerning if used in other places (it puts a \relax
in even when it's not needed). In some LaTeX3 work, we've experimented with an approach to 'pick up' the alignment cell using e-TeX. At one point we had something very elaborate to test for this
\documentclass{article}
\newcommand{\signal}[1]{%
\ifnum\currentgrouptype = 6 %
\ifnum\lastnodetype = 0 %
\else
\ifnum\lastnodetype = 7 %
\else
\relax
\fi
\fi
\fi
\ifmmode
1#1
\else
2$#1$
\fi
}
\begin{document}
\begin{displaymath}
\begin{array}{cc}
\signal{a} \signal{b} & \signal{c} \signal{d} \\
\end{array}
\end{displaymath}
\end{document}
This uses the various diagnostics from e-TeX to pick up the start of a cell. However, this is really not a good idea as it pretends to be expandable but does insert \relax
: not expandable! A much cleaner approach is to use \protected
\documentclass{article}
\protected\def\signal#1{%
\ifmmode
1#1
\else
2$#1$
\fi
}
\begin{document}
\begin{displaymath}
\begin{array}{cc}
\signal{a} \signal{b} & \signal{c} \signal{d} \\
\end{array}
\end{displaymath}
\end{document}
as e-TeX does not expand protected macros here.