2

When using AUCTeX with pdflatex, one can run C-c C-c (TeX-command-master) or C-c C-v (TeX-view) to spawn a PDF viewer.

I use Evince on Debian Wheezy, in case it's relevant.

This might sound a little absurd, but I find it easy to lose track of where the Evince viewer attached to that particular buffer is. Sometimes it can be buried under other windows or mostly off the screen. I run other instances of Evince too. If it makes any difference, I've got Synctex at least partly working.

So, is there some way of telling emacs/AUCTeX to "find" my Evince viewer, or failing that, is there some convenient way to tell emacs/AUCTeX to kill the viewer and start a new one?

2

Linux/UNIX users

I'm guessing that you're going to need control over the window manager in order to find/raise the external PDF window. Here's a function that depends on the UNIX/Linux command line tool wmctrl.

The function (minimally tested) will locate the PDF window and raise it, or spawn a new window if it doesn't exist yet. The initial part of the code cribs directly from TeX-view:

(defun TeX-view-dwim ()
  "Locate PDF file if open and raise it.  Requires wmctrl."
  (interactive)
  (let ((output-file (TeX-active-master (TeX-output-extension))))
    (when (file-exists-p output-file)
      (let ((code (call-process-shell-command (format "wmctrl -a %s"
                                                      output-file))))
        (cond ((= code 0)               ; pdf has a window, so switch to it
               (message "Switched to pdf file"))
              ((= code 1)               ; pdf not open, so open it
               (message "Opening previously unopened pdf file")
               (TeX-view))
              ((= code 127)             ; wmctrl not installed
               (error "Please install wmctrl to enable this functionality!"))
              (t                        ; Something weird just happened
               (error "Unexpected problem with wmctrl!")))))))
  • Thanks for the answer, Dan. I thought AUCTeX might have something suitable built in. Apparently not. – Faheem Mitha Dec 29 '14 at 16:30
  • @FaheemMitha: you might also want to check out the EmacsWiki on Synctex and Evince, which appears to rely on dbus. I've not used synctex or dbus before, so can't comment on them -- hopefully someone else will provide a more general answer. – Dan Dec 29 '14 at 16:41
2

The simplest way is to maybe enable SyncTeX using TeX-source-correlate-mode. Then the default C-c C-v binding will either spawn or raise the Evince window and additionally jump to the current source location.

If you do not want to jump, this function, which is mostly copied from TeX-evince-sync-view, will only raise the window (should work on Evince 3.14).

(defun TeX-evince-view-dwim ()
  (interactive)
  (require 'url-util)
  (let* ((uri (concat "file://" (let ((url-unreserved-chars (cons ?/ url-unreserved-chars)))
                                  (url-hexify-string
                                   (expand-file-name
                                    (TeX-active-master (TeX-output-extension)))))))
         (owner (dbus-call-method
                 :session "org.gnome.evince.Daemon"
                 "/org/gnome/evince/Daemon"
                 "org.gnome.evince.Daemon"
                 "FindDocument"
                 uri
                 t)))
    (if owner
        (dbus-call-method
         :session owner
         "/org/gtk/Application/anonymous"
         "org.gtk.Application"
         "Activate"
         (list :array :signature "{sv}"))
      (error "Couldn't find the Evince instance for %s" uri))))

This causes Evince to send a raise request to your window manager; if the WM does not support this, it won't work.

  • What is the C-c C-v binding supposed to do, exactly? I tried it, and nothing happened. I thought I had synctex enabled. That highlighting thing does work - if I move the cursor in the emacs buffer, and run C-c C-c , the corresponding portion of Evince is highlighted. – Faheem Mitha Dec 29 '14 at 17:20
  • So it is highlighted when you use C-c C-c and select the View command, but not when pressing C-c C-v? That's weird, the latter should be a shortcut for the former. – ChrisR Dec 29 '14 at 17:23
  • Yes, C-c C-v does nothing as far as I can tell. But C-c C-c -> View command: TeX-evince-sync-view doesn't raise the window. It would be nice if it did. It does refresh the highlighting. – Faheem Mitha Dec 29 '14 at 17:31
  • What Evince version and window manager are you using? – ChrisR Dec 29 '14 at 17:31
  • 3.4.0-3.1 (Debian wheezy default) and KDE. I've got a vague feeling I tried to backport something more recent, but ran into problems. Would a more recent version be helpful? – Faheem Mitha Dec 29 '14 at 17:32

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.