0

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.

RosesBouquet
  • 569
  • 3
  • 13
  • you are using \llap which is an \hbox so will never linebreak – David Carlisle Jan 14 '23 at 10:15
  • @David Carlisle - Another permutation of \myuline I've tried that does not use \llap (but also fails) is: \newrobustcmd*{\myuline}[1]{\uline{\phantom{#1}}\makebox[0pt][r]{\contour{white}{#1}}}. Does \makebox suffer from the same limitation? – RosesBouquet Jan 14 '23 at 10:21
  • phantom and makebox are both boxes so will not linebreak – David Carlisle Jan 14 '23 at 10:22
  • @David Carlisle - Thank you. So I'm out of ideas. I thought Alex Chan's implementation was so elegant. That resolves the issue for me. Thanks again!! – RosesBouquet Jan 14 '23 at 10:23
  • Is there any way to perform an \if test for a failed linebreak, that may then allow LaTeX to select a plain \uline, without using any of Alex Chan's code? I'm only attempting to eliminate a small percentage of cases where the linebreaks do not occur. So switching to a less elegant \uline won't diminish the larger document. – RosesBouquet Jan 14 '23 at 10:37
  • why not simply linebreak by hand and underline (if you really must) each line separately? – David Carlisle Jan 14 '23 at 10:46
  • The underlines that are in question are generally linked to the glossary, so the reader can instantly hover the cursor above the underlined text to have a quick view of the underlying definition. It all works so well and provides the reader with information on terms that are not usually within the public domain. I thought Alex Chan's code was just the icing on the cake for the thousands of such cases where I have such links! Thank you for asking. I cannot thank you enough for responding ... I've been looking at this issue for years! Thanks again! – RosesBouquet Jan 14 '23 at 11:02
  • Which engine do you use? – Skillmon Jan 14 '23 at 13:58
  • Also, \contour uses 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
  • After some research, I've found that by including the code: '\setlength{\emergencystretch}{3em}' right after \begin{document} seems to do the trick in minimizing the lack of line breaking over the right margin, as per: \url{https://tex.stackexchange.com/questions/9107/how-can-i-make-my-text-never-go-over-the-right-margin-by-always-hyphenating-or-b}. May not be a perfect fix, but works OK for now. – RosesBouquet Jan 14 '23 at 14:02
  • If you're fine with not using \contour things are linebreakable in ulem and soul (soul supports automatic hyphenation but is more fragile, ulem is pretty robust but requires manual hyphenation with \-), though the underlining will be less pretty than with \contour. – Skillmon Jan 14 '23 at 14:06
  • @Skillmon - LaTeX to create a PDF file. I'm getting the distinct impression that I've been barking up the wrong digital tree re Alex Chan's code. I'll keep his code warm for those instances where an 'overfull hbox' won't be an issue! Alex's code was just so elegant! ;-( Thanks for asking! – RosesBouquet Jan 14 '23 at 14:06

0 Answers0