# Redefine \uline so \colorbox is locally defined to have underlined text, too

Initial problem:

I want to underline the text with a continuous line with the following code:

\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}


Workaround:

A solution to get a continuous line for the text inside \colorbox as well, is to do:

\sbox0{\uline{\hspace{\fboxsep}everything\hspace{\fboxsep}}}
\uline{No underlining for \colorbox{lightgray}{\hspace{-\fboxsep}\usebox0\hspace{-\fboxsep}} inside the colorbox.}


Question:

How can I redefine \uline, so every \colorbox inside \uline is locally defined as shown above in the workaround to have underlined text, too?

MCVE:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}

\begin{document}

\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}

\end{document}


• Underlining is not the best way of typography, however. Do you really want to have gray box portions below the line? That does not look nice! – user31729 Jan 2 at 19:35

Here is a way with tikz and using a node, drawing a line at the bottom of the node. However, this does not work with text - wrapping, i.e. if the text is wider than text width, it will fail.

In principle, underlining is not the best way of typographical markup, in my point of view.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
%\usepackage[normalem]{ulem}
\usepackage{tikz}

\newcommand{\underlinethis}[3][0.15\baselineskip]{%
\tikz[remember picture,baseline=(A.base)]{%
\node[inner sep=0pt,outer sep=0pt] (A) {#3}; % Place the node and typeset the text
\draw[#2] ([yshift=#1]A.south west) -- ([yshift=#1]A.south east); % Draw the line, shifted up by some value
}%
}

\begin{document}
\underlinethis{blue, line width=1pt}{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}

\underlinethis{red, line width=1pt,dashed}{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}
\end{document}


• Two wonderful examples of beautiful typography ;-) – user31729 Jan 2 at 20:06
• This isn't line breakable is it? – Skillmon Jan 2 at 22:41
• @Skillmon: Nope, I don't think so... and if I change the text options of the node, it will break, but the underline would occur on the last line only, for the full width of the text, not only for the width of the remainder of the text that is wrapped into there. – user31729 Jan 2 at 23:06
• I couldn't find the original one, but tex.stackexchange.com/a/417634/117050 does use the same mechanism. Sadly it seems like I couldn't find the original one when I wrote that answer, too, and didn't cite it properly... And I don't know who posted the original code which I modified, so finding it is hard. (the code there is modified from a version I modified for myself of that code) – Skillmon Jan 3 at 18:39
• Found the original ones. They were created by marmot: tex.stackexchange.com/a/411361/117050 and tex.stackexchange.com/a/411655/117050 use the approach. – Skillmon Jan 3 at 18:43

Redefining \uline to include a redefinition of \colorbox.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}

\makeatletter
\long\def\afterelsefi#1\else#2\fi{\fi#1}
\long\def\afterfi#1\fi{\fi#1}
\def\q@mark{\q@mark}
\newcommand\uline@colorbox[3][\q@mark]
{%
\ifx\q@mark#1%
\afterelsefi\colorbox@orig{#2}%
\else
\afterfi\colorbox@orig[#1]{#2}%
\fi
{%
% nested \uline becomes a second rule which is a bit lower than first
% the following changes the values of \UL@height and \ULdepth so that the
% calculation done by ulem results in the original values
\hskip-\fboxsep
\uline{\hskip\fboxsep#3\hskip\fboxsep}%
\hskip-\fboxsep
}%
}
\let\colorbox@orig\colorbox
\protected\def\uline
{%
\relax
\ifmmode
\expandafter\underline
\else
\bgroup
\let\colorbox\uline@colorbox % this is added compared to the original definition
\expandafter\ULset
\fi
}
\makeatother

\begin{document}

\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}

\end{document}


• @Jayjayyy if I knew with certainty it wouldn't be necessary to use this ugly value. Seems to be because of the nesting of \uline (I'll take another thorough look). .42774ex was determined by trial and error and a big zoom. – Skillmon Jan 2 at 22:28
• @Jayjayyy found the issue and fixed it. – Skillmon Jan 2 at 22:33

Don't underline. Ever.

If you don't want to adhere to the advice above, be aware that when you call \uline inside \uline the macro adds a further 1.2pt, because it thinks you want to double underline.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[normalem]{ulem}

\let\ulemuline\uline
\renewcommand{\uline}[1]{%
\begingroup
\redefinecolorbox
\ulemuline{#1}%
\endgroup
}
\let\latexcolorbox\colorbox
\newcommand\redefinecolorbox{%
\renewcommand\colorbox[2]{%
\latexcolorbox{##1}{%
\kern-\fboxsep
\ulemuline{\hspace{\fboxsep}##2\hspace{\fboxsep}}%
\kern-\fboxsep
}%
}%
}

\begin{document}

\uline{No underlining for \colorbox{lightgray}{everything} inside the colorbox.}

\end{document}


• @Jayjayyy I mean “don't underline”. At all. – egreg Jan 6 at 9:37
• @Jayjayyy Underlining is not considered a typographic device. It used to be common with typewriters lacking any other method for emphasis. – egreg Jan 6 at 13:34
• Okay, thanks :-) That's important to know. Could you add that to your answer? I have removed my former comments. Do you have a link to a good explanation on that? – finefoot Jan 6 at 14:00
• @Jayjayyy using common sense (so no reference here), it just looks too intrusive when there are other means to emphasize stuff. For the same reason bold should only be used sparsely and not for regular emphasizes, which is the reason why \emph doesn't print bold but italic by default. Also it destroys the balanced look of text if used inside of a paragraph. Maybe the most dominant reason: Word processor users tend to underline. – Skillmon Jan 6 at 14:09
• @Skillmon I think, I have to say that I agree after hearing both your and egreg's arguments. However, it wasn't obvious to me before. I'm not as familiar with typesetting - even with something as basic as emphasis - so "Using common sense" didn't help me here. I think a reference as to why the statement "Don't underline. Ever." makes sense would improve this answer since I could have just clicked on it instead of bothering you. It might help someone who's in the same shoes as I was a few moments ago. :-) – finefoot Jan 6 at 14:16