32

I have emacs23 and AucTeX installed on my Ubuntu system. I would like to change the default pdf viewer in emacs and enable forward and inverse search. How do I do it?

I would like to share my knowledge and experience with emacs by posting an answer.I have done the following for only keyboard shortcuts. I would be happy if it is extended to such a way that, if I double click my source in emacs, my pdf gets generated (forward search) and when I double click on some text in the pdf, it gets to the approximate location in the source(backward search). It's really awesome, requires less space and the best editor I have ever worked with.

subham soni
  • 9,673

2 Answers2

39

This procedure will set up Emacs, AUCTeX, and the Okular viewer to handle integrated forward and inverse search. (These instructions were tested on a Debian system)

  1. Install Emacs. To install Emacs, open up the terminal and type the command:

    sudo apt-get install emacs
    
  2. Install AUCTeX.

    Within Emacs, run M-x package install RET auctex RET.

    To test for a successful installation, just open up a .tex file Emacs with and you will see new menus concerning LaTeX.

    enter image description here

  3. Install Okular.

    sudo apt-get install okular
    
  4. If not already done, open a tex file with emacs.

  5. In the LaTeX menu, go to "Customize AUCTeX" and click on "Extend this menu".
  6. Then go again to LaTeX, "Customize AUCTeX", go to the "Tex Command" submenu and click on the included "Tex command" item.
  7. From within Emacs, run

    M-x customize-group RET TeX-command RET
    

    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.

    You should see something like this: enter image description here

    For the "Save for Future Sessions", the button with the green down arrow can also be selected. When this is done, your .emacs will be updated accordingly.

    enter image description here

  8. 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.
  9. 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.
  10. To set Okular as the default PDF viewer:

    1. (NOTE: this is necessary only if you are using AUCTeX 11.87 or lower, in which case you really should upgrade the package to a more recent version) item Tex View Program List then click INS
      • name: Okular
      • choice: Command
      • command: okular --unique %o#src:%n%b
    2. item Tex View Program Selection then click INS

      • choice: Single predicate
      • single predicate: output-pdf
      • viewer: Okular

        If you just added Okular to the Tex View Program List, this choice might not appear. Restart Emacs or Set for Current Session to have it show up.

  11. Set and save both for future sessions.

  12. To configure Okular: in the Okular window, go to the "Settings" menu, item "Configure Okular", "Editor", select Emacs client.

  13. To use Forward search from Emacs to Okular, just hit C-c C-v or click on the glasses button of the tool-bar to call the viewer. Okular will center its display on the intended line of the pdf.

  14. To use Inverse search from Okular to Emacs, make sure that the Browse Tool (in the Tools menu of Okular) is enabled; then 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.


As a sidenote, AUCTeX can also jump-to errors generated in the log file with a C-c `. If this keystroke throws an error, see Uninformative error message when using AUCTeX for an Emacs-based (i.e. cross-platform, works with the major distributions) solution.

subham soni
  • 9,673
  • 1
    Actually it isn't necessary to edit a system file to get the file:line:error style messages, see http://tex.stackexchange.com/a/124259/31416 – giordano Feb 22 '14 at 13:58
  • @giordano Good resource. Unfortunately that part of the answer wasn't exactly related to the question at all, so I struck it out; do you think it should be put back in? – Sean Allred Feb 22 '14 at 14:14
  • @SeanAllred I haven't a strong opinion about it. It's true that it wasn't explicitly requested in the answer but it could be a useful extra. – giordano Feb 22 '14 at 14:34
  • @SeanAllred I agree with giordano, the error styling would make emacs a completed editor for LaTeX and I would recommend to add the answer provided by giordano because in my answer,because editing the system file might be troublesome sometimes – subham soni Feb 22 '14 at 14:48
  • 1
    @subhamsoni I will note that the solution linked to by giordano was successful for me (I was actually having the same problem but was far too lazy to research it). A link to this answer is certainly appropriate. – Sean Allred Feb 22 '14 at 14:56
  • @giordano Can you please help me in the second half of the question, searching by means of mouse click rather than keyboard shortcuts – subham soni Feb 22 '14 at 15:34
  • @subhamsoni Do you want to repeat all the steps using mouse or just the forward search (i.e. step 13)? – giordano Feb 22 '14 at 15:42
  • @giordano steps 13 and 14 – subham soni Feb 22 '14 at 15:57
  • @subhamsoni Ok, done. Why do explain how to use forward/inverse search with Okular? From the screenshots I guess you use Gnome, forward/inverse search with Evince works flawless as well. – giordano Feb 22 '14 at 16:52
  • @SeanAllred package.el is built-in only with Emacs 24. The last time I tried it, package.el didn't even work with Emacs 23, although it used to work before some incompatible change. – giordano Feb 22 '14 at 16:57
  • @giordano I find okular the document viewer in all the Linux distros. Secondly I find this something similar to Sumatra pdf in windows. So went for okular – subham soni Feb 23 '14 at 04:31
  • Just a small addition to the excellent walk-through by @subham-soni, since I am not allowed to post a comment yet: The Okular command should be okular --unique %o#src:%n%b with two dashes – Fred Schoen Oct 09 '14 at 14:04
  • With recent auctex versions (at least the one coming with emacs 25) there is no more need to edit Tex View Program List since okular is configured by default. – pavel Dec 08 '16 at 21:18
10

Following subham soni's instructions will add the following lines to your .emacs

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(TeX-source-correlate-method (quote synctex))
 '(TeX-source-correlate-mode t)
 '(TeX-source-correlate-start-server t)
 '(TeX-view-program-list (quote (("Okular" "okular --unique %o#src:%n%b"))))
 '(TeX-view-program-selection (quote ((engine-omega "dvips and gv") (output-dvi "xdvi") (output-pdf "Okular") (output-html "xdg-open")))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
Sav-econ
  • 335