[Tex/LaTex] How to force emacs with AUCTeX to show compilation in new buffer

auctexxetex

So I am using AUCTeX with xetex in emacs and each time after I compile the file I get a split window showing the compilation process. I don't want to completely hide this since I find it useful for debugging, but I want it to appear in a different buffer and not bother me when there are no errors. Is this possible? Another option, although less great, would be to automatically hide it except when there are compilation errors.

I have the option:

(setq TeX-show-compilation t)

Best Answer

I think the best solution it to hook onto TeX-LaTeX-sentinel. In this way, you can check for the presence of errors after the compiler has finished its job. Add the following code to your initialization file:

(defadvice TeX-LaTeX-sentinel
  (around mg-TeX-LaTeX-sentinel-open-output activate)
  "Open output when there are errors."
  ;; Run `TeX-LaTeX-sentinel' as usual.
  ad-do-it
  ;; Check for the presence of errors.
  (when
      (with-current-buffer TeX-command-buffer
    (plist-get TeX-error-report-switches (intern (TeX-master-file))))
    ;; If there are errors, open the output buffer.
    (TeX-recenter-output-buffer nil)))