Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to setup SyncTeX with Emacs, or, more specifically, with AUCTeX, and Evince in Ubuntu. How can I do that? I would like both forward search (from source to output) and inverse search (from output to source).

I have tried the following without success:

  • I tried to do as http://www.emacswiki.org/emacs/AUCTeX#toc19 describes but when I try backward search (Ctrl+left click) I get the following message in Emacs:

    apply: Wrong number of arguments: (lambda (file linecol) (let ((buf (get-buffer file)) (line (car linecol)) (col (cadr linecol))) (if (null buf) (message "Sorry, %s is not opened..." file) (switch-to-buffer buf) (goto-line (car linecol)) (unless (= col -1) (move-to-column col))))), 3

  • If I try the solution in http://ubuntuforums.org/showpost.php?p=11010827&postcount=12 I get the following message in Emacs:

    Symbol's value as variable is void: TeX-view-program-list

  • When I try http://ubuntuforums.org/showthread.php?p=11010827#post11010827 Emacs freezes a while and then I get the following message in Emacs:

    let*: D-Bus error: "Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken."

I am using AUCTeX 11.86 with Emacs 23.3.1 and Evince 3.2.1 in Ubuntu 11.10.

share|improve this question
1  
The emacswiki.org code should now work with newer versions of Evince. (Or, at least, it does here) –  Rupert Swarbrick Mar 28 '12 at 14:58
    
@RupertSwarbrick I get it to work too but it only seems to backwards search and no forward search. How can I get forward search? –  N.N. Mar 28 '12 at 16:02
    
Ah, for that you'll need to use and/or adapt one of the solutions below. –  Rupert Swarbrick Mar 28 '12 at 16:20
    
@RupertSwarbrick Thanks, I adapted it to an answer. –  N.N. Mar 28 '12 at 17:22
    
