Overleaf LaTeX:使用列表包时,双引号在 Latex 中显示为结束引号

Overleaf LaTeX: Double quotes showing as End Quotes in Latex when using listings package

提问人:Sid Meka 提问时间:6/24/2023 更新时间:6/24/2023 访问量:98

问:

嘿 Stack Overflow 社区,

我在LaTeX中的以下代码中遇到了以下问题:

\begin{lstlisting}[language=Python]

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}

每当我尝试使用双引号时,代码都会显示为结束引号。我尝试在 LaTeX 中使用 upquote 包,这对我的单引号有所帮助。但是,双引号并未因此而固定。我尝试在列出的 for 循环中使用反引号,但这没有帮助。

如果可以的话,请帮帮我!

LaTeX 报价 Overleaf lstlisting

评论


答:

0赞 rubber_duck 6/24/2023 #1

双引号 (“) 不是用来分隔参数或标记字符串的开头和结尾吗?

当您在“lstlisting”环境中使用双引号时,LaTeX 会将它们解释为字符串的开头和结尾,从而导致代码出现不正确。

您可以尝试“morestring”选项;指定应被视为字符串分隔符的字符。

\begin{lstlisting}[language=Python,morestring=[s]{"""}{"""}]
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}

此外,请确保在文档序言中包含“列表”包,以便“lstlisting”环境正常工作。

我希望这会有所帮助!当心!