0

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.

enter image description here

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}

Peter Grill
  • 223,288
  • Any reason you don't use the tools from 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 the ifmtarg manual, 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
  • \NameExtended is 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 prefer etoolbox's tests… – cgnieder May 17 '17 at 07:48
  • xparse's manual suggests \ifblank from etoolbox or the expl3 conditional \tl_if_blank:nTF – David Carlisle May 17 '17 at 07:59
  • 1
    @DavidCarlisle \ifblank does the same as ifmtarg, it does not expand the argument, so \ifdefempty or \ifdefvoid is better. I tend to use the later – daleif May 17 '17 at 08:25
  • @daleif true but the need for expansion is self inflicted here \IfIsEmptyArg{\NameExtended}{% could be \IfIsEmptyArg{#2}{% and then you wouldn't have to expand it. – David Carlisle May 17 '17 at 08:34
  • @daleif: Thanks. I did not know about the ones form etoolbox. \ifdefempty and \ifdefvoid both seem to work in this case. From etoolbox documentation \ifdefempty seems to be what is needed. – Peter Grill May 17 '17 at 08:53
  • @DavidCarlisle: The \ShowName macro (where the \IfIsEmptyArg{\NameExtended}{%) test is, does not have parameters, so that won't work. However, I had tried modifying \SetTitle to \IfIsEmptyArg{#2}{\renewcommand*{\NameExtended}{}}{\renewcommand*{\NameExtended}{#2}} but that too did not work. – Peter Grill May 17 '17 at 08:56
  • @PeterGrill I rewrote my LaTeX version of my university letter class using etoolbox saved me soo much code and strange expansions. – daleif May 17 '17 at 09:06

0 Answers0