Can one customize lstlisting environment?
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}
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.