简体   繁体   English

使Sphinx在PDF输出中生成未混淆的代码块

[英]Making Sphinx produce untypogrified code blocks in PDF output

Code blocks look ugly (check the quotes) in PDF output: 代码块在PDF输出中看起来很丑陋(请检查引号):

在此处输入图片说明

I use version 1.1.3 of Sphinx, and used the following command to produce the doc: 我使用Sphinx的1.1.3版本,并使用以下命令来生成doc:

$ make latexpdf

Also, copying the snippet from the PDF destroys the indenting when pasting: 另外,从PDF复制片段会破坏粘贴时的缩进:

@view_config(route_name=’hello’)
def hello_world(request):
return Response(’Hello World!’)

I would expect this: 我希望这样:

@view_config(route_name=’hello’)
def hello_world(request):
    return Response(’Hello World!’)

This would be even nicer: 这样会更好:

@view_config(route_name='hello')
def hello_world(request):
    return Response('Hello World!')

Sphinx is really an excellent tool but I also have a few issues with the default PDF output of the latexpdf target. Sphinx确实是一个出色的工具,但是latexpdf目标的默认PDF输出也存在一些问题。

Specifically: 特别:

  • Single quotes in code blocks are converted to acute style quotes which doesn't look right in source code. 代码块中的单引号会转换为在源代码中看起来不正确的尖锐样式引号。
  • The code blocks aren't indented from the main text. 代码块没有从正文缩进。 For me this makes them less readable. 对我来说,这使得它们的可读性较差。
  • I prefer other fonts and pygments but that is just a personal choice and can be configured. 我更喜欢其他字体和pygments,但这只是个人选择,可以配置。

Some of this can be fixed in the LaTeX pre-amble section of the Sphinx conf.py but the quotes are modified by Sphinx to custom LaTeX entities so the upquote LaTeX package can't be used to correct them. 其中一些可以在Sphinx conf.py的LaTeX前同步码部分中进行修复,但是Sphinx会将引号修改为自定义LaTeX实体,因此不能使用upquote LaTeX包对其进行更正。

After a good bit of experimentation with different config options I ended up writing a small script to modify the LaTeX source prior to building the PDF. 在对不同的配置选项进行了大量试验之后,我最终编写了一个小脚本来修改LaTeX源,然后再构建PDF。 The script is here and the output that I wanted to generate is here . 脚本在这里 ,我想生成的输出在这里 (For comparison here is the default output for the same document.) (为了进行比较, 是同一文档的默认输出。)

If someone has a cleaner solution, for example one that could be done completely through Sphinx conf.py so the changes would be picked up by ReadTheDocs then I would be interested. 如果有人有一个更干净的解决方案,例如可以通过Sphinx conf.py完全完成的解决方案,那么更改将由ReadTheDocs负责,那么我将很感兴趣。

Also, the issue with losing indentation when copying and pasting from the PDF probably isn't a Sphinx/LaTeX issue. 另外,从PDF复制和粘贴时缩进的问题可能不是Sphinx / LaTeX问题。

This is only a partial answer which may lead toward an ultimate solution. 这只是部分答案,可能会导致最终解决方案。 To disable typographer quotes (also known as curly or smart quotes) for HTML output in Sphinx, change the default setting in conf.py for SmartyPants from True to False . 要为Sphinx中的HTML输出禁用印刷机引号(也称为卷曲引号或智能引号),请将conf.py中SmartyPant的默认设置从True更改为False

I assume that one could find the function in Sphinx that transforms quotes and use the same logic from the HTML output and apply it for PDF output. 我假设人们可以在Sphinx中找到转换引号的函数,并使用HTML输出中的相同逻辑并将其应用于PDF输出。

The option proposed by Steve Piercy now no longer works (since Sphinx 1.6): Steve Piercy提出的选项现在不再起作用(自Sphinx 1.6起):

Deprecated since version 1.6: Use the smart_quotes option 
in the Docutils configuration file (docutils.conf) instead.

What was not obvious to me was how to apply this setting. 对我来说,不明显的是如何应用此设置。 You need to create a docutils.conf file and put it in one of these locations : 您需要创建一个docutils.conf文件并将其放在以下位置之一

  • /etc/docutils.conf /etc/docutils.conf
  • ./docutils.conf ./docutils.conf
  • ~/.docutils 〜/ .docutils

The easiest option is to put it where you are building the docs from - YMMV. 最简单的选择是将其放在您要通过-YMMV构建文档的位置。 It should contain at least this: 它至少应包含以下内容:

[general]
smart_quotes: no

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM