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:
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:
- type
M-x customize-variable <RET> TeX-PDF-mode <RET>
- toggle "Tex Pdf Mode" on (non-nil)
- click "State" and choose "Save for Future Sessions"
- Enable SyncTeX:
- type
M-x customize-variable <RET> TeX-source-correlate-mode <RET>
- toggle "Tex Source Correlate Mode" on (non-nil)
- 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:
- Define new expander to get the directory of the main
.tex
file:
- type
M-x customize-variable <RET> TeX-expand-list <RET>
- add a new entry to the list:
- Key:
%(masterdir)
- Expander:
(lambda nil (file-truename (TeX-master-directory)))
- Arguments: none
- click "State" and choose "Save for Future Sessions"
Define a new view program for Okular:
- type
M-x customize-variable <RET> TeX-view-program-list <RET>
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.
- click "State" and choose "Save for Future Sessions"
- Configure Okular as the PDF viewer:
- type
M-x customize-variable <RET> TeX-view-program-selection <RET>
- for
output-pdf
change the viewer to Okular
- 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))
Best Answer
I combined some of the links mentioned here, you will find the links in the source comments. This code supports:
C-c C-v
)C-Mouse-1
, that is Ctrl + "Left Click" in Evince)This will likely break for:
I used these helpful links:
I tested with