答案1
使用\lstset
。这是 MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{keywords}{RGB}{255,0,90}
\definecolor{comments}{RGB}{0,0,113}
\definecolor{red}{RGB}{160,0,0}
\definecolor{green}{RGB}{0,150,0}
\begin{document}
\lstset{language=Python,
basicstyle=\ttfamily\small,
keywordstyle=\color{keywords},
commentstyle=\color{comments},
stringstyle=\color{red},
showstringspaces=false,
identifierstyle=\color{green},
keywords=[2]{pow},
keywordstyle=[2]{\color{orange}},
}
\begin{lstlisting}
#This is a comment with keywords: and or for in range
n = 5
for i in range(0, n):
print( pow(i,3) )
#Over and out
\end{lstlisting}
\end{document}