3

I want to knit AND latexmk Knitr documents using one AUCtex command. I don't know how to code in lisp, and web search didn't turn up anything like this.

I have something close. The extension of the file needs to be changed for latexmk. Any help will be appreciated.

Following line is for my .emacs file.

(add-hook 'LaTeX-mode-hook (lambda () (push '
  ("KnitrLaTeX" "Rscript -e \"library(knitr)\; knit('%s')\" && latexmk -pdf %s" 
   TeX-run-TeX nil t :help "Run knitr and latexmk on file") 
   TeX-command-list)))

When I run C-c C-c (KnitrLaTeX), emacs runs the following command:

Running `KnitrLaTeX' on `slides.Rnw' with ``Rscript -e "library(knitr); knit('slides.Rnw')" && latexmk -pdf slides.Rnw''

Which is wrong. It should read "... && latexmk -pdf slides.tex"

Thanks in advance.

elemakil
  • 3,681
  • 28
  • 53
Sang
  • 535
  • 5
  • 14

1 Answers1

5

It appears that you are having trouble with how the second usage of %s is being interpreted at the tail end of your compile command -- i.e., you want the second usage of %s to mean slides.tex instead of slides.Rnw.

Although I am not familiar with knit, I am familiar with creating custom variables for use with AUCTeX. Set forth below are some examples of how to create custom variables and add them to the TeX-expand-list.

Rather than of using %s for a second time (i.e., at the tail end of your compilation command), perhaps consider using %(tex-file-name) instead. This assumes that your *.tex file is open in the buffer with focus when you begin your compilation command -- i.e., the full file name will be inserted into your compilation command.

If you have a file with a different extension that is open in the buffer with focus when you run your compilation command, and if you want the base name to be the same (but with a different extension), then you would do something similar to the example of %(pdf-file-name) -- i.e., remove whatever extension is there and replace it with the new one.

(eval-after-load "tex" '(progn

  (add-to-list 'TeX-expand-list '("%(tex-file-name)" (lambda ()
    (concat "\"" (buffer-file-name) "\""))))

  (add-to-list 'TeX-expand-list '("%(pdf-file-name)" (lambda ()
    (concat
      "\"" (car (split-string (buffer-file-name) "\\.tex"))
      ".pdf" "\""))))

  (add-to-list 'TeX-expand-list '("%(line-number)" (lambda ()
    (format "%d" (line-number-at-pos))))) ))
elemakil
  • 3,681
  • 28
  • 53
lawlist
  • 13,099
  • 3
  • 49
  • 158
  • This works great! I added `(add-to-list 'TeX-expand-list '("%(ppp-file-name)" (lambda () (concat "\"" (car (split-string (buffer-file-name) "\\.Rnw")) ".tex" "\""))))` And I modified the compile command to `(add-hook 'LaTeX-mode-hook (lambda () (push '("KnitrLaTeX" "Rscript -e \"library(knitr)\; knit('%s')\" && latexmk -pdf %(ppp-file-name)" TeX-run-TeX nil t :help "Run knitr and latexmk on file") TeX-command-list)))`. An example of the complete command emacs runs after the modification is:`Rscript -e "library(knitr); knit('slides.Rnw')" && latexmk -pdf "[path]/slides.tex"` Thank you so much! – Sang Oct 28 '13 at 17:47