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?