发现列表代码块

发现列表代码块

我正在尝试突出显示 listings 包中的代码行以进行演示。以下工作示例展示了我如何通过破解找到解决方案,但我现在需要一些建议。

让我提请您注意这个问题末尾的图片。它显示了一帧是如何变化的。我希望您注意到,当我逐步解释代码的不同行时,只有“矢量”被揭示出来。布局如下:

                 _______________
* vector        |               |
* matrix        | Code examples |
* list          |_______________|

问题是,当我想继续讨论矩阵的演示时,我无法更改示例代码,即我想发现一个新的代码块。

问题: 我可以“\发现”列表代码块吗?

下面的代码展示了一个最小的工作示例:

\documentclass[14pt]{beamer}
%
% author:   Sigve Karolius
% summary:  Minimal example of listings code highlighting 
% typeset:  pdflatex <filename>.tex

\usetheme[secheader]{Boadilla}
\usepackage{listings}

\lstnewenvironment{beamerR}{%
  \lstset{%
    language=R,%
    identifierstyle=\color{blue}\scriptsize\ttfamily,%
    keywordstyle=\scriptsize\ttfamily,%
    commentstyle=\scriptsize\ttfamily,%
    numberstyle=\tiny\ttfamily,%
    stringstyle=\scriptsize\ttfamily,%
    basicstyle=\scriptsize\ttfamily,%
    emphstyle=\scriptsize\bfseries\ttfamily,%
    moredelim=**[is][\only<+>{\lstset{style=highlight}}]{@}{@},%
    escapechar={|},%
    }%
  }{%
}

