I have the source in one emacs buffer and the compiled pdf in another DocView buffer with auto-revert-mode enabled. Using latexmk -pvc
to compile gives me live previews. Now I want to setup forward and inverse search. All the instructions I can find about using emacs with synctex involves some external viewer. Is there any way to do this at all?
[Tex/LaTex] Set up SyncTeX with Emacs DocView
emacsforward-inverse-search
Related Solutions
It would help to know which, if any, LaTeX plugin you're using for vim. (E.g., the latex-suite, vim-auctex, latex-box, etc.)
Next, as far as viewer choice, the only widely used open source PDF viewer for Linux which currently supports SyncTeX well out of the box is Okular. That's probably your best choice.
There are instructions fo setting up SyncTeX with Okular with the vim-latex plugin here, and some related observations here. I had mixed results following those directions. Here's what seems to be important.
Be sure that your LaTeX compilation method (which will depend on your plugin) calls pdflatex (or xelatex or whatever) with the
-synctex=1
flag.I think something like this should suffice for Forward Searches with Okular, though it might be better to try to rewrite or modify the forward search function for your plugin (there's some info on that in one of the links above). Put this in your .vimrc (and change the mapping to whatever you like).
function! SyncTexForward() let execstr = "silent !okular --unique %:p:r.pdf\\#src:".line(".")."%:p &" exec execstr endfunction nmap <Leader>f :call SyncTexForward()<CR>
For reverse searches, set the editor line in Okular to
gvim --servername GVIM --remote +%l %f
. It might also work to usegvim --servername GVIM --remote-send "<Esc>%lgg"
if you only use it with the file already open. Change the servername to whatever you use. (Not sure if it's different with regular vim, but this doesn't make much sense out of a graphical environment.)
I do not have Okular installed right now, however, so I could not test any of that. (And the links I gave earlier have slightly different advice which is worth trying.) I really hope someone with both Okular and gvim installed can test this advice, and correct where I went wrong.
And all of that advice isn't going to work well if you're using subdocuments called through \input{...}
or \include{...}
, where the PDF name doesn't match the name of the document you're editing. There are ways around that, but it would require knowing more about what LaTeX plugins and methods you're already using, if any.
However, other choices are kinda/sorta already available. The next version of evince will support SyncTeX through D-Bus, and apparently someone is already working on a plugin for vim to make use of it. Details here. However, it's very unlikely that this version of evince is already available for your Linux distribution, and there may be some problems with it.
There's an old fork of an old version evince that provides synctex support; there are instructions that come with that detail how to set this up with gvim. It works fine. It's easy to set up if you're using Arch Linux, since this is in the AUR. If you're not, I don't know how hard it would be to compile. (I used to use Ubuntu before Arch, and couldn't get it working there, but that may have been my ignorance.)
Next, I wrote some scripts that provide very limited, very poor, but still better than nothing, synctex support between gvim and the open-source vim-like PDF viewer Zathura, which uses vim-like keybindings. You'll find them mentioned and detailed in this thread in the Arch forums here. (post #370)
Finally, I think this kind of stuff will work its way into the major LaTeX plugins for vim soon, and then you don't have to resort to so much trial and error.
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 https://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 https://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:
- https://tex.stackexchange.com/a/46157 (forward search)
- http://emacswiki.org/emacs/AUCTeX#toc20 (backward search, space handling)
- http://lists.gnu.org/archive/html/emacs-orgmode/2009-11/msg01049.html (fix for missing setf)
I tested with
- Emacs 24.2
- Evince 3.4.0
- OpenSUSE 12.2
Related Question
- [Tex/LaTex] Configure forward search with Texmaker + SumatraPDF
- [Tex/LaTex] Setting up forward search between Emacs+AUCTeX and Evince
- [Tex/LaTex] How to set up Okular for forward/backward search with TeX Live 2011? (not trivial anymore)
- [Tex/LaTex] Customizing preview-latex command to use LaTeX when in TeX-Pdf mode
- [Tex/LaTex] Emacs + SyncTex + Evince
Best Answer
You can use pdf-tools. It adds support for forward and inverse search using synctex. Quoting the commentary in one of the synctex related functions:
After setting up AUCTex, yous should be able to forward search with
C-c C-g
and reverse search by double clicking on the pdf in doc-view (with pdf-tools enabled; i.e. invokepdf-tools-enable
first).