1

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?

1 Answers1

2

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}

enter image description here

egreg
  • 1,121,712