3

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.}

screenshot


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.}

screenshot


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}

finefoot
  • 529
  • 5
  • 15
  • 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! –  Jan 02 '19 at 19:35

3 Answers3

4

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}

enter image description here

  • 2
    Two wonderful examples of beautiful typography ;-) –  Jan 02 '19 at 20:06
  • 1
    This isn't line breakable is it? – Skillmon Jan 02 '19 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. –  Jan 02 '19 at 23:06
  • There are answers on this site using TikZ that work for multiline underlining (I've wrote one but it wasn't original by me, I can search if you want). – Skillmon Jan 02 '19 at 23:19
  • 1
    I couldn't find the original one, but https://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 03 '19 at 18:39
  • 1
    Found the original ones. They were created by marmot: https://tex.stackexchange.com/a/411361/117050 and https://tex.stackexchange.com/a/411655/117050 use the approach. – Skillmon Jan 03 '19 at 18:43
  • @Skillmon: Thanks, I will come back to those and change eventually. –  Jan 03 '19 at 20:11
4

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
      \advance\UL@height\ULdepth
      \advance\ULdepth-\thr@@\UL@height
      \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}

enter image description here

Skillmon
  • 60,462
2

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}{%
      \advance\ULdepth-1.2pt
      \kern-\fboxsep
      \ulemuline{\hspace{\fboxsep}##2\hspace{\fboxsep}}%
     \kern-\fboxsep
    }%
  }%
}

\begin{document}

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

\end{document}

enter image description here

egreg
  • 1,121,712
  • @Jayjayyy I mean “don't underline”. At all. – egreg Jan 06 '19 at 09:37
  • @Jayjayyy Underlining is not considered a typographic device. It used to be common with typewriters lacking any other method for emphasis. – egreg Jan 06 '19 at 13:34
  • @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 06 '19 at 14:09
  • @Jayjayyy I agree with you, it would certainly improve the answer. And I personally think it should be better supported in TeX, even if it is bad practise. I have to underline stuff, because I must match a specific layout, in some of my documents, and the fact that there is no perfect solution bothers me a lot. But I have no idea how to write something that is better than ulem or soul or a TikZ approach (which can be found somewhere on this site). Every approach has some downsides, as far as I know... And I did search for quite some time. – Skillmon Jan 06 '19 at 14:25