How to configure Emacs and AUCTeX to work with a PDF viewer

I’m writing my thesis in LaTeX, using Emacs and the AUCTeX environment. Today I managed to configure AUCTeX to generate PDFs (that’s the easy part), but also to correlate the LaTeX source and the PDF file (that’s way trickier). As it was painful to make it work, here is an easy how-to which only uses Emacs’ graphical interface.

Please note that this procedure works as is for AUCTeX 11.86 and TeX Live 2009; it needs to be amended in case you have :

Here is the procedure :

  1. Open a tex file with Emacs.
  2. In the LaTeX menu, go to “Customize AUCTeX” and click on “Extend this menu”.
  3. Then go again to LaTeX, “Customize AUCTeX”, go to the “Tex Command” submenu and click on the included “Tex command” item.
  4. You are now in the “Tex command group” configuration buffer, scroll down until you reach “Tex Pdf Mode”. Its value is off (nil), click on the “Toggle” button to activate this mode. Click on the “State” button and select “Save for Future Sessions”. AUCTeX will now compile your tex file using pdflatex and produce a pdf output. That was the easy part.
  5. Still in the “Tex command group” configuration buffer, scroll all the way down and click on the special item called “Tex View”. In the “Tex view group” buffer, you have two things to do : activate the correlate mode between the PDF and the tex file, and declare a PDF viewer which can read these correlations. At the moment, only Okular does the trick in Debian Sid.
  6. To activate the correlate mode :
    1. item “Tex Source Correlate Method”, click on the “Value Menu” button and select “synctex” and save for future sessions,
    2. item “Tex Source Correlate Start Server”, select “Always” and save,
    3. item “Tex Source Correlate Mode”, toggle it on and save.
  7. To set Okular as the default PDF viewer :
    1. item “Tex View Program List”, right arrow, then button “INS”
      1. name: Okular
      2. choice: Command
      3. command: okular –unique %o#src:%n%b
    2. item “Tex View Program Selection”, right arrow, then “INS”
      1. choice: Single predicate
      2. single predicate: output-pdf
      3. viewer: Okular (if you just added Okular to the Tex View Program List, this choice might not appear, but if you restart Emacs it will show up)
  8. Save both for future sessions. Close Emacs, re-open your tex file and voilĂ .
  9. To configure Okular: in the Okular window, go to the “Settings” menu, item “Configure Okular”, “Editor”, select Emacs client.

To use Forward search from Emacs to Okular, just hit Ctrl+C Ctrl+V to call the viewer. Okular will center its display on the intended line of the pdf.

To use Inverse search from Okular to Emacs, just hit Shift+Left mouse click on the pdf. Emacs will come up and position the cursor at the intended line of the TeX source.

I started using information from bleedingmind, lost hours and was finally saved by Luis Finotti’s post.

AUCTeX can also display the compilation errors in Emacs and point exactly to the wrong part of your source code. To enable this or fix its behaviour (e.g. when it opens a blank buffer strangely named “Debian” in place of your TeX source), you might need to set up TeX to produce file:line:error style messages. To do that,

  • as a root or sudo user,
  • open /etc/texmf/texmf.d/95NonPath.cnf (on Debian),
  • change “file_line_error_style = f” to “file_line_error_style = t” and save,
  • run “update-texmf” which regenerates /etc/texmf/texmf.cnf from the files in /etc/texmf/texmf.d/ .

Figuring the cause and finding the relevant information online took me two terrible hours of sweat and anger at a critical time. Now I’m happily back to writing.