0

I use LaTeX in a lot of org files, in particular my notes in org-roam. As such I have generated a lot of LaTeX preview images/fragments via C-c C-x C-l/org-latex-preview when creating these notes, that are just automatically loaded the next time I visit the file, using the header #+STARTUP: latexpreview.

I recently updated to Emacs 28.2 from 27.2, and now I find that every pre-existing org file I open re-generates the LaTeX previews upon startup, despite the fact that the images have not been removed from the associated ./ltximg directory.

I'm guessing this is some kind of cache/storage issue, but is there some way to stop re-generating previews and use the existing ones? By e.g. specifying that the ./ltximg directory should be searched first?

1 Answer 1

1

The ./ltximg directory (or more precisely the value of org-preview-latex-image-directory) is searched: not only is it searched first, it is the only place where Org mode looks for images.

The problem is the filename it is looking for: it is of the form org-ltximg_<hash>.<imgtype>. The image type is png by default and, although it can be changed, I suspect that all of your images are of that type (if not, that explains the regeneration).

More problematic however is the calculated <hash> value, which depends on a number of things:

             ...
             (hash (sha1 (prin1-to-string
                      (list org-format-latex-header
                        org-latex-default-packages-alist
                        org-latex-packages-alist
                        org-format-latex-options
                        forbuffer value fg bg))))
             ...

If any of the things that it depends on happen to have changed, then the calculated hash value will be different and the file will be regenerated. Some of these things are easy to check: the various options listed may have changed default value between the two versions, or you might have customized one or more differently between the two versions.

The four things on the last line: forbuffer, value, fg and bg, are more dynamic and therefore more difficult to check.

forbuffer is an optional parameter to org-format-latex (where all this cookery is done) and its value is a constant and unlikely to be different in the two versions (for the record it should be the symbol forbuffer). Both ox-html and ox-odt call org-format-latex with the forbuffer parameter set to nil, but if you are generating previews manually, that is unlikely to cause the hash to be different.

value is calculated as the :value property of the context of the image element in the Org mode file, so if you have not changed the Org mode file, it is unlikely to have changed (but maybe there was a change in the org-element parser that produces a different value - this is more difficult to check and I have not gone deeply into it).

fg and bg are the foreground and background colors of the generated image and may have changed.

All of these calculations are done in thirty lines of code, starting on line 15769 of org.el in my version of org.el (very recent update from upstream).

The upshot is that it will not be trivial to figure out what has changed, but you are the only one who can do that, since you have the filenames with the (previous) hash already on hand. If you can go back to your previous version on another system while using your current system for comparison, then you could use a debugger (e.g. edebug) to put a breakpoint after the hash calculation and then compare the values of the various variables that you get in the two versions. That's tedious and fiddly and will require some patience: I'm not sure it's worth it, but you are the only one who can decide that. Otherwise, I'm afraid you'll have to live with regenerating the images, plus the possibility that the same thing will happen after the next Emacs update.

6
  • 1
    Thanks very much for your detailed response - I guessed there was some hashing going on as repeating the same LaTeX command in a file results in a pre-existing preview being displayed. I have a previous version of my Emacs setup on another machine and I'll take a look at that and keep digging until I find it too hopeless. Dec 1, 2022 at 19:13
  • Good luck! Let me know if I can be of help.
    – NickD
    Dec 1, 2022 at 19:52
  • 1
    With some difficult I managed to manually build Emacs 27.2, and amazingly enough the LaTeX previews that had been generated when 27.2 was my main version work perfectly -- no new previews need to be generated. I created a new .org file with a new LaTeX expression and ran org-latex-preview with debug-on-entry on sha1 in both 28.2 and 27.2 and found that in 28.2, the fg and bg values are #F8F8F2 and #272822 while in 27.2 they are both evaluated as default. There's also changes to org-latex-default-packages-alist that I found. Dec 21, 2022 at 14:09
  • If you have the time, could you suggest how/what to do? I think I can see how to change org-latex-default-packages-alist, but not the fg and bg values. Might be better to move this to chat, I can share more of the relevant output. Dec 21, 2022 at 14:11
  • 1
    Not sure how much help/time I can afford, but if you are not impatient, I'd be interested. I just can't promise frequent updates. Moving to chat seems like the right move at this point.
    – NickD
    Dec 27, 2022 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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