Install or update AUCTeX using the package system, and paste the following code in your emacs init file. The code tries to do:
- make AUCTeX use SumatraPDF for viewing pdf output files, while keeping the default PDF reader for your MS Windows to be Adobe Reader or Foxit Reader or whatever you use.
- works with a portable SumatraPDF exe file put in any folder within the path to Emacs binary or with the SumatraPDF installed in the usual way.
- make forward search and inverse search work.
- make inverse search work with multiple instances of Emacs as well.
;; Enables forward search and inverse search. (add-hook 'TeX-mode-hook 'TeX-source-correlate-mode) ;; Make default output format to be PDF (rather than DVI) (add-hook 'TeX-mode-hook 'TeX-PDF-mode) (defvar use-sumatra-for-tex-view t "Non-nil to mean wanting to use SumatraPDF for viewing PDF output.") (eval-after-load 'tex '(progn (when use-sumatra-for-tex-view (defvar full-path-to-sumatrapdf (or ;; finding the portable SumatraPDF (let ((dir (locate-dominating-file invocation-directory "SumatraPDF.exe"))) (if dir (expand-file-name "SumatraPDF.exe" dir))) ;; finding SumatraPDF installed in the usual way (let ((test-path (expand-file-name "SumatraPDF/SumatraPDF.exe" (getenv "ProgramFiles")))) (if (file-exists-p test-path) test-path)))) (if full-path-to-sumatrapdf (progn (add-to-list 'TeX-view-program-selection '(output-pdf "Sumatra")) (defun sumatra-ii1550xe () (require 'server) ; for server-name ;; see TeX-expand-list to see meaning of %o, %n, %b. (let* ((the-b (TeX-command-expand "%b" 'TeX-active-master)) (the-n (TeX-command-expand "%n" 'TeX-active-master)) (the-o ;; ;; following is shell quoted. See def of TeX-command-expand to see the cause ;; (TeX-command-expand "%o" 'TeX-active-master) ;; following is not shell quoted. (TeX-active-master (TeX-output-extension) t)) (ecw-path (expand-file-name "emacsclientw.exe" invocation-directory)) (inverse-search (list "-inverse-search" (concat ;; path with quotes and it works. "\"" ecw-path "\"" ;; ;; path without quotes and it works too. ;; ecw-path " --no-wait" " --server-file=" server-name " +%l \"%f\""))) (forward-search (list "-forward-search" the-b the-n))) (apply 'call-process full-path-to-sumatrapdf nil 0 nil `("-reuse-instance" ,@(if TeX-source-correlate-mode (append inverse-search forward-search)) ,the-o)))) (add-to-list 'TeX-view-program-list `("Sumatra" sumatra-ii1550xe))) (warn "full-path-to-sumatrapdf is nil"))))) (defun my-alphanumeric-random-word (size) (require 'cl-lib) (apply 'string (cl-loop with chars = "abcdefghijklmnopqrstuvwxyz0123456789" with len = (length chars) for i below size collect (elt chars (random len))))) (defadvice TeX-source-correlate-start-server-maybe (after my-ensure-server activate) ;; TeX-view may fails start a server when another Emacs instance is already running a server. ;; When that happens, this tries a random server-name to start a server. (when (and TeX-source-correlate-start-server-asked (not (TeX-source-correlate-server-enabled-p))) (setq server-name (my-alphanumeric-random-word 8)) (server-start) (if (TeX-source-correlate-server-enabled-p) (warn "Emacs server, named %s, is now running." server-name) (warn "o1hhnjmc. Weird."))))
My tests say that the code works fine on:
- MS Windows 7
- latest official version of GNU Emacs (version 24.3.1)
- latest AUCTeX from the GNU package archive (version 11.86)
- latest version of SumatraPDF (version 2.3.2)