# TikZ annotation inside listing inside TikZ node

In the below code, on the first frame I typeset a code snippet with verbatim, and all is well.

On the second frame I want to add a TikZ annotation to the code snippet, but I have to switch back to manually specifying spaces and line breaks, because trying to use TikZ inside alltt - or a fancyvrb environment with commandchars enabled - gives me error messages about TikZ not liking the verbatim packages messing with the dollar sign. Also it messes up the spacing in the annotated line, the numbers do not align vertically with the others.

On the third frame I want to put the annotated snippet in a callout. In order to do that I have to use a savebox, which is fine, but for some reason I have to enclose the snippet in a tikzpicture, otherwise the savebox gives me an error message about a missing \item.

My question is

• How to add this TikZ annotation to a verbatim (or preferably lstlisting) environment?
• Please keep in mind that the solution must work inside the callout too.
• Also it would be nice if the spacing was preserved.
• Why did I need a tikzpicture inside the savebox?
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.callouts}
\newcommand{\circleerror}[1]{\tikz[baseline=(char.base)]\node(char){#1} [overlay] circle (10pt) [at=(char), draw, red];}
\newsavebox{\mybox}

\begin{document}
\begin{frame}[fragile]
\begin{verbatim}
CONTROL
0.0 0.0  0.0  0.0  0.0
0.0 0.0  0.15 0.0  0.0
0.0 0.15 0.45 0.15 0.0
0.0 0 0  0.15 0.0  0.0
0.0 0.0  0.0  0.0  0.0
\end{verbatim}
\end{frame}

\begin{frame}
\ttfamily\raggedright
CONTROL\\
0.0 0.0\ \ 0.0\ \ 0.0\ \ 0.0\\
0.0 0.0\ \ 0.15 0.0\ \ 0.0\\
0.0 0.15 0.45 0.15 0.0\\
0.0\
\circleerror{0 0}%
\ 0.15 0.0\ \ 0.0\\
0.0 0.0\ \ 0.0\ \ 0.0\ \ 0.0
\end{frame}

\begin{frame}
Some text.

Some other text.\tikz[overlay, remember picture] \coordinate (note);

\sbox{\mybox}{\begin{tikzpicture}
\node [align=left, font=\ttfamily] {
CONTROL\\
0.0 0.0\ \ 0.0\ \ 0.0\ \ 0.0\\
0.0 0.0\ \ 0.15 0.0\ \ 0.0\\
0.0 0.15 0.45 0.15 0.0\\
0.0\
\circleerror{0 0}%
\ 0.15 0.0\ \ 0.0\\
0.0 0.0\ \ 0.0\ \ 0.0\ \ 0.0};
\end{tikzpicture}}

\begin{tikzpicture}[overlay, remember picture]
\node at (7,2) [fill=yellow, ellipse callout, callout absolute pointer=(note)] {\usebox{\mybox}};
\end{tikzpicture}
\end{frame}
\end{document}

• The tikzpicture inside the savebox is needed, because a \node alone cannot be compiled to a TeX box content. It would be the same as using a \node directly (without tikzpicture). – Thomas F. Sturm Jan 7 '15 at 16:58
• @ThomasF.Sturm But why can't I put only the text in the savebox? Why do I need a TikZ node at all? – marczellm Jan 7 '15 at 17:56
• You could do it without the TikZ node, but you would need another boxing like a minipage or a \parbox to use the line breaks inside the savebox. In any case: it would not work for verbatim text. – Thomas F. Sturm Jan 8 '15 at 7:55

Maybe, the following is what you are looking for:

• The verbatim text is set with the listings package. Options can be set by listing options.

• The red circle is set inside the finish option. Note that positioning has to be done by hand, because it is drawn over the listing.

• For the callout, the listing is saved and reloaded again. Please, note the additional hbox option to restrict the size of the listing.

• The operations are done using the tcolorbox package and its listings library.

This gives:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{shapes.callouts}
\usepackage[most]{tcolorbox}

\begin{document}

\begin{frame}[fragile]
\begin{tcblisting}{listing only,
listing options={aboveskip=0pt,belowskip=0pt,
columns=fullflexible, keepspaces=true,
basicstyle=\ttfamily,nolol},
blanker}
CONTROL
0.0 0.0  0.0  0.0  0.0
0.0 0.0  0.15 0.0  0.0
0.0 0.15 0.45 0.15 0.0
0.0 0 0  0.15 0.0  0.0
0.0 0.0  0.0  0.0  0.0
\end{tcblisting}
\end{frame}

\begin{frame}[fragile]
\begin{tcblisting}{listing only,
listing options={aboveskip=0pt,belowskip=0pt,
columns=fullflexible, keepspaces=true,
basicstyle=\ttfamily,nolol},
blanker,
finish={\draw[red] (1.1,0.75) circle (10pt);}}
CONTROL
0.0 0.0  0.0  0.0  0.0
0.0 0.0  0.15 0.0  0.0
0.0 0.15 0.45 0.15 0.0
0.0 0 0  0.15 0.0  0.0
0.0 0.0  0.0  0.0  0.0
\end{tcblisting}
\end{frame}

\begin{frame}[fragile]
Some text.

Some other text.\tikz[overlay, remember picture] \coordinate (note);

\begin{tcboutputlisting}
CONTROL
0.0 0.0  0.0  0.0  0.0
0.0 0.0  0.15 0.0  0.0
0.0 0.15 0.45 0.15 0.0
0.0 0 0  0.15 0.0  0.0
0.0 0.0  0.0  0.0  0.0
\end{tcboutputlisting}

\begin{tikzpicture}[overlay, remember picture]
\node at (7,2) [fill=yellow, ellipse callout, callout absolute pointer=(note)]
{\tcbinputlisting{listing only,
listing options={aboveskip=0pt,belowskip=0pt,
columns=fullflexible, keepspaces=true,
basicstyle=\ttfamily,nolol},
blanker,hbox,
finish={\draw[red] (1.1,0.75) circle (10pt);}}
};
\end{tikzpicture}

\end{frame}

\end{document}


• Thomas, if you're not a TUG member already, you should put your name down for this :) – jub0bs Jan 9 '15 at 9:18
• @Jubobs I was too dumb or tired when I read your comment to see the link you gave me. But now I found it and I thank you for your kind invitation :-) I was'nt aware that there is something like a TUG Membership of TeX.SX. Now it is a little bit too late to put my name down, but I take the chance to vote :-) – Thomas F. Sturm Jan 13 '15 at 7:01
• Ah, that's too bad. Remember it for next year! – jub0bs Jan 13 '15 at 10:48
• It seems that it's not too late to put your name down! Sean Allred just did. – jub0bs Jan 13 '15 at 23:03
• @Jubobs You're right, thank you. I've added my name now. Better late than never :-) – Thomas F. Sturm Jan 15 '15 at 7:41