\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}
答案1
我不确定目的到底是什么。无论如何
\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}
答案2
OP 回答了自己的问题。由 @egreg 的评论提示并使用这个帖子,我发现这有效。
\NewDocumentCommand{\funcD}{m}{%
\ifx{#1\@empty}{}\else\begin{aligned}#1\end{aligned}\fi
}