为文本块添加阴影

为文本块添加阴影

我想用浅色阴影框围绕我的 beamer 演示文稿中的某些源代码。

更具体地说,在下面给出的 C++ 源代码中,我想用vecAddKernel浅灰色框遮盖函数的代码块。我该怎么做。

这是我的投影仪框架的代码

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

这是编译后的输出。 在此处输入图片描述

答案1

这是使用该lstlinebgrd包的一个选项:

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

在此处输入图片描述

对于更精美的框架,还有另一种选择,即使用改进版本\tizmark(感谢 Andrew Stacey);其想法是放置一些标记,然后使用这些标记来绘制彩色背景:

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

在此处输入图片描述

相关内容