1
\documentclass{article}
\usepackage{xparse,amsmath,xifthen}

\NewDocumentCommand{\funcA}{m}{%
    \begin{aligned}#1\end{aligned}
}
\NewDocumentCommand{\funcB}{m}{%
    \ifthenelse{1=1}{}{\begin{aligned}#1\end{aligned}}
}
\NewDocumentCommand{\funcC}{m}{%
    \ifthenelse{\isempty{#1}}{}{\begin{aligned}#1\end{aligned}}
}

\begin{document}
    \begin{align*}
        \funcA{a} % works
        \funcA{&a} % works
        \funcB{a} % works
        \funcB{&a} % works
        \funcC{a} % works
        \funcC{&a} % doesn't work
    \end{align*}
\end{document}
mjc
  • 745

2 Answers2

1

I'm not sure what the aim really is. Anyway

\documentclass{article}
\usepackage{amsmath}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later

\ExplSyntaxOn

\NewDocumentCommand{\func}{O{c}m} { \tl_if_blank:nF { #2 } { \begin{aligned}[#1]#2\end{aligned} } }

\ExplSyntaxOff

\begin{document}

\begin{align} x+y&= \func{a} % works \ x+y&= \func{x&=a\y&=b} % works \ x+y&= \func[t]{x&=a\y&=b} % works \ x+y&= \func{} % works \ x+y&= \func{ } % works \end{align}

\end{document}

enter image description here

egreg
  • 1,121,712
0

OP answering own question. Prompted by a comment by @egreg and using this post, I've found that this works.

\NewDocumentCommand{\funcD}{m}{%
    \ifx{#1\@empty}{}\else\begin{aligned}#1\end{aligned}\fi
}
mjc
  • 745
  • 1
    You're comparing { with the first token in #1. This will definitely not work as intended. Try with \funcD{{a}} and see. The syntax for \ifx is quite different from what you seem to think. – egreg Apr 16 '21 at 15:14