[Tex/LaTex] Customize Color for lstset morekeywords

colorlistingspython

I have done some extensive searching and have yet to find the answer to changing the color for lstset morekeywords. I want them to be a different color than my regular keywords. Here is what I have right now:

\documentclass{article}

\usepackage{listings,lstautogobble}
\usepackage[usenames,dvipsnames]{color}

% Custom Python Syntax
\lstset
{
    basicstyle=\small\ttfamily,
    commentstyle=\color{Green},
    keywordstyle=\color{Cerulean},
    frame=single,
    language=python,
    morekeywords={True, False},
    numbers=left,
    numbersep=10pt,
    numberstyle=\footnotesize\color{Gray},
    showstringspaces=false,
    stringstyle=\color{Mulberry},
    tabsize=3,
}

% Color Numbers
\lstset
{
    literate=%
    {0}{{{\color{Orange}0}}}1
    {1}{{{\color{Orange}1}}}1
    {2}{{{\color{Orange}2}}}1
    {3}{{{\color{rOrange}3}}}1
    {4}{{{\color{Orange}4}}}1
    {5}{{{\color{Orange}5}}}1
    {6}{{{\color{Orange}6}}}1
    {7}{{{\color{Orange}7}}}1
    {8}{{{\color{Orange}8}}}1
    {9}{{{\color{Orange}9}}}1
}

\begin{document}

    % A basic function that tests if a number is prime
    \lstinputlisting{isPrime.py}

\end{document}

Which produces the following resultScreenshot from my computer

As you can see on lines 16 and 17 True and False are the same color as return and right next to each other. I am just wondering if I could just change the color of selected keywords. For instance changing True and False to a darker blue or another color entirely.

Also: This is less of a concern but is there a better way to change the color of the numbers (not the line numbers, the numbers in the code)? What I have clearly works but I feel like it's a little inefficient.

Best Answer

You need to set the classoffset before you define the morekeywords. The specifications below say that True should be in WildStrawberry and False in LimeGreen:

enter image description here

Here is hack of your customisation:

% Custom Python Syntax
\lstset
{
    basicstyle=\small\ttfamily,
    commentstyle=\color{Green},
    frame=single,
    language=python,
    numbers=left,
    numbersep=10pt,
    numberstyle=\footnotesize\color{Gray},
    showstringspaces=false,
    stringstyle=\color{Mulberry},
    otabsize=3,
    % the more interesting/new bits
    classoffset=1,% starting a new class
    morekeywords={True},
    keywordstyle=\color{WildStrawberry},
    classoffset=2,% starting another class
    morekeywords={False},
    keywordstyle=\color{LimeGreen},
    classoffset=0,% restore to default class if more customisations...
}
Related Question