# How do I use AUCTeX to perform forward search/synchronization in an included document?

I’m trying to sync my .tex file with the .pdf output with a forward search on OS X using Emacs + AUCTeX and Skim.app. I have added the following code to my Emacs configuration:

``````(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(setq reftex-plug-into-AUCTeX t)
(setq TeX-PDF-mode t)

;; Use Skim as viewer, enable source <-> PDF sync
;; make latexmk available via C-c C-c
;; Note: SyncTeX is setup via ~/.latexmkrc (see below)
(push
'("latexmk" "latexmk -pdf %s" TeX-run-TeX nil t
:help "Run latexmk on file")
TeX-command-list)))
(push
'("pdflatex" "pdflatex -synctex=1 %s" TeX-run-TeX nil t
:help "Run pdflatex with SyncTex on file")
TeX-command-list)))
(add-hook 'TeX-mode-hook '(lambda () (setq TeX-command-default "pdflatex")))

;; use Skim as default pdf viewer
;; Skim's displayline is used for forward search (from .tex to .pdf)
;; option -b highlights the current line; option -g opens Skim in the background
(setq TeX-view-program-selection '((output-pdf "PDF Viewer")))
(setq TeX-view-program-list
'(("PDF Viewer" "/Applications/Skim.app/Contents/SharedSupport/displayline -b -g %n %o %b")))

(lambda () (local-set-key (kbd "<M-S-mouse-1>") #'TeX-view)))

(setenv "PATH" (concat "/usr/texbin" ":" "/Applications/Emacs.app/Contents/MacOS" ":" "/usr/local/bin" ":" (getenv "PATH")))
(setq exec-path (append
'("/usr/texbin"
"/Applications/Emacs.app/Contents/MacOS/libexec"
"/Applications/Emacs.app/Contents/MacOS/bin"
"/usr/local/bin") exec-path))
``````

Everything works fine in the main document, however, when I try to use forward search in an included file, say `chapter1.tex`, I get an error:

``````Output file "chapter1.pdf" does not exist.
``````

Being used to TeXShop I didn’t know that I had to set the master variable manually. In the master file `maindocument.tex` file I added this (note the declaration of the `pdflatex` command in my question above):

``````%%% Local Variables:
%%% mode: latex
%%% TeX-engine: pdflatex
%%% End:
``````

and at the end of each chapter file:

``````%%% Local Variables:
%%% mode: latex
%%% TeX-master: "maindocument.tex"
%%% End:
``````

It works fine now, thanks @giordano!

It seems one can alternatively specify these variables per directory in a separate file called `.dir-locals.el`: https://tex.stackexchange.com/a/167688/9610

• Indeed you don't need to do it completely manually: since you set `TeX-master` to nil, whenever you open a new *TeX buffer you'll be prompted for the master file. If you open an old file, issue `C-c _` to set the master file. Ah, there is no such `pdflatex` engine, did you mean `default` (see gnu.org/software/auctex/manual/auctex.html#index-TeX_002dengine). – giordano Dec 3 '14 at 9:24
• I don’t quite remember where I have these variable declarations from, but I have declared a command for using `pdflatex` (see the code in my question). I’m not entirely sure whether that is necessary, but it does work. – Lenar Hoyt Dec 3 '14 at 22:36