navark
navark

Reputation: 169

How to regenerate latex fragments in org-mode?

I have latex fragments in org-mode that were generated with my previous emacs theme and now that I switched my theme, the old latex fragments still have my old background color instead of the new one. How can I clear them and regenerate them (linux)?

Upvotes: 4

Views: 2263

Answers (4)

Mariano M.
Mariano M.

Reputation: 63

I'm using Doom Emacs and my previews are cached in the following directory:

~/.emacs.d/.local/cache/org-latex/

I need to delete the folder to make sure the previews are regenerated with the new foreground color.

Upvotes: 3

user3496846
user3496846

Reputation: 1665

Below is a solution which will change the cache folder automatically when you call load-theme. It will update the images as well. To avoid unnecessary operations, this is done only in the buffers where org-toggle-latex-fragment has been called.

The LaTeX fragments are computed for the whole buffer once a new theme is loaded, but you can remove '(16) in the last function below and it won't happen that way. Instead, only the current section will be updated (with the latex images in the rest of the buffer simply removed).

Make sure to transfer your latex fragment options to my/org-latex-set-options. That's because keeping these options within org-mode-hook will not work if you use #+STARTUP: latexpreview.

(defun my/org-latex-set-options ()
  (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5)))

(defvar my/org-latex-toggle-fragment-has-been-called nil
  "Tracks if org-toggle-latex-fragment has ever been called (updated locally).")

(defadvice org-toggle-latex-fragment (before my/latex-fragments-advice activate)
  "Keep Org LaTeX fragments in a directory with background color name."
  (if (not my/org-latex-toggle-fragment-has-been-called) (my/org-latex-set-options))
  (setq-local my/org-latex-toggle-fragment-has-been-called t)
  (my/org-latex-set-directory-name-to-background))

(defadvice load-theme (after my/load-theme-advice-for-latex activate)
  "Conditionally update Org LaTeX fragments for current background."
  (if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))

(defadvice disable-theme (after my/disable-theme-advice-for-latex activate)
  "Conditionally update Org LaTeX fragments for current background."
  (if my/org-latex-toggle-fragment-has-been-called (my/org-latex-update-fragments-for-background)))

(defun my/org-latex-set-directory-name-to-background ()
  "Set Org LaTeX directory name to background color name: c_Red_Green_Blue."
  (setq org-preview-latex-image-directory
        (concat "ltximg/c"
                (let ((color (color-values (alist-get 'background-color (frame-parameters)))))
                  (apply 'concat (mapcar (lambda (x) (concat "_" x)) (mapcar 'int-to-string color))))
                "/")))

(defun my/org-latex-update-fragments-for-background ()
  "Remove Org LaTeX fragment layout, switch directory for background, turn fragments back on."
  ;; removes latex overlays in the whole buffer
  (org-remove-latex-fragment-image-overlays)

  ;; background directory switch
  (my/org-latex-set-directory-name-to-background)

  ;; recreate overlay
  ;; Argument '(16) is same as prefix C-u C-u,
  ;; means create images in the whole buffer instead of just the current section.
  ;; For many new images this will take time.
  (org-toggle-latex-fragment '(16)))

Upvotes: 1

navark
navark

Reputation: 169

The latex fragments are stored in a folder named "ltximg" in the same folder as where the org file is located. To recreate the fragments, delete that folder, restart emacs and do org-toggle-latex-fragment again.

Upvotes: 2

NickD
NickD

Reputation: 6412

C-c C-x C-l is bound to org-toggle-latex-fragment. Do it once to get rid of the overlay and do it again to regenerate the overlay. Depending on where you are in the buffer and whether you invoke it with one C-u or two C-u or no C-u, it will do different things (affect the current latex fragment, all fragments in a subtree, or all fragments in the buffer). You should read the doc string of the funcion with

C-h C-f org-toggle-latex-fragment RET

Upvotes: 2

Related Questions