Assume we have some mathematical object that is used in formulae as well as in "natural" language (in my case, complexity classes). When defining a macro for such an object, I like to use \xspace so that spacing works as expected in all circumstances.
That has been working fine for me until I discovered the nice hyphenation control features of babel; \xspace seems to conflict with these:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{xspace}
\newcommand{\NP}{\ensuremath{\mathsf{NP}}\xspace}
\begin{document}
$L \in \NP$, daher foo.\\
$L$ ist in \NP. \\
$L$ ist \NP"=vollständig.
\end{document}
The combination with "= -- which would imply the breaking/hyphenation desired in this case -- leads to an obviously wrong result:

I notice that I have probably been overusing \xspace but I don't know a different way. How can I fix this?

\xspace;\NPis math, so$\NP$will avoid all problems with spaces in text. – egreg Oct 08 '13 at 16:51\ensuremathis to avoid having to use math delimiters in such cases? I have been enjoying being able to drop them. :/ Nevertheless, adding dollars in just these contentious places does solve the issue (even with\xspace). Maybe add an answer? – Raphael Oct 08 '13 at 16:57