# Different command definitions with and without optional argument renders incorrectly

I used answers given to this question (in particular, this answer by egreg) to come up with the following code:

\documentclass{article}

\makeatletter
\def\ifemptyarg#1{%
\if\relax\detokenize{#1}\relax % H. Oberdiek
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother

\newcommand{\InfinityCircle}[1][]{%
\ifemptyarg{#1}
{S^1_\infty}%
{S^1_\infty\left(#1\right)}%
}

\begin{document}
\begin{enumerate}
\item Correct: $\InfinityCircle$
\item Incorrect: $\InfinityCircle{\lambda}$
\item What (2) should be: $\InfinityCircle(\lambda)$
\end{enumerate}
\end{document}


As mentioned in the output, however, the command isn't quite correct as it seems to be ignoring the parentheses in its definition:

Regrettably, I don't understand the back-end enough to know why this is happening or how to fix it. Can someone help me get the desired output and/or to show an alternative method better-suited for what I'm trying to accomplish? Note: I really want to have a method which avoids loading any packages, if at all possible.

• Optional arguments for \newcommand macros are given with [...] brackets, not with {...} -- I assume, this is a typo – user31729 Mar 28 '16 at 21:25

Well, the command checks for an empty argument, in this case the empty argument is an optional argument and should be given as \InfinityCircle[\lambda], not \InfinityCircle{\lambda}.

The command definition of \newcommand{\foo}[1][] will give only an non-empty argument, if the optional [...] are given. [] however is still empty.

In my point of view, there should some negative space after the \infty or just use (...) instead of \left(...\right).

\documentclass{article}

\makeatletter
\def\ifemptyarg#1{%
\if\relax\detokenize{#1}\relax % H. Oberdiek
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother

\newcommand{\InfinityCircle}[1][]{%
\ifemptyarg{#1}
{S^1_\infty}%
{S^1_\infty\left(#1\right)}%
}

\begin{document}
\begin{enumerate}
\item Correct: $\InfinityCircle$
\item Incorrect: $\InfinityCircle[\lambda]$
\item What (2) should be: $\InfinityCircle(\lambda)$
\end{enumerate}
\end{document}


Update Version without optional argument

\documentclass{article}

\makeatletter
\def\ifemptyarg#1{%
\if\relax\detokenize{#1}\relax % H. Oberdiek
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother

\newcommand{\InfinityCircle}[1][]{%
\ifemptyarg{#1}{S^1_{\infty}}%
{S^1_{\infty}\left(#1\right)}%
}

\newcommand{\OtherInfinityCircle}[1]{%
\ifemptyarg{#1}{S^1_{\infty}}%
{S^1_{\infty}\left(#1\right)}%
}

\begin{document}
\begin{enumerate}
\item Correct: $\InfinityCircle$
\item Correct: $\InfinityCircle[\lambda]$
\item Also correct: $\OtherInfinityCircle{}$
\item Also correct: $\OtherInfinityCircle{\lambda}$
\end{enumerate}
\end{document}


UPDATE -- Bad command/coding style with optional {} delimited - argument ahead ;-)

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\InfinityCircle}{g}{%
\IfValueTF{#1}{%
S^1_{\infty}\left(#1\right)%
}{%
S^1_{\infty}%
}%
}

\begin{document}
\begin{enumerate}
\item Correct: $\InfinityCircle$
\item Correct: $\InfinityCircle{\lambda}$
\end{enumerate}
\end{document}

• That explains it perfectly! Follow-up question: Is there a related construction that will still allow me to use {...} instead of [...]? It's clearly a non-issue but I'm afraid I'll forget to make the swap later as I'm (a) used to arguments being in braces rather than brackets, and (b) using the same command with braces across lotsssss of tex files. :\ – cstover Mar 28 '16 at 21:29
• @cstover: Well, this more tricky, unless you're willing to provide an empty {} pair after the command if you want the non-parenthesis version – user31729 Mar 28 '16 at 21:35
• I would be okay with this! Until recently, I had no need for the non-parenthesis version, so adding and empty {} would be a much smaller-scale implementation than changing {...} to [...] throughout. – cstover Mar 28 '16 at 21:37
• @cstover: See the update -- but use it with care. If you use \OtherInfinityCircle \beta with just white space in between will be interpreted like having {\beta} then – user31729 Mar 28 '16 at 21:40
• And - after double-checking - xparse works! :D :D :D – cstover Mar 28 '16 at 22:18