# Beamer \only<2> with lstlisting and tikz picture that references remember-nodes of the listing

I use TikZ, listings and Beamer like this:

\defverbatim\modsecondcodeafter{%
\begin{lstlisting}[mathescape,escapechar=\%]
// Iteriere
for(i = 0 to N-1) {
%\tikzmark{obbegin}%// Calculate indices
$\ldots$
// Calculate coefficient indices
%\tikzmark{obend}%$\ldots$
// Evaluate polynomial
for(j = 0 to 19) {
if(j == 0) {
%\tikzmark{ibtarg}%
}
$\ldots$
}
}
\end{lstlisting}
}
\begin{frame}
\frametitle{Codestructure sample modification}
\modsecondcodeafter
\begin{tikzpicture}[overlay, remember picture, thick]
\draw[decorate,decoration={brace,amplitude=5pt,mirror}] ([shift={(-2pt,6pt)}]pic cs:obbegin)
-- ([xshift=-2pt]pic cs:obend)
coordinate[midway,xshift=-6pt](Btip);

\draw[->, rounded corners] (Btip) -- +(-4pt,0) |- (pic cs:ibtarg);
\end{tikzpicture}
\end{frame}


To get this output for the slide

But as soon as I add \only<2>{ ... } around the listing and TikZ picture, the picture is not displayed correctly anymore on slide 2

Can anyone please help me?

EDIT: Minimal working example

\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usepackage{pgfplots}
\lstset{
language=C++,
basicstyle=\ttfamily\scriptsize,
numbers=left,
captionpos=b
}
\usetikzlibrary{positioning,arrows,tikzmark}
% This is the file main.tex
\usetheme{Darmstadt}

\begin{document}

\defverbatim\modsecondcodeafter{%
\begin{lstlisting}[mathescape,escapechar=\%]
// Iterate
for(i = 0 to N-1) {
%\tikzmark{obbegin}% // Calculate indices
$\ldots$
// Calculate coefficient indices
%\tikzmark{obend}%$\ldots$
// Evaluate polynomial
for(j = 0 to 19) {
if(j == 0) {
%\tikzmark{ibtarg}%
}
$\ldots$
}
}
\end{lstlisting}
}
\begin{frame}
\frametitle{Codestructure sample modification}
\modsecondcodeafter
\begin{tikzpicture}[overlay, remember picture, thick]
\draw[decorate,decoration={brace,amplitude=5pt,mirror}] ([shift={(-2pt,6pt)}]pic cs:obbegin)
-- ([xshift=-2pt]pic cs:obend)
coordinate[midway,xshift=-6pt](Btip);

\draw[->, rounded corners] (Btip) -- +(-4pt,0) |- (pic cs:ibtarg);
\end{tikzpicture}
\end{frame}

\end{document}


And minimal "not working" example (I only added \only<2>{...}).

\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usepackage{pgfplots}
\lstset{
language=C++,
basicstyle=\ttfamily\scriptsize,
numbers=left,
captionpos=b
}
\usetikzlibrary{positioning,arrows,tikzmark}
% This is the file main.tex
\usetheme{Darmstadt}

\begin{document}

\defverbatim\modsecondcodeafter{%
\begin{lstlisting}[mathescape,escapechar=\%]
// Iterate
for(i = 0 to N-1) {
%\tikzmark{obbegin}% // Calculate indices
$\ldots$
// Calculate coefficient indices
%\tikzmark{obend}%$\ldots$
// Evaluate polynomial
for(j = 0 to 19) {
if(j == 0) {
%\tikzmark{ibtarg}%
}
$\ldots$
}
}
\end{lstlisting}
}
\begin{frame}
\frametitle{Codestructure sample modification}
\only<2>{\modsecondcodeafter
\begin{tikzpicture}[overlay, remember picture, thick]
\draw[decorate,decoration={brace,amplitude=5pt,mirror}] ([shift={(-2pt,6pt)}]pic cs:obbegin)
-- ([xshift=-2pt]pic cs:obend)
coordinate[midway,xshift=-6pt](Btip);

\draw[->, rounded corners] (Btip) -- +(-4pt,0) |- (pic cs:ibtarg);
\end{tikzpicture}
}
\end{frame}

\end{document}


## 1 Answer

Instead of the \defverbatim detour, you could use a fragile frame and directly put the lstlisting there. This can then be wrapped in an onlyenv environment.

\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usepackage{pgfplots}
\lstset{
language=C++,
basicstyle=\ttfamily\scriptsize,
numbers=left,
captionpos=b
}
\usetikzlibrary{positioning,arrows,tikzmark}
\usetheme{Darmstadt}

\begin{document}

\begin{frame}[fragile]
\frametitle{Codestructure sample modification}

\begin{onlyenv}<2>
\begin{lstlisting}[mathescape,escapechar=\%]
// Iterate
for(i = 0 to N-1) {
%\tikzmark{obbegin}% // Calculate indices
$\ldots$
// Calculate coefficient indices
%\tikzmark{obend}%$\ldots$
// Evaluate polynomial
for(j = 0 to 19) {
if(j == 0) {
%\tikzmark{ibtarg}%
}
$\ldots$
}
}
\end{lstlisting}
\begin{tikzpicture}[overlay, remember picture, thick]
\draw[decorate,decoration={brace,amplitude=5pt,mirror}] ([shift={(-2pt,6pt)}]pic cs:obbegin)
-- ([xshift=-2pt]pic cs:obend)
coordinate[midway,xshift=-6pt](Btip);

\draw[->, rounded corners] (Btip) -- +(-4pt,0) |- (pic cs:ibtarg);
\end{tikzpicture}
\end{onlyenv}

\end{frame}

\end{document}


• Thanks, awesome. I guess the problem with \only is that it throws away the text on the first slide, so that tikz will not get the correct positions, or something like that? I'm not sure I fully understand. – Johannes Schaub - litb Jan 9 '18 at 20:59
• @JohannesSchaub-litb I think this is combination of multiple problems -> hard to debug :) – user36296 Jan 9 '18 at 21:03
• Just a wild suggestion: create the same tikz nodes in other branches, i.e. in \only<1>, but leave them empty, so they are there, but produce no visible output. – Oleg Lobachev Jan 9 '18 at 21:40
• @OlegLobachev Sounds interesting, can you show a working example? – user36296 Jan 9 '18 at 21:53
• Sure. pastebin.com/0LrfUg5k I don't post it as an answer, as it does nothing with code listing. – Oleg Lobachev Jan 9 '18 at 22:27