4

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.

5
  • 1
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Sep 17, 2014 at 22:39
  • morekeywords merely adds more keywords to the existing list; it won't allow you to give True and False a distinct style from the existing one. If you want to create additional but separate lists of keywords, each list with a distinct style, you should check out tex.stackexchange.com/questions/148193/…
    – jub0bs
    Sep 17, 2014 at 22:41
  • Hey Christian, thanks for the hint. I forgot the document class but I have added that now. Sep 17, 2014 at 22:41
  • @StephenClark You should also add the code for your listing. If you load the filecontents package, you will be able put the contents of isPrime.py inside a \begin{filecontents*}{isPrime.py} ... \end{filecontents*} environment.
    – jub0bs
    Sep 17, 2014 at 22:44
  • Also, you're missing a colon at the end of for val in range(1, num).
    – jub0bs
    Sep 17, 2014 at 23:01

1 Answer 1

3

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...
}
1
  • 3
    Using classoffset is not very robust, though; it's easy to make mistakes with it. A better alternative, IMO, is to specify the class offset like so: keywords=[<class-offset>]<style>.
    – jub0bs
    Sep 18, 2014 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .