8

I haven't been able to find much on how to set up Zathura to work with AUCTeX/Emacs in terms of forward/inverse-searching. Has anyone been successful in this?

  • 2
    Thanks to these instructions I managed to open the viewer and activate forward/inverse search. The problem is that you have first to open the viewer with the -s (to enable SyncTeX) and -x (to specify the editor) switches and then re-run zathura with --synctex-forward. This isn't really comfortable. – giordano Oct 19 '14 at 10:42
  • @giordano: yes, I had seen those instructions, but they seem to be for vim, and I wasn't 100% certain how to adapt them to emacs. I also found this: wiki.archlinux.org/index.php/Emacs#SyncTeX_support - but, again, it requires adapting - to AUCTeX rather than the built-in emacs TeX support. – emacsomancer Oct 27 '14 at 4:42
11

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.

  • Nice! I improved formatting and simplified the determination of pdfname ;-). – giordano Jan 7 '15 at 15:49
6

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"))
  • Now Zathura is among AUCTeX default viewers, thanks for sharing the news! – giordano Feb 8 '16 at 9:38
  • I tried all three listed possibilities but none of them worked for me. Can you help me out? @giordano – Tim Hilt Dec 2 '18 at 21:53
  • @TimHilt please write to AUCTeX's mailing list at auctex@gnu.org ;-) – giordano Dec 5 '18 at 20:46
1

Zathura+AUCTeX's forward search is almost perfect, but one thing is missing: Zathura's window is not automatically raised. Here's a hacky solution which monitors DBus messages and uses xdotool and wmctrl (yes, both) to raise the window.

~/bin/zathura-raise.sh:

#!/bin/bash
dbus-monitor --profile  "type='method_call', path='/org/pwmt/zathura',member=SynctexView" | sed -Eune '/zathura/{s/^.*PID-([0-9]+).*$/\1/;p}' | xargs -L 1 xdotool search --pid | sed -Eune '2~2p' | xargs -L 1 wmctrl -iR

While you can run zathura-raise.sh manually, here's a systemd service file, zathura-raise.service. Save it in ~/.config/systemd/user, start it by systemctl --user start zathura-raise.service, enable it by systemctl --user enable zathura-raise.service, and forget about it.

~/.config/systemd/user/zathura-raise.service

[Unit]
Description=Raise Zathura window on Synctex forward search

[Service]
ExecStart=%h/bin/zathura-raise.sh
Restart=always

[Install]
WantedBy=multi-user.target

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.