How to set up forward/inverse searches with AUCTeX and Zathura

The Zathura SyncTeX interface has recently changed (I'm using v0.3.2 at time of writing): there is now no -s flag to activate SyncTeX. It might be the case that SyncTeX is active all the time.

The SyncTeX support is complicated by two annoying quirks of Zathura:

  1. Zathura doesn't accept --synctex-forward if the file is not already open.
  2. The Zathura window doesn't steal focus when receiving a --synctex-forward.

My Emacs/AUCTeX setup is therefore the following:

  • I am running an Emacs server such that emacsclient takes care of finding my frame and file.

  • Add the following to your .emacs to let AUCTeX know you want to call zathura-forward-search for opening compiled pdfs:

    (TeX-source-correlate-mode)        ; activate forward/reverse search
    (TeX-PDF-mode)
    (add-to-list 'TeX-view-program-list '("zathura" zathura-forward-search))
    (setq TeX-view-program-selection (quote ((output-pdf "zathura") (output-dvi "xdvi"))))
  • The function zathura-forward-search is complicated: it takes care of which Zathura processes have been opened by Emacs and are still alive, and which are therefore safe to send --synctex-forward directives to. Otherwise, it spawns a new Zathura process for the file. Furthermore, it uses the small tool wmctrl to give focus to the Zathura window afterwards. This should be installed on the machine and in the path for that line to work.

    (setq zathura-procs ())
    (defun zathura-forward-search ()
      ;; Open the compiled pdf in Zathura with synctex. This is complicated since
      ;; 1) Zathura refuses to acknowledge Synctex directive if the pdf is not
      ;; already opened
      ;; 2) This means we have to bookkeep open Zathura processes ourselves: first
      ;; open a new pdf from the beginning, if it is not already open. Then call
      ;; Zathura again with the synctex directive.
      (interactive)
      (let* ((zathura-launch-buf (get-buffer-create "*Zathura Output*"))
             (pdfname (TeX-master-file "pdf"))
             (zatentry (assoc pdfname zathura-procs))
             (zatproc (if (and zatentry (process-live-p (cdr zatentry)))
                          (cdr zatentry)
                        (progn
                          (let ((proc (progn (message "Launching Zathura")
                                             (start-process "zathura-launch"
                                                            zathura-launch-buf "zathura"
                                                             "-x" "emacsclient +%{line} %{input}" pdfname))))
                            (when zatentry
                              (setq zathura-procs (delq zatentry zathura-procs)))
                            (add-to-list 'zathura-procs (cons pdfname proc))
                            (set-process-query-on-exit-flag proc nil)
                            proc))))
             (pid (process-id zatproc))
             (synctex (format "%s:0:%s"
                              (TeX-current-line)
                              (TeX-current-file-name-master-relative)))
             )
        (start-process "zathura-synctex" zathura-launch-buf "zathura" "--synctex-forward" synctex pdfname)
        (start-process "raise-zathura-wmctrl" zathura-launch-buf "wmctrl" "-a" pdfname)
        ))
    

    Errors from the launched processes will be printed in the created buffer *Zathura Output*.

    The wmctrl call might fail to raise the correct window if more than one window has the name of the pdf in its title.


A few of months ago, Zathura was updated so that calling --synctex-forward will open the file if it isn't already open. As a result, the answer by @jsrn actually opens two instances of Zathura and the alternative is now much simpler.

Adding this to your emacs config or 'TeX-mode-hook should work if you have v11.89.1 or earlier:

(add-to-list 'TeX-view-program-list
             '("Zathura"
               ("zathura "
                (mode-io-correlate " --synctex-forward %n:0:%b -x \"emacsclient +%{line} %{input}\" ")
                " %o")
               "zathura"))
(add-to-list 'TeX-view-program-selection
             '(output-pdf "Zathura"))

Once Giordano's change makes it into the next release of AUCTeX, then the you will only need to add Zathura to the view program selection:

(add-to-list 'TeX-view-program-selection
             '(output-pdf "Zathura"))