[Tex/LaTex] Latex lstlistings keywords with multiple colors

colorlistings

I would like to define one of my own language using Latex lstlistings. And there are two types of keywords that I want to color them differently. For example, I want to paint operators in blue, such as JOIN, FOREACH, etc. And for variable types I want to paint them in red, such as int, chararray, etc. Is it possible using \lstdefinelanguage?

Best Answer

According to the links provided by @Cragfelt, I just solved my problem using the following codes:

\definecolor{PigBlue}{RGB}{42, 0, 255}
\definecolor{PigRed}{RGB}{255, 0, 0}
\lstdefinelanguage{PigLatin}
{
    keywords=[1]{
        register ,define ,load ,store ,into ,using ,as ,join ,by ,foreach ,
        generate ,filter ,group ,all ,flatten ,and ,count 
    },
    keywordstyle=[1]\color{PigBlue},
    keywords=[2]{chararray ,int ,float },
    keywordstyle=[2]\color{PigRed},
    sensitive=false,
    morestring=[b]',
    morecomment=[l]{--}
}

And after the definition I can use the language PigLatin anywhere in my codes. For example:

\lstset{language=PigLatin}
\begin{lstlisting}
    raw = LOAD 'raw' USING PigStorage(',') AS (name:chararray, score:int);
\end{lstlisting}

And here is a minimal example:

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\definecolor{PigBlue}{RGB}{42, 0, 255}
\definecolor{PigRed}{RGB}{255, 0, 0}

\lstdefinelanguage{PigLatin}
{
    keywords=[1]{
        register ,define ,load ,store ,into ,using ,as ,join ,by ,foreach ,
        generate ,filter ,group ,all ,flatten ,and ,count 
    },
    keywordstyle=[1]\color{PigBlue},
    keywords=[2]{chararray ,int ,float },
    keywordstyle=[2]\color{PigRed},
    sensitive=false,
    morestring=[b]',
    morecomment=[l]{--}
}

\begin{document}
    \lstset{
        numbers=left,
        language=PigLatin
    }
    \begin{lstlisting}
raw = LOAD 'raw' USING PigStorage(',') AS (name:chararray, score:int);
    \end{lstlisting}
\end{document}