求分享 org-mode 导出 pdf 的设置。

最近需要大量导出 pdf 文档,目前我通过下面的配置可以导出,

(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"))

中文的问题通过以下的方式解决

#+LATEX_HEADER: \usepackage{xeCJK}
#+LATEX_HEADER: \setCJKmainfont{WenQuanYi Micro Hei Light}

求分享一些设置让导出的 pdf 好看一些,排板,还有 table 等。 或有没有比较好的 md -> pdf 的工具也是可以的。

6 个赞

分享我过去在网上找到的一版(比较丑,楼主可以自己改一改,我还没来得及改)

;;===============================================================
;;          begin: org-to-latex
;;===============================================================
;; if you want use it,you should:
;; #+LATEX_CLASS: cn-article
;; or:
;; #+LATEX_CLASS: beamer

(add-hook 'org-mode-hook 
(lambda () (setq truncate-lines nil)))
(require 'org-install)
;; (require 'org-latex) org-latex可以被自动加载。

;; 使用xelatex一步生成PDF
(setq org-latex-pdf-process
'("xelatex -interaction nonstopmode %f"
"xelatex -interaction nonstopmode %f"))
;; code执行免应答(Eval code without confirm)
(setq org-confirm-babel-evaluate nil)
;; Auctex
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(defun org-mode-article-modes ()
  (reftex-mode t)
  (and (buffer-file-name)
       (file-exists-p (buffer-file-name))
       (reftex-parse-all)))
(add-hook 'org-mode-hook
          (lambda ()
            (if (member "REFTEX" org-todo-keywords-1)
                (org-mode-article-modes))))
(unless (boundp 'org-export-latex-classes)
  (setq org-export-latex-classes nil))

(require 'ox-latex)


(add-to-list 'org-latex-classes
             '("org-article"
               "\\documentclass{article}
                 \\usepackage{graphicx}
                 \\usepackage{xcolor}
                 \\usepackage{xeCJK}
                 \\usepackage{fixltx2e}
                 \\usepackage{longtable}
                 \\usepackage{float}        
                 \\usepackage{tikz}         
                 \\usepackage{wrapfig}      
                 \\usepackage{latexsym,amssymb,amsmath}
                 \\usepackage{textcomp}
                 \\usepackage{listings}     
                \\usepackage{marvosym}     
                \\usepackage{textcomp}     
                \\usepackage{latexsym}     
                \\usepackage{natbib}       
                \\usepackage{geometry}     
                [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}")))


(add-to-list 'org-latex-classes
             '("cn-article"
               "\\documentclass[10pt,a4paper]{article}
\\usepackage{graphicx}
\\usepackage{xcolor}
\\usepackage{xeCJK}
\\usepackage{lmodern}
\\usepackage{verbatim}
\\usepackage{fixltx2e}
\\usepackage{longtable}
\\usepackage{float}
\\usepackage{tikz}
\\usepackage{wrapfig}
\\usepackage{soul}
\\usepackage{textcomp}
\\usepackage{listings}
\\usepackage{geometry}
\\usepackage{algorithm}
\\usepackage{algorithmic}
\\usepackage{marvosym}
\\usepackage{wasysym}
\\usepackage{latexsym}
\\usepackage{natbib}
\\usepackage{fancyhdr}
\\usepackage[xetex,colorlinks=true,CJKbookmarks=true,
linkcolor=blue,
urlcolor=blue,
menucolor=blue]{hyperref}
\\usepackage{fontspec,xunicode,xltxtra}
\\setmainfont[BoldFont=Adobe Heiti Std]{Adobe Song Std}  
\\setsansfont[BoldFont=Adobe Heiti Std]{AR PL UKai CN}  
\\setmonofont{Bitstream Vera Sans Mono}  
\\newcommand\\fontnamemono{AR PL UKai CN}%等宽字体
\\newfontinstance\\MONO{\\fontnamemono}
\\newcommand{\\mono}[1]{{\\MONO #1}}
\\setCJKmainfont[Scale=0.9]{Adobe Heiti Std}%中文字体
\\setCJKmonofont[Scale=0.9]{Adobe Heiti Std}
\\hypersetup{unicode=true}
\\geometry{a4paper, textwidth=6.5in, textheight=10in,
marginparsep=7pt, marginparwidth=.6in}
\\definecolor{foreground}{RGB}{220,220,204}%浅灰
\\definecolor{background}{RGB}{62,62,62}%浅黑
\\definecolor{preprocess}{RGB}{250,187,249}%浅紫
\\definecolor{var}{RGB}{239,224,174}%浅肉色
\\definecolor{string}{RGB}{154,150,230}%浅紫色
\\definecolor{type}{RGB}{225,225,116}%浅黄
\\definecolor{function}{RGB}{140,206,211}%浅天蓝
\\definecolor{keyword}{RGB}{239,224,174}%浅肉色
\\definecolor{comment}{RGB}{180,98,4}%深褐色
\\definecolor{doc}{RGB}{175,215,175}%浅铅绿
\\definecolor{comdil}{RGB}{111,128,111}%深灰
\\definecolor{constant}{RGB}{220,162,170}%粉红
\\definecolor{buildin}{RGB}{127,159,127}%深铅绿
\\punctstyle{kaiming}
\\title{}
\\fancyfoot[C]{\\bfseries\\thepage}
\\chead{\\MakeUppercase\\sectionmark}
\\pagestyle{fancy}
\\tolerance=1000
[NO-DEFAULT-PACKAGES]
[NO-PACKAGES]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

;; 使用Listings宏包格式化源代码(只是把代码框用listing环境框起来,还需要额外的设置)
(setq org-export-latex-listings t)
;; Options for \lset command(reference to listing Manual)
(setq org-export-latex-listings-options
      '(
        ("basicstyle" "\\color{foreground}\\small\\mono")           ; 源代码字体样式
        ("keywordstyle" "\\color{function}\\bfseries\\small\\mono") ; 关键词字体样式
        ("identifierstyle" "\\color{doc}\\small\\mono")
        ("commentstyle" "\\color{comment}\\small\\itshape")         ; 批注样式
        ("stringstyle" "\\color{string}\\small")                    ; 字符串样式
        ("showstringspaces" "false")                                ; 字符串空格显示
        ("numbers" "left")                                          ; 行号显示
        ("numberstyle" "\\color{preprocess}")                       ; 行号样式
        ("stepnumber" "1")                                          ; 行号递增
        ("backgroundcolor" "\\color{background}")                   ; 代码框背景色
        ("tabsize" "4")                                             ; TAB等效空格数
        ("captionpos" "t")                                          ; 标题位置 top or buttom(t|b)
        ("breaklines" "true")                                       ; 自动断行
        ("breakatwhitespace" "true")                                ; 只在空格分行
        ("showspaces" "false")                                      ; 显示空格
        ("columns" "flexible")                                      ; 列样式
        ("frame" "single")                                          ; 代码框:阴影盒
        ("frameround" "tttt")                                       ; 代码框: 圆角
        ("framesep" "0pt")
        ("framerule" "8pt")
        ("rulecolor" "\\color{background}")
        ("fillcolor" "\\color{white}")
        ("rulesepcolor" "\\color{comdil}")
        ("framexleftmargin" "10mm")
        ))
;; Make Org use ido-completing-read for most of its completing prompts.
(setq org-completion-use-ido t)
;; 各种Babel语言支持
(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)
   (emacs-lisp . t)
   (matlab . t)
   (C . t)
   (perl . t)
   (shell . t)
   (ditaa . t)
   (python . t)
   (haskell . t)
   (dot . t)
   (latex . t)
   (js . t)
   ))

;; 导出Beamer的设置
;; allow for export=>beamer by placing #+LaTeX_CLASS: beamer in org files
;;-----------------------------------------------------------------------------
(add-to-list 'org-export-latex-classes
             ;; beamer class, for presentations
             '("beamer"
               "\\documentclass[11pt,professionalfonts]{beamer}
\\mode
\\usetheme{{{{Warsaw}}}}
%\\usecolortheme{{{{beamercolortheme}}}}

\\beamertemplateballitem
\\setbeameroption{show notes}
\\usepackage{graphicx}
\\usepackage{tikz}
\\usepackage{xcolor}
\\usepackage{xeCJK}
\\usepackage{amsmath}
\\usepackage{lmodern}
\\usepackage{fontspec,xunicode,xltxtra}
\\usepackage{polyglossia}
\\setmainfont{Times New Roman}
\\setCJKmainfont{DejaVu Sans YuanTi}
\\setCJKmonofont{DejaVu Sans YuanTi Mono}
\\usepackage{verbatim}
\\usepackage{listings}
\\institute{{{{beamerinstitute}}}}
\\subject{{{{beamersubject}}}}"
               ("\\section{%s}" . "\\section*{%s}")
               ("\\begin{frame}[fragile]\\frametitle{%s}"
                "\\end{frame}"
                "\\begin{frame}[fragile]\\frametitle{%s}"
                "\\end{frame}")))

(setq ps-paper-type 'a4
      ps-font-size 16.0
      ps-print-header nil
      ps-landscape-mode nil)

;;From:https://blog.csdn.net/pfanaya/java/article/details/7669364

;;===============================================================
;;          end: org-to-latex
;;===============================================================

好像不是太好用啊,我好像差了太多字体还是怎么着。总是有各种错

用 pandoc-mode 的话这个 template 感觉还可以 GitHub - Wandmalfarbe/pandoc-latex-template: A pandoc LaTeX template to convert markdown files to PDF or LaTeX.

简单的话我都是直接加几行设置:

#+LATEX_HEADER: \usepackage{ctex}
#+LATEX_HEADER: \usepackage[margin=1in]{geometry}
#+OPTIONS: toc:nil
#+mathspec: true
(add-to-list 'org-latex-packages-alist
             '("UTF8,fontset=macnew" "ctex" t ))

或者

#+LATEX_HEADER: \usepackage[UTF8,fontset=macnew]{ctex}

ctex的选项可以查看 http://mirrors.ctan.org/language/chinese/ctex/ctex.pdf

导出的 pdf 好看主要是要找个好看的 latex 模板。导出基本上设置就这样了。

我的在doom-emacs下没有错(require 'ox-latex) (setq org-export-latex-listings t) (add-to-list 'org-latex-listings '("" “listings”)) (add-to-list 'org-latex-listings '("" “color”))

(add-to-list 'org-latex-packages-alist '("" “xcolor” t)) (add-to-list 'org-latex-packages-alist '("" “listings” t)) (add-to-list 'org-latex-packages-alist '("" “indentfirst” t)) (add-to-list 'org-latex-packages-alist '("" “geometry”)) (add-to-list 'org-latex-packages-alist '("" “tikz”)) (add-to-list 'org-latex-packages-alist '("" “fancyhdr”)) (add-to-list 'org-latex-packages-alist '("" “tabularx” t)) (add-to-list 'org-latex-packages-alist '("" “booktabs” t))

(add-to-list 'org-latex-classes '(“ctexart” “\documentclass[UTF8,a4paper,11pt]{ctexart}” ("\section{%s}" . “\section*{%s}”) ("\subsection{%s}" . “\subsection*{%s}”) ("\subsubsection{%s}" . “\subsubsection*{%s}”) ("\paragraph{%s}" . “\paragraph*{%s}”) ("\subparagraph{%s}" . “\subparagraph*{%s}”) ) )

(add-to-list 'org-latex-classes '(“ctexrep” “\documentclass[UTF8,a4paper,11pt]{ctexrep}” ("\part{%s}" . “\part*{%s}”) ("\chapter{%s}" . “\chapter*{%s}”) ("\section{%s}" . “\section*{%s}”) ("\subsection{%s}" . “\subsection*{%s}”) ("\subsubsection{%s}" . “\subsubsection*{%s}”) ) )

(add-to-list 'org-latex-classes '(“my-report” “\documentclass[UTF8,a4paper,11pt]{ctexrep}” ("\chapter{%s}" . “{\ctexset{chapter={numbering=false}}\chapter{%s}}”) ("\section{%s}" . “\section*{%s}”) ("\subsection{%s}" . “\subsection*{%s}”) ("\subsubsection{%s}" . “\subsubsection*{%s}”) ("\paragraph{%s}" . “\paragraph*{%s}”)))

(add-to-list 'org-latex-classes '(“ctexbook” “\documentclass[UTF8,a4paper,11pt]{ctexbook}” ("\chapter{%s}" . “\chapter*{%s}”) ("\section{%s}" . “\section*{%s}”) ("\subsection{%s}" . “\subsection*{%s}”) ("\subsubsection{%s}" . “\subsubsection*{%s}”) ) )

(add-to-list 'org-latex-classes '(“beamer” “\documentclass{beamer} \usepackage[fontset=none,UTF8,a4paper,zihao=-4]{ctex}” org-beamer-sectioning) )

(setq org-latex-default-class “ctexart”) (setq org-latex-pdf-process '(“xelatex -interaction nonstopmode -output-directory %o %f” “bibtex %b” “xelatex -interaction nonstopmode -output-directory %o %f” “xelatex -interaction nonstopmode -output-directory %o %f”))

(add-hook 'LaTeX-mode-hook 'turn-on-reftex) (add-hook 'LaTeX-mode-hook 'turn-on-cdlatex) (add-hook 'LaTeX-mode-hook 'LaTeX-math-mode) (add-hook 'LaTeX-mode-hook 'outline-minor-mode) (add-hook 'LaTeX-mode-hook 'auto-fill-mode) (add-hook 'LaTeX-mode-hook 'flyspell-mode) (add-hook 'LaTeX-mode-hook 'hide-body t) (add-hook 'LaTeX-mode-hook 'TeX-fold-mode t)

(add-hook 'outline-minor-mode-hook (lambda () (local-set-key “\C-o” outline-mode-prefix-map)))

(setq TeX-auto-save t) (setq TeX-parse-self t) (setq TeX-auto-untabify t) (setq TeX-save-query nil ) (setq TeX-show-compilation t) (setq TeX-clean-confirm t) (setq TeX-source-correlate-mode t) (setq TeX-source-correlate-method 'synctex) (setq TeX-PDF-mode t) (setq-default TeX-master nil)

(setq TeX-view-program-list '((“Sumatra PDF” ("“E:/Program Files/SumatraPDF/SumatraPDF.exe” -reuse-instance " (mode-io-correlate " -forward-search %b %n “) " %o”)))) (setq TeX-view-program-selection '(((output-dvi style-pstricks) “dvips and start”) (output-dvi “Yap”) (output-pdf “Sumatra PDF”) (output-html “start”))) ;; Open TeX error list after compilation ;; and show all warnings (setq TeX-error-overview-open-after-TeX-run t) (setq TeX-debug-warnings t) (add-hook 'LaTeX-mode-hook (lambda() (setq TeX-engine 'xetex) (imenu-add-menubar-index) (add-to-list 'TeX-command-list ‘(“XeLaTeX” "%`xelatex -shell-escape --synctex=1%(mode)%’ %t" TeX-run-TeX nil t)) (setq TeX-command-default “XeLaTeX”) (assq-delete-all 'output-pdf TeX-view-program-selection) (add-to-list 'TeX-view-program-selection '(output-pdf “Sumatra PDF”))))

这个是?是配置吗?

导出到PDF的格式,主要还是靠已经有的LaTeX模板。在看到Org-mode 导出中文 PDF的效果不错、配置和使用简单,目前也用的elegantpaper.cls

elegantpaper.cls的基础上,再在use-package org里加上下面的各种小配置来配合,用起来效果不错。

;; 使用xelatex,配合当前org文件最开始的配置来正常输出中文
  ;; 这类笔记基本不可能是全英文,所以就安心用xelatex算了
  (setq org-latex-pdf-process '("xelatex -file-line-error -interaction nonstopmode %f"
                                "bibtex %b"
                                "xelatex -file-line-error -interaction nonstopmode %f"
                                "xelatex -file-line-error -interaction nonstopmode %f"))

  ;; 生成PDF后清理辅助文件
  ;; https://answer-id.com/53623039
  (setq org-latex-logfiles-extensions 
    (quote ("lof" "lot" "tex~" "tex" "aux" 
      "idx" "log" "out" "toc" "nav" 
      "snm" "vrb" "dvi" "fdb_latexmk" 
      "blg" "brf" "fls" "entoc" "ps" 
      "spl" "bbl" "xdv")))

  ;; 图片默认宽度
  (setq org-image-actual-width '(300))

  (setq org-export-with-sub-superscripts nil)

  ;; 不要自动创建备份文件
  (setq make-backup-files nil)

  ;; elegantpaper.cls
  ;; https://github.com/ElegantLaTeX/ElegantPaper/blob/master/elegantpaper.cls
  (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"))
  (setq org-latex-listings t)
  (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}")))
  )
6 个赞

遇到一个问题就是,org 里面比较长的表格导出来后可能会超出页面

请问 org 里的小配置能否提供一个事例呀,我似乎用这个会报错

建议使用latexmk来自动化构建过程