5

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:

enter image description here

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:

enter image description here

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.

  • Please give a fully compilable code. – AndréC Dec 22 '19 at 8:07
  • All right, now a fully compilable code is available – Josemi Dec 22 '19 at 8:36
  • I didn't quite understand your problem, can you illustrate it with a screenshot showing what you think is wrong? – AndréC Dec 22 '19 at 10:30
  • I have made some changes, if it isn't completely clear let me know – Josemi Dec 22 '19 at 11:11
  • Yes, it is very well explained, everyone can understand the problem without making too much effort. :-) – AndréC Dec 22 '19 at 11:14
1
+50

You can use package minted. You have to install pygments (can easily be installed in linux by sudo apt-get install python-pygments or pip install pygments) and use -shell-escape flag in the command. eg. If you are using pdflatex, your regular command is pdflatex <filename>. It should now be pdflatex -shell-escape <filename>. See the following MWE.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[spanish]{babel}
\usepackage{minted} %Código en latex
\usemintedstyle{vim}
\usepackage{xcolor}
\title{Foo}
\author{Josemi}

\begin{document}
    \maketitle
\begin{minted}[linenos,breaklines]{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{minted}
\end{document}

This gave me the following output.

2

PS - You can remove optional argument [linenos] or [breaklines] if you don't need those functions. I've added them because I like them ;)

| improve this answer | |
  • I have seen in overleaf.com/learn/latex/Code_Highlighting_with_minted that there are some colour styles, but it could be defined a new colour style? – Josemi Jan 2 at 21:48
  • Here is the reference guide for color-themes of package minted. You can add command \usemintedstyle{-style-name-} (vim is a nice style with really good colors.) in the preamble to change the color scheme. – Niranjan Jan 3 at 4:27
  • Thank you, but that is what I was refering to. I mean if a could create a new style that is not predefined – Josemi Jan 3 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.