Cool Text Highlighting in LaTeX

Much to my surprise, this is doable, by combining TikZ and soul. I don't think I like how it looks (though to each their own), but it was a fun challenge. The idea is to use TikZ to draw the necessary boxes up to line breaks, and then restart each box on a new line. But how to do this? Well, it turns out that soul works by inserting things after every possible hyphenation point (which it calls "syllables", a terminology misuse I find irritating)—importantly, this includes the beginnings and ends of words. And the only time we ever see a line break is after a hyphenation point! This is good: we'll use soul to insert a TikZ node at each syllable break, and then draw them together. For this, we use TikZ's remember picture and overlay options; the former enables you to refer to nodes in the labeled picture from outside, and the latter makes the picture take up no space.

To implement this, we surround each hyphenation unit with two "marks" (TikZ pictures). The "start" mark checks to see if it's on a new line; if it is, it draws the highlighting rectangle from the last recorded start position to the last recorded end position, and then records the new start position. The "stop" mark just records the new stop position. We also have to make sure that at a hyphenation point, the stop position is after the hyphen. And of course, we surround the whole picture with a start position and a stop position. Note that due to the remember picture machinery, you have to compile the document twice.

Here's a working document with ragged highlights:

\documentclass{minimal}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter

\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{yellow}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [ decoration = {random steps, amplitude=1pt, segment length=15pt}
        , outer sep = -15pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter ]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-3pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\begin{document}
  Lorem ipsum \highlight{dolor sit amet, consectetur adipis-icing elit, sed do
eiusmod tempor} incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation \highlight[red]{ullamco $laboris$ nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit} in
voluptate velit esse cillum dolore eu fugiat nulla pariatur.  Excepteur sint
occaecat \highlight[green, draw=blue]{cupidatat non proident,
suntinculpaquiofficiadeseruntmollitanimidestlaborum.
Loremipsumdolorsitametconsecteturadipisicingelitseddoeiusmodtemporincididuntutlabore-etdoloremagnaaliqua.}
I suppose I could write some more text here.
\end{document}

This produces the following output:

Lipsum text with ragged highlights.

There are two formatting modes: \defhighlighter[misc]{color}{opacity}, which sets the fill color to color, the fill opacity to opacity, and styles every highlighter block (via every highlighter) with those plus the misc options. Secondly, for a specific block of highlighted text, you can use \highlight[tikz-opts]{...} to set options locally. These are both demonstrated in the above code.

There are currently four caveats. The first is that this will write one line to your aux file for each syllable break in the highlighted text, in addition to one for the beginning of each, one for the end of each, and one for each line break after a (real or inserted) hyphen. For instance, the example document wrote 222 lines to my aux file. This may or may not be a problem for you, but I'm not sure how to make it better. The second is that strange things happen if you let highlighting extend over a page break. (Although I might be able to fix this one….) The third is that I don't see a way to draw the highlighting in the background. (Because it's drawn so late, the pgfonlayer environment doesn't work.) This is why the text looks faded. You can adjust the opacity value if you don't like this, but I don't see how to avoid it (suggestions?). And the fourth is that I guessed the height of the font, because while I figure this should be a dimension in TeX somewhere, I don't know where (again, suggestions?).


I can't see a difference between the picture you provided and

\documentclass{article}
\usepackage[width=5.05cm]{geometry}
\usepackage{color,soul}

\begin{document}
\noindent\hl{Highlighting} text feels good.
You can draw attention of people to a \hl{word} or perhaps 
\hl{even a whole sentence that spans across multiple lines
in such a way that hyphenation etc. are not affected.}
\end{document}

rendered example

I suspect getting TeX to draw actual randomly wiggly highlights that work well with line breaks is a lot more work.


If figured that I can adjust my code written for Test if a paragraph has a page break in it? to do underlining and also highlighting. This solution marks the begin and the end of the text with both TikZ and zref marker (the latter to test for page breaks) and draws the lines using tikz between the markers. The current line width is taken into account and new markers are automatically set at every paragraph break. This has the benefit that the normal text typesetting is not influenced at all! I'm working to generalize this (see Place TikZ coordinate or \zlabel at every (base)line and Installing background and foreground page layers with TikZ) and provide a better UI in form of a package as soon I find time.

\documentclass[twoside,11pt]{book}

\usepackage{zref-abspage}
\usepackage{zref-user}
\usepackage{tikz}
\usepackage{atbegshi}
\usetikzlibrary{calc,decorations.pathmorphing}

\makeatletter
\newcommand{\currentsidemargin}{%
  \ifodd\zref@extract{textarea-\thetextarea}{abspage}%
    \oddsidemargin%
  \else%
    \evensidemargin%
  \fi%
}

\newcounter{textarea}
\newcommand{\settextarea}{%
   \stepcounter{textarea}%
   \zlabel{textarea-\thetextarea}%
   \begin{tikzpicture}[overlay,remember picture]
    % Helper nodes
    \path (current page.north west) ++(\hoffset, -\voffset)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, minimum height=\paperheight]
        (pagearea) {};
    \path (pagearea.north west) ++(1in+\currentsidemargin,-1in-\topmargin-\headheight-\headsep)
        node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\textwidth, minimum height=\textheight]
        (textarea) {};
  \end{tikzpicture}%
}


