# How can I use \uline from the ulem package to underline text inside a \colorbox?

Screenshot:

I have the following code:

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


Which looks like this:

How can I get a continuous line for the text inside \colorbox, 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}


Constraints:

I cannot use soul and I think plain \underline doesn't look very good. (By the way, those who can use soul should have a look at Underline text with background (soul and ulem nesting) )

One way is to underline in a box first:

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

\begin{document}

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

\end{document}

• Wow, okay. Thanks! :-) That's more complicated than I thought it would be. I mean \colorbox is inside of \uline, I thought there would be a simple way to raise the "z-index" of the line or something like that... Can you explain why the line doesn't show up in front of the colorbox? – finefoot Jan 2 at 16:44
• @Jayjayyy not exactly, I started to trace but didn't have much time yesterday so i just nested the uline (the hspace are so the line spans over the fboxsep padding in the colorbox) – David Carlisle Jan 2 at 19:06
• Yes, it's definitely a valid answer to the question. :-) Unfortunately, I cannot use it like this because I cannot change the content inside \uline I'm afraid. Do you think there is hope for a solution without nesting? – finefoot Jan 2 at 19:13
• @Jayjayyy you could redefine \uline so that locally \colorbox is defined as I show above, or (given the \uline works with \fbox someone may suggest a different extension so that it works directly with colorbox.. – David Carlisle Jan 2 at 19:15
• Thank you, I wasn't aware that this is possible. Opened a new question Redefine \uline so \colorbox` is locally defined to have underlined text, too – finefoot Jan 2 at 19:27