On Emacs, I am trying to get forward search with Okular. Right now, I have the following command:

okular --unique %o#src:%n`pwd`/./%b

But, this does not take me to the right line, as I'd expect. So, there is a problem here. In trying to fix this, I tried to read the manual for okular I could find.

Suspiciously, none of the % commands appear there. The three that do appear are:

(1) %f - file name

(2) %l - line number of the file

(3) %c - column number of the file

I'd be glad if you explain what are the % commands that appear in the command I wrote and also help me find a working forward search command in this situation.

Relevant versions, you might want to know:

Emacs ------- 24.3.1

Okular ------- 0.16.2

AucTeX ------- 11.86

TeXLive ------ 2012

Please feel free to ask anything that may be relevant.

P.S. In my humble opinion, this question is not a duplicate of the previous questions. I have asked a very precise question, not a working method, but a fix to what I have. :-) But, I do agree that this is only border line TeXnical.

  • This is exactly the line I use and the set-up I have and it works reasonably well for me. How far off is it for you?
    – sebschub
    May 29, 2013 at 8:46
  • It just begins where I last left off... :( @sebschub
    – kan
    May 29, 2013 at 8:55
  • I am now of the view that, %o, %n and %b are emacsy stuff...
    – kan
    May 29, 2013 at 9:07
  • This is default okular behaviour and forward search is not working at all. Did you set (setq TeX-source-correlate-mode t) (setq TeX-source-correlate-method 'synctex) (setq TeX-source-correlate-start-server t)?
    – sebschub
    May 29, 2013 at 12:05
  • Yes, all these have been done via the menu bar of emacs. So, yes, I can see them in the .emacs file.
    – kan
    May 29, 2013 at 12:35

3 Answers 3


When --synctex=1 is passed to the TeX engine, SyncTeX creates metadata that Okular can use for forward searches. To get Okular to open the PDF at the right spot, you must generate SyncTeX metadata and invoke Okular like this:

okular document_name#named_destination

As of TeX Live 2011, the syntax for named_destination is:


Note that <directory_containing_main_tex_file> must be an absolute path. This requirement is new as of TeX Live 2011, which is why forward searching in AUCTeX doesn't "just work" anymore.

<relative_path_to_file.tex> is the relative path from the directory containing the main .tex file to the file being visited in Emacs.


If you have the following two TeX files:

  • /tmp/foo/main.tex:

  • /tmp/foo/bar/stuff.tex:

    hello world
    %%% Local Variables:
    %%% mode: latex
    %%% TeX-master: "../main"
    %%% End:

and run:

cd /tmp/foo
pdflatex --synctex=1 main

then the following command:

okular 'main.pdf#src:3/tmp/foo/./bar/stuff.tex' &

should jump to page 2 (where "blah" is printed).

Enable SyncTeX in AUCTeX

To configure AUCTeX to pass --synctex=1 to the TeX engine and generate PDFs, do the following:

  • Enable PDF mode:
    1. type M-x customize-variable <RET> TeX-PDF-mode <RET>
    2. toggle "Tex Pdf Mode" on (non-nil)
    3. click "State" and choose "Save for Future Sessions"
  • Enable SyncTeX:
    1. type M-x customize-variable <RET> TeX-source-correlate-mode <RET>
    2. toggle "Tex Source Correlate Mode" on (non-nil)
    3. click "State" and choose "Save for Future Sessions"

Configure AUCTeX to Launch Okular

To configure AUCTeX to launch Okular with the proper command-line arguments, do the following:

  1. Define new expander to get the directory of the main .tex file:
    1. type M-x customize-variable <RET> TeX-expand-list <RET>
    2. add a new entry to the list:
      • Key: %(masterdir)
      • Expander: (lambda nil (file-truename (TeX-master-directory)))
      • Arguments: none
    3. click "State" and choose "Save for Future Sessions"
  2. Define a new view program for Okular:

    1. type M-x customize-variable <RET> TeX-view-program-list <RET>
    2. add a new entry to the list:

      • Name: Okular
      • Choice: Command
      • Command: okular --unique %o#src:%n%(masterdir)./%b

        Note that %o, %n, %(masterdir), and %b are substituted by AUCTeX before the command is executed, so okular never sees those (it sees the expansion).

        %o is the name of the generated PDF, %n is the line number of the file you are visiting, %(masterdir) is the absolute path of the directory containing the main .tex file, and %b is the relative path from the main .tex file to the file you are visiting.

    3. click "State" and choose "Save for Future Sessions"
  3. Configure Okular as the PDF viewer:
    1. type M-x customize-variable <RET> TeX-view-program-selection <RET>
    2. for output-pdf change the viewer to Okular
    3. click "State" and choose "Save for Future Sessions"

Or you can put the following in your .emacs:

;; use Okular to view AUCTeX-generated PDFs
(when (require 'latex nil t)
  (push '("%(masterdir)" (lambda nil (file-truename (TeX-master-directory))))
  (push '("Okular" "okular --unique %o#src:%n%(masterdir)./%b")
  (push '(output-pdf "Okular") TeX-view-program-selection))
  • Why using "absolute path of TeX-master-directory" + "path of current file relative to TeX-master-directory", instead of "absolute path to current file"?
    – giordano
    Feb 17, 2014 at 21:38
  • @giordano: Because that's how it gets embedded in the synctex data (including that strange ./ between TeX-master-directory and the relative path to the current file). The version of Okular I'm using (0.18.1) seems to be OK with "absolute path to current file", but IIRC some older versions of Evince require exactly the path that is embedded in the synctex data. I'm not sure if older versions of Okular have the same limitation. Maybe it's actually not an Okular/Evince limitation but a SyncTeX limitation. Also see tex.stackexchange.com/q/25578/6185 Feb 17, 2014 at 21:53

In your .emacs put

(eval-after-load "tex"
  '(add-to-list 'TeX-expand-list '("%a" (lambda nil (expand-file-name (buffer-file-name))))))

Now you can use

("okular --unique %o" (mode-io-correlate "#src:%n%a"))

for Okular command.

Update: for the record, this change has been introduced in development version of AUCTeX, then starting from next stable version (11.88?) there will be no more need of dirty hacks to make forward search with Okular work.

  • I think this one only recasts the command in another form. I am not sure how this helps. Indeed, it does not help, I am sorry.
    – kan
    May 29, 2013 at 20:53
  • But it does help me: without that change I'm not able to do forward PDF search :-). If you press C-c C-c View, what's the command you see in minibuffer?
    – giordano
    May 29, 2013 at 21:43
  • Does okular --unique <file>.pdf#src:<line number>/path/to/file.tex work if executed in a terminal? Does it point to the right line?
    – giordano
    May 29, 2013 at 21:54
  • No! I think that is the trouble... (I should have mentioned this in the question, I guess
    – kan
    May 29, 2013 at 22:32
  • So it's not an Emacs/AUCTeX problem. My answer makes forward PDF search work in AUCTeX since it passes to Okular a relative path but Okular wants an absolute one.
    – giordano
    May 29, 2013 at 22:45

Someday my nicely adapted Emacs/Okular config refused to work and I've stolen this code somewhere:

   ;; Quick-Fix BEGIN
(add-hook 'LaTeX-mode-hook
            '(lambda ()
                 (add-to-list 'TeX-expand-list
                                  '("%u" Okular-make-url))))
(defun Okular-make-url ()
   (expand-file-name (funcall file (TeX-output-extension) t)
                          (file-name-directory (TeX-master-file)))
   (expand-file-name (TeX-master-directory))
;; Quick-Fix END

(setq TeX-view-program-list
       ("okular --unique %u")))) ;; <<-- This is part of the quick-fix.
                                 ;; If all is in sync again, one can
                                 ;; hopefully use the regular method
                                 ;; from the next line again.
       ;; ("okular --unique %o" (mode-io-correlate "#src:%n%b")))))

(setq TeX-view-program-selection '((output-pdf "Okular")))

;;(setq TeX-view-program-list '(("Okular" "okular --unique %o#src:%n%(dir)./%b")))


  • I am afraid this is not working either. I think forward search is broken in okular for some reason...
    – kan
    May 30, 2013 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.