\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{contour}
\usepackage[showframe=true]{geometry}
\usepackage{titlesec}
\usepackage{ulem}
% See: "Four ways to underline text in LATEX" at \url{https://alexwlchan.net/2017/10/latex-underlines/}
\renewcommand{\ULdepth}{1.8pt}
\contourlength{0.8pt}
\newcommand{\myuline}[1]{%
\uline{\phantom{#1}}%
\llap{\contour{white}{#1}}%
}
% See: "Using llap in titleformat prevents line breaking" for egreg's answer on how to use '\makebox[0pt][r]{...}' to replace '\llap' at \url{https://tex.stackexchange.com/questions/277716/using-llap-in-titleformat-prevents-line-breaking}
\newcommand{\mXuline}[1]{%
\uline{\phantom{#1}}%
\makebox[0pt][r]{\contour{white}{#1}}%
}
\begin{document}
The \textbf{myuline} function provides a really nice underline, drawn on a consistent level, flush against the bottom of the text, with gaps as appropriate for descenders. However, it does NOT break at the end of the printing space where it should.\
\textbf{myuline:} \myuline{A happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long title}\
The \textbf{mXuline} function (using egreg's \verb|\makebox[0pt][r]{...}| to replace \verb|\llap{...}|) provides a really nice underline, drawn on a consistent level, flush against the bottom of the text with gaps as appropriate for descenders. However, it ALSO does NOT break at the end of the printing space where it should.\
\textbf{mXuline:} \mXuline{A happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long title}\
The \textbf{uline} function underlines the text and it breaks at the end of the printing space where it should. However, uline does NOT draw the underline on a consistent level, flush against the bottom of the text, with gaps as appropriate for descenders.\
\textbf{uline:} \uline{A happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long happy long title}\
\textbf{Question: How can we create underline text flush against the bottom of the text, with gaps as appropriate for descenders, but that ALSO breaks at the end of the printing space where it should.}\
Normal text.
\end{document}
\llapwill definitely not allow line breaks. – egreg Jan 25 '22 at 17:10\contourfeature necessary? – Skillmon Jan 29 '22 at 11:09