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

自闭症网瘾萝莉.ら 提交于 2019-11-30 03:28:06

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.

Tyler

Does this do what you want?

(defun my-tex ()
"Saves the current buffer and runs LaTeX, all with no prompts or further interaction."
  (interactive)
  (save-buffer)
  (TeX-command "LaTeX" 'TeX-master-file -1))

I don't use latexmk, but to make that work all you need to do is switch the string "LaTeX" for the name string you use for latexmk in TeX-command-list (which is probably just "latexmk" or "Latexmk").

I'm glad you asked, as this will be useful for me now!

Ben Lerner

Assuming you've already used the answer in How to call latexmk in emacs, and jump to next-error to add latexmk to the command list for AUCTeX, you can use the following function:

(defun run-latexmk ()
  (interactive)
  (save-buffer)
  (TeX-command "Latexmk" 'TeX-master-file 0)
  (if (plist-get TeX-error-report-switches (intern (TeX-master-file)))
      (next-error))) ;; 0 -> suppress confirmation

And use any of the key-binding techniques to bind it to C-0; here's one that's local to the TeX mode:

(define-key TeX-mode-map (kbd "C-0") 'run-latexmk)

The run-latexmk function is based on digging through TeX-command-master, and simplifying it to your needs. Note that the call to (next-error) may not always happen, because LaTeX may get confused by your error and pause waiting for input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!