列表环境中不同关键字的不同颜色不起作用



我已经阅读了几个答案,我可以使用[number]listings环境中为不同的关键字设置不同的颜色。然而,当我以这种方式尝试时,它不起作用。

以下是我的序言和一个小脚本示例:

usepackage{listings}
usepackage{color}
usepackage[dvipsnames]{xcolor}
definecolor{gray}{rgb}{0.95,0.95,0.95}
definecolor{Green}{rgb}{0.1,0.69,0.1}
renewcommand{lstlistingname}{Codice}
lstdefinelanguage{Python}
{
keywords={from, import, def, return, as, for, if, in, len},
keywordstyle=color{Green},
keywords=[2]{centers}
keywordstyle=[2]{blue} 
morecomment=[l]{#},
morestring=[b]",
alsodigit={-},
alsoletter={&}
}

lstdefinestyle{custompython}{
language=Python,
frame=tlrb,
aboveskip=3mm,
belowskip=5mm,
backgroundcolor=color{gray},
showstringspaces=true,
columns=flexible,
basicstyle={smallttfamily},
numbers=left,
numberstyle=tinycolor{orange}ttfamily,
numbersep=5pt,
commentstyle=color{orange},
stringstyle=color{purple},
commentstyle=smallcolor{red}ttfamily,
breaklines=false,
breakatwhitespace=true
tabsize=5
}
begin{lstlisting}[firstnumber=1,language=Python, style=custompython]
from pyLensLib.maputils import map_obj, contour_fit
def getImageEllipticity( img, fsize, f=0.05):

m, cnt = map_obj(img), m.get_contours(lev=f)
centers, axesList = [], []

return centers
end{lstlisting}

用户Symbol 1已经给出了一个非常好的答案。示例语言是用C++编写的,但设计代码样式的方法是一样的:

使用lslisting 突出显示其他关键字和符号

documentclass[10pt,a4paper]{scrartcl}

usepackage{xcolor}
definecolor{main-color}{rgb}{0.6627, 0.7176, 0.7764}
definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}
definecolor{string-color}{rgb}{0.3333, 0.5254, 0.345}
definecolor{key-color}{rgb}{0.8, 0.47, 0.196}
usepackage{listings}

lstdefinestyle{mystyle}
{
language = C++,
basicstyle = {ttfamily color{main-color}},
backgroundcolor = {color{back-color}},
stringstyle = {color{string-color}},
keywordstyle = {color{key-color}},
keywordstyle = [2]{color{lime}},
keywordstyle = [3]{color{yellow}},
keywordstyle = [4]{color{teal}},
otherkeywords = {;,<<,>>,++},
morekeywords = [2]{;},
morekeywords = [3]{<<, >>},
morekeywords = [4]{++},
}
begin{document}
begin{lstlisting}[firstnumber=1,language=C++, style=mystyle]
#include <iostream>
using namespace std;
int x = 2;
//comment
for (int i = 0; i < x; ++i) {
cout << "stand_alone_complex" << endl;
}
end{lstlisting}
end{document}

最新更新