Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to surround some source code in my beamer presentation with a lightly shaded box.

More specifically in the C++ source code given below, I would like to shade the code block of the vecAddKernel function with a light grey box. How do I do this.

Here is my code for my beamer frame

\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}
\lstset{ language=C++,
         basicstyle=\ttfamily\scriptsize,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{green}\ttfamily
       }
   \begin{lstlisting}
   // Compute vector sum C = A+B
   // Each thread performs one pair-wise addition
   __global__
   void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
    {
      int i = threadIdx.x + blockDim.x * blockIdx.x;
      if(i<n) C_d[i] = A_d[i] + B_d[i];
    }
    int vecAdd(float* A, float* B, float* C, int n) 
     {
       // A_d, B_d, C_d allocations and copies omitted
       // Run ceil(n/256) blocks of 256 threads each
          dim3 DimGrid(ceil(n/256.0), 1, 1);
          dim3 DimBlock(256, 1, 1);
          vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
     }
    \end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}

Here is the output of compiling it. enter image description here

share|improve this question
    
    
Thank you for accepting answers. –  Gonzalo Medina Aug 23 '12 at 2:15

1 Answer 1

up vote 5 down vote accepted

Here's an option using the lstlinebgrd package:

\documentclass{beamer}
\usepackage{listings}
\usepackage{lstlinebgrd}

\lstset{ language=C++,
         basicstyle=\ttfamily\scriptsize,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{green!60!black}\ttfamily,
         escapeinside=||,
        linebackgroundcolor={\ifnum\value{lstnumber}>3 \ifnum\value{lstnumber}<9\color{gray!30}\fi\fi}
}

\begin{document}

\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}

\begin{lstlisting}
// Compute vector sum C = A+B
// Each thread performs one pair-wise addition
__global__
void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
{
  int i = threadIdx.x + blockDim.x * blockIdx.x;
  if(i<n) C_d[i] = A_d[i] + B_d[i];
}
int vecAdd(float* A, float* B, float* C, int n) 
{
  // A_d, B_d, C_d allocations and copies omitted
  // Run ceil(n/256) blocks of 256 threads each
  dim3 DimGrid(ceil(n/256.0), 1, 1);
  dim3 DimBlock(256, 1, 1);
  vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
}
\end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}

\end{document}

enter image description here

For fancier frames, ther's another option using the improved version of \tizmark (thanks to Andrew Stacey); the idea is to put some marks and then use themarks to draw the colored background:

\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}
\makeatother

\newcommand<>\MyBox[2]{%
  \tikz[remember picture,overlay]
  \filldraw[draw=gray,fill=gray!40,line width=1pt,rectangle,rounded corners]
( $ (pic cs:#1) + (-2pt,1.3ex) $ ) rectangle ( $ (pic cs:#2) +
(\textwidth,-0.5ex) $ )
;}%

\lstset{ language=C++,
         basicstyle=\ttfamily\scriptsize,
         keywordstyle=\color{blue}\ttfamily,
         stringstyle=\color{red}\ttfamily,
         commentstyle=\color{green!60!black}\ttfamily,
         escapeinside=||,
}

\begin{document}

\begin{frame}[fragile]{Simple CUDA Code: A Vector Sum}
\MyBox{start}{end}

\begin{lstlisting}
// Compute vector sum C = A+B
// Each thread performs one pair-wise addition
__global__
|\tikzmark{start}|void vecAddKernel(float* A_d, float* B_d, float* C_d, int n)
{
   int i = threadIdx.x + blockDim.x * blockIdx.x;
   if(i<n) C_d[i] = A_d[i] + B_d[i];
}|\tikzmark{end}|
int vecAdd(float* A, float* B, float* C, int n) 
{
  // A_d, B_d, C_d allocations and copies omitted
  // Run ceil(n/256) blocks of 256 threads each
     dim3 DimGrid(ceil(n/256.0), 1, 1);
      dim3 DimBlock(256, 1, 1);
      vecAddKernnel<<<DimGrid,DimBlock>>>(A_d, B_d, C_d, n);
}
    \end{lstlisting}
\textbf{Note: } Calls to kernels are \textbf{\color{orange}asynchronous}.
\end{frame}

\end{document}

enter image description here

share|improve this answer
    
Why doing something that may seem trivial, in reality when you look at the code, seems complicated to achieve? –  azetina Aug 22 '12 at 13:44
    
@azetina it all depends on the definition of "trivial" ;-). I've updated my answer with another approach. –  Gonzalo Medina Aug 22 '12 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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