Using SumatraPDF with AUCTeX (with inverse search and forward search)

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
     (when use-sumatra-for-tex-view
       (defvar full-path-to-sumatrapdf
          ;; 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)
       (if full-path-to-sumatrapdf
             (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))
                       ;; ;; 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))
                       (list "-inverse-search"
                              ;; 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
                          ,@(if TeX-source-correlate-mode
                                (append inverse-search forward-search))
             (add-to-list 'TeX-view-program-list
         (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))
    (if (TeX-source-correlate-server-enabled-p)
        (warn "Emacs server, named %s, is now running." server-name)
      (warn "o1hhnjmc. Weird."))))

1 Test

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)
This entry was posted in Emacs and tagged , . Bookmark the permalink.

1 Response to Using SumatraPDF with AUCTeX (with inverse search and forward search)

  1. toto says:

    Thanks a lot for this .emacs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s