导出不同样式的 org-mode 代码块和结果

14

我正在使用org-mode和Babel准备一份演示文稿,并希望将其导出为beamer pdf。

在输出中,源代码和结果具有相同的样式(LaTeX中的verbatim)。因此,很难区分它们。

是否可以使用不同的样式(最好是不同的颜色)导出源代码和结果?

非常感谢!


我发现使用代码清单可以解决这个问题。我会稍微调整一下样式并自己回答。当然,如果有其他方法,更多的答案也是受欢迎的。 - Yi Wang
1个回答

22

你可以使用LaTeX的minted包来对源代码进行语法高亮:

C-h v org-latex-listings

...

  (setq org-latex-listings 'minted)

causes source code to be exported using the minted package as
opposed to listings.  If you want to use minted, you need to add
the minted package to `org-latex-packages-alist', for example
using customize, or with

  (require 'ox-latex)
  (add-to-list 'org-latex-packages-alist '("" "minted"))

In addition, it is necessary to install pygments
(http://pygments.org), and to configure the variable
`org-latex-pdf-process' so that the -shell-escape option is
passed to pdflatex.

The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment').  If you run
into previewing problems, please consult

  http://orgmode.org/worg/org-tutorials/org-latex-preview.html

我在我的初始化文件中有以下代码:

(require 'ox-latex)
(add-to-list 'org-latex-packages-alist '("" "minted"))
(setq org-latex-listings 'minted)

(setq org-latex-pdf-process
      '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
        "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))

在minted中有不同的颜色主题可以使用,例如您可以将此选项放入组织文件中以使用"monokai":

#+LaTeX_HEADER: \usemintedstyle{monokai}

获取 pygmentize 支持的样式列表:

pygmentize -L styles

3
非常感谢!我目前需要的语言(Mathematica)可以使用listings包支持,而不是minted。尽管如此,您的回答完美地回答了我的问题。对于Mathematica,我写了一篇关于如何做到这一点的博客,链接为http://cosmosimple.blogspot.co.uk/2014/01/colored-code-in-org-mode-export.html。 - Yi Wang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接