Spacing issues with xspace
Asked Answered
S

3

6

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?

Showily answered 23/2, 2011 at 17:38 Comment(1)
I think this should go on tex.SE... it doesn't seem to be about the programming aspects of LaTeX.Spondylitis
R
6

\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}
Rocket answered 1/11, 2012 at 19:5 Comment(0)
H
4

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}
Hodges answered 14/10, 2011 at 0:30 Comment(0)
C
3

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...

Ceciliacecilio answered 3/3, 2014 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.