使用LaTeX的"列表"包时,如何显示直引号而不是弯引号?

Pau*_*ven 21 latex miktex

我正在使用LaTeX的" listing "包来格式化源代码.不幸的是,我得到的是引号而不是直引号.由于卷曲引号并不总是指向正确的方向,因此它看起来很糟糕.我怎样才能得到直引号?

我不想更改或过滤源代码本身.过滤代码以正确地改变"to"或"'会起作用,但这比在一行上使用多个引号或引用多行引用更容易.或者你可以使用符号或许多其他东西.但我我真的想保持源不变.

示例LaTeX:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}
Run Code Online (Sandbox Code Playgroud)

示例输出(在Windows上使用Miktex): 源代码的图像

(直接链接到错误输出的图像)

dmc*_*kee 22

我在文档中看到(应该与包装一起分发,但可以在http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings.pdf上找到),listings因为可调整的属性调用upquote来处理这个问题.

从文档:

upquote=?true|false?                                                false
  determines whether the left and right quote are printed ‘’ or `'. This 
  key requires the textcomp package if true. 
Run Code Online (Sandbox Code Playgroud)

做点什么

\lstset{upquote=true}
Run Code Online (Sandbox Code Playgroud)

begin列表环境之前,或使用

\begin{lstlisting}[upquote=true]
...
\end{lstlisting}
Run Code Online (Sandbox Code Playgroud)

也可能已在适当的语言定义中为您设置了tis属性(请参阅文档,第12页的预定义语言的大列表).

使用:

\lstloadlanguages{<dialects you need>}
Run Code Online (Sandbox Code Playgroud)

在标题中.然后使用上述任一惯例设置语言以选择选项.


Dav*_*nak 8

dmckee上面的答案可能有效.如果你放弃你的最后一个条件,即你允许更改代码,那么有一个更通用的解决方案,我倾向于使用它(La)TeX呈现一个字符的方式与我预期的不同是使用\symbol命令.我在这里列出它因为它在其他情况下也很有用:

\newcommand{\qq}{\symbol{34}} % 34 is the decimal ascii code for "
Run Code Online (Sandbox Code Playgroud)

然后你的例子:

\begin{lstlisting}
...
print{\qq}The temperature is{\qq},Celsius,{\qq}degrees Celsius{\qq}
...
\end{lstlisting}
Run Code Online (Sandbox Code Playgroud)

注意花括号,据说将列表恢复到LaTeX模式(参见escapechars包的选项.)


Chr*_*isN 6

您是否考虑过将monospaced(打字机)字体用于列表?以下示例有效:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily} % <<< This line added
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}
Run Code Online (Sandbox Code Playgroud)

  • 在基本样式中使用 \ttfamily 对我也很有用,尽管它会导致我失去列表包为我的语言提供的样式(例如加粗关键字)。除了重新定义语言风格之外还有什么想法吗? (2认同)

Buğ*_*dik 6

这是一个解决方案

\usepackage[T1]{fontenc}  
\usepackage{textcomp}  
\usepackage{lmodern}  

% in the listings package configuration, try:  
literate={"}{\textquotedbl}1,  
Run Code Online (Sandbox Code Playgroud)


Len*_*bro 6

我有同样的问题,使用fontspec,并且解决方案是没有设置\defaultfontfeatures{Mapping=tex-text},而是Mapping=tex-text专门设置仅主要和sans字体,并将tt字体留给它自己的设备.:)