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 color a few keywords in my slides under the listings environment

In the following contrived example, I would like to color the __shared__ word with orange and WIDTH with blue. How would I modify this?

    \begin{frame}[fragile]
               \lstset{language=C++,
               basicstyle=\ttfamily\scriptsize,
               keywordstyle=\color{blue}\ttfamily,
               stringstyle=\color{red}\ttfamily,
               commentstyle=\color{green}\ttfamily,
               breakline=true
              }
      \begin{lstlisting}
    __global__ 
    void MatMulKernelFast(double* d_M,double* d_N,double* d_P,int WIDTH)
    {
     __shared__ double ds_M[TILE_WIDTH][TILE_WIDTH];
     __shared__ double ds_N[TILE_WIDTH][TILE_WIDTH];
     }
  \end{lstlisting} 

\end{frame}

Here is the output of the previous code enter image description here

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 2 at 11:25

1 Answer 1

You can use keywords=[2]{...}, keywordstyle=[2] for a second group of keywords with different style (in this case, for the keywords in orange color); using otherkeywords= you can use the blue color for WIDTH:

\documentclass{beamer}
\usepackage{listings}

\lstset{language=C++,
               basicstyle=\ttfamily\scriptsize,
               keywordstyle=\color{blue}\ttfamily,
               otherkeywords={WIDTH},
               keywords=[2]{__shared__},
               keywordstyle=[2]\color{orange}\ttfamily,
               stringstyle=\color{red}\ttfamily,
               commentstyle=\color{green}\ttfamily,
               breaklines=true,
}

\begin{document}

\begin{frame}[fragile]
\begin{lstlisting}
__global__ 
void MatMulKernelFast(double* d_M,double* d_N,double* d_P,int WIDTH)
{
  __shared__ double ds_M[TILE_WIDTH][TILE_WIDTH];
  __shared__ double ds_N[TILE_WIDTH][TILE_WIDTH];
}
\end{lstlisting} 

\end{frame}

\end{document}

enter image description here

share|improve this answer
    
This is a great answer. @smilingbuddha may I suggest you accept this as the accepted response? –  Rob Stewart Apr 22 at 13:44

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.