1
votes

I'm trying to automate some of the processing of org-mode files through knitr and finally to a pdf. To do this I am using ox-ravel.el from https://github.com/chasberry/orgmode-accessories . Basically I want another entry in the org-mode export that allows me to C-c C-e l q that runs org-ravel-latex-noweb-pdf-dispatch:

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

This seems to remove the current entry for a similar derived backend in 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))))

Any tips to make both entries appear are appreciated. Now for the trickier part. I want org-ravel-latex-noweb-pdf-dispatch to first export to an Rnw file as is done by:

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

After the .Rnw file is export I need to run ess-swv-weave to export a .tex file. Then I want to run org-latex-compile to obtain a final pdf. Below is a portion of org-latex-export-to-pdf which may be relevant:

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

Any help in combining the above ideas to make C-c C-e l q produce the desired pdf would be greatly appreciated!

1

1 Answers

2
votes

The first argument of org-export-define-derived-backend is the name of the new backend. As you have two definitions with the same name 'latex-noweb, the original one gets overwritten.

So rename your backend to, say, latex-knitr, and create a function org-ravel-latex-noweb-pdf-dispatch, copy the definition of org-ravel-latex-noweb-dispatch and modify. The key is org-export-to-file, which can take one additional argument POST-PROCESS, you can use to weave the .Rnw file and compile the resulting tex file:

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