2014-07-22 48 views
4

後您的RNW文件,我想針織的.Rnw文件後啓動膠乳。元代碼shoudl是這樣的:有無Emacs的自動乳膠針織

(defun knit() 
    (interactive) 
    (setq rproc (ess-get-process ess-current-process-name)) 
    (setq c "knit('foo.rnw')\n") 
    (process-send-string rproc c) 
    ;; Wait for ESS to finish 
    (shell-command-to-string "cd foo/path & pdflatex foo")) 

的主要問題是有Emacs的等待下緩衝完成針織纔開始latexing後。我發現了一些有趣的功能:(ess-wait-for-process ...)(低級別標記爲忙碌...),這可能有效,但我無法理解。

注意(shell-command-to-string ...僅是說明性的。最終的選擇可能是:

(TeX-command "LaTeX" 'rnw-to-tex-ext -1)) 

knit2pdf()可能是走一條不同的道路,但我會失去AUCTeX的利益。


PS:我的問題被SE機器人視爲「主觀且可能關閉」!

回答

1

我使用texi2pdf並且在編織之後有R做它,但這顯然相當於knit2pdf。雖然我不知道你能不能用system打電話給你想要的任何命令TEX R裏面的knit命令,類似於如何使用texi2pdf後。

我不是專家的emacs無論如何但對於什麼是值得這裏是我的.emacs文件我具備的功能。

; use knitr (was Sweave) script as compile function for Rnw files 
(defun ess-swv-SweaveSh() 
    "Use knitr script to knit an Rnw file and create a pdf." 
    (interactive) 
    (let 
    ((compilation-buffer-name-function (function (lambda(ign)(concat "*" (buffer-file-name) "*"))))) 
    (compile (concat "Rscript -e \".n <- '" (buffer-file-name) "'; library(knitr); knit(.n); library(tools); texi2pdf(sub('Rnw$','tex',.n))\"")) 
) 
) 
+0

knit2pdf只是針織+ TEXI2PDF,你並不需要擔心的只是延長通過.Rnw – antonio

+0

整潔,謝謝!我想這對OP來說並不有用,因爲他們指定'knit2pdf'對他們沒有用處。由於沒有其他答案,我會添加另一個想法... – Aaron

+0

我找到了一個可能的解決方案。請看看。 – antonio

1

可能的解決方案是檢查劣質ESS過程的'busy屬性。 等待它的while循環是nil塊命令回顯。這就是您看到(redisplay)的原因。爲了避免壓力你的親愛的,我還設置了刷新延遲(sleep-for .5)
爲了防止ESS卡住,60秒後存在循環。調整它,如果你有重量碼。

最後我連着latexing到AUCTeX。因此,您可以在TeX-expand-list文檔的幫助下自定義LaTeX-command-style

該功能會自動設置適當的名稱的文件進行編織和乳膠,並設置R工作目錄到.Rnw文件進行鍼織,這樣你就可以設置其它腳本或加載數據。
因此,您可以使用M-x knit在任何地方運行它,或者將它與快捷方式關聯。 該功能在編織之前保存最後更改的緩衝區,如果沒有可用的R打開,則打開劣質R緩衝區;否則它使用現有的。

(defun knit() 
    "Save the buffer, knit and latex" 
    (interactive) ; You will associate this to you favourite key 

    (let* (
    ;; Get names & path 
     (cur-dir (file-name-directory (buffer-file-name))) 
    (rnw-name (file-name-nondirectory (buffer-file-name))) 
    (tex-name (concat (file-name-base (buffer-file-name)) ".tex")) 

    ;; Create knit command 
    (cmd (format "require(knitr); setwd('%s'); knit('%s')" cur-dir rnw-name)) 

    ;; Time the knitting 
    (start-time (float-time)) 
    (wait 60) ; Lifeboat to exit loop if smt wrong 
    ) 

    ;; Save rnw buffer... you are lazy, I know) 
    (save-buffer) 


    ;; Send string to R at low-level 
    ;;(setq rproc (ess-get-process ess-current-process-name)) 
    ;;(process-send-string rproc c) 

    ;; or Send line with the ESS wrapper 
    (ess-eval-linewise cmd) 

    ;; While loop to check when 
    (setq start-time (float-time) 
     wait 60) ; Lifeboat to exit loop after x secs 

    ;; Wait for 'busy property nil, nut not more than wait seconds 
    (setq rproc (ess-get-process ess-current-process-name)) 
    (while (< (- (float-time) start-time) wait) 
     (sleep-for .5) 
     ;; (accept-process-output rproc .5) ;alt. way for process-send-string 
     (redisplay) 
     (if (not (process-get rproc 'busy)) 
     (setq wait 0) 
    (message "Knitting... "))) 
    (message "Knitting finished, starting latexing") 

    ;; Set LaTeX your fav options. See TeX-expand-list for % pars 
    (setq LaTeX-command-style '(("" "%(PDF)%(latex) -file-line-error %S%(PDFout)"))) 
    ;; TeX-command requires a 'file function (anonymous here) returning the filename. 
    ;; TeX-command/TeX-expand-list say 'file has one opt arg: extension. 
    ;; Actually they are 2, despite the second seems always passed true. 
    (TeX-command "LaTeX" (lambda (&optional ext dummy) tex-name)))) 
+0

太好了,我想知道爲什麼它不是明顯的答案呢。從那時起,你有沒有更新過這個'knit'函數?如何將此函數添加到'TeX-command-list'? – doctorate