[Tex/LaTex] pdflatex, latexmk, synctex: Adding an option to pdflatex causes synctex to fail

forward-inverse-searchlatexmkpdftex

The user "Mike" kindly helped me on Latex, Emacs: automatically open TeX Help buffer on error and close it after correction of the error? to create the following function which makes error detection to play nicely together with latexmk.

(defun run-latexmk ()
  (interactive)
  (let ((TeX-save-query nil)
        (TeX-process-asynchronous nil)
        (master-file (TeX-master-file)))
    (TeX-save-document "")
    (TeX-run-TeX "latexmk"
         (TeX-command-expand "latexmk -pdf %s" 'TeX-master-file)
                 master-file)
    (if (plist-get TeX-error-report-switches (intern master-file))
        (TeX-next-error t)
      (progn
    (demolish-tex-help)
    (minibuffer-message "latexmk: done.")))))

I would like to add another option to the latexmk call like this:

(TeX-command-expand "latexmk -pdflatex='pdflatex -file-line-error' -pdf %s" 'TeX-master-file)

to make pdflatex to report not only the line of an error but also the file name (which is crucial for AUCTeX to open the correct buffer in case of an error). The problem is that after I add this option, a compiled myfile.tex does not have an accompanying myfile.synctex.gz anymore, which causes forward and backward search to fail (see How to set up Okular for forward/backward search with TeX Live 2011? (not trivial anymore)).

Where is the problem?

Best Answer

The

"latexmk -pdflatex='pdflatex -file-line-error' -pdf %s"

instruction should contain all options we want to pass to pdflatex at execution time, so

"latexmk -pdflatex='pdflatex -file-line-error -synctex=1' -pdf %s"

should work. The part within single quotes is the command latexmk will issue for compiling the document.