For me, sometimes .synctex.gz is not produced, so I added the following code to force it. (custom-set-variables '(LaTeX-command "latex -synctex=1") ) –  user15917 Jun 25 '12 at 0:29

8 Answers 8

up vote 12 down vote accepted

I combined some of the links mentioned here, you will find the links in the source comments. This code supports:

  • Forward search (Emacs to Evince, via C-c C-v)
  • Backward/Inverse search (Evince to Emacs, via C-Mouse-1, that is Ctrl + "Left Click" in Evince)
  • Path names with spaces
  • Multifile setups (TeX files requested by Evince will be opened if they aren't open yet)
; SyncTeX basics

; un-urlify and urlify-escape-only should be improved to handle all special characters, not only spaces.
; The fix for spaces is based on the first comment on http://emacswiki.org/emacs/AUCTeX#toc20

(defun un-urlify (fname-or-url)
  "Transform file:///absolute/path from Gnome into /absolute/path with very limited support for special characters"
  (if (string= (substring fname-or-url 0 8) "file:///")
      (url-unhex-string (substring fname-or-url 7))
    fname-or-url))

(defun urlify-escape-only (path)
  "Handle special characters for urlify"
  (replace-regexp-in-string " " "%20" path))

(defun urlify (absolute-path)
  "Transform /absolute/path to file:///absolute/path for Gnome with very limited support for special characters"
  (if (string= (substring absolute-path 0 1) "/")
      (concat "file://" (urlify-escape-only absolute-path))
      absolute-path))


; SyncTeX backward search - based on http://emacswiki.org/emacs/AUCTeX#toc20, reproduced on http://tex.stackexchange.com/a/49840/21017

(defun th-evince-sync (file linecol &rest ignored)
  (let* ((fname (un-urlify file))
         (buf (find-file fname))
         (line (car linecol))
         (col (cadr linecol)))
    (if (null buf)
        (message "[Synctex]: Could not open %s" fname)
      (switch-to-buffer buf)
      (goto-line (car linecol))
      (unless (= col -1)
        (move-to-column col)))))

(defvar *dbus-evince-signal* nil)

(defun enable-evince-sync ()
  (require 'dbus)
  ; cl is required for setf, taken from: http://lists.gnu.org/archive/html/emacs-orgmode/2009-11/msg01049.html
  (require 'cl)
  (when (and
         (eq window-system 'x)
         (fboundp 'dbus-register-signal))
    (unless *dbus-evince-signal*
      (setf *dbus-evince-signal*
            (dbus-register-signal
             :session nil "/org/gnome/evince/Window/0"
             "org.gnome.evince.Window" "SyncSource"
             'th-evince-sync)))))

(add-hook 'LaTeX-mode-hook 'enable-evince-sync)


; SyncTeX forward search - based on http://tex.stackexchange.com/a/46157

;; universal time, need by evince
(defun utime ()
  (let ((high (nth 0 (current-time)))
        (low (nth 1 (current-time))))
   (+ (* high (lsh 1 16) ) low)))

;; Forward search.
;; Adapted from http://dud.inf.tu-dresden.de/~ben/evince_synctex.tar.gz
(defun auctex-evince-forward-sync (pdffile texfile line)
  (let ((dbus-name
     (dbus-call-method :session
               "org.gnome.evince.Daemon"  ; service
               "/org/gnome/evince/Daemon" ; path
               "org.gnome.evince.Daemon"  ; interface
               "FindDocument"
               (urlify pdffile)
               t     ; Open a new window if the file is not opened.
               )))
    (dbus-call-method :session
          dbus-name
          "/org/gnome/evince/Window/0"
          "org.gnome.evince.Window"
          "SyncView"
          (urlify-escape-only texfile)
          (list :struct :int32 line :int32 1)
  (utime))))

(defun auctex-evince-view ()
  (let ((pdf (file-truename (concat default-directory
                    (TeX-master-file (TeX-output-extension)))))
    (tex (buffer-file-name))
    (line (line-number-at-pos)))
    (auctex-evince-forward-sync pdf tex line)))

;; New view entry: Evince via D-bus.
(setq TeX-view-program-list '())
(add-to-list 'TeX-view-program-list
         '("EvinceDbus" auctex-evince-view))

;; Prepend Evince via D-bus to program selection list
;; overriding other settings for PDF viewing.
(setq TeX-view-program-selection '())
(add-to-list 'TeX-view-program-selection
         '(output-pdf "EvinceDbus"))

This will likely break for:

  • Path names with special characters other than spaces

I used these helpful links:

I tested with

  • Emacs 24.2
  • Evince 3.4.0
  • OpenSUSE 12.2
share|improve this answer
1  
Does this also care about multifile setups, i.e. backwards search from evince to a file which is not currently open in emacs? I keep getting a message, say, chap4.tex not open from emacs –  daleif Oct 17 '12 at 15:43
    
@daleif: I managed to add support for multifile setups in the latest revision of my answer. –  Milan Oct 17 '12 at 16:08
    
Thanks! Working with GNU Emacs 23.3.1 and Evince 3.4.0 in Ubuntu 12.04. Note that you require dbus twice. Once in the beginning and once in enable-evince-sync. –  N.N. Oct 17 '12 at 19:35
    
Thanks, works great! I made a little edit to correct the requirements of 'dbus and 'cl and enhance the un-urlify function to use url-unhex-string. I use emacs 24.2.1 and auctex 11.86 on Arch Linux. It seems forward search works out-of-the-box now and evince is the standard pdf viewer. I only added the code for the backward search in my .emacs –  sebastian Oct 30 '12 at 11:54
    
NICE. Thanks a lot. –  daleif Nov 1 '12 at 14:00

What are you using? (Linux, Windows, Mac)

If you're using Linux, then Okular's probably the easiest PDF viewer to set-up synctex forward/backward search with emacs. Once you've installed Okular, you can add the following code to your .emacs config file:

;; Okular

(setq TeX-view-program-list '(("Okular" "okular --unique %u")))

(add-hook 'LaTeX-mode-hook '(lambda ()
                  (add-to-list 'TeX-expand-list
                       '("%u" Okular-make-url))))

(defun Okular-make-url () (concat
               "file://"
               (expand-file-name (funcall file (TeX-output-extension) t)
                         (file-name-directory (TeX-master-file)))
               "#src:"
               (TeX-current-line)
               (expand-file-name (TeX-master-directory))
               "./"
               (TeX-current-file-name-master-relative)))

(setq TeX-view-program-selection '((output-pdf "Okular")))
share|improve this answer
    
Thanks for your answer. I'm using Ubuntu 11.04 and I'd prefer to use Evince as a viewer. –  N.N. Oct 7 '11 at 5:46
2  
Evince is, or at least used to be, harder to set up to do forward/backward sync with Emacs. There's a forum thread here which discusses setting up Evince to handle forward/backward sync with Emacs ubuntuforums.org/showthread.php?t=1716268 . –  BeSlayed Oct 10 '11 at 13:30
    
That should read (harder) "than Okular". –  BeSlayed Oct 10 '11 at 13:36
    
I have tried the solutions of that thread but without success. See my edited question. –  N.N. Jan 5 '12 at 9:39

I posted a workaround for your problem here. I'll paste the relevant part here for your convenience and just in case anyone else has the same problem. Good luck!


I had to add a couple of lines in order to make it work.

Initially, I was getting the error:

Symbol's value as variable is void: TeX-view-program-list

Then I added the following line between lines 38 and 39:

(setq TeX-view-program-list '())

Sadly, I started getting the error:

Symbol's value as variable is void: TeX-view-program-selection

So I added the following line between lines 43 and 44:

(setq TeX-view-program-selection '())

It fixed the problem. I'm not sure why :-P It was a matter of trial and error. Now everything works as it should.

Edit: As percusse suggested, I think it is better to combine my two answers in one. So here goes the full explanation:

Please take a look at the relevant part of my ~/.emacs file regarding forward and inverse search (see below). After adding the two lines I mentioned and three more lines (just in order to choose pdflatex, start the server and choose and activate synctex), I got a working environment with inverse and forward search for evince + emacs + auctex in my system (Debian Wheezy + Gnome 3.2.1 + Emacs 23.3.1 + Evince 3.2.1).

Of course, all the credit for this goes to the authors, referenced through the links mentioned before. I'll paste it here just for your convenience.

;; Forward/inverse search with evince using D-bus.

(server-start)
(add-hook 'LaTeX-mode-hook 'TeX-PDF-mode)
(add-hook 'LaTeX-mode-hook 'TeX-source-correlate-mode)
(setq TeX-source-correlate-method 'synctex)

(if (require 'dbus "dbus" t)
(progn
  ;; universal time, need by evince
  (defun utime ()
(let ((high (nth 0 (current-time)))
      (low (nth 1 (current-time))))
  (+ (* high (lsh 1 16) ) low)))

  ;; Forward search.
  ;; Adapted from http://dud.inf.tu-dresden.de/~ben/evince_synctex.tar.gz
  (defun auctex-evince-forward-sync (pdffile texfile line)
    (let ((dbus-name
       (dbus-call-method :session
                 "org.gnome.evince.Daemon"  ; service
                 "/org/gnome/evince/Daemon" ; path
                 "org.gnome.evince.Daemon"  ; interface
                 "FindDocument"
                 (concat "file://" pdffile)
                 t     ; Open a new window if the file is not opened.
                 )))
      (dbus-call-method :session
            dbus-name
            "/org/gnome/evince/Window/0"
            "org.gnome.evince.Window"
            "SyncView"
            texfile
            (list :struct :int32 line :int32 1)
    (utime))))

  (defun auctex-evince-view ()
    (let ((pdf (file-truename (concat default-directory
                      (TeX-master-file (TeX-output-extension)))))
      (tex (buffer-file-name))
      (line (line-number-at-pos)))
      (auctex-evince-forward-sync pdf tex line)))

  ;; New view entry: Evince via D-bus.
  (setq TeX-view-program-list '())
  (add-to-list 'TeX-view-program-list
           '("EvinceDbus" auctex-evince-view))

  ;; Prepend Evince via D-bus to program selection list
  ;; overriding other settings for PDF viewing.
  (setq TeX-view-program-selection '())
  (add-to-list 'TeX-view-program-selection
           '(output-pdf "EvinceDbus"))

  ;; Inverse search.
  ;; Adapted from: http://www.mail-archive.com/auctex@gnu.org/msg04175.html
  (defun auctex-evince-inverse-sync (file linecol timestamp)
    (let ((buf (get-file-buffer (substring file 7)))
      (line (car linecol))
      (col (cadr linecol)))
      (if (null buf)
      (message "Sorry, %s is not opened..." file)
    (switch-to-buffer buf)
    (goto-line (car linecol))
    (unless (= col -1)
      (move-to-column col)))))

  (dbus-register-signal
   :session nil "/org/gnome/evince/Window/0"
   "org.gnome.evince.Window" "SyncSource"
   'auctex-evince-inverse-sync)))
share|improve this answer
    
The forward search works fine with the Victor's .emacs script under texlive 2011, forcing auctex to absolute path file name compiling. You can see the details in this post: tex.stackexchange.com/a/50383/13180 Hope this help you. –  carlosv Apr 2 '12 at 13:29

Revision 83 of http://www.emacswiki.org/emacs/AUCTeX#toc19 contains a fix for newer versions of Evince which makes it work for me. Note that it only gives you backward search.

To make it work you need to put the following code in your .emacs then you use it by Ctrl+left clicking in Evince to get to the corresponding line in AUCTeX. I have made some very small changes to the code compared to the one at EmacsWiki.

;; PDF mode (rather than DVI-mode)
(setq TeX-PDF-mode t)

;; Use Evince as default viewer C-c C-v
(setq TeX-view-program-list '(("Evince" "evince %o"))
      TeX-view-program-selection '((output-pdf "Evince")))

;;; Backward search Evince to AUCTeX adapted from http://www.emacswiki.org/emacs/AUCTeX#toc19

(add-hook 'LaTeX-mode-hook 'TeX-source-correlate-mode)
(setq TeX-source-correlate-start-server t); Automatically start server without asking

(defun un-urlify (fname-or-url)
  "A trivial function that replaces a prefix of file:/// with just /."
  (if (string= (substring fname-or-url 0 8) "file:///")
      (substring fname-or-url 7)
    fname-or-url))

(defun th-evince-sync (file linecol &rest ignored)
  (let* ((fname (un-urlify file))
         (buf (find-buffer-visiting fname))
         (line (car linecol))
         (col (cadr linecol)))
    (if (null buf)
        (message "[Synctex]: %s is not opened..." fname)
      (switch-to-buffer buf)
      (goto-line (car linecol))
      (unless (= col -1)
        (move-to-column col)))))

(defvar *dbus-evince-signal* nil)

(defun enable-evince-sync ()
  (require 'dbus)
  (when (and
         (eq window-system 'x)
         (fboundp 'dbus-register-signal))
    (unless *dbus-evince-signal*
      (setf *dbus-evince-signal*
            (dbus-register-signal
             :session nil "/org/gnome/evince/Window/0"
             "org.gnome.evince.Window" "SyncSource"
             'th-evince-sync)))))

(add-hook 'LaTeX-mode-hook 'enable-evince-sync)
share|improve this answer
    
Please contribute if you know how to add forward search to this solution! –  N.N. Mar 28 '12 at 17:24

In case someone is not running an DE(gnome/kde etc.), emacs might throw the following error, when using the above script from victor

D-Bus error: "No connection to bus", :session

Then just run

eval `dbus-launch` 
export DBUS_SESSION_BUS_ADDRESS 

from terminal before starting emacs. Or modify it to .xinitrc

I use TexLive 2011, AucTex from CVS, emacs 24.1, evince 3.4.0 and no DE.

share|improve this answer

This was supposed to be a comment to Victor's answer, but I do not have enough reputation points. Also, I can confirm forward and inverse search using his answer, with the modification below.

Inserting the line (setq TeX-view-program-list '()) deletes the references to other viewers you may have configured (dvi viewers for example).

Possible work around is to delete the lines

;; New view entry: Evince via D-bus.
(setq TeX-view-program-list '())
(add-to-list 'TeX-view-program-list
         '("EvinceDbus" auctex-evince-view))

;; Prepend Evince via D-bus to program selection list
;; overriding other settings for PDF viewing.
(setq TeX-view-program-selection '())
(add-to-list 'TeX-view-program-selection
         '(output-pdf "EvinceDbus"))

and add "Evince" or "EvinceDbus" (any name would do) with the command "auctex-evince-view" using Custom (LaTeX > Customize AucTeX > Browse Options > TeX Command > TeX View > TeX View Program List). Then you need to verify it appears in TeX View Program Selection in "output-pdf".

share|improve this answer

This emacs code snippet seems to work with recent versions of evince:

http://coderuins.wordpress.com/2011/10/04/using-synctex-with-emacs-and-evince-3-2/

share|improve this answer
    
When I try this and Ctrl+left click for forward search Emacs freezes for a minute and when it unfreezes I see the following in *Messages*: "let: D-Bus error: "Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken."" –  N.N. Feb 28 '12 at 9:26

On some Linux distributions there might be a problem of TeX Live version.

For instance pdflatex supplied with TeX Live 2007 (which is by default shipped with Fedora 16) do not support synctex. This breaks down the inverse/forward search between emacs/auctex and evince.

It is recommended to check the defualt available version of TeX Live and, if necessary, install the newest TeX Live from http://www.tug.org/texlive/acquire-iso.html.

Credits for this remark goes to RupertPupkin, see the following thread on fedora forum.

share|improve this answer
    
I use TeX Live 2011. Your linked solution does not work for me. –  N.N. Mar 28 '12 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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