我使用TikZ
,listings
并且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}