# What's the difference between ~ and {\mbox{$~$}}

I'm looking at the source code of a book where I found:

D.{\mbox{$~$}}Knuth


What's the purpose of this?

• it looks like whoever wrote that is trying to print a "similar" symbol, perhaps to show how someone should input the name so that it won't break at the end of a line. it's a very peculiar way to do that; a verbatim string would be better suited. more context needed to allow a better guess. – barbara beeton Feb 12 '15 at 15:44
• @barbarabeeton Why would you think that someone was trying to print a \sim? I think it's a simple mistake because he doesn't know that ~ just works. The same I've seen inputing {$\left.\right.$} between \\ so the error doesn't appears. – Manuel Feb 12 '15 at 16:01
• @Manuel -- i think my mind has been polluted by looking at too much unicode lately. you've absolutely correct -- this won't print under ordinary circumstances. (i still think it's misguided. if an unstretchable space is wanted, \, is better anyhow.) – barbara beeton Feb 12 '15 at 16:18
• You do not say but the "book" you are looking at is presumably the wikibook that you asked about the other day. As such remember that the tex markup is not hand written or considered it is the automatic output of a program that is taking in highly variable wiki markup and trying to produce some usable latex to make a pdf. The tex markup is almost bound to be strange in places, getting something that generates error free tex from a wiki that size is quite an accomplishment, without worrying on using good tex style – David Carlisle Feb 12 '15 at 17:23
• @barbarabeeton not "whoever", "whatever" – David Carlisle Feb 12 '15 at 17:24

In the \mbox, the space will not be compressed (nor expanded, for that matter) by line-width considerations.

As Manuel points out and egreg emphasizes, the construct \mbox{$~$} "is really useless," as it is functionally indistinguishable from and less efficient than \mbox{ }. Nonetheless, I use it in the MWE below to most directly answer the OP's original question. Were the reader to mimic the technique, please use \mbox{ }.

\documentclass[10pt]{book}

\begin{document}
\noindent\hrulefill

\def\dx{4.2in}

\hspace{\dx}D.{\mbox{$~$}}Knuth

\hspace{\dx}D.~Knuth

\def\dx{0in}

\hspace{\dx}D.{\mbox{$~$}}Knuth

\hspace{\dx}D.~Knuth
\end{document}


To see this on a grand scale, here I use \sloppy to give LaTeX all the rope it needs to hang itself:

\documentclass[10pt]{book}
\sloppy
\begin{document}
\noindent\hrulefill
\def\dx{3.64in}

\hspace{\dx} D.{\mbox{$~$}}Knuth rocks!

\hspace{\dx} D.~Knuth rocks!
\end{document}


• Yet, unless I'm mistaken, there's no need for that much, just D.\mbox{ }Knuth. Even if you want the tie, there's no need for $..$, just \mbox{~}. – Manuel Feb 12 '15 at 16:10
• @Manuel I concur, but was addressing the particulars of the OP's question. – Steven B. Segletes Feb 12 '15 at 16:11
• Please, add to your answer that \mbox{$~$} is really useless and \mbox{ } is much better (if one really wants a non expanding space. – egreg Feb 12 '15 at 21:52

You can define a new command that uses the normal interword space with the possibility to shrink it if the line needs shrinking, but not stretching it. It's necessary to add \@ in order to properly set the space factor (the \  command does it implicitly).

In the example I use the low level \hbox spread command in order to show the effect; it typesets the text in a box stretched (or shrinked) by the stated amount.

\documentclass{article}

\DeclareRobustCommand{\nostretchspace}{%
\nolinebreak\@\hspace{\fontdimen2\font minus \fontdimen4\font}%
}

\begin{document}


Note that \mbox{ } doesn't give a good result in the third line.