# beamer, overlay in allt or fancyvrb

I'd like to highlight some text inside an alltt or verbatim or Verbatim (or whatever environment/package that would produce what I want) on some slides. So, I create a newcommand<> that does the job:

\newcommand<>{\highlight}[1]{{\alt#2{\colorbox#2{yellow}{#1}}{#1}}}


Indeed, if I use this \highlight<2>{bla bla} in a mere frame environment, it does work as I expect (though it adds a space before "bla bla" when highlighted... I don't understand why).

But if I use it inside an alltt or a Verbatim environment, it does not work.

I try to propose a minimal example:

\documentclass{beamer}
\usepackage{alltt,fancyvrb}
\newcommand<>{\highlight}[1]{{\alt#2{\colorbox#2{yellow}{#1}}{#1}}}
\begin{document}
\begin{frame}
some text

\highlight<2>{some text to highlight on slide 2 only}

some more text
\end{frame}

\begin{frame}[fragile]
\begin{alltt}
some \textit{text}
{\highlight<2>{some text to highlight on slide 2 only}}
some \textbf{more} text
\end{alltt}

\begin{Verbatim}[commandchars=\\\{\}]
some \textit{text}
\highlight<2>{some text to highlight on slide 2 only}
some \textbf{more} text
\end{Verbatim}
\end{frame}

\end{document}


The first frame does what I want. The second does not, by far!

I'm not sure I use the Verbatim environment correctly. I mention it to show what I tried. If highlighting with alltt was working, I would be happy.

I've seen some questions about \alert<x> in fancyvrb, but I'm not sure the answer applies to my situation (or I don't understand the answer).

• Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. A suggestion: Do us a favour and change your username to something more telling than "user1234". – Martin Schröder Jan 22 '16 at 20:55

While alltt doesn't bother changing the category code of < and >, because it assumes that ligatures with these characters are intended for output, verbatim modes do and there is no interface in fancyvrb for changing them back. So you have to do it manually:

\documentclass{beamer}

\usepackage{alltt,fancyvrb}
\makeatletter % fix the ligature list and hope for the best
\def\verbatim@nolig@list{\do\\do\,\do\'\do\-}
\makeatother

\newcommand<>{\highlight}[1]{{\alt#2{\colorbox#2{yellow}{#1}}{#1}}}

\begin{document}

\begin{frame}[fragile]
\begin{alltt}
some \textit{text}
{\highlight<2>{some text to highlight on slide 2 only}}
some \textbf{more} text
\end{alltt}

\begin{Verbatim}[commandchars=\\\{\}]
some \textit{text}
\highlight<2>{some text to highlight on slide 2 only}
some \textbf{more} text
\end{Verbatim}
\end{frame}

\end{document}


Be advised that << and >> in Verbatim will appear as « and » if you use the T1 output encoding.

Beamer's built-in semiverbatim environment serves the same purpose as alltt` but preserves overlay specifications.