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

发布于 2024-07-11 09:50:36 字数 967 浏览 5 评论 0原文

我正在使用 LaTeX 的“listings" 包用于格式化源代码。 不幸的是我得到的是弯引号而不是直引号。 由于大引号并不总是指向正确的方向,所以看起来很糟糕。 我怎样才能得到直接引号?

我不想更改或过滤源代码本身。 过滤代码以正确地将 " 更改为 `` 或 '' 是可行的,但这比在一行上使用多个引号或跨越多行的引号更容易。或者您可以使用符号或许多其他东西。但是我真的很想保持源代码不变。

示例 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}

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

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

I'm using LaTeX's "listings" package to format source code. Unfortunately I get curly quotes instead of straight quotes. Since the curly quotes don't always point in the right direction, it looks bad. How can I get straight quotes instead?

I'd prefer not to change or filter the source code itself. Filtering the code to properly change " to `` or '' would work, but this is easier done than said with multiple quotes on a line, or quotes spanning multiple lines. Or you could use symbol or a host of other things. But I'd really like to keep the source unchanged.

Example 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}

Example output (using Miktex on windows):
Image of source code

(Direct link to image of incorrect output)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尐偏执 2024-07-18 09:50:36

我在文档中看到(应该与 packge 一起分发,但可以在 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. 

执行类似的操作

\lstset{upquote=true}

在开始列表环境之前

\begin{lstlisting}[upquote=true]
...
\end{lstlisting}

,或者使用也可能已经以适当的语言为您设置了 tis 属性
定义(再次参见文档,第 12 页上的预定义语言的大列表)。

使用:

\lstloadlanguages{<dialects you need>}

在标题中。 然后使用上述任一约定来选择选项来设置语言。

I see in the documentation (which should have been distributed with the packge, but is available at http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings.pdf) for listings that there is a settable property called upquote to take care of this.

From the documentation:

upquote=⟨true|false⟩                                                false
  determines whether the left and right quote are printed ‘’ or `'. This 
  key requires the textcomp package if true. 

Do something like

\lstset{upquote=true}

before begining the list environment, or use

\begin{lstlisting}[upquote=true]
...
\end{lstlisting}

It is also possible that tis property is already set for you in the appropriate language
definition (see the docs again, big list of predefined languages on page 12).

Use:

\lstloadlanguages{<dialects you need>}

in the header. And then set the language using either of the above conventions for choosing options.

离笑几人歌 2024-07-18 09:50:36

您是否考虑过在列表中使用等宽(打字机)字体? 以下示例有效:

\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}

Have you considered using a monospaced (typewriter) font for the listing? The following example works:

\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}
时常饿 2024-07-18 09:50:36

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

\newcommand{\qq}{\symbol{34}} % 34 is the decimal ascii code for "

然后是你的示例:

\begin{lstlisting}
...
print{\qq}The temperature is{\qq},Celsius,{\qq}degrees Celsius{\qq}
...
\end{lstlisting}

注意花括号,它应该将列表带回 LaTeX 模式(请参阅包的 escapechars 选项。)

dmckee's answer above probably works. If you drop your last condition, i.e. you permit changes to the code, then there is a more generic solution, which I tend to use whenever (La)TeX renders a character somehow differently than I expect it to do is to use the \symbol command. I list it here because it can be useful in other situations as well:

\newcommand{\qq}{\symbol{34}} % 34 is the decimal ascii code for "

And then your example:

\begin{lstlisting}
...
print{\qq}The temperature is{\qq},Celsius,{\qq}degrees Celsius{\qq}
...
\end{lstlisting}

Note the curly braces which supposedly take listings back to LaTeX mode (see escapechars option of the package.)

污味仙女 2024-07-18 09:50:36

这是一个解决方案

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

% in the listings package configuration, try:  
literate={"}{\textquotedbl}1,  

Here is a solution

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

% in the listings package configuration, try:  
literate={"}{\textquotedbl}1,  
温折酒 2024-07-18 09:50:36

我使用 fontspec 时遇到了同样的问题,解决方案是不设置 \defaultfontfeatures{Mapping=tex-text},而是设置 Mapping=tex- text 特别是仅在主字体和无字体上,并将 tt 字体留给它自己的设备。 :)

I had the same problem, using fontspec, and the solution was to not set \defaultfontfeatures{Mapping=tex-text}, but instead setting Mapping=tex-text specifically on only the main and sans font, and leaving the tt font to it's own devices. :)

原来是傀儡 2024-07-18 09:50:36

也许是因为我作为 LaTeX 用户很早就安装了列表,但我很惊讶地发现,如果没有列表包,行为会有所不同。

我的解决方案与 David Hanak 的类似,但我使用了 LaTeX Cheat Sheet 中描述的双引号符号 (http://stdout.org/~winston/latex

\newcommand{\QQ}[1]{``#1''}

Maybe it's because I installed listings early as a LaTeX user, but I'm surprised to learn that without the listings package the behaviour is any different.

My solution was similar to David Hanak's, but I used the symbols for double-quote as described in the LaTeX Cheat Sheet (http://stdout.org/~winston/latex)

\newcommand{\QQ}[1]{``#1''}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文