\newcommand{\GreenHilight}[1]{% color listings line, necessary strategy for practical reasons.
\makebox[0pt][l]{\color{green!30!white}\rule[-0.316em]{#1}{1.06em}}%Color - |***-------------|
\makebox[0pt][r]{\color{green!30!white}\rule[-0.316em]{0.876em}{1.06em}}%   |---*************|
}

\begin{document}
% ------------------------------------------------------------------------------
%%
%%%
%%
% ------------------------------------------------------------------------------
\begin{frame}[fragile]{Datastructures}
%
\begin{columns}
  %
  \begin{column}{0.28\textwidth}
   \setbeamercovered{transparent}
   \begin{itemize}
     \item<1-7> vector
     \item<8-8> matrix
     \item<9-9> array
     \item<10-10> data frame
     \item<11-11> list
    \end{itemize}
  \end{column}
  \begin{column}{0.68\textwidth}
    \begin{beamerR}
|\uncover<1-2>{\GreenHilight{\linewidth}}|> c(1,2,3,4); 1:4
|\uncover<2-2>{\GreenHilight{\linewidth}}|[1] 1 2 3 4
|\uncover<3-7>{\GreenHilight{\linewidth}}|> lst <- rnorm(1000,mean=0,sd=1)
|\uncover<4-5>{\GreenHilight{\linewidth}}|> lst[-c( (1:990), (992:1000) )]
|\uncover<5-5>{\GreenHilight{\linewidth}}|[1] -0.2585032
|\uncover<6-6>{\GreenHilight{\linewidth}}|> summary( lst )
|\uncover<7-7>{\GreenHilight{\linewidth}}|   Min.   1st Qu.    Median  
|\uncover<7-7>{\GreenHilight{\linewidth}}|-3.878000 -0.694100 -0.002885
|\uncover<7-7>{\GreenHilight{\linewidth}}|   Mean   3rd Qu.      Max.
|\uncover<7-7>{\GreenHilight{\linewidth}}|-0.013390  0.672200  2.919000
    \end{beamerR}
  \end{column}
  %
\end{columns}
\end{frame}
%
\end{document}

在此处输入图片描述

答案1

事实证明,阅读 Beamer 手册是个不错的主意。使用 可以轻松完成此操作\begin{uncoverenv}< > ... \end{uncoverenv}。任何有兴趣尝试的人都可以查看以下示例代码:

我发现这\begin{onlyenv}< > ... \end{onlyenv}更适合我的需求。执行示例代码并比较结果时,原因就很明显了。或者,请参阅 Beamer 版本 3.33 用户指南中的第 83 页。

\documentclass[14pt]{beamer}
%psnup -sscale -nup 10 stackoverflow.ps scaled.ps
% @author:   Sigve Karolius

\usetheme[secheader]{Boadilla}

\usepackage{listings}

\lstdefinestyle{highlight}{%
  language=R,
  identifierstyle=\color{red}\scriptsize\ttfamily,
  keywordstyle=\color{red}\scriptsize\bfseries\ttfamily,,%\color{LSTcolorKeyword}\ttfamily,
  commentstyle=\color{red}\scriptsize\ttfamily,
  numberstyle=\color{red}\tiny,
  stringstyle=\color{red}\scriptsize\ttfamily,
  basicstyle=\color{red}\scriptsize\ttfamily,
  emphstyle=\color{red}\scriptsize\bfseries\ttfamily,
}%
\lstnewenvironment{beamerR}{%
  \lstset{%
    language=R,%
    identifierstyle=\color{blue}\scriptsize\ttfamily,%
    keywordstyle=\scriptsize\ttfamily,%
    commentstyle=\scriptsize\ttfamily,%
    numberstyle=\tiny\ttfamily,%
    stringstyle=\scriptsize\ttfamily,%
    basicstyle=\scriptsize\ttfamily,%
    emphstyle=\scriptsize\bfseries\ttfamily,%
    moredelim=**[is][\only<+>{\lstset{style=highlight}}]{@}{@},%
    escapechar={|},%
    }%
  }{%
}

\newcommand{\GreenHilight}[1]{% color listings line, necessary strategy for practical reasons.
\makebox[0pt][l]{\color{green!30!white}\rule[-0.316em]{#1}{1.06em}}%Color - |***-------------|
\makebox[0pt][r]{\color{green!30!white}\rule[-0.316em]{0.876em}{1.06em}}%   |---*************|
}

\begin{document}
% ------------------------------------------------------------------------------
%%
%%%
%%
% ------------------------------------------------------------------------------
\begin{frame}[fragile]{Datastructures}
%
\begin{columns}
  %
  \begin{column}{0.28\textwidth}
   \setbeamercovered{transparent}
   \begin{itemize}
     \item<1-7> vector
     \item<8-8> matrix
     \item<9-9> array
     \item<10-10> data frame
     \item<11-11> list
    \end{itemize}
  \end{column}
  \begin{column}{0.68\textwidth}
\begin{uncoverenv}<1-7>
\begin{beamerR}
|\uncover<1-2>{\GreenHilight{\linewidth}}|> c(1,2,3,4); 1:4
|\uncover<2-2>{\GreenHilight{\linewidth}}|[1] 1 2 3 4
|\uncover<3-7>{\GreenHilight{\linewidth}}|> lst <- rnorm(1000,mean=0,sd=1)
|\uncover<4-5>{\GreenHilight{\linewidth}}|> lst[-c( (1:990), (992:1000) )]
|\uncover<5-5>{\GreenHilight{\linewidth}}|[1] -0.2585032
|\uncover<6-6>{\GreenHilight{\linewidth}}|> summary( lst )
|\uncover<7-7>{\GreenHilight{\linewidth}}|   Min.   1st Qu.    Median  
|\uncover<7-7>{\GreenHilight{\linewidth}}|-3.878000 -0.694100 -0.002885
|\uncover<7-7>{\GreenHilight{\linewidth}}|   @Mean@   3rd Qu.      Max.
|\uncover<7-7>{\GreenHilight{\linewidth}}|@-0.013390@  0.672200  2.919000
\end{beamerR}
\end{uncoverenv}
%
\begin{uncoverenv}<8-8>
\begin{beamerR}
|\uncover<8-8>{\GreenHilight{\linewidth}}|> c(1,2,3,4); 1:4
\end{beamerR}
\end{uncoverenv}
  \end{column}
  %
\end{columns}
%
\end{frame}
%
\end{document}

相关内容