# Overlay red rectangles on top of verbatim text

I am using Beamer to prepare a presentation. In some slide I need to print a great deal of verbatim text and, also, I need to draw some red rectangles on top of it.

What would be the most straightforward way to do it? I'm struggling with fancyvrb and also with tikz.

\documentclass[pdf]{beamer}
\usepackage{semiverbatim}

\begin{document}

\newsavebox\myv
\begin{frame}[fragile]
\frametitle{IAEA phase-space file format}
\begin{lrbox}{\myv}\begin{minipage}{\textwidth}
\begin{semiverbatim}
$xxd -b -l 100 ELEKTA_PRECISE_6mv_part1.IAEAphsp 0000000: 00000001 01101001 01010011 01001000 10111111 10100011 .iSH.. 0000006: 11110110 10101101 11000000 01111110 00000100 10011110 ...~.. 000000c: 11000000 11101011 10001011 10110101 10111110 01110011 .....s 0000012: 01110010 10100111 10111110 11001101 11001100 01001100 r....L 0000018: 00111101 00000100 00000000 00000000 00000000 01100111 =....g 000001e: 00000000 10000000 00000001 00000001 00100011 11011111 ....#. 0000024: 01101110 00111110 00000100 10110010 10000010 00111111 n>...? 000002a: 10110101 01000001 11010000 10111110 01101011 01111111 .A..k. 0000030: 00011111 00111101 10111011 00101111 01010000 10111100 .=./P. 0000036: 11001101 11001100 01001100 00111101 00000000 00000000 ..L=.. 000003c: 00000000 00000000 01100111 00000000 10000000 00000001 ..g... 0000042: 00000001 01101000 10100000 11001110 00111111 01000010 .h..?B 0000048: 10011010 10101100 10111111 00000111 00010110 01100010 .....b 000004e: 01000000 01000010 00101010 01000011 10111101 11110111 @B*C.. 0000054: 11101011 00000101 00111110 11001101 11001100 01001100 ..>..L 000005a: 00111101 00000000 00000000 00000000 00000000 01100111 =....g 0000060: 00000000 10000000 00000001 00000001 ....$
\end{semiverbatim}
\end{frame}
\end{document}

This is how I'd like the image to show up (notice the red rectangles):

-
I upvoted both answers. I accepted the first one because even though it seems a bit more complex at first sight, it doesn't require to enter the coordinates of the bounding boxes. – Zet May 16 '14 at 20:35

Here's one possibility:

\documentclass[pdf]{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\lstset{
basicstyle=\ttfamily\small,
columns=fullflexible,
escapeinside={/@}{@/}
}

\newcommand\DrawBox[2]{%
\begin{tikzpicture}[remember picture,overlay]
\draw[red]
([yshift=1.7ex,xshift=-2pt]pic cs:#1) rectangle ([yshift=-.25ex,xshift=2pt]pic cs:#2);
\end{tikzpicture}%
}
\begin{document}

\begin{frame}[fragile,shrink=15]
\frametitle{IAEA phase-space file format}

\DrawBox{starta}{enda}
\DrawBox{startb}{endb}
\DrawBox{startc}{endc}

\begin{lstlisting}
$xxd -b -l 100 ELEKTA_PRECISE_6mv_part1.IAEAphsp 0000000: /@\tikzmark{starta}@/00000001 01101001 01010011 01001000 10111111 10100011 .iSH.. 0000006: 11110110 10101101 11000000 01111110 00000100 10011110 ...~.. 000000c: 11000000 11101011 10001011 10110101 10111110 01110011 .....s 0000012: 01110010 10100111 10111110 11001101 11001100 01001100 r....L 0000018: 00111101 00000100 00000000 00000000 00000000 01100111/@\tikzmark{enda}@/ =....g 000001e: /@\tikzmark{startb}@/00000000 10000000 00000001 00000001 00100011 11011111 ....#. 0000024: 01101110 00111110 00000100 10110010 10000010 00111111 n>...? 000002a: 10110101 01000001 11010000 10111110 01101011 01111111 .A..k. 0000030: 00011111 00111101 10111011 00101111 01010000 10111100 .=./P. 0000036: 11001101 11001100 01001100 00111101 00000000 00000000/@\tikzmark{endb}@/ ..L=.. 000003c: /@\tikzmark{startc}@/00000000 00000000 01100111 00000000 10000000 00000001 ..g... 0000042: 00000001 01101000 10100000 11001110 00111111 01000010 .h..?B 0000048: 10011010 10101100 10111111 00000111 00010110 01100010 .....b 000004e: 01000000 01000010 00101010 01000011 10111101 11110111 @B*C.. 0000054: 11101011 00000101 00111110 11001101 11001100 01001100/@\tikzmark{endc}@/ ..>..L 000005a: 00111101 00000000 00000000 00000000 00000000 01100111 =....g 0000060: 00000000 10000000 00000001 00000001 ....$
\end{lstlisting}
\end{frame}

\end{document}

The idea is to switch to the listings package, since it gives the possibility to escape to LaTeX and then use the tikzmark library to place some marks at appropriate locations; those marks are then used to draw the frames using a simple \DrawBox command; the two arguments for the command are for the begin and end marks for each box.

The code needs two or three runs to stabilize.

-

Here I use the verbatimbox package to set the verbatim content in a box in \scriptsize to fit on the page. Then I use 3 nested \stackinsets to place the red rectangles over it.

\documentclass[pdf]{beamer}
\usepackage{verbatimbox, stackengine}
\usepackage{xcolor}
\begin{document}
\begin{frame}[fragile]
\frametitle{IAEA phase-space file format}
\begin{verbbox}[\scriptsize]
$xxd -b -l 100 ELEKTA_PRECISE_6mv_part1.IAEAphsp 0000000: 00000001 01101001 01010011 01001000 10111111 10100011 .iSH.. 0000006: 11110110 10101101 11000000 01111110 00000100 10011110 ...~.. 000000c: 11000000 11101011 10001011 10110101 10111110 01110011 .....s 0000012: 01110010 10100111 10111110 11001101 11001100 01001100 r....L 0000018: 00111101 00000100 00000000 00000000 00000000 01100111 =....g 000001e: 00000000 10000000 00000001 00000001 00100011 11011111 ....#. 0000024: 01101110 00111110 00000100 10110010 10000010 00111111 n>...? 000002a: 10110101 01000001 11010000 10111110 01101011 01111111 .A..k. 0000030: 00011111 00111101 10111011 00101111 01010000 10111100 .=./P. 0000036: 11001101 11001100 01001100 00111101 00000000 00000000 ..L=.. 000003c: 00000000 00000000 01100111 00000000 10000000 00000001 ..g... 0000042: 00000001 01101000 10100000 11001110 00111111 01000010 .h..?B 0000048: 10011010 10101100 10111111 00000111 00010110 01100010 .....b 000004e: 01000000 01000010 00101010 01000011 10111101 11110111 @B*C.. 0000054: 11101011 00000101 00111110 11001101 11001100 01001100 ..>..L 000005a: 00111101 00000000 00000000 00000000 00000000 01100111 =....g 0000060: 00000000 10000000 00000001 00000001 ....$
\end{verbbox}
\stackinset{c}{2pt}{c}{58pt}{\textcolor{red}{\framebox(230,46){}}}{%
\stackinset{c}{2pt}{c}{10pt}{\textcolor{red}{\framebox(230,46){}}}{%
\stackinset{c}{2pt}{c}{-38pt}{\textcolor{red}{\framebox(230,46){}}}{%
\theverbbox}}}
\end{frame}
\end{document}

-