Make non-breakable spaces (~) visible in the PDF and preserve their glue
Here is a possible solution:
\documentclass{article}
\usepackage{color}
\newcommand\testlinei{Lorem~ipsum dolor~sit\linebreak}
\newcommand\testlineii{Lorem~ipsum dolor~sit~skdjfh~skdhf~skfdh~sdf\linebreak}
\begin{document}\noindent\hsize 5cm
\testlinei
\testlineii
\let\oldtilde=~
\def~{\rlap{\textcolor{blue}{$\cdot$}}\oldtilde}
\testlinei
\testlineii
\end{document}
You can use leaders:
\documentclass{article}
\usepackage{color}
\newcommand\testline{Lorem~ipsum dolor~sit\linebreak no underfull}
\renewcommand~{\visiblenobreakspace{}}
\DeclareRobustCommand\visiblenobreakspace{%
\leavevmode\nobreak
\cleaders\hbox to 0.3em{\hss\textcolor{blue}{\v{}}\hss}\hskip
\ifdim\spaceskip>0pt
\spaceskip
\else
\fontdimen2\font plus \fontdimen3\font minus \fontdimen4\font
\fi
}
\begin{document}
\noindent\hsize 5cm
\testline
\end{document}
With a rule, instead:
\documentclass{article}
\usepackage{color}
\newcommand\testline{Lorem~ipsum dolor~sit\linebreak no underfull}
\renewcommand~{\visiblenobreakspace{}}
\DeclareRobustCommand\visiblenobreakspace{%
\leavevmode\nobreak
{\color{blue}%
\cleaders\hrule height 1ex depth -0.7ex\hskip
\ifdim\spaceskip>0pt
\spaceskip
\else
\fontdimen2\font plus \fontdimen3\font minus \fontdimen4\font
\fi
}%
}
\begin{document}
\noindent\hsize 5cm
\testline
\end{document}
Here are two solutions that center the dot between the two words:
\def\visiblenobreakspacesymbol{$\cdot$}
\let\invisiblenobreakspace=~
\newdimen\fontdimentwo
\newdimen\fontdimenthree
\newdimen\fontdimenfour
\protected\def\visiblenobreakspace{%
\fontdimentwo=\fontdimen2\font
\fontdimenthree=\fontdimen3\font
\fontdimenfour=\fontdimen4\font
\fontdimen2\font=\dimexpr\fontdimen2\font/2\relax
\fontdimen3\font=\dimexpr\fontdimen3\font/2\relax
\fontdimen4\font=\dimexpr\fontdimen4\font/2\relax
\invisiblenobreakspace
\hbox to 0pt{\hss\visiblenobreakspacesymbol\hss}%
\invisiblenobreakspace
\fontdimen2\font=\fontdimentwo
\fontdimen3\font=\fontdimenthree
\fontdimen4\font=\fontdimenfour}
\def~{\visiblenobreakspace}
and
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcommand{\visiblenobreakspacesymbol}{$\cdot$}
\let\invisiblenobreakspace=~
\newcounter{nbsp}
\DeclareRobustCommand{\visiblenobreakspace}{%
\stepcounter{nbsp}%
\tikzmark{{nbsp-left\arabic{nbsp}}}%
\invisiblenobreakspace
\tikzmark{{nbsp-right\arabic{nbsp}}}%
\tikz[remember picture,overlay]
\path ({pic cs:nbsp-left\arabic{nbsp}})
-- ({pic cs:nbsp-right\arabic{nbsp}})
node [midway,anchor=base] {\visiblenobreakspacesymbol};}
\AtBeginDocument{\renewcommand{~}{\visiblenobreakspace}}
Both produce the following output:
The first solution should run on any TeX engine with e-TeX available. The second solution requires two passes, but it is more powerful; a small change gives the following output:
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\let\invisiblenobreakspace=~
\newcounter{nbsp}
\DeclareRobustCommand{\visiblenobreakspace}{%
\stepcounter{nbsp}%
\tikzmark{{nbsp-left\arabic{nbsp}}}%
\invisiblenobreakspace
\tikzmark{{nbsp-right\arabic{nbsp}}}%
\tikz[remember picture,overlay]
\draw ({pic cs:nbsp-left\arabic{nbsp}}) -- +(0,-0.3ex)
-| ({pic cs:nbsp-right\arabic{nbsp}});}
\AtBeginDocument{\renewcommand{~}{\visiblenobreakspace}}