python - PDF 中的 Pandoc 语法突出显示不起作用

标签 python pdf latex syntax-highlighting pandoc

pandoc --version 产生:

pandoc 1.12.2.1
Compiled with texmath 0.6.5.2, highlighting-kate 0.5.5.1.
Syntax highlighting is supported for the following languages:
    actionscript, ada, apache, asn1, asp, awk, bash, bibtex, boo, c, changelog, clojure, cmake, coffee, coldfusion, commonlisp, cpp, cs, css, curry, d, diff, djangotemplate, doxygen, doxygenlua, dtd, eiffel, email, erlang, fortran, fsharp, gnuassembler, go, haskell, haxe, html, ini, java, javadoc, javascript, json, jsp, julia, latex, lex, literatecurry, literatehaskell, lua, makefile, mandoc, markdown, matlab, maxima, metafont, mips, modelines, modula2, modula3, monobasic, nasm, noweb, objectivec, objectivecpp, ocaml, octave, pascal, perl, php, pike, postscript, prolog, python, r, relaxngcompact, rhtml, roff, ruby, rust, scala, scheme, sci, sed, sgml, sql, sqlmysql, sqlpostgresql, tcl, texinfo, verilog, vhdl, xml, xorg, xslt, xul, yacc, yaml
Default user data directory: /home/xiaolong/.pandoc
Copyright (C) 2006-2013 John MacFarlane
Web:  http://johnmacfarlane.net/pandoc
This is free software; see the source for copying conditions.  There is no
warranty, not even for merchantability or fitness for a particular purpose.

我正在尝试以下操作:

```python
Python 3.4.3 |Anaconda 2.3.0 (64-bit)| (default, Oct 19 2015, 21:52:17)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random  # seed is determined here, if not set by the developer themself
>>> random.randint(0, 100)
50
>>> random.randint(0, 100)
62
>>> random.randint(0, 100)
53
>>> random.randint(0, 100)
17
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.randint(0, 100)
88
>>> random.seed("I am a hashable object.")
>>> random.randint(0, 100)
41
>>> random.randint(0, 100)
88
````````````````````````````````````````````````````````````````````````````````

我使用的编译 PDF 的命令是:

pandoc --read=markdown --table-of-contents --toc-depth=2 --preserve-tabs --standalone --template=template.latex --latex-engine=xelatex Hausarbeit.md --highlight-style=pygments -o Hausarbeit.pdf

这是我的 template.latex 中的代码:

$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$

根据 Pandoc 文档 Pandoc documentation这应该会导致 Python 语言的文本突出显示。但是,该代码块中的所有内容都只会变成等宽,并且不会应用任何颜色。

另外,编译时出现错误:

pandoc: Error producing PDF from TeX source.
! LaTeX Error: Environment Shaded undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

\GenericError  ...                                
                                                  \endgroup 
l.200 \begin{Shaded}

我的猜测是,模板中缺少用于定义 Shaded 的内容。我什至安装了一些名为 libghc-highlighting-kate-devhighlighting-kate 包,因为我在某处读到,这就是 pandoc 使用的,但无济于事,文本保持黑色。

如何使语法高亮显示正常工作?

编辑#1

当我简单地缩进代码以对代码使用标准 markdown 语法时,该错误没有出现。但在那种情况下,我本身不会有任何语法突出显示。

编辑#2

标签“PDF”、“Python”和“syntax-highlighting”确实相关,因为这是专门关于创建 PDF 文件和语法高亮的。也可能只是在创建的 PDF 文件中突出显示 python 代码有问题,因此 python 标签也是相关的。请在过快结束之前阅读问题。

最佳答案

尝试将 --listings 选项添加到您的 pandoc 编译命令中:

pandoc --read=markdown --table-of-contents --toc-depth=2 --preserve-tabs --standalone --template=template.latex --latex-engine=xelatex --listings Hausarbeit.md --highlight-style=pygments -o Hausarbeit.pdf

关于python - PDF 中的 Pandoc 语法突出显示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180355/

相关文章:

python - 有效地检查两个数是否互质(相对质数)?

python - 将 JSON 反序列化为对象

iphone - 在 UIWebView 中禁用 PDF 文件中的超链接

ubuntu - 符号作为变量的值是 void : debian-emacs-flavor when running ispell on tex file

python: flake8 找不到不存在的方法

python - 如何检查命令是否完成?

java - 如何使用iText从书签在PDF文件中创建目录页?

java - 如何勾选 "choose paper source by pdf page size"iText7?

latex - 使用latex绘制并发图

latex - 将 LaTeX 转换为 MediaWiki 语法