11

I'm trying to use TikZ in beamer to point to a line of code. Here's what I have so far:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{fancyvrb}

\begin{document}

\begin{frame}[fragile,t] 
\frametitle{test}
\begin{Verbatim}
example code 
lines
\end{Verbatim}
\tikz \node [circle,fill=black] {};
\begin{Verbatim}
here 
\end{Verbatim}
\end{frame}

\end{document}

The node, however, appears on a new line. I want to put it on the right of the line saying, "lines" so that I can then use TikZ overlays to point an arrow to it and put some explanatory lines on the right.

I have seen examples of this but they either use math or plain text. How can I do this with Verbatim?

1 Answer 1

14

You need to use commandchars. In my example |replaces \. §and !are used to replace the accolades {and }

\documentclass{beamer}
\usepackage{tikz}
\usepackage{fancyvrb}

\begin{document}
 \def\firstnode{\tikz[remember picture] \node[circle,fill=blue] (n1) {};}  
 \def\secondnode{\tikz[remember picture] \node[circle,fill=red] (n2) {};} 

\begin{frame}[fragile,t] 
\frametitle{test} 

\begin{Verbatim} [commandchars=\|\§\! ]
example code 
lines here |firstnode  
\end{Verbatim}

\begin{Verbatim}[commandchars=\|\§\! ]      
 |secondnode   Explanation
\end{Verbatim} 

 \begin{tikzpicture}[remember picture,overlay]
        \path[->,line width=1pt,dashed,red!40,>=latex] (n1) edge [bend left] (n2);
\end{tikzpicture} 
\end{frame}     
\end{document} 

enter image description here

4
  • Works like a charm. just one follow-up though. How do I insert that weird snake-like symbol? Can I use another one in its place?
    – recluze
    Nov 20, 2011 at 10:17
  • @recluze You can use the symbol that you want. Examples in Latex Companion use [ and ]. The most important here is | Nov 20, 2011 at 10:28
  • Great. A +1 for the output :)
    – recluze
    Nov 20, 2011 at 10:30
  • taking inspiration from this great answer, I made a little variation: instead of doing \def..., I have the following \newcommand{\mytikzrefnode}[1]{\tikz[remember picture,baseline=-.5ex] \coordinate (#1) {};}, which allows me to do \mytikzrefnode{n1}
    – MarcoS
    Mar 11, 2013 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .