queen
queen

Reputation: 61

Spacing issues with xspace

Let's say my macro is \newcommand{\k}{king\xspace}. Then the spacings in "... said the \k." will be fine. But what do I do if I want no spacing in the middle of \k\k.? I want "kingking." not "king king."

Is there a way to do this?

Upvotes: 6

Views: 5051

Answers (3)

Ron Bandes
Ron Bandes

Reputation: 31

The xspace documentation says that the way to handle this is with {} immediately after your macro invocation:

 \k{}\k

Recent versions of xspace also permit you to specify additional macros that should not generate space after your macro:

 \xspaceaddexceptions{\k}

I wanted to use this for \xspaceaddexceptions{\textsuperscript}, but it didn't work for me since my shop has xspace v1.06 and that's not recent enough. So I used:

 \newcommand{\unix}{\textsc{unix}\xspace}
 \unix{}\textsuperscript{\textregistered}

Which worked fine except in bold section headings, since there's no bold small caps in the font that I'm using. Sigh...

Upvotes: 3

Werner
Werner

Reputation: 15105

\unskip removes any previous skip inserted (which includes a space), so using \K\unskip\K for the odd occasions when you want to remove it could also suffice:

enter image description here

\documentclass{article}
\usepackage{xspace}% http://ctan.org/pkg/xspace
\newcommand{\K}{king\xspace}
\begin{document}
Here is the \K. Here is the \K\K. Here is the \K\unskip\K.
\end{document}

Upvotes: 6

Peter Grill
Peter Grill

Reputation: 548

The whole point of \xspace is to add a space between words and not add space before punctuation. So, if you don't want spaces between the two uses of the macro don't use \xspace. But, of course this will require you to you to use a {} at the end:

\documentclass{article}
\newcommand{\K}{king}%

\begin{document}
At end of sentence \K.\par
In between \K\K{} you want one long word.
\end{document}

Upvotes: 4

Related Questions