Beamer \only 带有 lstlisting 和引用列表记忆节点的 tikz 图片

Beamer \only 带有 lstlisting 和引用列表记忆节点的 tikz 图片

我使用TikZlistings并且Beamer像这样:

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

要获取幻灯片的输出

在此处输入图片描述

但是,当我添加\only<2>{ ... }列表和 TikZ 图片时,幻灯片 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}
      \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}

以及最小的“不工作”示例(我只添加了\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

除了绕行\defverbatim,您还可以使用fragile框架并直接将其放在lstlisting那里。然后可以将其包裹在onlyenv环境中。

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

在此处输入图片描述

相关内容