Hello,

With the new org-latex-preview and the dvisvgm process, a fragment can be
rendered without any color, so it shows black (invisible on a dark theme).
It happens when the fragment is generated in the same call as a fragment
before it that is already cached, and both have the same foreground and
background.

* Cause

In `org-latex-preview-place', `prev-fg' and `prev-bg' are updated for every
entry:

          (setq prev-fg fg prev-bg bg)

including entries whose image came from the cache (and empty fragments),
which are not part of the LaTeX run.  The next fragment with the same
colors then gets `:continue-color t', so `org-latex-preview--tex-styled'
emits neither \color nor \special{dvisvgm:currentcolor on}.  In the LaTeX
document it is the first page (or follows a page without a color command),
so no color is active: the SVG has no fill and is drawn black instead of
with currentColor.

The broken SVG is then cached.  The cache key does not include
:continue-color, so regenerating the fragment reuses the black image
until the cache is cleared with `org-latex-preview-clear-cache'.

* Reproduction (emacs -Q, batch)

The attached org-latex-preview-repro.el places previews in a buffer with
\(a\), then adds a second line with \(b\) and places previews again, and
reports whether each SVG uses currentColor:

  REPRO_DIR=/tmp/repro emacs -Q --batch \
    --eval '(progn (add-to-list (quote load-path) "/path/to/org/lisp")
                   (setq org-persist-directory "/tmp/repro/persist/"))' \
    -l org-latex-preview-repro.el

Output:

  step 1 (only a): (("\\(a\\)" "currentColor"))
  step 2 (a cached, b new): (("\\(a\\)" "currentColor") ("\\(b\\)" "NO FILL"))

Interactively: open an Org file with a dark theme, preview a fragment,
then type a new fragment after it in the same paragraph and preview the
section: the new fragment is black.

* Suggested fix

Only update prev-fg/prev-bg when the entry is actually added to the LaTeX
run (patch attached, org-latex-preview-continue-color.patch).  With the
patch the reproduction gives:

  step 2 (a cached, b new): (("\\(a\\)" "currentColor") ("\\(b\\)"
"currentColor"))

* Versions

Org mode version 10.0-pre (10.0pre0.20261108.969 from GNU-devel ELPA)
GNU Emacs 32.0.50 (x86_64-pc-linux-gnu, GTK+ 3.24.52, cairo 1.18.6)
dvisvgm 3.6.1
pdfTeX 3.141592653-2.6-1.40.29 (TeX Live 2026/Arch Linux)

Thanks for the new preview system!
;;; org-latex-preview-repro.el --- Reproduce: -*- lexical-binding: t; -*-
;;; Reproduce: a new fragment after a cached one with the same colors gets no \color.
(require 'org)
(require 'org-latex-preview)
(setq org-latex-preview-process-default 'dvisvgm)
(defun repro-wait ()
  (let ((n 0))
    (while (and (< n 300) (seq-some #'process-live-p (process-list)))
      (accept-process-output nil 0.1) (setq n (1+ n)))))
(defun repro-svgs ()
  (let (out)
    (dolist (ov (overlays-in (point-min) (point-max)))
      (when (eq (overlay-get ov 'org-overlay-type) 'org-latex-overlay)
        (let* ((disp (overlay-get ov 'display))
               (file (and (eq (car-safe disp) 'image) (plist-get (cdr disp) :file))))
          (push (list (buffer-substring-no-properties (overlay-start ov) (overlay-end ov))
                      (and file (with-temp-buffer (insert-file-contents file)
                                                  (if (search-forward "currentColor" nil t) "currentColor" "NO FILL"))))
                out))))
    (sort out (lambda (a b) (string< (car a) (car b))))))
(defun repro-place ()
  (org-latex-preview-place 'dvisvgm
    (org-element-map (org-element-parse-buffer) '(latex-fragment latex-environment)
      (lambda (e) (list (org-element-begin e)
                        (- (org-element-end e) (org-element-post-blank e)))))))
(with-current-buffer (find-file-noselect (expand-file-name "test.org" (getenv "REPRO_DIR")))
  (erase-buffer) (insert "Text \\(a\\) here.\n") (org-mode)
  (repro-place) (repro-wait)
  (princ (format "step 1 (only a): %S\n" (repro-svgs)))
  (goto-char (point-max)) (insert "More text \\(b\\) here.\n")
  (repro-place) (repro-wait)
  (princ (format "step 2 (a cached, b new): %S\n" (repro-svgs))))
--- a/lisp/org-latex-preview.el
+++ b/lisp/org-latex-preview.el
@@ -1711,8 +1711,8 @@
                                    processing-type value options)
                           :overlay (org-latex-preview--ensure-overlay beg end)
                           :key hash)
-                    fragment-info)))
-          (setq prev-fg fg prev-bg bg))))
+                    fragment-info)
+              (setq prev-fg fg prev-bg bg))))))
     (when fragment-info
       (org-latex-preview--create-image-async
        processing-type

Reply via email to