emacsauctexlatexmk

How do I bind latexmk to one key in Emacs and have it show errors if there are any


I'm using AUCTeX and I would like to bind a key, e.g. C-0, that does the following:

  1. Saves the active file without prompting me.
  2. Runs latexmk on the active file without prompting me.
  3. Shows me errors if latexmk encounters any by .

My problem is not how to bind a key (for which Tyler posted a link in a comment below) but how to come up with a function that accomplishes item 1–3.


I call Latexmk by

(add-hook 'LaTeX-mode-hook (lambda ()
  (push 
    '("Latexmk" "latexmk %s" TeX-run-TeX nil t
      :help "Run Latexmk on file")
    TeX-command-list)))

This is my .latexmkrc

$pdf_mode = 1;
$recorder = 1;
$latex = 'latex -recorder -halt-on-error -interaction=nonstopmode -shell-escape';
$pdflatex = 'pdflatex -recorder -halt-on-error -interaction=nonstopmode -shell-escape';

I'm using Emacs 23.3 and AUCTeX 11.86.


Solution

  • Something like this?

    (require 'tex-buf)
    
    (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" "latexmk" master-file)
        (if (plist-get TeX-error-report-switches (intern master-file))
            (TeX-next-error t)
          (minibuffer-message "latexmk done"))))
    
    (add-hook 'LaTeX-mode-hook
              (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))
    

    Edit: TeX-save-document saves your master file and any sub-files (if you just have one file, it's your master file), and when TeX-save-query is nil, it doesn't ask you for confirmation. Then TeX-run-TeX runs latexmk using the mechanism usually used for running TeX, which includes error message parsing, but because it usually starts an asynchronous process, we set TeX-process-asynchronous to nil to wait for it to end. The odd-looking plist-get form is the documented way to check for errors from TeX-run-TeX (see comments in tex-buf.el), and if there are errors, we jump to the first one; if there are no errors, we show a message in the minibuffer just for fun.

    Finally, the local-set-key is one way to bind a key to the function.