org 通过latex导出 PDF

org 导出latex 时在 org 文件的配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#+options: \n:t
#+OPTHIONS: H:3
#+PROPERTY: comments yes
#+BEAMER_HEADER: \AtBeginSection{\frame{\sectionpage}}
#+BEAMER_THEME: Boadilla
#+BEAMER_COLOR_THEME: default
#+LATEX_HEADER: \usepackage{ctex}
#+LATEX_HEADER: \usepackage{minted}
#+LATEX_HEADER: \usepackage{tikz}
#+LATEX_HEADER: \usepackage[a4paper]{geometry}

其中 #+option: \n:t 这是为了在排版时把\n 当换行, 否则latex 默认不换行
其中 #+LATEX_HEADER: \usepackage{ctex} 是为了支持中文
其中 #+LATEX_HEADER: \usepackage{minted} 是为了代码高亮和代码格式化
其中 #+LATEX_HEADER: \usepackage{tikz} 是为了画图
#+OPTHIONS: H:3 是生成的目录最多可以包含三级标题的意思
如果代码太长 A4 显示不下可以用 #+LATEX_HEADER: \usepackage[a4paper]{geometry} 改变纸张大小 如把a4 改成 a3

emacs 配置

config.el 中加入如下配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  (setq org-latex-pdf-process
        '("xelatex -interaction nonstopmode -output-directory %o %f"
          "xelatex -interaction nonstopmode -output-directory %o %f"
          "xelatex -interaction nonstopmode -output-directory %o %f"))
  (with-eval-after-load 'ox-latex
    ;; http://orgmode.org/worg/org-faq.html#using-xelatex-for-pdf-export
    ;; latexmk runs pdflatex/xelatex (whatever is specified) multiple times
    ;; automatically to resolve the cross-references.
    (setq org-latex-pdf-process '("latexmk -xelatex -quiet -shell-escape -f %f"))
    (add-to-list 'org-latex-classes
                 '("elegantpaper"
                   "\\documentclass[lang=cn]{elegantpaper}
                   [NO-DEFAULT-PACKAGES]
                   [PACKAGES]
                   [EXTRA]"
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\paragraph{%s}" . "\\paragraph*{%s}")
                   ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

    ;; 如果 org 设置了#+LATEX_HEADER: \usepackage{minted} 的话 则支持代码高亮和代码格式化
    (setq org-latex-listings 'minted)
    ;; Windows 加了这句的话 org 里 C-c C-x C-l 无法预览公式
    ;; 为了兼容性就不加这句 然后 org 里加上 #+LATEX_HEADER: \usepackage{minted} 即可
    ;; (add-to-list 'org-latex-packages-alist '("minted"))
    
    (defvar-local my/dvipng-process
      '(:programs
        ("latex" "dvipng")
        :description "dvi > png" :message "you need to install the programs: latex and dvipng." :image-input-type "dvi" :image-output-type "png" :image-size-adjust
        (1.0 . 1.0)
        :latex-compiler
        ("latex -interaction nonstopmode -shell-escape -output-directory %o %f")
        :image-converter
        ("dvipng -D %D -T tight -o %O %f")
        :transparent-image-converter
        ("dvipng -D %D -T tight -bg Transparent -o %O %f"))
      " 用于预览org 里面的 latex 的命令")
    ;; 解决 org 文件里面执行  org-latex-preview 生成的 .tex 格式不对的问题
    ;; 通过 log 可以看到错误信息 ! Package minted Error: You must invoke LaTeX with the -shell-escape flag
    (map-put! org-preview-latex-process-alist 'dvipng my/dvipng-process))

  ;; scale 默认1.5 是 org 里预览latex 的大小比例 由于 windows 的话 当设置了 #+LATEX_HEADER: \usepackage{minted} 的话会报错
  (cond (*is-windows*
         (setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0)))
        (*is-mac* ;; Mac OS X
         (setq org-format-latex-options (plist-put org-format-latex-options :scale 4.0))))
  ;; (setq org-format-latex-options (plist-put org-format-latex-options :html-scale 1.0)) ;; 导出HTML 时 Latex 的大小
  (defun org-TeX--mode-preview-bind ()
    (interactive)
    (define-key TeX-mode-map (kbd "C-c v") 'org-latex-preview))
  (add-hook 'TeX-mode-hook 'org-TeX--mode-preview-bind)     ;; 由于 Tex-mode 只有在 org 里 C-c ' 之后才会出现 所以这里必须用hook
  (define-key org-mode-map (kbd "C-c v") 'org-latex-preview)

  (defun my/clear-all-org-latex-preview ()
    (interactive)
    (org-latex-preview '(64)))
  (define-key org-mode-map (kbd "C-c c") 'my/clear-all-org-latex-preview)

安装依赖

编译 latex 使用的工具

  • Windows
    https://miktex.org/download 下载安装 Miktex
  • MacOS
    • 安装完整版的 MacTex
      直接到 https://www.tug.org/mactex/下载 MacTex 安装 这个安装包接近4G 装好之后更大…

    • 安装简易版的 MacTex
      直接到 https://www.tug.org/mactex/下载 BasicTex 安装 这个安装包只有不到80M 都是最基础的包
      所以导出 latex 的时候肯定会缺少很多包 我们只有自己手动去装这些包
      为了装这些包 我们需要用到 TeX Live Utility 直接到 http://amaxwell.github.io/tlutility/ 下载 Latest Version 点击会跳到 github 上下载
      安装完成之后第一步先换源 设置 => 管理软件源 把源换成国内的阿里云的源

    • MAC 版安装完后如果 MacTex 或者 BasicTex 的bin 目录不在 PATH 里要手动添加环境变量

      1. 在 ~/.zshrc 末尾加上 PATH=/usr/local/texlive/2020basic/bin/x86_64-darwin:$PATH

      2. source

        1
        
                source ~/.zshrc
        
      3. doom sync

      4. 重启 emacs

使用过程中可能出现的问题

1
major issue: So far, no MiKTeX administrator has checked for updates

这种问题是 Miktex 需要更新了 查看 Org PDF LateX Output Minibuffer 可以看到 Miktex 的安装路径
进入到 bin/x64 目录 用管理员身份打开 miktex-console_admin.exe


Check for updates 然后更新完之后再重启 Emacs 就可以了

代码高亮用的 minted 依赖于 pygments

1
pip3 install pygments

org mode 中编辑和预览

编辑

由于我们导出成pdf 时是用latex 导出 所以可以直接在 org mode 里直接写latex 语法
如果是要导出成markdown 或者 html 则 latex 需要写在 latex 语法块里如:

\( \P{} \S{} \dag{} \ddag{} \)
\[ \P{} \S{} \dag{} \ddag{} \copyright{} \pounds{} \]
\[ \P{} \S{} \dag{} \ddag{} \copyright{} \pounds{} \]

为了有方便的补全 可以进入Tex-mode 模式编辑 latex => 光标移到 latex 代码块上 按 C-c ' 进入

预览

在org 的latex 代码块内 或者 tex-mode 里面执行 org-latex-preview 我绑定的快捷键是 C-c v
:@ 注意: 在org-mode 里只在有 \(\) \[\] $$ $$ 里的latex 才可以正确的 preview $ $ 内的不行
由于org-latex-preview 是调用系统的 Latex 工具来生成图片显示的如果你在MAC 上装的是 Basic Tex 的话可能会缺一些包如 (dvipng)
不过没有关系 到时候看Message 缺什么包就在 Tex Live Utility 里装就行了


如果要退出预览的话 org-mode 里在图片上按Enter 即可 在Tex-mode 里再执行一次 org-latex-preview 即可

简单的数学公式

直接在babel 写

该方法只有在用latex 导出为 pdf 时才能显示 否则会被忽略

直接用latex 块

\[ x^n + y^n = z^n \]

图片排版

设定图片大小

图片并排排放

:@ 注意: 必须用 CENTER 块包起来, 如果:height 属性太大, 图片并排放不下的话会上下排布

导出 Latex 实操

在org-mode 文件中 C-c C-e l o 即可
一切顺利的话就会在当前目录生成 同名的.pdf 文件
如果有错误就查看当前文件夹下生成的 .log 文件 如果是使用 BasicTex 的话 一般来说就是缺少latex 相关的包
直接在 .log 文件中搜索 Error 找出缺少的包然后在 Tex Live Utility 里面搜索安装即可
示例:

如果org 里有大表格 导出成 pdf 的时候会有问题需要做如下调整
#+LaTeX: \resizebox{\textwidth}{!}{ xxxx #+LaTeX: }
把表格包起来如:

1
2
3
4
5
#+LaTeX: \resizebox{\textwidth}{!}{
| 姓名 | 身高 | 颜值 | 爱好 | 交往系数 |
|------+-----+------+------+---------|
|      |     |      |      |         |
#+LaTeX: }
Licensed under CC BY-NC-SA 4.0