Following David Carlisle's answer in How to underline text from a macro including a line break?, I am attempting to adapt David Carlisle's use of \expandafter to allow linebreaks (when underlining text using \uline from the soul package) to Alex Chan's implementation of 'a really nice underline' on his webpage Four ways to underline text in LATEX.
\documentclass{scrreport}
\usepackage[showframe]{geometry}
\usepackage{soul, ulem}
% From Alex Chan's web page 'Four ways to underline text in LATEX' *
\usepackage{contour}
\renewcommand{\ULdepth}{1.8pt}
\contourlength{0.8pt}
% From Alex Chan's web page 'Four ways to underline text in LATEX' *
\newcommand{\myuline}[1]{%
\uline{\phantom{#1}}%
\llap{\contour{white}{#1}}%
}
% My 1st permutation of David Carlisle's code: \mXuline
\newcommand{\mXuline}[1]{%
\expandafter\uline{\phantom{#1}}%
\llap{\contour{white}\expandafter{#1}}%
}
% My 2nd permutation of David Carlisle's code: \mZuline
\newcommand{\mZuline}[1]{%
\expandafter\uline{\expandafter\phantom{#1}}%
\expandafter\llap{\contour{white}\expandafter{#1}}%
}
%%% In class definition:
% Default value
\def\printtitle{default title}
% Provide command for user to set own title
\newcommand{\settitle}[1]{\def\printtitle{1) #1}}%
%%% In preamble to the document --> visible to the user
%\settitle{my own title}
\settitle{long title for testing long title for testing long title for testing long title for testing long title for testing }
\begin{document}
\vspace{5mm}
David Carlisle's answer incorporates \verb|\expandafter| to allow for line-breaks when underlining with macro expansion using the \verb|\uline| command from the soul package.
% \uline with \expandafter
\expandafter\uline\expandafter{\printtitle}
\vspace{5mm}
Using Alex Chan's code, I would like my underlines ( using \verb|\uline{}| ) to be drawn "on a consistent level, flush against the bottom of the text, with gaps as appropriate for descenders" as shown immediately below, but also breaking at the end of the line which it currently does not do.
\vspace{5mm}
% \uline using \myuline from Alex Chan's web page 'Four ways to underline text in LATEX' *
\myuline{2) long title for testing long title for testing long title for testing long title for testing long title for testing}
\vspace{5mm}
My 1st permutation of David Carlisle's answer incorporating \verb|\expandafter| to allow for line-breaks when underlining using Alex Chan's code; it fails to break at the end of the line.
% \uline using \mXuline - 1st attempt to implement \expandafter akin to David Carlisle's solution
\mXuline{3) long title for testing long title for testing long title for testing long title for testing long title for testing}
\vspace{5mm}
My 2nd permutation of David Carlisle's answer incorporating \verb|\expandafter| to allow for line-breaks when underlining using Alex Chan's code; it also fails to break at the end of the line.
% \uline using \mZuline - 2nd attempt to implement \expandafter akin to David Carlisle's solution
\mZuline{4) long title for testing long title for testing long title for testing long title for testing long title for testing}
\end{document}
% https://alexwlchan.net/2017/latex-underlines/
I've tried a few perturbations of David Carlisle's \expandafter code to achieve Alex Chan's implementation of 'a really nice underline', but to no avail.
Any thoughts to supplement my limited LaTeX skill set would be appreciated.
Note: I am not using LuaLaTeX, so the answer posted by Marcel Krüger is not available to me.
\llapwhich is an\hboxso will never linebreak – David Carlisle Jan 14 '23 at 10:15\contouruses boxes as well, as long as you intend to use it you won't be able to have it line breakable. – Skillmon Jan 14 '23 at 14:00\contourthings are linebreakable inulemandsoul(soulsupports automatic hyphenation but is more fragile,ulemis pretty robust but requires manual hyphenation with\-), though the underlining will be less pretty than with\contour. – Skillmon Jan 14 '23 at 14:06