My answer is using the same idea as Claudio Fiandrino did. But it uses tcolorbox
(v2.61) and you may use as many highlights as you want on a page.
\documentclass{beamer}
\usepackage{etex}
\setbeamertemplate{navigation symbols}{}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage{times}
\def\remember#1#2{%
\tcbox[enhanced,remember as={#1},frame hidden,interior hidden,boxrule=0pt,nobeforeafter,
tcbox raise base,shrink tight]{#2}%
\pgfkeyssetvalue{/myremember/#1}{#2}%
}
\def\highlighton<#1>#2{%
\alt<#1>{\tikz[overlay,remember picture]\node at (#2){%
\tcbox[enhanced,boxrule=0pt,colback=red!50,interior style={opacity=0.7},
frame style={opacity=0.5},nobeforeafter,tcbox raise base,shrink tight,
extrude by=5mm]{\pgfkeysvalueof{/myremember/#2}}};}{}%
}
\begin{document}
\begin{frame}
\textbf{Extrude}, \textit{v}.\ trans. \textbf{1}.\ To thrust (a person) out or forth;
to urge or force out; to expel. \textbf{a}.\ with obj.\ a person. \textbf{b}.\ with
obj.\ a material thing; in mod.\ use esp.\ to exclude (an embryo, ova, etc.). Also
occas.\ with sense \remember{protrude}{`to protrude out'}. \textbf{c}. with an immaterial
thing as obj. \textbf{d}.\ To shape \remember{etc}{(metals, plastics, etc.)}\ by forcing them through
dies. \textbf{2}.\ intr.\ for refl. To protrude out. rare.%
\highlighton<2>{protrude}
\highlighton<3>{etc}
\pause
\end{frame}
\end{document}
While not exactly what the OP asks, it may suffice. First, it uses \detokenize
, not verbatim
, which means 1) that braces {}
must be balanced, 2) that the %
symbol is still interpreted as a comment, 3) that a space is inserted after all detokenized macro names, and 4) Hash marks #
are doubled in number.
Also, I just \allowbreak
between words, using the \fboxsep
of the colorbox to create the interword spaces.
EDITED to use \ttfamily
for the \mytokens
macro.
EDITED to fix line height, since the combination of a \strut
and \fboxsep
blew the allowed baselineskip budget.
Originally, I was not using interword space but allowed the \fboxsep
to suffice. But this caused margin issues, as nothing could stretch. So, upon RE-EDIT, I insert a space of 0pt minus \fboxsep
between words, which seems to allow enough compression to address the margin issues, while still leaving enough space between words (otherwise 2\fboxsep
).
I also rearranged the \grayspace
and allowbreak
, upon RE-EDIT, so that a linebreak would not occur before trailing punctuation.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\newcommand\mytokens[1]{\mytokenshelp#1 \relax\relax}
\def\mytokenshelp#1 #2\relax{\allowbreak\grayspace\tokenscolor{#1}\ifx\relax#2\else
\mytokenshelp#2\relax\fi}
\newcommand\tokenscolor[1]{\colorbox{gray!20}{\textcolor{blue}{%
\ttfamily\mystrut\smash{\detokenize{#1}}}}}
\def\mystrut{\rule[\dimexpr-\dp\strutbox+\fboxsep]{0pt}{%
\dimexpr\normalbaselineskip-2\fboxsep}}
\def\grayspace{\hspace{0pt minus \fboxsep}}
\begin{document}
Here is my leading text
\mytokens{Fie Fi Fo Fum, I smell the $#@*& blood of an \Englishman.
I will continue this text to observe the margins.
Fie Fi Fo Fum, I smell the \textbf{blood} of an Englishman.}
and my trailing text. And here is more text to find where the margin end
really should be.
Standard MATLAB formats are allowed, such as \mytokens{1.2}, \mytokens{-.2i},
\mytokens{1.}, \mytokens{-1.2e3}, \mytokens{+.2E-3j}. \mytokens{i} and
\mytokens{j} denote...
\end{document}
SUPPLEMENT
In response to a comment request, here is a version that takes the background color as an optional argument.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\newcommand\mytokens[2][gray!20]{\mytokenshelp{#1}#2 \relax\relax}
\def\mytokenshelp#1#2 #3\relax{\allowbreak\grayspace\tokenscolor[#1]{#2}\ifx\relax#3\else
\mytokenshelp{#1}#3\relax\fi}
\newcommand\tokenscolor[2][gray!20]{\colorbox{#1}{\textcolor{blue}{%
\ttfamily\mystrut\smash{\detokenize{#2}}}}}
\def\mystrut{\rule[\dimexpr-\dp\strutbox+\fboxsep]{0pt}{%
\dimexpr\normalbaselineskip-2\fboxsep}}
\def\grayspace{\hspace{0pt minus \fboxsep}}
\begin{document}
Here is my leading text
\mytokens{Fie Fi Fo Fum, I smell the $#@*& blood of an \Englishman.
I will continue this text to observe the margins.
Fie Fi Fo Fum, I smell the \textbf{blood} of an Englishman.}
and my trailing text. And here is more text to find where the margin end
really should be.
Standard MATLAB formats are allowed, such as \mytokens{1.2},
\mytokens{-.2i}, \mytokens[red!40]{1.}, \mytokens{-1.2e3},
\mytokens{+.2E-3j}. \mytokens[cyan!30]{i} and
\mytokens[green!20]{j} denote...
\end{document}
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:
underline "overlap" in red
%
underline "ping " in red and then, 2 levels below that, in cyan
%
underline "high" in red, then blue, then cyan
%
underline "light" at the 2nd level in blue, and below that, in cyan
%
underline "ing" at the 3rd level in cyan.