# Can I inject \xspace in all commands from the complexity package?

The complexity package is a very useful one for a Computer Scientist. Recently, I've discovered the xspace package, which provides the \xspace macro that helps defining macros that do not need the \ escape to avoid globbing a subsequent whitespace:

\newcommand{\myname}{Nicola\xspace}
...
\myname is walking on the street % No need to write \myname\ or \myname{}


Now, I use a lot of commands from the complexity package, which defines tens of commands that are commonly used inside text so that the need of escaping is very frequent. I would like to have them use \xspace by default, but redefining, say, \ComplexityFont does not work, because apparently \xspace must be put exactly at the end of the macro being invoked (not a macro expanded from it). The package defines all its macros with simple \newcommands, so I don't think there is a way to hook into it.

Is this redefinition possible to automate in some way, or should I suggest this feature to the package author directly?

• From the manual: “Thus, it’s best to always use complexity commands inside math mode.” – egreg Feb 18 '16 at 9:35
• Didn't think about that. But if I have to write $\NP$ I could as well write \NP{}. Does \xspace cause problems in math mode? If not, it could still be a nice thing to do. – gigabytes Feb 18 '16 at 9:37
• It's never a good idea to use \xspace. ;-) – egreg Feb 18 '16 at 9:38
• And, looking at the package, you also must train into using $\protect\NP$ whenever \NP appears in a section title. – egreg Feb 18 '16 at 9:44
• – egreg Feb 18 '16 at 9:54

Since essentially all commands are defined in terms of \ComplexityFont, it is quite easy:

\documentclass{article}
\usepackage[small]{complexity}
\usepackage{etoolbox,xspace}

\robustify\ComplexityFont
\apptocmd\ComplexityFont{\xspace}{}{}

\begin{document}

\section{An \NP problem}

An \NP problem

\end{document}


I also robustified the main command, so you don't need \protect in front of the complexity macros when in moving arguments, such as those of \section and \caption.

However, my advice is to follow what the author says in the manual:

Thus, it’s best to always use complexity commands inside math mode.

Read Drawbacks of xspace for reasons why not using \xspace.

\documentclass{article}
\usepackage[small]{complexity}
\usepackage{etoolbox}

\robustify\ComplexityFont

\begin{document}

\section{An $\NP$ problem}

An $\NP$ problem

\end{document}


• +1 for having solved the problem in the question even if in disagree with the goal :) – gigabytes Feb 18 '16 at 10:08
• Btw, the same is needed for \lang and \func, because \ComplexityFont is only used for class names. – gigabytes Feb 18 '16 at 10:16
• @gigabytes That's highly possible. – egreg Feb 18 '16 at 10:18
• @gigabytes he always likes a chance to use my packages. – David Carlisle Feb 18 '16 at 14:29