[Tex/LaTex] What’s the difference between ~ and {\mbox{$~$}}

boxesspacing

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

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

What's the purpose of this?

Best Answer

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}

enter image description here

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}

enter image description here