Latex lstlistings keywords with multiple colors

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?

• You might take a look at this and this. – Cragfelt Dec 17 '17 at 10:09
• @Cragfelt Thanks for the link! It just solved my problem perfectly. I defined two sets of keywords and keywordstyle and got what I desired. – Trams Dec 18 '17 at 1:55
• In spite of that you have solved your problem, you should post it to respond this question for others can value your work. – Cragfelt Dec 18 '17 at 2:03

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}

• Amazing answer. I didn't realize it was possible to define your own languages, and this was such a perfect answer. Both the code to create the language and the minimal example showing its usage. Thank you! – Greg Hilston Nov 8 '18 at 18:34