Bidirectional jumping between LaTeX source document and PDF
SyncTeX is a utility written by Jérôme Laurens which enables synchronization between the LaTeX source document and the PDF output. With the combination of Emacs AUCTeX and Okular on Linux, PDF can be opened from Emacs arriving at the target position which is approximately equivalent to the current cursor’s position in the TeX buffer, or a click in the PDF will take us back to the appropriate place in the LaTeX source document. So this small functionality greatly relieves our burden of source code editing and multiplies the fun and comfort for practicing LaTeX art.
For jumping from the PDF in Okular to the LaTeX source document opened in Emacs, we need to select Emacs client as the default editor in the Okular’s Editor configuration window as show in Figure 1.
Figure 1 Select Emacs client as the default editor to be called by Okular.
Then a simple Shift+Left
click will do the trick.
For jumping from Emacs to Okular, some configurations are needed.
In Emacs, we can temporarily enable the TeX-source-correlate-mode
by pressing C-c C-t C-s
, which is responsible for correlating the LaTeX and PDF files. Or, to permanently enable it, we customize this variable or set its value to t
in ~/.emacs
. Then make sure the variable TeX-source-correlate-method
has the value ((dvi . source-specials) (pdf . synctex))
, so that pdflatex
will use the information provided by SyncTex to build the correlation.
Next, we enforce Okular to use a unique session for a single PDF file. In this way, there won’t be duplicated Okular windows opened.
In Emacs, we need to make sure the alist
variable TeX-view-program-selection
contains the value pair (output-pdf "Okular")
, which tells AUCTeX to use Okular for viewing a PDF file. In addition, we should check if the alist
variable TeX-view-program-list-builtin
contains the following values, which specifies the starting command for Okular. Here we can see the option --unique
, which ensures there is only one instance of Okular. Usually, this is the default configuration in AUCTeX and we do not need to modify it.
("Okular"
("okular --unique %o"
(mode-io-correlate "#src:%n%a"))
"okular")
Then we come to Okular. According to here, the first start of Okular should also be passed the option --unique
. Otherwise, if there are multiple calls of the View
command from AUCTeX, there will be a same number of tab windows opened in Okular for a single PDF file, even though there is only one Okular process. Then the Okular icon in the quick launch bar should be assigned the command okular --unique %U
as shown in the following figure.
Figure 2 Command associated with Okular icon.
Now, the configuration is done. After compiling LaTeX source file in Emacs AUCTeX mode, we press C-c C-c
and execute the View
command. The following message will appear in the mini buffer of Emacs, which indicates the correlation mechanism via SyncTex
really takes effect.
okular --unique math-notes.pdf#src:33"/path-to-latex-file/math-notes.tex"