Emacs + Auctex + Synctex + Okular on Ubuntu 12.04

It’s amazing how difficult it is to gather information on getting emacs to work with a pdf viewer like okular to perform forward and backward searches.  One problem is that the pdf viewers in Ubuntu 12.04 and newer have broken reverse searches, so people have to put hacks in their .emacs files to support viewers like xpdf and evince.  I don’t really care which pdf viewer I use and I found it was easiest to get Okular to work with emacs and synctex.  I haven’t tried this on other versions of Ubuntu, but it should work.

First, you need to install some dependencies for latex, auctex, and okular…

$ sudo apt-get install okular texlive-full auctex

The texlive-full package is quite large, so grab some coffee.

Then add the following lines to your .emacs file your home directory:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Latex for Emacs
;; 
;; Dependencies: okular, texlive-full, auctex
;;
;; Okular setup: 
;; 1.) Open Okular and go to...
;; 2.) Settings -> Configure Okular -> Editor
;; 3.) Set Editor to "Emacs client"
;; 4.) Command should automatically set to: 
;; emacsclient -a emacs --no-wait +%l %f
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; only start server for okular comms when in latex mode
(add-hook 'LaTeX-mode-hook 'server-start)
(setq TeX-PDF-mode t) ;; use pdflatex instead of latex

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Standard emacs/latex config
;; http://emacswiki.org/emacs/AUCTeX
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)

(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)

; enable auto-fill mode, nice for text
(add-hook 'LaTeX-mode-hook 'auto-fill-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTeX t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Enable synctex correlation
(setq TeX-source-correlate-method 'synctex)
;; Enable synctex generation. Even though the command shows
;; as "latex" pdflatex is actually called
(custom-set-variables '(LaTeX-command "latex -synctex=1") )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Use Okular as the pdf viewer. Build okular 
;; command, so that Okular jumps to the current line 
;; in the viewer.
(setq TeX-view-program-selection
 '((output-pdf "PDF Viewer")))
(setq TeX-view-program-list
 '(("PDF Viewer" "okular --unique %o#src:%n%b")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

You can read through the comments on individual lines if you are interested in what’s going on.

As described in the comments, you have to configure Okular to accept commands from emacs.

  1. Open Okular
  2. Navigate to: Settings -> Configure Okular -> Editor
  3. Set Editor to “Emacs client”
  4. This should automatically set Command to :  emacsclient -a emacs –no-wait +%l %f

That’s it for installation!  Congratulations!

To work with latex in emacs, open your favorite latex file (.tex extension)

You can use the buttons to compile the latex file and view the pdf viewer, but you really should use the emacs short cuts! Here is an example of my work flow after I open the latex file…

C-c C-c  : This command automatically runs the most appropriate latex command.  Thus, if you are starting from scratch, when you issue a C-c C-c, it will run latex (really pdflatex) on your .tex file.  If you have a .bib file with the same name as your latex file in the same directory, when you run C-c C-c again, it will automatically run bibtex! Then, due to how latex works, you issue C-c C-c two more times to resolve reference numbers.  Now that the latex file has been fully processed, if you run C-c C-c one more time, it will automatically issue the view pdf command.  When Okular opens up, it centers the pdf on the line that your cursor in emacs is located.  To test the backward search from Okular, click a line in the Okular pdf with SHIFT + LEFT CLICK and the cursor in emacs will jump to that same line!  All is right in the world.

Here is a cheat sheet for latex/auctex commands: http://devcheatsheet.com/static/pdf/auctex-ref.pdf

Also, the users at tex.stackexchange came up with a list of very useful commands:

http://tex.stackexchange.com/questions/20843/useful-shortcuts-or-key-bindings-or-predefined-commands-for-emacsauctex