ESS + AUCTeX + Sweave。来自.Rnw< - >的SyncteX集成PDF查看器?

时间:2012-06-16 01:20:27

标签: emacs sweave ess auctex

我正在将我的工具链从R + TeXShop改造成我希望的更高效的Sweave - > LaTeX - > SyncteX - > pdfview(skim)通过emacs(Aquamacs,具体)AUCTeX模式(使用emacs-speaks-statsitics'sweave minor-mode)。使用来自AUCTeX的SyncteX,一切都很好用 - > pdfview链,从Sweave到.pdf的一切都很好。

当我添加Sweave作为第一步时,麻烦就出现了。 .Rnw文件现在成为主要原始文档,它生成的.tex文件只是一个中间步骤。当然,它是SyncteX锁定的.tex文件。

我对.emacs文件所做的唯一更改是注册两个新命令来从.Rnw构建.pdf。

有人可以提供有关如何让emacs使用SyncteX将输出.pdf同步到.Rnw而不是.tex的建议吗?

谢谢!

编辑1: 警告:这最终成为我整个下午摸索的总结。我非常接近解决方案,但最后因为我找不到文档的错误而被挫败。 警告2:今天是我第一次看到这个深入到emacs内部工作的任何地方。肯定存在错误或效率低下。

VitoshKa的回答让我大部分都在那里。将他的推荐添加到我的.emacs(嗯,Preferences.el,但功能相同)文件在启动时导致Symbol’s value as variable is void: noweb-minor-mode-map错误,因为Aquamacs似乎在其余的init文件之前加载了.emacs文件。

我通过在VitoshKa的(require 'ess-site) (require 'ess-eldoc)之前立即添加(define-key noweb-minor-mode-map (kbd "\C-c \C-c") 'Rnw-command-master)来修复此问题。不幸的是,Aquamacs的ess-swv.elc文件已经以二进制形式出货,迫使M-n P阅读器成为acroread。所以我将ess-swv.el文件的一部分的以下编辑添加到我自己的.emacs的底部:

(defun ess-makePDFandView ()
(interactive)
(setq namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
(setq tex-filename (concat "\"" namestem "\".tex"))
(setq my-command-string (concat "pdflatex -synctex=1" tex-filename))
(shell-command my-command-string)
(setq my-command-string (concat "/Applications/Skim.app/Contents/MacOS/Skim \"" namestem ".pdf\" &"))
(shell-command my-command-string))

(define-key noweb-minor-mode-map "\M-nv" 'ess-makePDFandView)

现在,emacs将使用 M-n v C-c C-c View RET 从emacs启动带有正确pdf名称的skim。还有一个障碍要通过......

命令转换 - 在emacs中单击尝试同步到filename.Rnw.pdf(但丢弃错误消息至少仍会导致pdfviewer中的正确同步)。 Command-Shift-在pdfviewer中单击以搜索源强制emacs打开filename.tex文件并同步到该文件。 Fortunately, Cameron Bracken has a fix for that。他只是不太喜欢emacs集成。

所以我将ess-swv.el进一步加入了我的.emacs:

(defun ess-swv-latex-rnw-sync ()
  "Run LaTeX on the product of Sweave()ing the current file."
  (interactive)
  (save-excursion
    (let* ((namestem (file-name-sans-extension (buffer-file-name)))
           (latex-filename (concat namestem ".tex"))
       (synctex-filename (concat namestem ".synctex.gz"))
           (tex-buf (get-buffer-create " *ESS-tex-output*"))
       (patchDVI-command-string (concat "Rscript -e \"library('patchDVI');patchSynctex('" synctex-filename "')\"")))
    (message "synctex string: %s" patchDVI-command-string) 
    (message "Running LaTeX on '%s' ..." latex-filename)
    (switch-to-buffer tex-buf)
    (call-process "latex" nil tex-buf 1 "-synctex=1 " latex-filename)
    (switch-to-buffer (buffer-name))
    (display-buffer tex-buf)
    (message "Finished running LaTeX" )
    (message "Running patchDVI...")
    (shell-command patchDVI-command-string))))

(define-key noweb-minor-mode-map "\M-nq" 'ess-swv-latex-rnw-sync)

认为缺少修补DVI并将其同步...但没有! Command-Shift-Click上的相同行为如上所述...回到黑客攻击ess-swv。我删除了ess-makePDFandView函数,而是将以下内容添加到我的.emacs中:

(defun ess-swv-PDF-rnw-sync (&optional pdflatex-cmd)
"From LaTeX file, create a PDF (via 'texi2pdf' or 'pdflatex', ...), by
default using the first entry of `ess-swv-pdflatex-commands' and display it."
(interactive
  (list
    (let ((def (elt ess-swv-pdflatex-commands 0)))
    (completing-read (format "pdf latex command (%s): " def)
                   ess-swv-pdflatex-commands ; <- collection to choose from
                   nil 'confirm ; or 'confirm-after-completion
                   nil nil def))))
(let* ((buf (buffer-name))
     (namestem (file-name-sans-extension (buffer-file-name)))
     (latex-filename (concat namestem ".tex"))
     (tex-buf (get-buffer-create " *ESS-tex-output*"))
     (pdfviewer (ess-get-pdf-viewer))
     (pdf-status)
     (cmdstr-win (format "start \"%s\" \"%s.pdf\""
                         pdfviewer namestem))
     (cmdstr (format "\"%s\" \"%s.pdf\" &" pdfviewer namestem)))
;;(shell-command (concat "pdflatex " latex-filename))
(message "Running '%s' on '%s' ..." pdflatex-cmd latex-filename)
(switch-to-buffer tex-buf)
(setq pdf-status
      (call-process pdflatex-cmd nil tex-buf 1 "-synctex=1 " 
                    (if (string= "texi2" (substring pdflatex-cmd 0 5))
                        ;; workaround (bug?): texi2pdf or texi2dvi *fail* to work with      full path:
                        (file-name-nondirectory latex-filename)
                      latex-filename)))
(if (not (= 0 pdf-status))
    (message "** OOPS: error in '%s' (%d)!" pdflatex-cmd pdf-status)
  ;; else: pdflatex probably ok
  (shell-command
   (concat (if (and ess-microsoft-p (w32-shell-dos-semantics))
               cmdstr-win
             cmdstr))))
(switch-to-buffer buf)
(display-buffer tex-buf)))

(define-key noweb-minor-mode-map "\M-nv" 'ess-swv-PDF-rnw-sync)

越来越近,但我们仍然需要告诉auctex要通过哪些文件名来浏览。所以我咬了一口子并编辑了auctex-config.el。我注释掉了现有的aquamacs-call-viewer函数定义,并将其替换为我自己的:

(defun aquamacs-call-viewer (line source)
"Display current output file as PDF at LINE (as in file SOURCE).
Calls `aquamacs-tex-pdf-viewer' to display the PDF file using the
Skim AppleScript protocol."
  (cond 
    ((string= (file-name-extension (TeX-active-master)) "Rnw") 
      (let* ((full-file-name 
     (expand-file-name
      ;; as in TeX-view
      ;; C-c C-c view uses %o (from TeX-expand-list), which
      ;; is the same.
      (concat (file-name-sans-extension (TeX-active-master)) "." (TeX-output-extension))
      default-directory))
    (full-source-name
     (expand-file-name 
      (concat (file-name-sans-extension (source)) ".Rnw") ;; this is relative to the master 
      (file-name-directory (full-file-name)))))
   (message "full pdf name: %s" full-file-name)
   (message "full rnw sournce name: %s" full-source-name)
   (do-applescript
(format 
 "
 set theSink to POSIX file \"%s\" 
 set theSource to POSIX file \"%s\" 
 tell application \"%s\" 
 activate 
 open theSink 
 tell front document to go to TeX line %d from theSource%s
 end tell
 " 
 full-file-name full-source-name aquamacs-tex-pdf-viewer line
 ;; do not say "showing reading bar false" so users can override in future
 (cond ((eq t aquamacs-skim-show-reading-bar)
    " showing reading bar true")
       ((eq nil aquamacs-skim-show-reading-bar)
    " showing reading bar false")
       (t ""))))))
(t
 (let* ((full-file-name 
     (expand-file-name
      ;; as in TeX-view
      ;; C-c C-c view uses %o (from TeX-expand-list), which
      ;; is the same.
      (TeX-active-master (TeX-output-extension))
      default-directory))
    (full-source-name
     (expand-file-name 
      source ;; this is relative to the master 
      (file-name-directory full-file-name))))
   (message "IN OTHERWISE!! WAT: %s" (file-name-extension (TeX-active-master)))
   (do-applescript
(format 
 "
 set theSink to POSIX file \"%s\" 
 set theSource to POSIX file \"%s\" 
 tell application \"%s\" 
 activate 
 open theSink 
 tell front document to go to TeX line %d from theSource%s
 end tell
 " 
 full-file-name full-source-name aquamacs-tex-pdf-viewer line
 ;; do not say "showing reading bar false" so users can override in future
 (cond ((eq t aquamacs-skim-show-reading-bar)
    " showing reading bar true")
       ((eq nil aquamacs-skim-show-reading-bar)
    " showing reading bar false")
       (t ""))))))))

还有一个最终测试(注意我将工具链的键绑定更改为:Mn s - > Mn q - &gt; Mn v依次为sweave - >&gt; latex - &gt; view [注意这最后一个步骤你必须使用pdflatex而不是texi2pdf]),一切都建立,没有错误,启动观众......

"Skim" "/Users/myname/text/testing/myfile.pdf": exited abnormally with code 127.

现在我又完全迷失了。谁熟悉Skim?

编辑2:好吧,错误127只是在二进制文件中找不到文件。在那里的某个地方我必须破坏路径。所以我在我的auctex-config.el顶部附近添加了(setenv "PATH" (concat (getenv "PATH") ":" "/Applications/Skim.app/Contents/MacOS"))。现在一切都编译并启动没有错误......但是命令转换 - 点击emacs不会让Skim做出响应(至少它没有给出任何错误)。 Command-Shift-Click on Skim仍强制emacs打开.tex文件。

我想这已成为一个非常Aquamacs + Skim特定的问题,因为大多数其余的交互都是在AppleScript中,除非我错过了上面指出的lisp关键行中的明显内容。

1 个答案:

答案 0 :(得分:3)

澄清。您希望来自Rnw文件的 C-c C-c 在Sweave的输出上运行AUCTeX命令( M-n s )。

问题是Auctex不知道它必须对不同的文件采取行动。这是如何让它意识到:

(defun Rnw-command-master (&optional override-confirm)
  "Run command on the current document.

If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
depend on it being positive instead of the entry in `TeX-command-list'."
  (interactive "P")
  (let ((TeX-transient-master (concat
                               (file-name-sans-extension (buffer-file-name))
                               ".tex"))
        (TeX-master nil))
    (TeX-command (TeX-command-query (TeX-master-file)) 'TeX-master-file
                 override-confirm)))


(define-key noweb-minor-mode-map (kbd "\C-c \C-c") 'Rnw-command-master)

为了让Auctex首先运行sweave,需要一些额外的工作。 Noweb支持现在在ESS中得到了积极的重写,在下一个版本中,您将拥有一个全新的AucTeX集成系统以及默认激活的许多其他有用的东西。