# Automization of preview-latex

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.

• I haven't tried this but it seems very relevant: kitchingroup.cheme.cmu.edu/blog/2015/10/09/… – Kaushal Modi Oct 14 '15 at 17:46
• Seems precisely what I'm looking for, but for the preview-latex package instead of org-mode. – Or Sattath Oct 14 '15 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 Oct 14 '15 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 Oct 14 '15 at 19:31
• I'm assuming you use AUCTeX, right? – giordano Oct 14 '15 at 19:36

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)))

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?
• 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 Oct 15 '15 at 17:19
• @Name `(setq preview-auto-cache-preamble t)`. – giordano Dec 14 '15 at 9:56