7

I'm using org-mode in Spacemacs (0.200.10.x).

To preview Latex when I open a file, I understand that I can either set the variable org-startup-latex-with-latex-preview to t in my init.el, or set the local #+STARTUP: latexpreview option.

However, I want org-mode to preview Latex fragment as soon as a finish typing it. Is that possible? If possible, I'd love a solution that's already included in Spacemacs so I can keep my init.el clean.

Heisenberg
  • 433
  • 4
  • 9

2 Answers2

11

I wrote a plugin that does this. You can install the plugin, then add

(add-hook 'org-mode-hook 'org-fragtog-mode)

to your config.

The plugin enables latex previews when your cursor leaves the fragment, and disables them again when the cursor enters, for further editing.

https://github.com/io12/org-fragtog

io12
  • 111
  • 1
  • 2
5

While I do not know how to do it realtime, I know it is possible to do after each save. You can use emacs local variables to do the trick. https://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html#Specifying-File-Variables . This will generate all latex fragments whenever you save the file. Just append this to the end each file where you want this.

# Local variables:
# after-save-hook: org-preview-latex-fragment
# end:

If you would like to apply it to all org mode files you will need to use hooks. See this reference for how to apply a hook in only one mode https://stackoverflow.com/questions/6138029/how-to-add-a-hook-to-only-run-in-a-particular-mode#6141681. This hook will only be active when you are in org mode.

(add-hook 'org-mode-hook 
      (lambda () 
         (add-hook 'after-save-hook 'org-preview-latex-fragment nil 'make-it-local)))

Note After reading the org.el definitions for org-preview-latex-fragment it is obsolete past org-mode > 8.3. It looks like you need to toggle the latex fragment display. What the following code does is check if the latex fragments are currently rendered if so toggle twice if not toggle once on save)

(defun my/org-render-latex-fragments ()
  (if (org--list-latex-overlays)
      (progn (org-toggle-latex-fragment)
             (org-toggle-latex-fragment))
    (org-toggle-latex-fragment)))

(add-hook 'org-mode-hook
          (lambda ()
            (add-hook 'after-save-hook 'my/org-render-latex-fragments nil 'make-the-hook-local)))
serycjon
  • 381
  • 2
  • 8
costrouc
  • 421
  • 3
  • 8
  • This is smart! Previewing upon saving is a decent option. Do you know how to add this hook for all `.org` files? – Heisenberg Jan 19 '18 at 16:45
  • 1
    It looks like the function org--list-latex-overlays has been removed in newer versions of org-mode, so the hook doesn't work anymore. – Fredrik Meyer Apr 03 '20 at 09:00
  • @FredrikMeyer Do yo see a way to make it work again? – student Aug 25 '20 at 09:10
  • @student I did not get toggling to work. Now I use (add-hook 'org-mode-hook (lambda () (add-hook 'after-save-hook 'org-preview-latex-fragment nil 'make-it-local))) to preview after save. And just press backspace to edit. – Fredrik Meyer Aug 25 '20 at 09:44
  • Thanks that works. Maybe you should use `org-latex-preview` since `org-preview-latex-fragment` seems to be obsolete. Maybe you could add your solution as an answer for other visitors. – student Aug 25 '20 at 09:53