I must be missing something obvious here, but I can't see it. The \SetTitle nacro defines a title along with an extended title. The extended title is printed in parenthesis, if it is provided. Thus \SetTitle[$\sin$]{Sine Function} should produce
Sine Function (sin)
and \SetTitle[]{Sine Function} should produce
Sine Function
but I get the trailing () for some reason.
Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{ifmtarg}
\makeatletter
\newcommand{\IfIsEmptyArg}[3]{@ifmtarg{#1}{#2}{#3}}%
\makeatother
\DeclareMathOperator{\MyOperator}{op}%
\newcommand{\NameExtended}{}
\newcommand{\Name}{}
\NewDocumentCommand{\SetTitle}{%
s% #1 = UNUSED in this MWE
O{}% #2 = extended name
m% #3 = name
}{%
\renewcommand{\Name}{#3}%
\renewcommand{\NameExtended}{#2}%
}%
\newcommand{\ShowName}{%
\par\hspace{1.0cm}\Name
\IfIsEmptyArg{\NameExtended}{%
%% Since \NameExtended not provided don't print it.
}{%
~(\NameExtended)%
}%
}%
\begin{document}
\SetTitle[$\sin$]{Sine Function}
\ShowName
\SetTitle[$\MyOperator$]{My Operator}
\ShowName
\medskip\par
Following should NOT have trailing "()":
\SetTitle{Sine Function}% Default parameter not specified
\ShowName
\SetTitle[]{My Operator}% Default parameter specified
\ShowName
\end{document}

etoolbox, they are IMO a lot more powerfull, including methods to see if a macro is really empty. If you look at the examples in theifmtargmanual, none seems to be expanding the argument (haven't looked at the implementation). I tend to use\ifdefvoid{}{}{}– daleif May 17 '17 at 07:30\NameExtendedis not nothing. You want to test if the expansion of it is empty or not:\expandafter\@ifmtarg\expandafter{\NameExtended}{x}{y}. Personally like @daleif I also preferetoolbox's tests… – cgnieder May 17 '17 at 07:48\ifblankdoes the same asifmtarg, it does not expand the argument, so\ifdefemptyor\ifdefvoidis better. I tend to use the later – daleif May 17 '17 at 08:25\IfIsEmptyArg{\NameExtended}{%could be\IfIsEmptyArg{#2}{%and then you wouldn't have to expand it. – David Carlisle May 17 '17 at 08:34etoolbox.\ifdefemptyand\ifdefvoidboth seem to work in this case. From etoolbox documentation\ifdefemptyseems to be what is needed. – Peter Grill May 17 '17 at 08:53\ShowNamemacro (where the\IfIsEmptyArg{\NameExtended}{%) test is, does not have parameters, so that won't work. However, I had tried modifying\SetTitleto\IfIsEmptyArg{#2}{\renewcommand*{\NameExtended}{}}{\renewcommand*{\NameExtended}{#2}}but that too did not work. – Peter Grill May 17 '17 at 08:56etoolboxsaved me soo much code and strange expansions. – daleif May 17 '17 at 09:06