Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex, Emacs: automatically open *TeX Help* buffer on error and close it after correction of the error?

Tags:

emacs

latex

elisp

I use the function TeX-parse-error defined by Ivan Andrus at the bottom of Emacs latexmk function throws me into an empty buffer in order to automatically open the *TeX Help* buffer when there was an error during the compilation (C-c C-c). After correcting an error and compiling again, the *TeX Help* buffer remains open (although the error has been corrected). Is there any way to adjust the function (unfortunately, I'm not experienced in elisp programming) so that the *TeX Help* buffer is closed if the error was resolved and updated (and still open) if the error wasn't resolved? That would save a lot of typing like C-c ' to show the *TeX Help* buffer and C-x 1 to hide it again.

like image 207
Marius Hofert Avatar asked Feb 12 '12 14:02

Marius Hofert


1 Answers

First, let's define a function that finds the *TeX Help* buffer, if it exists, closes its window, and then kills the buffer:

(defun demolish-tex-help ()
  (interactive)
  (if (get-buffer "*TeX Help*") ;; Tests if the buffer exists
      (progn ;; Do the following commands in sequence
        (if (get-buffer-window (get-buffer "*TeX Help*")) ;; Tests if the window exists
            (delete-window (get-buffer-window (get-buffer "*TeX Help*")))
          ) ;; That should close the window
        (kill-buffer "*TeX Help*") ;; This should kill the buffer
        )
    )
  )

Now, you have to call this when you call whatever function it is that you use to compile. Taking the example from that other page, you can modify Ivan Andrus's function to be:

(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 %t" '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")))))

(add-hook 'LaTeX-mode-hook
          (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))

(Note: This doesn't actually work for me, because my latexmk is screwed up, so I haven't successfully tested it. But if Ivan's version worked for you, then this should too.)

So now, any time you call latexmk with this function (by hitting C-0, for example), once the compilation is done, it checks for errors. If there were errors, it automatically opens the Help window and gets the first error. If there were none, it checks to see if the Help buffer is open; if so, it closes that window and kills the buffer.

like image 86
Mike Avatar answered Oct 27 '22 00:10

Mike