7

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.

Community
  • 1
  • 1
Marius Hofert
  • 6,546
  • 10
  • 48
  • 102

2 Answers2

7

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.

Mike
  • 19,114
  • 12
  • 59
  • 91
  • okay, great, thanks a lot, Mike! In principle, it works. There is still this issue with the TeX Live 2011 buffer: Once there is an error in the .tex, instead of just seeing the TeX Help buffer in the lower half of the window and the .tex code in the upper half, the upper half contains this empty TeX Live 2011 buffer which I then have to kill. For every found error, I have to do that. I hope I can figure out how to tell TeX Live to work correctly as described on http://neilernst.net/2011/04/21/forcing-auctex-to-properly-show-error-messages/#comment-469 (I tried it but it's not working) – Marius Hofert Feb 17 '12 at 19:34
  • okay, it works. I had to use `file_line_error_style=t` instead of `file-line-error=t` and then it worked – Marius Hofert Feb 17 '12 at 19:58
  • 1
    Even better, use `(TeX-command-expand "latexmk -pdflatex='pdflatex -file-line-error' -pdf %s" 'TeX-master-file)`, so the option `file-line-error` is passed to `pdflatex` and errors are indicated at the correct line in the original `.tex` file. No need to adjust `/usr/local/texlive/2010/texmf.cnf' as described on http://neilernst.net/2011/04/21/forcing-auctex-to-properly-show-error-messages/#comment-469 – Marius Hofert Jun 11 '12 at 11:06
1

Note: After some clarification, I see that the following is not quite what was asked for. My other answer to this question should do the job.

It looks like automatically opening the buffer requires changing the function that's called when you run latex. (At least, I can't find any hook that will let you do it.) That's actually one of the things that's done in the earlier answer to that question you mention. In particular, the lines

    (if (plist-get TeX-error-report-switches (intern master-file))
    (TeX-next-error t)
  (minibuffer-message "latexmk done"))))

check to see if there was an error. If so, the function runs TeX-next-error; if not it just prints the message. So you might want to try installing that function.

Automatically closing the Help buffer just requires a little adjustment to Ivan Andrus's function. In the area around "No more errors", just replace the relevant part with this:

     ((null
       (re-search-forward regexp nil t))
      ;; No more errors.
      (message "No more errors.")
      (beep)
  (delete-window (get-buffer-window (get-buffer "*TeX Help*")))
  (kill-buffer "*TeX Help*")
      (TeX-pop-to-buffer old)
      nil)

This actually kills the Help buffer after closing its window, but you can comment that out if you dislike it for some reason.

Another nice option is to put (setq TeX-display-help nil) in your ~/.emacs. With this, the Help buffer never even displays. Instead, a terse message describing the error is put into the minibuffer, and the cursor is put on the bad line in the original. (I never find most of that help output useful anyway.)

Finally, you might want to try another keybinding for TeX-next-error because C-c ' is awkward.

Mike
  • 19,114
  • 12
  • 59
  • 91
  • Just in case this matters, I'm using GNU Emacs 24.0.92.1 (x86_64-apple-darwin, NS apple-appkit-1038.36) of 2012-01-15 on bob.porkrind.org with AUCTeX 11.86 – Marius Hofert Feb 17 '12 at 14:12
  • I tried the code from above, but it does not seem to have any influence, I still see the *TeX Help* buffer even after successfully compiling the document. I'm wondering if the function TeX-parse-error is used at all and how I can make it available when using latexmk (or even just `latex') – Marius Hofert Feb 17 '12 at 14:52
  • Oh, sorry. I thought you wanted to step through all the errors, then close the Help window once there were no more errors. (Try stepping through them all, and you should see that window close.) I suppose what you really want to do is again change the function that gets called when you run latex, but add something to the beginning to close the window. I'll work on it, and add a second answer. – Mike Feb 17 '12 at 15:39
  • Thanks a lot for helping, Mike, very kind. My main workflow can be described as follows: I typically run latex (through latexmk) and if there is an error the *TeX Help* buffer pops up (unfortunately also an empty TeX Live buffer which is described in the link in my post above). Then I see the error. After correcting it, I simply run latex(mk) again. If there is another error, the *TeX Help* buffer is updated and I can fix the code again. Finally, running the code without errors just leaves the *TeX Help* buffer open but with `C-x 1` I can (manually) close it and be back in the .tex file. – Marius Hofert Feb 17 '12 at 18:29
  • What do you mean by "stepping through" the errors, which command can do that? – Marius Hofert Feb 17 '12 at 18:30
  • By "stepping through", I just mean to hit `C-c '` enough times that there are no more errors left. If you do what I suggested in the other answer, I think that'll give you the workflow you describe. In particular, it will automatically open the Help window if there was an error, and will automatically close it (if it exists) if there was no error. – Mike Feb 17 '12 at 18:54
  • But if latex finds an error, the buffer containing the .tex file is replaced by two vertically split buffers, the upper one being the TeX Live 2011 buffer, the lower one the *TeX Help* buffer. The point is put in the top (empty) TeX Live 2011 buffer. Executing `C-c '` then simply gives "C-c ' is undefined", so there is no "stepping through". I also found here http://neilernst.net/2011/04/21/forcing-auctex-to-properly-show-error-messages/#comment-469 that someone else had this problem, too, but the suggested fix did not work for me. use `C-c '` [well, its actually the backtick], then – Marius Hofert Feb 17 '12 at 19:09
  • Ah. Well, that's a particular type of error. I get that when I, for example, don't close a pair of braces -- like `\newcommand{\foo}{bar`. This happens because latex itself doesn't know where the error happened, so auctex can't (currently) figure it out either. The solution to that problem will have to come from auctex, but that's a bigger issue. For other types of errors -- like if you just forgot to define `\foo` -- then my answer above should work for you. – Mike Feb 17 '12 at 19:42
  • okay, it now works as expected (I always test with "a_1" so latex complains about missing "$"). I followed the advice on http://neilernst.net/2011/04/21/forcing-auctex-to-properly-show-error-messages/#comment-470 but had to use `file_line_error_style=t` instead of `file-line-error=t`. Many thanks, Mike. Cheers! – Marius Hofert Feb 17 '12 at 19:58
  • Cool. You should be sure to leave a comment about `file_line_error_style` on that other page too, because I tried it and had it fail, so I'm sure that'd be useful to other people. – Mike Feb 17 '12 at 20:38