考虑使用以下 MCE:
tikzmark
库及其listings
允许我们在lstlisting
环境中添加标记的库,- 高级
\balloon
宏(由tikzmark
作者提供https://tex.stackexchange.com/a/86457/18401)将一个奇特的节点放在代码的某些行后面(这里做了一些修改,以避免与第一个所需节点之前的行重叠,请参阅 Jellby 在 2014 年 3 月 9 日 18:26 的评论https://tex.stackexchange.com/a/79787/18401)。
关键是,此代码可以很好地与类配合使用,article
但不能与beamer
类配合使用:使用后者,节点不会显示。
% \documentclass{beamer}
\documentclass{article}\usepackage{beamerarticle}
%
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit,calc}
\usetikzmarklibrary{listings}
\lstset{basicstyle=\ttfamily}
\tikzset{
balloon/.style={
draw,
fill=blue!20,
opacity=0.4,
inner sep=4pt,
rounded corners=2pt
},
}
%
\newcommand{\balloon}[4]{%
\pgfmathtruncatemacro\firstline{%
#3-1%
}%
\iftikzmark{line-#2-\firstline-start}{%
\iftikzmark{line-#2-#3-first}{%
\xdef\blines{($ ({pic cs:line-#2-\firstline-start} -| {pic
cs:line-#2-#3-first})!.5!({pic cs:line-#2-#3-first}) $)}%
}{%
\iftikzmark{line-#2-#3-start}{%
\xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic
cs:line-#2-#3-start})}%
}{%
\xdef\blines{(pic cs:line-#2-\firstline-start)}%
}%
}%
}{%
\xdef\blines{}%
}%
\foreach \k in {#3,...,#4} {%
\iftikzmark{line-#2-\k-first}{%
\xdef\blines{\blines (pic cs:line-#2-\k-first) }%
}{} \iftikzmark{line-#2-\k-end}{%
\xdef\blines{\blines (pic cs:line-#2-\k-end) }%
}{}%
}%
\ifx\blines\empty
\else
\edef\temp{\noexpand\tikz[remember picture,overlay]
\noexpand\node[fit={\blines},balloon] (#1) {};}%
\temp
\fi
}
%
\begin{document}
\begin{frame}[fragile]
\balloon{comment}{code}{3}{4}
\begin{lstlisting}[name=code]
Foo Bar Baz
Foo Baz Bar
Bar Foo Baz
Bar Baz Foo
Baz Foo Bar
Baz Bar Foo
\end{lstlisting}
\end{frame}
\end{document}
结果
与article
(和beamerarticle
)
和beamer
答案1
beamer 重新定义\pgfmark
幻灯片并将其编号添加到内部名称中,以使其能够识别叠加。如果您查看辅助,您会发现您得到的名称是line-code-6-start-1
而不是line-code-6-start
。这意味着您的所有操作都\iftikzmark
失败了。
\documentclass{beamer}
%\documentclass{article}\usepackage{beamerarticle}
%
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit,calc}
\usetikzmarklibrary{listings}
\lstset{basicstyle=\ttfamily}
\tikzset{
balloon/.style={
draw,
fill=blue!20,
opacity=0.4,
inner sep=4pt,
rounded corners=2pt
},
}
\makeatletter
%
\newcommand{\balloon}[4]{%
\pgfmathtruncatemacro\firstline{%
#3-1%
}%
\iftikzmark{line-#2-\firstline-start-\the \beamer@slideinframe}{%
\iftikzmark{line-#2-#3-first-\the \beamer@slideinframe}{%
\xdef\blines{($ ({pic cs:line-#2-\firstline-start} -| {pic
cs:line-#2-#3-first})!.5!({pic cs:line-#2-#3-first}) $)}%
}{%
\iftikzmark{line-#2-#3-start-\the \beamer@slideinframe}{%
\xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic
cs:line-#2-#3-start})}%
}{%
\xdef\blines{(pic cs:line-#2-\firstline-start)}%
}%
}%
}{%
\xdef\blines{}%
}%
\foreach \k in {#3,...,#4} {%
\iftikzmark{line-#2-\k-first-\the \beamer@slideinframe}{%
\xdef\blines{\blines (pic cs:line-#2-\k-first) }%
}{} \iftikzmark{line-#2-\k-end-\the \beamer@slideinframe}{%
\xdef\blines{\blines (pic cs:line-#2-\k-end) }%
}{}%
}%
\ifx\blines\empty
\else
\edef\temp{\noexpand\tikz[remember picture,overlay]
\noexpand\node[fit={\blines},balloon] (#1) {};}%
\temp
\fi
}
%
\begin{document}
\begin{frame}[fragile]
\balloon{comment}{code}{3}{4}
\begin{lstlisting}[name=code]
Foo Bar Baz
Foo Baz Bar
Bar Foo Baz
Bar Baz Foo
Baz Foo Bar
Baz Bar Foo
\end{lstlisting}
\end{frame}
\end{document}
添加
重新定义的另一种方法\ballon
是重新定义\iftikzmark
:
\makeatletter
\renewcommand\iftikzmark[3]{%
\@ifundefined{save@pt@#1-\the\beamer@slideinframe}{%
#3%
}{%
#2%
}%
}%
答案2
看起来这个问题已经由上游修复了https://github.com/loopspace/tikzmark/commit/1427ccaa0be7932d964d3f51b26b5a4d3444e1af或者https://github.com/loopspace/tikzmark/commit/00141c09c0b53c14459c206dc23ea6ea90f96b0b
因此,如果您在最新的 TeXlive 发行版上使用上面介绍的解决方法(我曾在 Ubuntu 19.04 和 19.10 以及 Debian/unstable 上尝试过),它将不起作用……您现在需要返回到本页的第一个示例。