2

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:

enter image description here

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.

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

1 Answers1

4

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}

enter image description here

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 –  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 –  Mar 28 '16 at 21:40
  • @cstover: You're welcome -- by the way: Using xparse package you would have the opportunity to use {...} as an optional argument indicator. This would even allow to omit them, instead of providing them as an empty pair... but you don't want to use additional packages ;-) –  Mar 28 '16 at 21:50
  • Ooooooh! Honestly, I don't know if I need to avoid xparse (or other packages) but for a bit of background: This is for my dissertation and my University's (only acceptable) .cls file is horrendous about playing nicely with (even the simplest) packages. I'll explore some and see what my options are...and please feel free to expand your answer to include an xparse implementation, too, in the case that everything plays nicely on my end. ;) – cstover Mar 28 '16 at 21:54
  • @cstover: See the update, for the time after your thesis is finished ;-) –  Mar 28 '16 at 21:59
  • You are officially my favorite person of the week! And it's only Monday! :D :D :D (but in all seriousness: Thanks a ton!) – cstover Mar 28 '16 at 22:07
  • 1
    And - after double-checking - xparse works! :D :D :D – cstover Mar 28 '16 at 22:18
  • @ChristianHupfer: May I suggest using \mleft(#1\mright), from the mleftright package, to get rid of the spurious space before the parenthesis? – GuM Mar 28 '16 at 23:56
  • @GustavoMezzetti: I had mleftright package loaded in one of my posted versions (see the edits, if you like), but I tried with \! instead. And I stated to use \! or different methods in my post –  Mar 29 '16 at 07:38