我不能复现你的问题,我的listings
版本是v1.10c
。
\documentclass{ctexart}
\usepackage{zhlipsum}
\usepackage{listings}
\usepackage[svgnames]{xcolor}
\lstset{
basicstyle=\ttfamily,
breaklines=true,
keywordstyle=\bfseries\color{NavyBlue},
keywords={as, and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield},
emph={},
emphstyle=\bfseries\color{Rhodamine},
commentstyle=\itshape\color{black!50!white},
stringstyle=\bfseries\color{PineGreen!90!black},
columns=flexible,
numbers=left,
numbersep=1em,
numberstyle=\footnotesize,
frame=single,
framesep=0em,
}
\begin{document}
\zhlipsum[1]
\begin{lstlisting}[language=Python]
import matplotlib.pyplot as plt # 绘制图形的库
from matplotlib_venn import venn2 # 绘制二元韦恩图的库,如果是三元则是 venn3
plt.figure(figsize=(8, 8)) # 初始化设置图形的尺寸
\end{lstlisting}
\zhlipsum[2]
\end{document}
请提供你的完整MWE,从\documentclass
开始到\end{document}
结束。
Editted Version
从评论区中下载book.tex
删除无关部分之后,新的MWE(也就是你所说的“另一个样子”的\lstset
)如下:
\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{tikz}
\usepackage[dvipsnames]{xcolor}
\usepackage{ctex}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily, % 设置字体族
breaklines=true, % 自动换行
keywordstyle=\bfseries\color{NavyBlue}, % 设置关键字为粗体,颜色为 NavyBlue
emph=[1]{}, % 指定强调词,如果有多个,用逗号隔开
emph=[2]{as},
emphstyle=[1]\bfseries\color{Rhodamine}, % 强调词样式设置
emphstyle=[2]\bfseries\color{NavyBlue},
commentstyle=\itshape\color{black!50!white}, % 设置注释样式,斜体,浅灰色
stringstyle=\bfseries\color{PineGreen!90!black}, % 设置字符串样式
columns=flexible,
numbers=left, % 显示行号在左边
numbersep=1em, % 设置行号的具体位置
numberstyle=\footnotesize, % 缩小行号
frame=single, % 边框
framesep=0em % 设置代码与边框的距离
}
\begin{document}
\begin{lstlisting}[language=Python]
import matplotlib.pyplot as plt # 绘制图形的库
from matplotlib_venn import venn2 # 绘制二元韦恩图的库,如果是三元则是 venn3
plt.figure(figsize=(8, 8)) # 初始化设置图形的尺寸
\end{lstlisting}
\end{document}
在我的电脑上(windows11 TeXLive2024)也正常:
你应该修改/补充你的问题,给一份尽可能短的,能体现你的as
不变色的最小工作示例,而不是把大段的(几百行的代码)发上来,没人会看。
主要版本差异目前定位于xcolor
与listings
宏包上
Bug 复现使用TL2024
3月版本frozen version的v1.10
版本的listings.sty
:
BTW,恭喜你发现了华点.
另外,提供MWE总是能帮助自己和他人方便定位错误的....
问 listings显示python代码块时高亮问题