You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Overleaf LaTeX使用listings包时双引号显示异常求助

解决LaTeX listings包Python代码双引号显示异常问题
  • 先修正代码里的核心错误:你代码中使用的"是HTML转义字符,LaTeX环境下完全不需要,直接替换为普通双引号"即可。
  • 配合upquote包,给listings添加参数配置,强制双引号的显示样式:

修正后的完整示例代码:

\usepackage{listings}
\usepackage{upquote} % 确保已加载该包

\begin{document}

\begin{lstlisting}[language=Python, upquote=true, literate={"} {{\textquotedbl}}1]
for file in files:
    print(file) #Printing the file helped us know visually what was being run
    temp = pd.read_csv(file, compression = 'gzip', header = 0, sep= '\t', quotechar='"', error_bad_lines = False, lineterminator = "\n")
    temp['date'] = pd.to_datetime(temp['date']).dt.date
    temp["is_geotweet"] = temp[["latitude", "longitude"]].notnull().all(axis=1)
    temp["latitude"] = temp["latitude"]
    temp["longitude"] = temp["longitude"]
\end{lstlisting}

\end{document}
  • 关键参数说明:
    • upquote=true:让listings调用upquote包的直引号渲染规则,避免引号自动弯化为结束样式。
    • literate={"} {{\textquotedbl}}1:强制将代码中的双引号替换为LaTeX标准直双引号符号,彻底固定显示效果。
    • 顺便修正了pd.read_csv行末尾缺失的闭合括号,语法错误也可能间接影响渲染结果。

内容的提问来源于stack exchange,提问作者Sid Meka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 10:27:24