[Tex/LaTex] Highlighting text through stacked colored underlines

colorhighlightingtypography

I have sentences in which characters have been "marked" by some people and not by others. I'd like to present the markings simultaneously. Stacked colored underlining (in which a line represents each person's marks) seems like the best solution. However, I'm struggling to make that happen. How can I present multiple markings simultaneously?

Goal

overlapping_highlights

Code That Fails

Because } closes the most recent {, the following code only works for a subset of examples (it fails on the goal example):

\documentclass{article}
\usepackage{xcolor}
\newcommand{\rul}[1]{\textcolor{red}{\underline{\textcolor{black}{#1}}}}
\newcommand{\bul}[1]{\textcolor{blue}{\underline{\textcolor{black}{#1}}}}

\begin{document}
\rul{sample \bul{with blue embedded} and continuing red}
\end{document}

I've experimented a bit with \lefteqn and \phantom. Using these commands resolves the bracket mismatching, but it has two other problems: it's text in math mode, and there isn't a vertical gap between the two underlining colors:

\[\lefteqn{\rul{\phantom{overlapping high}}}overlapping \bul{highlighting}\]

Other Considerations

I need to make many pages of this sort of text. For readability, the solution should have the following properties:

  • Underlining automatically wraps across lines.
  • Underlining of a single color stays on the same horizontal level throughout the document (e.g., blue is always n units below baseline).
  • Scales up to ~10 stacked underlines in a document section.

Because of the amount of this text, a readable/maintainable solution would be aces.

Best Answer

This answer will NOT wrap. Nonethless, proceeding...

Here, I introduce \nunderline[]{}{}. The optional argument is the under-level for rule placement (relative to the prior placement). The first argument is the text, and the second argument is the color. The rule thickness is set with \rulethick and th relative spacing with \lunderset.

Nesting is used to obtain multiple lines under a given phrase. To take you through it, line by line:

  1. underline "overlap" in red%

  2. underline "ping " in red and then, 2 levels below that, in cyan%

  3. underline "high" in red, then blue, then cyan%

  4. underline "light" at the 2nd level in blue, and below that, in cyan%

  5. underline "ing" at the 3rd level in cyan.

\documentclass{article}
\usepackage{stackengine}
\usepackage{xcolor}
\newlength\lunderset
\newlength\rulethick
\lunderset=1.5pt\relax
\rulethick=.8pt\relax
\def\stackalignment{l}
\newcommand\nunderline[3][1]{\setbox0=\hbox{#2}%
  \stackunder[#1\lunderset-\rulethick]{\strut#2}{\color{#3}\rule{\wd0}{\rulethick}}}
\begin{document}
\nunderline{overlap}{red}%
\nunderline[2]{\nunderline{ping }{red}}{cyan}%
\nunderline{\nunderline{\nunderline{high}{red}}{blue}}{cyan}%
\nunderline{\nunderline[2]{light}{blue}}{cyan}%
\nunderline[3]{ing}{cyan}
\end{document}

enter image description here

Related Question