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:

\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, 2016 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. Feb 18, 2016 at 9:37
  • 1
    It's never a good idea to use \xspace. ;-)
    – egreg
    Feb 18, 2016 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, 2016 at 9:44
  • 3
    See Drawbacks of \xspace
    – egreg
    Feb 18, 2016 at 9:54

1 Answer 1


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




\section{An \NP problem}

An \NP problem


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.




\section{An $\NP$ problem}

An $\NP$ problem


enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .