2

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.

  • Would an idle-timer function be what you want here? – stsquad Jan 19 '18 at 16:26
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.