自动化组织 - > Rnw - > tex - > PDF格式

时间:2014-08-14 19:00:50

标签: emacs elisp org-mode knitr ess

我试图通过knitr自动执行org-mode文件的一些处理,最后是pdf。要做到这一点,我使用https://github.com/chasberry/orgmode-accessories的ox-ravel.el。基本上我想在org-mode导出中的另一个条目允许我运行org-ravel-latex-noweb-pdf-dispatch的C-c C-e l q:

(org-export-define-derived-backend 'latex-noweb 'latex
  :translate-alist '((src-block . org-ravel-src-block)
                    (inline-src-block . org-ravel-inline-src-block))
  :menu-entry
  '(?l 1
      ((?q "As KnitR PDF" org-ravel-latex-noweb-pdf-dispatch))))

这似乎删除了ox-ravel中类似派生后端的当前条目:

(org-export-define-derived-backend 'latex-noweb 'latex
  :translate-alist '((src-block . org-ravel-src-block)
                    (inline-src-block . org-ravel-inline-src-block))
  :menu-entry
  '(?l 1
      ((?r "As Rnw File" org-ravel-latex-noweb-dispatch))))

任何提示两个条目的提示都表示赞赏。现在是更棘手的部分。我希望org-ravel-latex-noweb-pdf-dispatch首先导出到Rnw文件,如下所示:

 (defun org-ravel-latex-noweb-dispatch 
  (&optional async subtreep visible-only body-only ext-plist)
"Execute menu selection. See org-export.el for meaning of ASYNC,
      SUBTREEP, VISIBLE-ONLY and BODY-ONLY."
(interactive)
(if async
    (message "No async allowed.")
  (let
      ((outfile  (org-export-output-file-name ".Rnw" subtreep)))
       (org-export-to-file 'latex-noweb 
                           outfile async subtreep visible-only 
                           body-only ext-plist))))

.Rnw文件导出后,我需要运行ess-swv-weave来导出.tex文件。然后我想运行org-latex-compile来获得最终的pdf。以下是org-latex-export-to-pdf的一部分可能是相关的:

(defun org-latex-export-to-pdf
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to LaTeX then process through to PDF."
  (interactive)
  (let ((outfile (org-export-output-file-name ".tex" subtreep)))
    (org-export-to-file 'latex outfile
      async subtreep visible-only body-only ext-plist
      (lambda (file) (org-latex-compile file)))))

任何帮助将上述想法结合起来使C-c C-e l q产生所需的pdf将不胜感激!

1 个答案:

答案 0 :(得分:2)

org-export-define-derived-backend的第一个参数是新后端的名称。由于您有两个名称相同的定义'latex-noweb,原始的定义会被覆盖。

因此,将您的后端重命名为latex-knitr,并创建一个函数org-ravel-latex-noweb-pdf-dispatch,复制org-ravel-latex-noweb-dispatch的定义并进行修改。关键是org-export-to-file,它可以使用一个额外的参数POST-PROCESS,你可以用来编织.Rnw文件并编译生成的tex文件:

(defun org-ravel-latex-noweb-pdf-dispatch 
    (&optional async subtreep visible-only body-only ext-plist)
  "Process org file through knitr. See org-export.el for meaning of 
SUBTREEP, VISIBLE-ONLY and BODY-ONLY."
  (interactive)
  (if async
      (message "No async allowed.")
    (let ((outfile  (org-export-output-file-name ".Rnw" subtreep)))
      (org-export-to-file
          'latex-noweb 
          outfile async subtreep visible-only 
          body-only ext-plist
          (lambda (file)
            << run `ess-swv-weave' on file >>
            (let ((texfile (concat
                            (file-name-sans-extension file)
                            ".tex")))
              (org-latex-compile texfile)))))))