11

On some slides I'd like to highlight some text by using a colored background, like so: text with some words highlighted

I've exaggerated the width of the border around the text in this example to indicate the effect I want.

I produced this example manually. I'd like to have a macro \highlighton so that I can produce the effect by writing \highlighton<1>{`to protrude out'}. The wonderful package tcolorbox provides macros that almost do the trick. Here's an example.

\documentclass{beamer}
\usepackage{etex}
\setbeamertemplate{navigation symbols}{}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage{times}
\def\highlighton<#1>#2{%
  \alt<#1>{\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]{#2}}{#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 \highlighton<1>{`to protrude out'}.  \textbf{c}. with an immaterial 
thing as obj. \textbf{d}.\ To shape (metals, plastics, etc.)\ by forcing them through 
dies. \textbf{2}.\ intr.\ for refl. To protrude out. rare.

\pause

\end{frame}
\end{document}

This code (in which extrude by 5mm generates the border) produces the following output. Text before the highlighted text is "dimmed", but text after the highlighted text isn't. Is there a way to produce exactly the effect I want?

enter image description here

  • You would need to create the red box after the next line of text is produced. I do not have the skills to tell you how to achieve this. Maybe, you would need to consider 2 compilation runs and write relevant information in the auxiliary file. There is this "overlay" option in Tikz that could be useful as well. – pluton Jan 30 '14 at 3:42
10

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}

enter image description here

  • +1, I was also thinking about using keys to store text correctly, but you were faster. – Claudio Fiandrino Jan 30 '14 at 10:15
  • @ClaudioFiandrino It's nice to have both approaches here. Your TikZ only solution is presumably better for people who don't use tcolorbox at other places in the text - one package less to include ;-) – Thomas F. Sturm Jan 30 '14 at 11:20
  • That's terrific! Is it possible to make \remember store/write the \highlighton commands and have them automatically executed at \end{frame}? Then one would not have to type the \highlighton commands explicitly. Icing on the cake really, but it would be nice! – Martin J. Osborne Jan 31 '14 at 4:09
  • @MartinJ.Osborne The next days, I will not have the time to try something, but a rough idea is to collect all that has to be done by appending to a macro. Finally, this macro may be hooked into \end{frame}. – Thomas F. Sturm Jan 31 '14 at 14:57
  • 1
    If you want to highlight math stuff, then have a look to the hf-tikz package. There are several examples, for instance see Beamer: highlighting aligned math with overlay. – Claudio Fiandrino Feb 4 '14 at 10:01
9

More difficult than what I originally thought. The problem lies in the fact that if one performs operations within the text, the opacity is not preserved uniformly in the box. Hence the need to mark somehow the text to be highlighted and to perform later the actual highlighting.

This is a possible solution with TikZ only:

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{times}

\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}

\newcounter{storetxt}
\newcounter{showtxt}

\makeatletter
\newcommand{\tikzmark}[2]{
\tikz[remember picture,baseline=-0.5ex] 
\node[inner sep=0pt,outer sep=0pt] (#1) {#2};%
\stepcounter{storetxt}
\global\@namedef{text@\thestoretxt\expandafter}\expandafter{#2}% storing text
}

\newcommand<>{\highlighton}[1]{
\stepcounter{showtxt}
\tikz[overlay,remember picture]{
\edef\txt{\@nameuse{text@\theshowtxt}}% retrieve the correct text
\node[opacity=0.8,text opacity=1,rounded corners,
minimum width=3cm,minimum height=1.5cm,% size of the box
background fill=red!50,fill on=#2,% box visibility
visible on=#2% we need this to have the filling only in some overlays,
% otherwise the opacity would affect all overlays
]at(#1){\txt};
}
}
\makeatother

\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 \tikzmark{mark}{`to protrude out'}.  \textbf{c}. with an immaterial 
thing as obj. \textbf{d}.\ To shape \tikzmark{m2}{(metals, plastics, etc.)}\ by forcing them through 
dies. \textbf{2}.\ intr.\ for refl. To protrude out. rare.

\highlighton<2>{mark}% important to have it here after the text
\highlighton<3>{m2}

\end{frame}
\end{document}

The result:

enter image description here

  • of course, "protrude" means "stick out", so "protrude out" presumably means "stick out out". – wasteofspace Jan 30 '14 at 10:05
  • @wasteofspace The definition comes from the OED, so you need to take that up with them :) – Martin J. Osborne Jan 31 '14 at 3:07
1

A similar effect can be achieved with the spot package:

\documentclass{beamer}
\usepackage{spot}

\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 \spot<1>(foo){`to protrude out'}.  \textbf{c}. with an immaterial 
thing as obj. \textbf{d}.\ To shape (metals, plastics, etc.)\ by forcing them through 
dies. \textbf{2}.\ intr.\ for refl. To protrude out. rare.

\end{frame}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.