(这个问题延伸这个。
以下 MCE:
在其第一次编译时,导致:
包 pgf 错误:没有已知的名为 comment 的形状。
如果第一次编译不停止地运行,那么后续的编译就会运行得很好(第三次编译时,所有的交叉引用都是正确的,并且生成的 PDF 文件也符合预期)。
你知道如何让这个示例在第一次编译时顺利运行吗?
\documentclass{beamer}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\usepackage{tikz}
\usetikzlibrary{%
tikzmark,fit,calc,%
positioning
}
\usetikzmarklibrary{listings}
\makeatletter
\renewcommand\iftikzmark[3]{%
\@ifundefined{save@pt@#1-\the\beamer@slideinframe}{%
#3%
}{%
#2%
}%
}%
\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},draw] (#1) {};%
}%
\temp%
\fi%
}
%
\newcommand<>{\balloon}[4][code\the\beamer@slideinframe]{%
\alt#5{\@balloon{#2}{#1}{#3}{#4}}{\tikz[overlay,remember picture]\path (0,0);}%
}
%
\lstnewenvironment{hllisting}[1][]{%
\lstset{name=code\the\beamer@slideinframe,#1}%
}{%
\lstset{name=}%
}
\makeatother
%
\begin{document}
\begin{frame}[fragile]
\begin{hllisting}
1st line of code
2nd line of code
3rd line of code
4th line of code
5th line of code
6th line of code
\end{hllisting}
\balloon{comment}{2}{3}%
\tikz[overlay,remember picture] {
\node[right=of comment] () {Foo};%
}
\end{frame}
\end{document}
答案1
您应该在替代路径中添加合适的节点,这意味着
\ifx@blines\empty
应该有一个
\tikz[remember picture,overlay]\node(#1){};
并且在气球定义中也在带有命令的分支中\tikz
(在这种情况下使用#2而不是#1)。