1

I am trying to highlight code lines in the listings package for a presentation. The following working example shows how I have been able to hack my way to a solution, however I am now at the point where I need some advice.

Let me draw your attention to the image at the end of this question. It shows how one frame changes. I want you to notice how only "vector" is uncovered while I walk through explaining different lines of the code. The layout is as follows:

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

The problem is that I am not able to change the example code when I want to continue the presentation talking about matrices, i.e. I want to uncover a new code block.

Question: Can I ´\uncover´ listings code blocks?

The following code shows a minimal working example:

\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}

enter image description here

2

It turns out that the beamer manual is a good idea to read. This is easily done using \begin{uncoverenv}< > ... \end{uncoverenv}. Anyone interested in trying this can check out the following example code:

I found that \begin{onlyenv}< > ... \end{onlyenv} suits my needs better. The reason is apparent when the example code is executed and the result is compered. Alternatively, page 83 in the Beamer version 3.33 user guide.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.