1

I am creating and editing latex source files using AUCTeX. When I use the associated command for compiling the source file (C-c C-a) the file is successfully compiled and the resulting PDF output is displayed in a buffer in a second window that is automatically opened within the current frame.

If I make changes to the LaTeX source file and compile again, these changes do not automatically update the buffer containing the PDF. To display these changes I need to switch to the PDF buffer and execute the "revert buffer" command.

I'm wondering if anyone can suggest some elisp code that I can put into my config file that will automatically compile my latex source file upon any modification which in turn will automatically activate the "revert buffer" command thereby automatically displaying the alterations to the latex source file in the PDF output.

I posted a related question a few days ago on this forum that can be found here which relates to automatically saving a buffer when modified.

I'm sure the elisp code I'm looking for is somewhat similar to the code suggested to me in the previous post but unfortunately I don't have enough experience with programming/elisp to alter this code to suit my needs.

*edit: the code for automatically saving a buffer when modified

(defun savebuf(begin end length)
  (if (and (buffer-file-name) (buffer-modified-p))
       (save-buffer)))
(add-hook 'after-change-functions 'savebuf)
3
  • This is not a solution to your problem, hence a comment rather than an answer. Why view the pdf in emacs at all? Most modern pdf viewers (at least on linux and macos) will automatically update the view when the pdf changes. I see that you're on windows, but I assume that is the case there too. I don't see the advantage offered by viewing the pdf in emacs.
    – MTS
    Commented Nov 9, 2018 at 22:41
  • interesting. Ill investigate if any pdf viewers on windows update as soon as the source file is changed. If so I'll report back with details
    – John Doyle
    Commented Nov 12, 2018 at 15:29
  • Are you aware of latexmk? See here mg.readthedocs.io/latexmk.html this script will run latex each time the source file is changed. How the change is produced (saved manually or from time to time) is then up to you.
    – andrej
    Commented Dec 10, 2018 at 11:54

1 Answer 1

2

The following, which I think I got off the auctex mailing list does the job for me:

  ;; revert pdf-view after compilation
  (add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)
4
  • Thanks but not exactly what I'm looking for. With this code when I change the source file I still have to manually save the file (by pressing C-x C-s) and then manually compile the source file (by pressing C-c C-a). After doing this it does successfully update the PDF output buffer automatically. But ideally I would like to automate the saving and compiling along with the auto reverting of the PDF buffer. Interestingly, the code you give seems to deactivate the code I use for automatically saving the latex source buffer. I have added this code as an edit to my question for you to see
    – John Doyle
    Commented Oct 10, 2018 at 17:11
  • 4
    FWIW, you can lose the C-x C-s: if you save TeX-save-query to nil, C-c C-a will silently save the file before firing LaTeX. More than that, I can't help though having LaTeX run after every change seems quite heavy on the CPU... Commented Oct 10, 2018 at 17:53
  • Thanks for the TeX-save-query tip. That works great. One last thing. When I compile the source file, the active buffer switches to the buffer with the pdf file. Is there a way of stopping this and keeping the focus on the source file?
    – John Doyle
    Commented Oct 11, 2018 at 20:16
  • Actually...one more thing :) With regards auto-compiling. is there a way to set the compile document command to execute after a certain amount of time of inactivity after the file has been modified? So if I modify the source file and stop for lets say 3 or 4 seconds, then the compile function would activate. That would reduce the demand on the CPU.
    – John Doyle
    Commented Oct 11, 2018 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.