3

The preview-latex package behaves in the following manner: it shows you an image of a formula / section header / etc. When the cursor gets "into" the image it is automatically replaced by the latex code. Once you're done, the user is supposed to hit C-c C-p C-p to update the preview.

What I would like: instead of a manual process (hitting C-c C-p C-p), I would prefer that whenever I finish editing a formula, the images should be updated automatically.

Is that possible?

Warning: I'm a novice emacs user.

FWIW, I'm using Aquamacs 3.2 GNU Emacs 24.4.51.2 , but I'm pretty sure this applies to standard emacs as well.

6
  • I haven't tried this but it seems very relevant: kitchingroup.cheme.cmu.edu/blog/2015/10/09/… Commented Oct 14, 2015 at 17:46
  • Seems precisely what I'm looking for, but for the preview-latex package instead of org-mode.
    – Or Sattath
    Commented Oct 14, 2015 at 17:54
  • Specify «whenever I finish editing a formula». How can Emacs know when you finish editing a formula? Would be an idle timer acceptable?
    – giordano
    Commented Oct 14, 2015 at 19:24
  • @giordano: whenever you move the cursor away from a formula you haven't edited, the text is replaced back to the image. I don't know exactly what mechanism takes care of that, but apparently there exists a mechanism which notices that you left the formula. Optimally, whenever you edit and leave a formula, emacs should recompile and present the correct image. In other words, I would like the same behavior as the one you can see in the youtube video in the link provided by kaushalmodi (which has no timers involved).
    – Or Sattath
    Commented Oct 14, 2015 at 19:31
  • I'm assuming you use AUCTeX, right?
    – giordano
    Commented Oct 14, 2015 at 19:36

2 Answers 2

2

This package, org-fragtog, seems to be a perfect solution. Although it's designed for org-mode, it works in LaTeX-mode as well. Simply install that package and enable org-fragtog-mode in your buffer. Then, after doing a preview-at-point, as your cursor goes in and out an equation, it automatically disables and enables rendering.

Animation from org-fragtog github page: enter image description here

1

Here is my start of solution, it can be improved, see below. Add the following code to your init file and restart Emacs:

(defun mg-LaTeX-preview-formulae ()
  (and (eq major-mode 'latex-mode)
       (not (texmathp))
       (preview-section)))

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (setq mg-LaTeX-preview-formulae-timer
          (run-with-idle-timer 1 nil 'mg-LaTeX-preview-formulae))))

With this code, after 1 second of idle time (I mean: every time you do nothing for 1 second, but you can change the time in the last line of the above code) it's run the preview of the current section (trying to be as conservative as possible). Emacs can't really know which is the last equation you edited if you exit from it. You could use preview-at-point to preview the current equation you are in, but this is probably uncomfortable while you're writing it.

As it is implemented above, I think this feature is really annoying while you write (but increasing the idle time can help), so you can run M-: (cancel-timer mg-LaTeX-preview-formulae-timer) RET in order to eliminate this for the current Emacs session, and remove the above code for the future sessions. The problem is: how do you think you can tell Emacs you want to see the preview?

4
  • "how do you think you can tell Emacs you want to see the preview?" Using the same mechanism that replaces the formula by the image, in case the formula hasn't been edited. I have no clue how exactly they implemented that. The code here: kitchingroup.cheme.cmu.edu/blog/2015/10/09/… used this: (add-hook 'post-command-hook ... ), which seems a bit of an overkill, but then again, I have little experience with emacs programming.
    – Or Sattath
    Commented Oct 15, 2015 at 14:50
  • There is no magic mechanism: preview-at-point (bound to C-c C-p C-p) replaces the equation you are on, but if you exit from it or any "previewable" item preview-at-point does nothing. DId you try my solution?
    – giordano
    Commented Oct 15, 2015 at 17:19
  • When trying this, emacs asks "Cache preamble?" two times, then asks "Process Preview-LaTeX for document *.tex running, kill it?". It would be nice to avoid these queries. Otherwise it works fine.
    – Name
    Commented Dec 14, 2015 at 7:43
  • 1
    @Name (setq preview-auto-cache-preamble t).
    – giordano
    Commented Dec 14, 2015 at 9:56

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.