In The Armchair

LaTeX Inverse PDF Search with Emacs

Posted in Computers by Armchair Guy on September 2, 2010

Latex is such a time hog that I’m always in search of ways to improve my efficiency. My editor of choice is emacs. I’ve tried other editors but it usually turns out that there is some “tail” feature that I can’t find in any other editor. By “tail” feature I mean a feature that is used either very rarely or by very few people.

I’ve tried LyX, which is described as a WYSIWYM (What You See Is What You Mean) editor. It’s actually quite brilliant, and I do use it for simple MS Word-style documents. I’d use it in conjunction with emacs, switching back and forth between LyX and LaTeX source code editing, if it were not for the fact that it messes up LaTeX source code big time (all source code formatting is lost, making it pretty unreadable).

So I stick to emacs. One of the problems with a non-WYSIWYG editor for a language like LaTeX is it is hard to spot what you’re looking at. If you’re looking at a PDF file, it’s really hard to find the tex code that corresponds to what you’re seeing in the PDF. This is where Inverse PDF Search comes in.

What inverse PDF search does is allow you to click on a location in the PDF file and be transported to the corresponding spot in the LaTeX source. I find this tremendously useful.

Under Ubuntu 10.04 “Lucid Lynx”, this works almost out of the box, with a little bit of work. Here are the steps.

  1. First, install Okular, the KDE PDF viewer. This may require a huge number of KDE libraries. Evince is nice but doesn’t support inverse PDF search yet.
  2. In okular, go to Settings > Configure Okular > Editor and change the editor to “emacs client”, and the command to “emacsclient -a emacs –no-wait +%l %f“.
  3. Make sure you have Emacs 23. This is the default in Ubuntu 10.04.
  4. We have to start emacs in server mode so that okular can talk to it. Add this line somewhere in your .emacs file: (server-start) The next time emacs is started, it will be in server mode.
  5. When emacs compiles using latex, it has to include “source specials”. Roughly, this is an index connecting every position in the PDF file to a line number in the source file. To create the index, a latex package called synctex must be installed. Under Ubuntu 10.04 this is included in the package texlive-extra-utils, so make sure that’s installed.
  6. To tell emacs to always use PDF (i.e. compile with pdflatex instead of latex) insert the line (add-hook ‘LaTeX-mode-hook ‘TeX-PDF-mode) into your .emacs.  This step is optional if you don’t want to automatically pick PDF each time, but everything in this post pertains to PDF.
  7. Tell emacs to compile using source specials by adding the following line to your .emacs: ‘(LaTeX-command “latex -synctex=1”) Alternatively, when editing a tex file, go to the menu LaTeX > Customize AUCTeX > Extend this menu; then LaTeX > Customize AUCTeX > TeX Command > LaTeX Command, then change “latex” to “latex -synctex=1” and click “Save for future sessions”.
  8. To tell emacs/AUCTeX to use okular rather than evince, add the following to .emacs: ‘(TeX-output-view-style ‘((“^pdf$” “.” “okular %s.pdf”))).  Alternatively, this can be done through the customization menu LaTeX > Customize AUCTeX > Extend this menu; then LaTeX > Customize AUCTeX > TeX Command > TeX Output View Style and change evince %o %(outpage) to okular %o %(outpage).  Click “Save for future sessions”.

When you restart emacs and use AUCTeX to compile under PDF (using C-c C-c), it should compile with source specials.  (You can manually compile with source specials using the command pdflatex -synctex=1 filename.tex at the command line, but it’s easier to use AUCTeX from emacs.) After compilation is done, the AUCTeX view command (also using C-c C-c) should start up okular. To use inverse search, Shift+Left Click on a position in the PDF file in okular. The cursor should jump to the corresponding line in emacs.

Note. It’s possible to omit step 7 and instead insert \usepackage{pdfsync} in the preamble of the TeX file.  This uses pdfsync, which is older than synctex.  But this has two drawbacks.  First, your LaTeX source file is now less portable (if you send it to someone without the pdfsync style file).  Second, the accuracy of the source specials generated using this method is atrocious: Shift+Left Click only places you in the general vicinity of the point you’re looking for, may be 15 or 20 lines above or below, and worst of all, is not even monotone (you click a later spot on the PDF, you might jump to an earlier spot in the source).  Using step 7 is much better.


6 Responses

Subscribe to comments with RSS.

  1. Cedric Ginestet said, on October 14, 2010 at 4:37 pm

    Dear Armchair Guy,

    Thanks a lot for your wealth of information. I am also using Emacs and Ubuntu and Latex. I have switched to Okular and I am extremely happy with this so far. However, I needed to following to get pass xdg-open by adding the following to my .emacs file:

    ;;; Okular as default.
    (defun pdf-with-okular ()
    (add-to-list ‘TeX-output-view-style
    (quote (“^pdf$” “.” “okular %o %(outpage)”))))
    (add-hook ‘LaTeX-mode-hook ‘pdf-with-okular t)

    However, I still couldn’t get the inverse PDF to work, although I put the following lines in my .emacs file:
    (add-hook ‘LaTeX-mode-hook ‘TeX-PDF-mode)
    ‘(LaTeX-command “latex -synctex=1”)

    Normally, inverse PDF should simply work by doing left click + shift?
    Any ideas would be most welcomed.


  2. Armchair Guy said, on October 16, 2010 at 2:45 am


    Glad to hear you found the information useful. Yes, I just shift+left click to use inverse search. I haven’t tried to get forward search to work.

    Have you configured okular to use emacs and also started emacs in server mode (steps 2-4)?

    I just checked my configuration and found that I had specified okular through the customization menu in emacs rather than using .emacs. In ~/.gnu-emacs-custom I have

    ‘(TeX-output-view-style (quote ([…other stuff…](“^pdf$” “.” “okular %o %(outpage)”) […other stuff…])))

    So doing it through the customize menu is one other thing to try.

    Hope you get it to work.

  3. B. Slade said, on November 10, 2010 at 3:51 am

    I found a melange of what Armchair Guy has posted with what’s here ( ) worked for me, for both forward and reverse search. (Forward search with C-c C-v)

    For some reason, the bleedingmind instructions don’t work for me as is. I still needed to add ‘(LaTeX-command “latex –synctex=1”)

    [make sure the — before synctex is a double hyphen, it won’t work otherwise]

    But if you specify synctex this way, you must make sure the path to your LaTeX tex file contains no spaces (in either folder or file names) or it won’t work.

    Once I got it working, it works beautifully, and I realise I really like Okular (it’s better than either evince or foxit). My workflow has improved immensely since switching from TexWorks to AUCTeX/emacs.

  4. Armchair Guy said, on November 10, 2010 at 8:12 pm

    @B. Slade:

    Thanks for the info! I’ll try to get forward search working too.

    There do seem to be some slight differences, though. ‘(LaTeX-command “latex -synctex=1”) works for me (with a single hyphen), though I have it in ~/.gnu-emacs-custom and not ~/.emacs. Also, in my setup there don’t seem to be any issues with inverse search even if there are spaces in the path to the tex file.

    Okular and evince seem about the same to me. IIRC the reason I preferred evince was incremental search within PDFs, but now Okular has it too. Any specific inputs on why you prefer Okular?

  5. […] on November 15, 2010 I wrote a blog post on implementing inverse PDF search with okular and emacs here.  This post is about the reverse: forward search.  That is, with point at any position in the […]

  6. Shnuggles said, on October 21, 2011 at 8:55 am

    The *only* thing that works for me is the following.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: