8

I want to add some Python source code to my LaTeX document, but I want to apply some customization. Currently, I have something like this:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

\begin{document}

\begin{lstlisting}[language=Python, caption=Python example]
# Importing the sys package
import sys

def testfunc(a):
    return a + a

print "Testing:", str(testfunc(3))
\end{lstlisting}

\end{document}

The point is that above customization that I have works to some extend, but I want to further customize it in two ways. First, I want to apply a specific color to all the numbers in the source code. For example, in the above example, the number 3 inside str(testfunc(3)) should have a specific color that I set (let's say blue). So I simply want all the numbers to have a custom color that I set. Second, I want certain keywords of my choice to have another color (let's say yellow). For example, I want the testfunc, print and str keywords to be yellow. So can one add these type of customization in lstlisting environment? If yes, how?

1

2 Answers 2

7

The following code does the trick for the three keywords; as far as I can remember, the listings package makes no provision for coloring the numbers in the code.

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{codeyellow}{rgb}{0.67,0.67,0.0}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    language=Python,
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    emph={testfunc,print,src},
    emphstyle=\color{codeyellow},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

\begin{document}

\begin{lstlisting}[style=mystyle, caption=Python example]
# Importing the sys package
import sys

def testfunc(a):
    return a + a

print "Testing:", str(testfunc(3))
\end{lstlisting}

\end{document}
3
  • Way cleaner solution than mine. +1
    – gebruiker
    Commented Mar 30, 2018 at 12:12
  • For the numbers you can probably make all numerals keywords...
    – gebruiker
    Commented Mar 30, 2018 at 12:13
  • @gebruiker: I think it is much cleaner with emph/emphstyle.
    – GuM
    Commented Mar 30, 2018 at 12:16
3

I got the result you want by using the literate-key (manual section 6.4).

literate=   {0}{{{\color{blue}0}}}1
            {1}{{{\color{blue}1}}}1
            {2}{{{\color{blue}2}}}1 
            {3}{{{\color{blue}3}}}1 
            {4}{{{\color{blue}4}}}1 
            {5}{{{\color{blue}5}}}1 
            {6}{{{\color{blue}6}}}1 
            {7}{{{\color{blue}7}}}1 
            {8}{{{\color{blue}8}}}1 
            {9}{{{\color{blue}9}}}1 
            {testfunc}{{{\color{yellow}testfunc}}}8

Add this to mystyle and you're good to go.

(I can't explain why you would need so many curly braces)

N.B. I cannot think of a way to make decimal points blue as well, whilst leaving normal period alone. As it is, only numerals are affected.

4
  • Right! (+1). For the number of braces, you could omit the pair around the “find text” (e.g., 3{{{\color{codeblue}3}}}1), but I’d agree with you that the “replacement text” apparently needs one pair more than what would seem logical. Keep in mind, however, that TeX sometimes strips a pair of braces off a macro argument.
    – GuM
    Commented Mar 30, 2018 at 12:30
  • One of us must resolve to include the other’s answer into their own, so that a complete answer is made. Since you have fewer rep points than I have, I think it is fair to let you do so. (:-)
    – GuM
    Commented Mar 30, 2018 at 12:39
  • I would say we just gave two different ways to solve the problem. I don't fell there's a need to merge. Both answers can hold their own. (I appreciate you would let my answer be the main one, but I pay very little attention to my rep points.)
    – gebruiker
    Commented Mar 30, 2018 at 12:46
  • @gebruiker This is quite interesting and seems to work, but the problem is that if let's say variable name contains a number like var1 then, the 1 there is also with a different color. Whereas I need just regular numbers, or more precisely lone numbers, not part of any other text. Also for a text, something like testfunc it seems to leave blank space before and after it.
    – pixel
    Commented Mar 30, 2018 at 15:05

You must log in to answer this question.

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