[Tex/LaTex] Forward Search in Emacs for Okular not working

auctexemacsforward-inverse-searchokular

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.

Best Answer

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:

src:<line_number><directory_containing_main_tex_file>/./<relative_path_to_file.tex>

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.

Example

If you have the following two TeX files:

  • /tmp/foo/main.tex:

    \documentclass{}
    \begin{document}
    \input{bar/stuff}
    \end{document}
    
  • /tmp/foo/bar/stuff.tex:

    hello world
    \clearpage{}
    blah
    %%% 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))))
        TeX-expand-list)
  (push '("Okular" "okular --unique %o#src:%n%(masterdir)./%b")
        TeX-view-program-list)
  (push '(output-pdf "Okular") TeX-view-program-selection))
Related Question