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?

2
  • Just don't use \xspace; \NP is math, so $\NP$ will avoid all problems with spaces in text.
    – egreg
    Oct 8, 2013 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, 2013 at 16:57

1 Answer 1

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

3
  • 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, 2013 at 10:19
  • @Raphael: Older versions of xspace do not provide \xspaceaddexceptions. Oct 15, 2013 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. Oct 15, 2013 at 8:49

You must log in to answer this question.

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