1

Is it possible to set two Colors to Keywords inside of a Codeblock?

I have the current settings for my Codeblock

\lstset{language=[Sharp]C,
captionpos=b,
%numbers=left, %Nummerierung
%numberstyle=\tiny, % kleine Zeilennummern
frame=lines, % Oberhalb und unterhalb des Listings ist eine Linie
showspaces=false,
showtabs=false,
numbers=left,
basicstyle = small,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
morekeywords={HaarCascade, Image, partial, var, value, get, set},
keywordstyle=\color{bluekeywords},
stringstyle=\color{redstrings},
basicstyle=\ttfamily\small,
}

I can defince such keywords as HaarCascade or Image, and they would appear in my code as blue. But what can I to set those two specific keywords in another color, lets say turquoise?

  • Welcome to TeX.SX!! It is better to post a full minimal working example that starts with a \documentclass command, has a minimal preamble and then \begin{document}...\end{document}. Unless the problem is a compilation error, the code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much ore likely that they will! – Andrew Feb 17 '16 at 10:30
  • Fir your question, see page 31 of the listings manual. In particular, the use of classoffset. – Andrew Feb 17 '16 at 10:34
2

You can use any number of different keyword classes with the syntax keyword=[class]... and keywordstyle=[class].... class is just a natural number used as id for the class.

In your case:

\documentclass{article}
\usepackage[usenames]{xcolor}

\definecolor{turquoise}{RGB}{64, 224, 208}
\colorlet{greencomments}{green!50!black}
\colorlet{bluekeywords}{blue}
\colorlet{redstrings}{red}

\usepackage{listings}
\lstset{language=[Sharp]C,
captionpos=b,
%numbers=left, %Nummerierung
%numberstyle=\tiny, % kleine Zeilennummern
frame=lines, % Oberhalb und unterhalb des Listings ist eine Linie
showspaces=false,
showtabs=false,
numbers=left,
basicstyle = small,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
morekeywords=[1]{partial, var, value, get, set},
morekeywords=[2]{HaarCascade, Image},
keywordstyle=\color{bluekeywords},
keywordstyle=[2]\color{turquoise},
stringstyle=\color{redstrings},
basicstyle=\ttfamily\small,
}
\begin{document}
\begin{lstlisting}
using System;
// From wikipedia
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, world!");
        Image img;
    }
}
HaarCascade, Image, partial, var, value, get, set
\end{lstlisting}

\end{document}

preview

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.