导出pdf时如何将代码块封装成frame?
How to encapsualte code blocks into a frame when exporting to pdf?
我使用 org-mode + emacs 已经有一段时间了,我喜欢它制作内容的简单性。我经常使用来自同一文档的 html + pdf 导出组合(首先是网页,随后是 pdf 文档)。我的问题是关于将代码块 (#+BEGIN_SRC
...) 导出为 pdf。
对html,导出命令(C-c C-e h h
)给了我一个满意的解决方案:它用一个框架来封装代码(显示编程当您将鼠标指针停留在语言上时)并为生成的消息使用不同的框架(正如我设置的 :export both
)。在 #+BEGIN_SRC
之前使用 #+CAPTION: my caption here
时,生成的 html 页面在代码框之前包含“Listing #: my caption here”。
至 pdf,由导出命令 (C-c C-e l p
) 生成的文档在代码或结果周围都没有框架(一团糟),并且标题显示在代码和结果之间显示为“图#:我的标题”。
当从org-mode 到 pdf?
这是一个最小的例子:
#+TITLE: EXPORT TESTINGS
#+OPTIONS: toc:nil
#+CAPTION: Caption, my caption!
#+BEGIN_SRC C :results output :exports both
int i, x = 10;
for(i = 0; i < x; i++)
printf("%d ",i);
printf(" ~ %d\n", x);
#+END_SRC
Org-mode 在 PDF/LaTeX
中使用 Minted 包来突出显示源代码
如果对特定代码块使用以下配置参数,则可以将选项传递给 Minted 包:
#+ATTR_LATEX: :options frame=single
或者,如果您想对所有文件进行这些更改,则需要自定义“org-latex-listings-options”and/or“org-latex-minted-options”变量。
根据 Alex Ott 的回答(以及几个小时的网页浏览),我终于做到了。
我们将使用 minted
包。为了完整起见,这是我必须设置所有内容的方式:
需要Python
minted
使用 Python 包来突出显示名为 Pygmets 的语法。你可以安装它:
pip install Pygments
Emacs 设置
- 在 org 文件序言中,您需要以下行:
#+LaTeX_HEADER: \usepackage{minted}
- 源代码块导出使用变量
org-latex-listings
。您必须将其设置为:(setq org-latex-listings 'minted)
.
- 最后但同样重要的是,您必须允许
pdflatex
执行 shell 命令才能使用 Python 包 Pygments
。选项是-shell-escape
。描述调用 LaTeX 编译器的 emacs 变量是 org-latex-pdf-process
.
为了达到这 3 点,我在我的初始文件中添加了这个片段
;; inside .emacs file
(setq org-latex-listings 'minted
org-latex-packages-alist '(("" "minted"))
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"))
注意:请参阅 here 以了解为什么需要对 pdflatex 进行三次调用。如果您使用 bibtex,则必须插入适当的行。
返回您的组织文件
现在您可以在源代码块的顶部添加 LaTeX 属性:
#+ATTR_LATEX: :options frame=single
#+BEGIN_SRC emacs-lisp
(defun Fib (n)
(if (< n 2) n (+ (Fib (- n 1)) (Fib (- n 2)))))
#+END_SRC
瞧瞧!
铸造手册
要使用不同的框架样式,请查看 manual
我使用 org-mode + emacs 已经有一段时间了,我喜欢它制作内容的简单性。我经常使用来自同一文档的 html + pdf 导出组合(首先是网页,随后是 pdf 文档)。我的问题是关于将代码块 (#+BEGIN_SRC
...) 导出为 pdf。
对html,导出命令(C-c C-e h h
)给了我一个满意的解决方案:它用一个框架来封装代码(显示编程当您将鼠标指针停留在语言上时)并为生成的消息使用不同的框架(正如我设置的 :export both
)。在 #+BEGIN_SRC
之前使用 #+CAPTION: my caption here
时,生成的 html 页面在代码框之前包含“Listing #: my caption here”。
至 pdf,由导出命令 (C-c C-e l p
) 生成的文档在代码或结果周围都没有框架(一团糟),并且标题显示在代码和结果之间显示为“图#:我的标题”。
当从org-mode 到 pdf?
这是一个最小的例子:
#+TITLE: EXPORT TESTINGS
#+OPTIONS: toc:nil
#+CAPTION: Caption, my caption!
#+BEGIN_SRC C :results output :exports both
int i, x = 10;
for(i = 0; i < x; i++)
printf("%d ",i);
printf(" ~ %d\n", x);
#+END_SRC
Org-mode 在 PDF/LaTeX
中使用 Minted 包来突出显示源代码如果对特定代码块使用以下配置参数,则可以将选项传递给 Minted 包:
#+ATTR_LATEX: :options frame=single
或者,如果您想对所有文件进行这些更改,则需要自定义“org-latex-listings-options”and/or“org-latex-minted-options”变量。
根据 Alex Ott 的回答(以及几个小时的网页浏览),我终于做到了。
我们将使用 minted
包。为了完整起见,这是我必须设置所有内容的方式:
需要
Python
minted
使用 Python 包来突出显示名为 Pygmets 的语法。你可以安装它:
pip install Pygments
Emacs 设置
- 在 org 文件序言中,您需要以下行:
#+LaTeX_HEADER: \usepackage{minted}
- 源代码块导出使用变量
org-latex-listings
。您必须将其设置为:(setq org-latex-listings 'minted)
. - 最后但同样重要的是,您必须允许
pdflatex
执行 shell 命令才能使用 Python 包Pygments
。选项是-shell-escape
。描述调用 LaTeX 编译器的 emacs 变量是org-latex-pdf-process
.
为了达到这 3 点,我在我的初始文件中添加了这个片段
;; inside .emacs file
(setq org-latex-listings 'minted
org-latex-packages-alist '(("" "minted"))
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"))
注意:请参阅 here 以了解为什么需要对 pdflatex 进行三次调用。如果您使用 bibtex,则必须插入适当的行。
返回您的组织文件
现在您可以在源代码块的顶部添加 LaTeX 属性:
#+ATTR_LATEX: :options frame=single
#+BEGIN_SRC emacs-lisp
(defun Fib (n)
(if (< n 2) n (+ (Fib (- n 1)) (Fib (- n 2)))))
#+END_SRC
瞧瞧!
铸造手册
要使用不同的框架样式,请查看 manual