Build & view
Again, this solution, instead of modifying the behaviour of the TeX-command-master
, rewrites it. The rewritten version of the command, named build-view
, follows a rather straightforward logic.
- If the LaTeX file buffer is not-modified, it runs the default viewer;
- If the buffer is dirty, it runs the default LaTeX compiler and, after the build, opens the output in the default viewer.
Here's the code:
(defun build-view ()
(interactive)
(if (buffer-modified-p)
(progn
(let ((TeX-save-query nil))
(TeX-save-document (TeX-master-file)))
(setq build-proc (TeX-command "LaTeX" 'TeX-master-file -1))
(set-process-sentinel build-proc 'build-sentinel))
(TeX-view)))
(defun build-sentinel (process event)
(if (string= event "finished\n")
(TeX-view)
(message "Errors! Check with C-`")))
You can now type M-x build-view
and start the told build-view process or associate it with a new keybinding such as “F2”:
(add-hook 'LaTeX-mode-hook '(lambda () (local-set-key (kbd "<f2>") 'build-view)))
Note: As suggested by Tyler, TeX-save-query
variable is changed locally, therefore the old C-c C-c
/ TeX-command-master
is unaffected and will keep asking confirmations.
Do edit this code to make it better or easier to read!