4

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:

enter image description here

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

  • Just don't use \xspace; \NP is math, so $\NP$ will avoid all problems with spaces in text. – egreg Oct 8 '13 at 16:51
  • @egreg I thought the purpose of \ensuremath is 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 8 '13 at 16:57
5

Package xspace checks the next token to decide, whether to set or suppress a space. New recognized tokens for omitting the space can be added by \xspaceaddexceptions. The next token is expanded and each expansion step checked. This can be used for "= that consists of two tokens. At some expansion step it expands to one token \ngerman@sh@"@=:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{xspace}[2004/12/07]

\expandafter\xspaceaddexceptions\expandafter{%
  \csname ngerman@sh@"@=@\endcsname
}

\newcommand{\NP}{\ensuremath{\mathsf{NP}}\xspace}

\begin{document}
  \noindent
  $L \in \NP$, daher foo.\\
  $L$ ist in \NP. \\
  $L$ ist \NP"=vollständig.
\end{document}

Result

| improve this answer | |
  • Nice, though something seems to be off. Adding this one rule seems to add exceptions for other dashes like "~ (or custom-made "/), and also gibberish like "(. Does it only require a " and then some other character? The rule does not seem to apply to ". (The version parameter for xspace does not seem to be neccessary?) – Raphael Oct 14 '13 at 10:19
  • @Raphael: Older versions of xspace do not provide \xspaceaddexceptions. – Heiko Oberdiek Oct 15 '13 at 8:48
  • @Raphael: The same trick also works for other shorthands: "~ is supported by \expandafter\xspaceaddexceptions\expandafter{\csname ngerman@sh@"@\string~\endcsname}. Adding the shorthand " does not seem a good idea to me. If a word starts with an umlaut, there should be a space before. – Heiko Oberdiek Oct 15 '13 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.