listings显示python代码块时高亮问题

发布于 2025-02-26 14:40:19

使用listings宏包显示python代码块,进行如下配置:

\lstset{
    basicstyle=\ttfamily, % 设置字体族
    breaklines=true, % 自动换行
    keywordstyle=\bfseries\color{NavyBlue}, % 设置关键字为粗体,颜色为 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{lstlisting}[language=Python]
    import matplotlib.pyplot as plt # 绘制图形的库
    from matplotlib_venn import venn2 # 绘制二元韦恩图的库,如果是三元则是 venn3

    plt.figure(figsize=(8, 8)) # 初始化设置图形的尺寸
\end{lstlisting}

编译后我发现 as 没有被当做关键字看待,我设置了半天 keyword,问 AI 也无济于事。

我的 listings 宏包版本是 1.10

查看更多

关注者
0
被浏览
193
Sagittarius Rover
我要成为Typst糕手/(ㄒoㄒ)/~~

我不能复现你的问题,我的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}

image.png

请提供你的完整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)也正常:
image.png

你应该修改/补充你的问题,给一份尽可能短的,能体现你的as不变色最小工作示例而不是把大段的(几百行的代码)发上来,没人会看。

主要版本差异目前定位于xcolorlistings宏包上

image.png


Bug 复现使用TL20243月版本frozen version的v1.10版本的listings.sty:

image.png


BTW,恭喜你发现了华点.

另外,提供MWE总是能帮助自己和他人方便定位错误的....

1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览