我有一些 Python 源代码,我想突出显示数字,但仅当数字是操作的一部分或声明时才这样,也就是说,如果我命名一个变量variable1
,我不希望数字被着色。为了实现这一点,下面是我编写的代码:
\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}
但是,在这种配置下,数字在任何地方都会被着色,即使在注释中也是如此,如下图所示:
为了避免这种情况,我发现用*{0}{{{\color{codered}0}}}1
代替{0}{{{\color{codered}0}}}1
可以解决问题。下图显示了该更改的结果:
尽管如此,虽然它解决了数字问题,但现在出现的问题是,如果我写一个特殊字符,例如á
在º
注释中,乳胶就无法识别它,而且我不知道如何解决这个问题并同时正确地为数字着色。
谢谢您的回答。
答案1
您可以使用包minted
。您必须安装(可以通过或pygments
轻松在 Linux 中安装)并在命令中使用标志。例如。如果您使用的是 pdflatex,您的常规命令是。现在应该是。请参阅以下 MWE。sudo apt-get install python-pygments
pip install pygments
-shell-escape
pdflatex <filename>
pdflatex -shell-escape <filename>
\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}
这给了我以下输出。
PS - 如果不需要这些函数,可以删除可选参数 [linenos] 或 [breaklines]。我添加了它们,因为我喜欢它们 ;)