![Latex 中 Python 注释关键字的颜色](https://linux22.com/image/414237/Latex%20%E4%B8%AD%20Python%20%E6%B3%A8%E9%87%8A%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E9%A2%9C%E8%89%B2.png)
答案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}