结果

结果

考虑使用以下 MCE:

关键是,此代码可以很好地与类配合使用,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 上尝试过),它将不起作用……您现在需要返回到本页的第一个示例。

相关内容