# Cool Text Highlighting in LaTeX

LaTeX has soul. ConTeXt, of course, has beautifully marked up "sharpie" highlighting, where you can put sweet hightligher under your text for free!

In the pictured example, you can see highlights that not only highlight, but that do it with cool edges that look so good, you can smell fumes! The fearless author says he'll show how that was done in another post; this one shows only how to lay down a straight, yellow box. But, it is possible to do word-wrapped highlighting with cool-shapped boxes within ConTeXt, presumably.

The trouble for me is that getting into the ConTeXt world is steep climb right now, given the state of documentation and the flux of changes, so I need to continue with LaTeX. But the real problem is that now I can't live without the Organic-Looking Highlights of Awesomeness in that picture!

As I said, Non-Organic-Looking Highlights of the Ordinary are easy with the soul package:

\hl{Some awesome text that word wraps fine.}


It just taunts me on the page with its straight lines. So, if I sacrifice word wrapping, I can use PGF/TiKz and create the following macro:

\newcommand\hl[1]{%
\tikz[baseline,%
decoration={random steps,amplitude=1pt,segment length=15pt},%
outer sep=-15pt, inner sep = 0pt%
]%
\node[decorate,rectangle,fill=navcolor,anchor=text]{#1\xspace};%
}%


Which creates something like this:

Now, my color is too dark and my randomness needs adjusting, but you see that a similar effect could be achieved.

My trouble is that I don't understand TeX enough to dig into the Soul package and add the TiKz/PGF goodness that it so sorely needs. So, I leave it to you guys. Anyone want to tackle this?

Or is it yet another piece of fruit, just a touch out of reach, that shall have to wait until ConTeXt settles down?

-
What is unstable about ConTeXt? Are you sure that you're using "stable" and not "beta"? –  Mica Nov 26 '10 at 1:41
Installing it on a Mac, for one. Also, there is nothing that makes good use of the project system that it implements. PGF/TiKz support worked after I dl'd a nightly from that site. All and all, it was probably fine and perhaps "unstable" was unintentionally unfair. It would have been better to say, "There isn't enough consistent and organized documentation around ConTeXt to efficiently get up and running on with it, yet." I look forward to going that direction in the future, however. –  Andrew Starks Nov 26 '10 at 1:55
If you look at wiki.contextgarden.net/Main_Page -- about half way down the page, under the heading "documentation," there is a ton of documentation. As I am learning context now (coming from latex), the first page of the wiki, the mailing list, and this site have answered all the questions I've had so far. –  Mica Nov 26 '10 at 4:46
As far as I can tell, the slight "wigglyness" in the picture is just an optical illusion. The lines seem to be perfectly straight. –  Caramdir Nov 26 '10 at 5:18
@Mica: I've read my share of ConTeXt documentation (see: simplefonts). I think, as alpha software, if you have the time, patience and brains, ConTeXt can work really well. For me, getting past the scattershot approach to organization, which if it is there at all is a mixture of fantastic, incomplete, out of date or wrong, wasn't worth it yet. I'm uber impressed with the potential, but for me, the capacity of greatness for any tech is boxed in by its documentation. I would have chosen to have the documentation be written first and code to that, but they chose to let it slide. That's a shame. –  Andrew Starks Nov 26 '10 at 23:05

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,
I suppose I could write some more text here.
\end{document}


This produces the following output:

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?).

-
@Antal, very nice! FYI, font dimensions can be found in (surprise) \fontdimenN, where N is a number, e.g., \fontdimen5 gives the ex-height. For your case, you might consider something like \setbox0=\hbox{some text here}\@tempdima=\ht0. –  Geoffrey Jones Nov 27 '10 at 2:38
@Geoffrey: Thanks, I'll check out \fontdimen! I thought about the boxing/unboxing, but the problem is that it it highlights an area of the screen, rather than scannable text (and, of course, we can't know the tallest/deepest character in the font ahead of time). Although it might be possible to leverage soul's process to keep track of the height…. –  Antal S-Z Nov 27 '10 at 3:24
@Antal... I'm freakin' speechless... This is quite possibly the coolest piece of TeXporn I've ever seen. Thank you! It's funny that how in the grand scheme of things, this probably doesn't matter to anyone, but now that you've done it, it's essential to my technical documentation. :) Also, I'm going to see if I can't apply the fadings library to this and some random overlapping to create the illusion of the marker running dry, the author flipping the tip and then restarting---just slightly overlapping his/her last starting point. After that, I'll start actually writing my documentation. :) –  Andrew Starks Nov 27 '10 at 4:04
@Antal: A thought for you. In the document, could you start every paragraph with a tikz node that you could use to either: A) set the depth of the paragraph to be over your highlighting, B) be a place where you dump marginpar code so that you can mark the paragraph as significant in a specified way (determined by the highlighter(s) that you used) in the margin and flush with the top of the paragraph or C) do nothing because no highlighter was used. This would create some sort of link/reference to the paragraph... \thispar I might be way out there, as I know little about TeX internals. –  Andrew Starks Nov 27 '10 at 4:15
@Andrew: \everypar is the way to go, yes (I think there's a package which provides hooks), though I don't know much about it. –  Antal S-Z Nov 28 '10 at 15:05

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}


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

