# How to make xparse play nicely inside another command?

I'm trying to define a command that inserts a space at the end if necessary, and then a starred version of the same command which does not include a space at the end.

I suspect that the problem has to do with \xspace being inside another command, but I have no idea how to fix it.

Minimal example:

\documentclass{article}
\usepackage{xparse, xspace}

\NewDocumentCommand{\setInSC}{sm}{%
\textsc{#2}%
\IfBooleanTF{}{%
\xspace%
}%
}

\begin{document}

\setInSC{test}should be separated with a space

\setInSC*{test}should not be separated with a space

\end{document}

• You shouldn't be using \xspace here; just say \IfBooleanF{#1}{ }, but it's not clear why you should want this. Just use \setInSC{test} text if you want a space and \setInSC{test}text otherwise. – egreg Mar 18 '13 at 10:36
• @egreg: Well, it's reduced to make it minimal. I'm using it together with other commands and there it makes sense. But thanks to Andrew Swann, it turned out that the problem was very simle. =) – gablin Mar 18 '13 at 10:39
• I believe that \xspace is never useful when using commands with arguments, because the user has full control of the space after the arguments. – egreg Mar 18 '13 at 10:42
• @egreg is correct here, xspace makes it harder not easier for the user to control the space after the command if used in this context. – David Carlisle Mar 18 '13 at 11:05
• @egreg: That may be so, but at least the starred version deactivates the use of \xspace so some level of control is retained. =) But I agree that it may cause problems; for me, though, I find that it works fine with having it inside the command. – gablin Mar 18 '13 at 12:53

You have forgotten to test the first argument, the "star", i.e. there is no #1 in your code. Put it as the first argument to \IfBooleanTF:

\documentclass{article}
\usepackage{xparse, xspace}

\NewDocumentCommand{\setInSC}{sm}{%
\textsc{#2}%
\IfBooleanTF{#1}{}{%
\xspace%
}%
}

\begin{document}

\setInSC{test}should be separated with a space

\setInSC*{test}should not be separated with a space

\end{document}

• Ah come on! Can't believe I missed that... Thanks! ^^ – gablin Mar 18 '13 at 10:38