\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}
Asked
Active
Viewed 82 times
1
mjc
- 745
2 Answers
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}
egreg
- 1,121,712

{with the first token in#1. This will definitely not work as intended. Try with\funcD{{a}}and see. The syntax for\ifxis quite different from what you seem to think. – egreg Apr 16 '21 at 15:14