-
You beat me to it while I got distracted with weird names for SVG colours. My example was going to use (via xcolor) "PaleGoldenrod" or perhaps "PapayaWhip". –  Will Robertson Nov 26 '10 at 5:47
People must have had fun inventing all those color names. –  Caramdir Nov 26 '10 at 5:52
Snap! You are correct. Maybe I should just grow up and go this route? It's fun to puzzle this out, but I'm just not a TeXpert. I think you really have to have a different sort of brain to go that far. –  Andrew Starks Nov 26 '10 at 22:46
+1 This is perfect for my purpose. I’ve also defined a new command to set the color per highlight instead of globally: \newcommand{\hlc}[2][yellow]{ {\sethlcolor{#1} \hl{#2}} }. And its usage: \hlc[CornflowerBlue]{Highlight}. –  Gumbo Mar 16 '11 at 10:58

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) {};
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}


-

You are not showing if and how context can do what you want. But in any case there is a major difference: LaTeX uses as engine pdftex while context uses luatex. So moving a context solution to latex would mean moving to lualatex.

In the case of "soul": \hl in soul is basically and \ul (underline) with a large and colored rule. Tweaking soul isn't so difficult: The complete code is about 900 lines. The important part which defines the user command start at line 525. soul splits the input in tokens, syllables, spaces, hyphen and you have to tell it what to do with the pieces. In the case of \ul/\hl eg. \leaders are used. It can be quite fun to change this (in real life you shouldn't redefine the \ul-commands as this will affect \ul. You should define your own new soul command). You can see quite good the "syllables":

 \documentclass{article}
\usepackage{soul,xcolor}
\begin{document}
\hl{highlight} text

\makeatletter

\textcolor{green}{\rule{2pt}{2.5ex}}%
\textcolor{red}{\rule{2pt}{2.2ex}}}\relax
}

\def\SOUL@uleveryspace#1{%
\SOUL@ulcolor{%
#1%
\hskip\spaceskip
}%
\null
}

\hl{highlight text}

\ul{test}
\end{document}

-
I like this idea, but I think that would be difficult to use with my TiKz idea... maybe. If you did non random paths vertically and used the width of the current character, it MAY work, except that you need to know where the vpos of the previous character ended? –  Andrew Starks Nov 26 '10 at 22:54
I think that what one could do is create a command like \hls and \hle which begin and end a highlight. These insert 0pt width characters. Then, you'd let TeX typset the paragraph and just prior to it laying a sentence down, scan for the (x,y) of the \hls and create your TiKz box, starting there. End at the \hle or the end of the line. If EOL, then insert \hls character at the beginning of the next line. I have no idea if any of that is reasonable, possible, the worst way in the world to do it, or the freakin' best idea anyone's ever had... –  Andrew Starks Nov 26 '10 at 22:59
@Ulriche: I didn't see your answer, but my solution ended up working on a similar principle, by using soul's tokenizing ability. (As did testing my answer, so I really wish I had seen yours :-) ) @Andrew: Again, I didn't see your comment, but that's basically the idea I had, except I used soul to do the insertion and accomplished the "checking" via inserting even more TikZ zero-width characters. –  Antal S-Z Nov 27 '10 at 4:04

I thought I had an easy solution to this. I was wrong. I knew that the xcolor package make it easy to color text using \color, and, though I hadn't used it, knew it bragged of being able to put a background color on text also using \colorbox. However, even though the documentation claims that \colorbox acts just like \color it doesn't. Here's my test code:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
{\color{blue}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent
tortor. Suspendisse scelerisque mi non risus.}} Duis vitae ligula eget
torquent per conubia nostra, per inceptos himenaeos.

\vspace{2em}

{\colorbox{yellow}{% highlighted part
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent
tortor. Suspendisse scelerisque mi non risus.}} Duis vitae ligula eget

You can see that the highlighted portion (i.e. with the yellow bg color) is not a part of the paragraph anymore, I think because of the use of \leavevmode. Anyway, I've emailed the author with this observation and I'm interested to see what he says.
Not all boxes are limited to one line, e.g.\parbox. What confused me is that there is no mention of this in the documentation (that I saw). His paragraph says: " \colorbox takes the same argument form as \textcolor, but the color specifies the background color of the box." His comparison to \textcolor makes one think they work the same, just one is fg and one is bg. I'm a big believer in accurate documention, Ulrike. –  bev Nov 26 '10 at 9:48