答案1
您可以使用tikzmark
库;想法是用来escapeinside
在指定位置放置一些标记,并将这些标记用于框架。
一个小例子(编译代码两到三次以使元素到达其最终位置):
\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\lstset{
language=Java,
basicstyle=\ttfamily,
keywordstyle=\color{green!70!black}
columns=fullflexible,
breaklines=true,
escapeinside=||
}
\begin{document}
\begin{frame}[fragile]
\frametitle{A test frame}
\begin{lstlisting}
class Complex {
|\tikzmark{starta}|public float real, imag;|\tikzmark{enda}|
void Complex(float _real = 0, float _imag = 0) {
|\tikzmark{startb}|real = _real; imag = _imag|\tikzmark{endb}|
}
public static void main(String[] args) {
if (new Complex(1, 1).add(new Complex(-1, -1).equal(0))
system.exit(0)
}
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\draw[red,rounded corners]
([shift={(-3pt,2ex)}]pic cs:starta)
rectangle
([shift={(3pt,-0.65ex)}]pic cs:enda);
\draw[red,rounded corners]
([shift={(-3pt,2ex)}]pic cs:startb)
rectangle
([shift={(3pt,-0.65ex)}]pic cs:endb);
\end{tikzpicture}
\end{frame}
\end{document}