3

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

enter image description here

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

enter image description here

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}
3

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}

enter image description here

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.