Hi there!  I've been using this excellent overhaul ever since it came out, and I'd like to contribute a bug fix. I hope replying to this thread is the right way to do that; if not, please let me know.

Recently, the previews stopped working after a system update.

* Symptom
On macOS with dvisvgm 3.2+, org-latex-preview can't typeset LaTeX fragments at all in a lot of files.  It gives an error like the following:

error in process filter: Opening input file: No such file or directory, /var/var/folders/8g/k689fc1j3gg2ny7xrzqchjsm0000gn/T/org-tex-ZqKlQw-000000001.svg [2 times]

** Steps to Reproduce
Put dvisvgm 3.2+ on your path and create a .org file anywhere outside temporary-file-directory that is shallower than (i.e. has fewer slashes than) temporary-file-directory.  Try to preview any LaTeX fragment in that file.

This should theoretically reproduce on non-macOS, but macOS has an especially deep temporary-file-directory that looks like /var/folders/8g/k689fc1j3gg2ny7xrzqchjsm0000g, making it easier to reproduce.  If temporary-file-directory is /tmp on your system, I think you have to make the .org file in the root directory or change temporary-file-directory to a deeper directory (haven't tried).

* Cause
This is because org-latex-preview:
1. Runs dvisvgm in the same directory as the .org file, and
2. Tells dvisvgm to report the output SVG file names as relative paths using ?svgfile, but
3. Then expands those file names under temporary-file-directory in org-latex-preview--dvisvgm-filter.

So dvisvgm relativizes in one path, then org-latex-preview expands in another.

For instance, if I try to typeset fragments in /Users/jun/org/test/test.org, then dvisvgm reports:

output written to: ../../../../var/folders/8g/k689fc1j3gg2ny7xrzqchjsm0000gn/T/org-tex-CWRtgi-000000001.svg

where /var/folders/8g... is the absolute path of the output file.  This is expanded under temporary-file-directory, which is /var/folders/8g/k689fc1j3gg2ny7xrzqchjsm0000gn/T, leading org-latex-preview to busy-wait for files to arrive in /var/var/folders/8g/k689fc1j3gg2ny7xrzqchjsm0000gn/T.

I think this is hard to reproduce on most non-macOS systems where the temporary-file-directory is shallow.  ?svgfile contains as many ../ as there are slashes in the directory hosting the .org file, and expanding that relative to (say) /tmp will effectively remove all those ../, making it expand to the correct absolute path by accident.

* Fix
Use ?svgpath instead of ?svgfile.  Here's a patch that applies to commit 9584a76a843e2e8122799e5653bb9120fe568f64 of [[https://git.tecosaur.net/tec/org-mode.git]].

modified   lisp/org-latex-preview.el
@@ -200,7 +200,7 @@ Place-holders only used by `:image-converter':
                  (list
                   (concat "dvisvgm --page=1- --optimize --clipjoin --relative --no-fonts"
                           (if (>= org-latex-preview--dvisvgm3-minor-version 2)
-                              " -v3 --message='processing page {?pageno}: output written to {?svgfile}'" "")
+                              " -v3 --message='processing page {?pageno}: output written to {?svgpath}'" "")
                           " --bbox=preview -o %B-%%9p.svg %f"))))))
 
 (defcustom org-latex-preview-compiler-command-map
@@ -2792,7 +2792,7 @@ EXTENDED-INFO, and displayed in the buffer."
         (when (save-excursion
                 (re-search-forward "output written to \\(.*.svg\\)$" end t))
           (setq fragment-info (nth (1- page) fragments))
-          (plist-put fragment-info :path (expand-file-name (match-string 1) temporary-file-directory))
+          (plist-put fragment-info :path (match-string 1))
           (when (save-excursion
                   (re-search-forward "^  page is empty" end t))
             (unless (plist-get fragment-info :error)

** Environment
GNU Emacs 29.1 (build 1, aarch64-apple-darwin21.6.0, Carbon Version 165 AppKit 2113.6) of 2023-08-09 (Brew tap railwaycat emacs-mac)

dvisvgm 3.2.2

On Sun, Mar 12, 2023 at 9:35 PM Timothy <orgmode@tec.tecosaur.net> wrote:
Hi All,

After months of work, Karthink and I have prepared a rather large patch-set
completely overhauling the LaTeX preview system. I hope to have a patch set
shortly, but in the mean time it would be good to get some more people testing
this.

To test this feature, please check out the `dev' branch of
<https://git.tecosaur.net/tec/org-mode.git> (it’s the default branch). There are
also some other changes there currently, but I don’t think anything is broken.

You can view the almost-ready path set/diff here:
<https://git.tecosaur.net/tec/org-mode/compare/c8401d2f..6d60738>, see the
ORG-NEWS and org-manual entries to get a bit more of an idea of what’s changed.
The short version is that now:
• Previews are generated in bulk, and hundreds of LaTeX fragments can be processed per second.
• Images are placed continuously as they are generated.
• Preview generation is asynchronous and will not block Emacs.
• Inline previews are aligned and scaled to match the font baseline and size.
• Previews scale along with text when the text scale is changed.
• Previews are coloured to match surrounding text and the active theme.
• SVG previews automatically change colors when the active theme changes.
• Error encountered when compiling LaTeX fragments can be accessed by mousing over preview images.
• Preview overlays can hide and show themselves dynamically based on cursor position.
• Org mode can auto-generate LaTeX previews as you type or edit the text of existing ones.
• Org mode can keep equation numbering consistent by regenerating previews as needed.

If you do come across any issues, please let me know either in a reply here or
the org-mode matrix room. If you could also run
<https://gist.github.com/karthink/0ac48411a81459c0f3fd7557c4e817db> and share the
diagnostic info, that would be quite helpful.

Lastly, Karthink has prepared a video giving an overview of the new
capabilities, you can give it a watch here:
<https://www.youtube.com/watch?v=n-AfvuV-bYo>

All the best,
Timothy

--
Timothy (‘tecosaur’/‘TEC’), Org mode contributor.
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/tec>.


--
Jun Inoue