I have some source code in Python and I would like to highlight numbers, but only if the number is part of an operation or in a declaration, that is, if I name a variable variable1
for example, I don't want the number to be coloured. To achieve that, here is the code I have written:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[spanish]{babel}
\usepackage{listings,lstautogobble} %Código en latex
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0.04314,0.6745,0.07843}
\definecolor{codegray}{rgb}{0.7059,0.6863,0.702}
\definecolor{codered}{rgb}{0.5373,0.02745,0.06275}
\definecolor{codeblue}{rgb}{0.071,0.0258,0.9882}
\definecolor{codepurple}{rgb}{0.6,0.02745,0.5961}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegray},
classoffset=1, % starting new class
otherkeywords={range, len},
keywordstyle=\color{codepurple},
classoffset = 2
morekeywords = {print}
keywordstyle=\color{codered},
classoffset = 0,
keywordstyle=\color{codeblue},
stringstyle=\color{codegreen},
basicstyle=\ttfamily,
breaklines=true,
showspaces=false,
showstringspaces=false,
autogobble=true
}
\lstset{literate=
{0}{{{\color{codered}0}}}1 {1}{{{\color{codered}1}}}1
{2}{{{\color{codered}2}}}1 {3}{{{\color{codered}3}}}1
{4}{{{\color{codered}4}}}1 {5}{{{\color{codered}5}}}1
{6}{{{\color{codered}6}}}1 {7}{{{\color{codered}7}}}1
{8}{{{\color{codered}8}}}1 {9}{{{\color{codered}9}}}1
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{Ã}{{\~A}}1 {ã}{{\~a}}1 {Õ}{{\~O}}1 {õ}{{\~o}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\euro}}1 {£}{{\pounds}}1 {«}{{\guillemotleft}}1
{»}{{\guillemotright}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1
{º}{{\textordmasculine}}1
}
\lstset{style=mystyle}
\title{Foo}
\begin{document}
\maketitle
\begin{lstlisting}[language=Python]
def extract_mfcc(audio,rate,winstep_nuevo):
'''Cálculo de los coeficientes MFCC y de su primera y segunda derivada'''
#Ventana de 20 ms, solapamiento de 10 ms, 22 coeficientes y 2048 puntos para la fft.
#Nº de filas=nº de tramas. nº de columnas= nº de coeficientes
mfcc_c= mfcc.mfcc(audio,rate,winlen=0.020,winstep=winstep_nuevo,numcep=22,nfft=2048,winfunc=np.hamming)
#Cálculo de la primera derivada de los coeficientes
delta=librosa.feature.delta(mfcc_c,order=1)
#Cáclulo de la segunda derivada de los coeficientes
delta2=librosa.feature.delta(mfcc_c,order=2)
return mfcc_c,delta,delta2
\end{lstlisting}
\end{document}
But with this configuration numbers are coloured wherever they are, even in comments, as it is shown in this image:
To avoid that, I saw that writing *{0}{{{\color{codered}0}}}1
instead of {0}{{{\color{codered}0}}}1
would solve the problem. The following image shows the result for that change:
Nevertheless, although it solves the numbers issue, the problem that arises now is that if I write a special character, for example á
or º
in a comment, latex doesn't recognise it, and I don't know how to solve this problem and colour the numbers correctly at the same time.
Thank you for your answers.