Automatic Latex Preview Toggling in org-mode

In my org-mode setup, initially I used to preview the whole buffer all the time. However, as my files grew bigger the waiting time for this has become prohibitively expensive. Since then I have set it up to continuously recompile the file in the background each time the file is saved. It works pretty well since instead of spawning a new async process on each save, I have a single background process which recomiles in sync and thus needs not reload all the emacs on each recompile. Overall, it takes a couple of seconds to see the updated pdf after each save.

Recently however, I updated to org-mode 9.3 and I noticed that the image creation has gotten significantly faster, and the caching has gotten better - to the point of loading instantly the images from previous sessions. This has led me searching again for a way to toggle the images whenever the cursor falls on them, like in auctex. I wanted this feature for a long while now, bug never bothered to write it myself.

This time however, I found the solution premade by John Kitchin. It essentially worked quite well but had a few bugs that irritated me. I took it upon me to fix them, and attached below is the version with the fixes, as well as a bit of refactoring.

The major points: - Before deleting an overlay, make sure that there is still a latex fragment at the current location. - Don’t move the point (cursor) when entering a fragment. - Refactoring of the latex fragment test and surroungins (I know there is also org-inside-LaTeX-fragment-p, but it doesn’t detect when located on the first character of a fragment, and this one does).

To use, add the following to your org-mode-hook

(add-hook 'post-command-hook 'kk/org-latex-fragment-toggle t)

And add the following code somewhere in init.el .