为什么这种在代码列表中突出显示文本的方式不适用于 beamer 类?

为什么这种在代码列表中突出显示文本的方式不适用于 beamer 类?

这个答案提供了很好的解决方案突出显示代码列表中的文本,同时保持语法突出显示改进之处另一个答案。关键是前者对于该类来说非常有效,beamer但如下面的 MCE 所示,后者却不行(没有突出显示)。

如何使后一种解决方案发挥作用beamer

(请注意其他答案,例如这个不错,提供与之合作的解决方案beamer。)

前一个答案正在与beamer

\documentclass{beamer}
\usepackage{listings,xcolor,tikz}
\newcommand\bh{\tikz[remember picture]
  \node (begin highlight) {};
}
\newcommand\eh{\tikz[remember picture]
  \node (end highlight) {};
  \tikz[remember picture, overlay]
  \draw[yellow,line width=10pt,opacity=0.3] (begin highlight) -- (end
  highlight);
}


\begin{document}

\begin{frame}[fragile]
\bh abc bce bde bde \eh

\begin{lstlisting}[escapechar=@,language=SQL]
WHERE name=@\bh@UNION SELECT@\eh@
\end{lstlisting}
\end{frame}

\end{document}

在此处输入图片描述

后者的答案是不是与...合作beamer

\documentclass{beamer}

% required packages
\usepackage{atbegshi,ifthen,listings,tikz}

% change this to customize the appearance of the highlight
\tikzstyle{highlighter} = [
  yellow,
  line width = \baselineskip,
]

% enable these two lines for a more human-looking highlight
%\usetikzlibrary{decorations.pathmorphing}
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}}}

\begin{document}

\begin{frame}[fragile]
Works in \bh{}plain text too\eh{} (but not across line breaks).

\begin{lstlisting}[escapechar=@, language=SQL, basicstyle=\sffamily, columns=fullflexible]
SELECT name, password FROM users WHERE name='@\bh@' UNION SELECT "10", 1 #@\eh@';
\end{lstlisting}
\end{frame}

\end{document}

在此处输入图片描述

答案1

@marmots 分析问题是正确的。第二个示例中的突出显示效果很好,它位于背景层下方。如果将背景颜色从默认颜色更改为whiteempty则可以看到突出显示:

\documentclass{beamer}

% required packages
\usepackage{atbegshi,ifthen,listings,tikz}

% change this to customize the appearance of the highlight
\tikzset{highlighter/.style={
  yellow,
  line width = \baselineskip,
}}

% enable these two lines for a more human-looking highlight
%\usetikzlibrary{decorations.pathmorphing}
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}}}

\setbeamercolor{background canvas}{bg=}

\begin{document}

\begin{frame}[fragile]
Works in \bh{}plain text too\eh{} (but not across line breaks).

\begin{lstlisting}[escapechar=@, language=SQL, basicstyle=\sffamily, columns=fullflexible]
SELECT name, password FROM users WHERE name='@\bh@' UNION SELECT "10", 1 #@\eh@';
\end{lstlisting}
\end{frame}

\end{document}

在此处输入图片描述

相关内容