\usepackage{lipsum}
\newcommand\xlipsum[1][]{{\let\par\relax\lipsum*[#1]}}

\tikzset{tikzul/.style={yshift=-.75\dp\strutbox}}

\newcounter{tikzul}%
\newcommand\tikzul[1][]{%
    \begingroup
    \global\tikzullinewidth\linewidth
    \def\tikzulsetting{[#1]}%
    \stepcounter{tikzul}%
    \settextarea
    \zlabel{tikzul-begin-\thetikzul}%
    \tikz[overlay,remember picture,tikzul] \coordinate (tikzul-\thetikzul) at (0,0);% Modified \tikzmark macro
    \ifnum\zref@extract{tikzul-begin-\thetikzul}{abspage}=\zref@extract{tikzul-end-\thetikzul}{abspage}
    \else
        \AtBeginShipoutNext{\tikzul@endpage{#1}}%
    \fi
    \bgroup
    \def\par{\ifhmode\unskip\fi\egroup\par\@ifnextchar\noindent{\noindent\tikzul[#1]}{\tikzul[#1]\bgroup}}%
    \aftergroup\endtikzul
    \let\@let@token=%
}

\newlength\tikzullinewidth

\def\tikzul@endpage#1{%
\setbox\AtBeginShipoutBox\hbox{%
\box\AtBeginShipoutBox
\hbox{%
\begin{tikzpicture}[overlay,remember picture,tikzul]
\draw[#1]
    let \p1 = (tikzul-\thetikzul), \p2 = ([xshift=\tikzullinewidth+\@totalleftmargin]textarea.south west) in
    \ifdim\dimexpr\y1-\y2<.5\baselineskip
        (\x1,\y1) -- (\x2,\y1)
    \else
        let \p3 = ([xshift=\@totalleftmargin]textarea.west) in
        (\x1,\y1) -- +(\tikzullinewidth-\x1+\x3,0)
        % (\x3,\y2) -- (\x2,\y2)
        (\x3,\y1)
       \myloop{\y1-\y2+.5\baselineskip}{%
           ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
       }%
    \fi
;
\end{tikzpicture}%
}}%
}%

\def\endtikzul{%
    \zlabel{tikzul-end-\thetikzul}%
    \ifnum\zref@extract{tikzul-begin-\thetikzul}{abspage}=\zref@extract{tikzul-end-\thetikzul}{abspage}
    \begin{tikzpicture}[overlay,remember picture,tikzul]
        \expandafter\draw\tikzulsetting
            let \p1 = (tikzul-\thetikzul), \p2 = (0,0) in
            \ifdim\y1=\y2
                (\x1,\y1) -- (\x2,\y2)
            \else
                let \p3 = ([xshift=\@totalleftmargin]textarea.west), \p4 = ([xshift=-\rightmargin]textarea.east) in
                (\x1,\y1) -- +(\tikzullinewidth-\x1+\x3,0)
                (\x3,\y2) -- (\x2,\y2)
                (\x3,\y1)
                \myloop{\y1-\y2}{%
                    ++(0,-\baselineskip) -- +(\tikzullinewidth,0)
                }%
            \fi
        ;
    \end{tikzpicture}%
    \else
    \settextarea
    \begin{tikzpicture}[overlay,remember picture,tikzul]
        \expandafter\draw\tikzulsetting
            let \p1 = ([xshift=\@totalleftmargin,yshift=-.5\baselineskip]textarea.north west), \p2 = (0,0) in
            \ifdim\dimexpr\y1-\y2<.5\baselineskip
                (\x1,\y2) -- (\x2,\y2)
            \else
                let \p3 = ([xshift=\@totalleftmargin]textarea.west), \p4 = ([xshift=-\rightmargin]textarea.east) in
                (\x3,\y2) -- (\x2,\y2)
                (\x3,\y2)
                \myloop{\y1-\y2}{%
                    ++(0,+\baselineskip) -- +(\tikzullinewidth,0)
                }
            \fi
        ;
    \end{tikzpicture}%
    \fi
    \endgroup
}

\def\myloop#1#2#3{%
    #3%
    \ifdim\dimexpr#1>1.1\baselineskip
        #2%
        \expandafter\myloop\expandafter{\the\dimexpr#1-\baselineskip\relax}{#2}%
    \fi
}

\makeatother

\begin{document}

text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text

{\tikzset{tikzul/.style={yshift=1ex}}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. \tikzul[line width=1.5\ht\strutbox,semitransparent,yellow]{Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.}
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
test
}

\begin{quote}
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
\end{quote}

{\tikzset{tikzul/.style={yshift=.5ex}}

\begin{quote}
text text text text text text text text text text text
text text text text text text text text text text text
\begin{quote}
text text text text text text text text text text text
text text \tikzul[green]{text text text text text text text text text
text text text text text text text text text text text
text text text text 
text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text} text text text text text text text
\end{quote}
text text text text text text text text text text text
text text text text text text text text text text text
\end{quote}

\large
new text text text text text text text text text text text
text text \tikzul[red]{first text text text text text text text text
teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt
teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt teXt
teXt teXt teXt last} teXt teXt teXt teXt teXt teXt teXt
text text text text text text text text text text text
text text text text text text text text text text text

text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text\par\noindent text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
}

\begin{itemize}
    \item \tikzul{test test test} test
    \item test \tikzul{test test test} test
    \item aa \tikzul{test test test} test
    \item b \tikzul{test test 
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
        test} test
\end{itemize}

text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text

 text text text text text text text text text
text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text

text text text text text text text text text text text
text text text text text text text text text text text
text text \tikzul[red]{text text text text text text text text text
text text

\noindent text text text text text text text text text
\par text text text text} text text text text text text text
text text text text text text text text text text text
text text text text text text text text text text text


\end{document}

enter image description here