2

Right now, when using AUCTeX's preview feature, I have to manually refresh the previews after I change existing sources or write new text. I have to run preview-document every time, which is both inconvenient and inefficient, and it may take a while to re-generate previews for the whole document. I'm looking for a way to

  1. Automatically generate previews when I open a document.
  2. Automatically update previews on save.
  3. Only (re-)compile previews for new and updated pieces of text.

Is there a way to do this with AUCTeX?

1
  • There is a whole set of functions that you can use, such as preview-at-point or preview-section. They are described in the manual.
    – Tobias
    Commented Mar 31, 2023 at 7:54

3 Answers 3

1

Why not use preview-at-point? Whenever I edit a snippet, I do a quick C-c C-c C-p (which I think is the default) to recompile only that one part. preview-region (C-c C-c C-r) is also useful to avoid having to recompile the entire document.

0

I am trying to achieve the same, this is what I could do so far

(use-package tex-mode
  :ensure auctex
  :hook (latex-mode . (lambda ()
                        (add-hook 'after-save-hook (lambda ()
                                                     (preview-document))))))

Now, this doesn't actually work and I don't know why, but it could be a step further.

Please reply if you were able to find a solution.

1
0

The following code in your init file solves your questions

  1. Preview on file-open
  2. Preview on save-buffer
(setq preview-auto-cache-preamble t
      TeX-show-compilation nil)

(defun my-initial-preview ()
  "Generate preview at `find-file'."
  (save-excursion
    (preview-document)))

(define-minor-mode my-preview-at-save-mode
  "Preview at `save-buffer'."
  :key ""
  (if my-preview-at-save-mode
      (add-hook 'after-save-hook #'my-initial-preview nil t)
    (remove-hook 'after-save-hook #'my-initial-preview)))

;; The Preview package puts `preview-mode-setup` into `LaTeX-mode-hook`.
;; It is important that `my-initial-preview` comes after `preview-mode-setup`.
;; Therefore, we use `with-eval-after-load` and
;; the value `t` for the `depth` argument of `add-hook`.
(with-eval-after-load 'preview
  (add-hook 'LaTeX-mode-hook #'my-initial-preview t)
  (add-hook 'LaTeX-mode-hook #'my-preview-at-save-mode))

Your question 3 has already been answered by mbsmu. You just use preview-at-point.

Tested with Gnu Emacs 28.1.

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.