# When to use \, { } or ~ to prohibit gobbling of a space following a macro

There are various ways to ensure that a space after a macro is not gobbled and wondering how to decide which one to use. Are there cases where it matters which solution is used.

\documentclass{article}
\begin{document}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\end{document}

-

The distinctive choice is between using a tie ~ or not. Ties are meant to keep document elements on either side joined, similar to HTML's &nbsp. Here's a couple more examples, illustrating the difference:

\documentclass{article}
\begin{document}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\TeX{ }followed by a group works.\par
\TeX\@ followed by a \verb|\@| works. \par
\TeX\space followed by \verb|\space| works.

\hrulefill

Here is some random text, as much as is needed to fill the line okay. \TeX{} followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\ followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX~followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX{ }followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\@ followed by something is ok.\par
Here is some random text, as much as is needed to fill the line okay. \TeX\space followed by something is ok.

\end{document}


Note how all spacing seems equal when no line-breaking is introduced (top part). However, as soon as a line-break is introduced, inter-word stretching/shrinking and possible hyphenation causes words to be moved so as to "optimize the display"; ties ~ avoid line-breaking at a space (by setting a very large \penalty - reducing the likelihood of contributing to the optimization, so to speak).

So, in answer to your question. "Are there cases where it matters which solution is used." Yes, but it depends on the location of the usage, as illustrated above.

For more on an actual description of ties from the TeX Book, see What is the difference in citing/referencing with or without tilde?.

The xspace package provides a means around this spacing choice by peeking ahead in the input stream in order to see whether there's a space following the control sequence. Typical usage would be something like (in a very minimal form):

\usepackage{xspace}% http://ctan.org/pkg/xspace
\newcommand{\TEX}{\TeX\xspace}


which would allow you to type \TEX followed by a space is ok. and obtain the appropriate output TeX followed by a space is ok.

-
Personally, I have always preferred {\TeX} over \TeX{}, because the latter looks like I am feeding an empty argument to the \TeX macro. Also, the former is more obviously a single unit. (When I write Norwegian and need to restrict myself to ASCII, I similarly write “blåbærsyltetøy” (blueberry jam) as bl{\aa}b{\ae}rsyltet{\o}y. Being consistent here is useful, as it lets me easily convert back to latin-1 or utf-8 without the macros.) – Harald Hanche-Olsen Jul 3 '12 at 6:48
@HaraldHanche-Olsen bl{\aa}b inhibits kerning between "l", "å" and "b", which bl\aa b doesn't. – egreg Jul 3 '12 at 6:54
@egreg: But, the kerning won’t be inhibited using LuaTeX, will it? – mhp Jul 4 '12 at 7:09
@mhp I believe that LuaTeX can insert automatic kerning in these cases, which is good. It's not the case for TeX and XeTeX. – egreg Jul 4 '12 at 7:35
@egreg: Urgh. I should have known that. To me, this is just another argument to use utf-8 (or latin-1) input encoding instead. I like my input files human readable, as far as possible. – Harald Hanche-Olsen Jul 4 '12 at 15:30

compare the output:

\documentclass{article}
\begin{document}
\minipage{1.25cm}
\TeX{} followed by curly brace is ok.\par
\TeX\ followed by a back slash is ok.\par
\TeX~followed by a tilde also works.\par
\endminipage

\bigskip
foo  bar\par
foo\ \ bar\par
foo~~bar

\end{document}


a default space -- a control space -- a non break space

A control space is required when using \nonfrenchspacing to fix the spacing after periods which are not the end of a sentence. ~ is used for 75~kg to prevent a line break between the two parts.

-
Is there any particular reason for not using the standard \begin{minipage} and \end{minipage}? – egreg May 31 '13 at 17:15