From fed2ebc16a759d42968bafbf811e0576b99af483 Mon Sep 17 00:00:00 2001 From: TEC Date: Tue, 14 Mar 2023 09:42:22 +0800 Subject: [PATCH] org-latex-preview: Fix snippet preamble hash * lisp/org-latex-preview.el (org-latex-preview--create-tex-file, org-latex-preview--hash, org-latex-preview-place, org-latex-preview-clear-cache): The only the default preamble was being hashed, which led to incorrect results across documents. To correctly hash the preamble, it is now determined at the start of `org-latex-preview-place', and used in `org-latex-preview--hash', `org-latex-preview--create-tex-file', and `org-latex-preview-clear-cache'. --- lisp/org-latex-preview.el | 19 ++++++++++--------- lisp/ox-html.el | 11 ++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index 05c3ec8ba..228c1f68b 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -947,6 +947,11 @@ Each entry of ENTRIES should be a list of 2-3 items, either (BEG END VALUE) Where BEG and END are the positions in the buffer, and the LaTeX previewed is either the substring between BEG and END or (when provided) VALUE." + (unless latex-preamble + (setq latex-preamble + (or org-latex-preview--preamble-content + (setq org-latex-preview--preamble-content + (org-latex-preview--get-preamble))))) (let* ((processing-info (cdr (assq processing-type org-latex-preview-process-alist))) (imagetype (or (plist-get processing-info :image-output-type) "png")) @@ -960,7 +965,7 @@ is either the substring between BEG and END or (when provided) VALUE." (`(,fg ,bg) (org-latex-preview--colors-around beg end)) (number (car (setq numbering-offsets (cdr numbering-offsets)))) (hash (org-latex-preview--hash - processing-type value imagetype fg bg number)) + processing-type latex-preamble value imagetype fg bg number)) (options (org-combine-plists org-latex-preview-options (list :foreground fg @@ -1029,7 +1034,7 @@ Faces in `org-latex-preview--ignored-faces' are ignored." ((symbolp (car face)) ; Spec like (org-level-1 default). (face-attribute (car face) attr nil (append (cdr face) '(default)))))) -(defun org-latex-preview--hash (processing-type string imagetype fg bg &optional number) +(defun org-latex-preview--hash (processing-type preamble string imagetype fg bg &optional number) "Return a SHA1 hash for referencing LaTeX fragments when previewing them. PROCESSING-TYPE is the type of process used to create the @@ -1049,9 +1054,7 @@ image. NUMBER is the equation number that should be used, if applicable." (sha1 (prin1-to-string (list processing-type - org-latex-preview-preamble - org-latex-default-packages-alist - org-latex-packages-alist + preamble org-latex-preview-options string (if (equal imagetype "svg") @@ -1365,10 +1368,7 @@ FRAGMENTS will be placed in order, wrapped within a The path of the created LaTeX file is returned." (let* ((header (concat - (or (plist-get processing-info :latex-header) - org-latex-preview--preamble-content - (setq org-latex-preview--preamble-content - (org-latex-preview--get-preamble))) + (plist-get processing-info :latex-header) (let ((w org-latex-preview-width)) (cond ((stringp w) @@ -2030,6 +2030,7 @@ the *entire* preview cache will be cleared, and `org-persist-gc' run." (org-latex-preview--remove-cached (org-latex-preview--hash org-latex-preview-default-process + org-latex-preview--preamble-content value imagetype fg bg number)))) (message "Cleared LaTeX preview cache for %s." (if (or beg end) "region" "buffer"))))) diff --git a/lisp/ox-html.el b/lisp/ox-html.el index 7d5084a19..591a7299f 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -3042,7 +3042,11 @@ CONTENTS is nil. INFO is a plist holding contextual information." "Make sure that appropriate preview images exist for all LaTeX TODO." (when (assq (plist-get info :with-latex) org-latex-preview-process-alist) - (let* ((elements + (let* ((latex-preamble + (or org-latex-preview--preamble-content + (setq org-latex-preview--preamble-content + (org-latex-preview--get-preamble)))) + (elements (org-element-map parse-tree '(latex-fragment latex-environment) #'identity @@ -3069,7 +3073,7 @@ TODO." (bg (plist-get html-options :background)) (number (car (setq numbering-offsets (cdr numbering-offsets)))) (hash (org-latex-preview--hash - processing-type value imagetype fg bg number)) + processing-type latex-preamble value imagetype fg bg number)) (options (org-combine-plists org-latex-preview-options html-options @@ -3091,7 +3095,8 @@ TODO." (apply #'org-async-wait-for (org-latex-preview--create-image-async processing-type - (nreverse fragment-info))))) + (nreverse fragment-info) + :latex-preamble latex-preamble)))) (plist-put info :html-latex-preview-hash-table element-hash-table) nil)))