org-latex-preview: Take advantage of dvisvgm3.2

* lisp/org-latex-preview.el (org-latex-preview--tex-styled,
org-latex-preview--dvisvgm-filter,
org-latex-preview--dvisvgm-version>=3.1,
org-latex-preview-process-alist,
org-latex-preview--dvisvgm3-minor-version): Take advantage of the new
--message flag of dvisvgm 3.2 to produce a more concise output.
This commit is contained in:
TEC 2024-01-16 02:02:42 +08:00
parent 3019d930d6
commit 157784392c
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 62 additions and 45 deletions

View File

@ -39,6 +39,16 @@
(defvar org-src-mode-hook nil) (defvar org-src-mode-hook nil)
(defvar org-src--beg-marker nil) (defvar org-src--beg-marker nil)
(defvar org-latex-preview--dvisvgm3-minor-version
(or (and (executable-find "dvisvgm")
(with-temp-buffer
(call-process "dvisvgm" nil t nil "--version")
(let ((ver (version-to-list
(string-trim (buffer-string) "dvisvgm "))))
(and (= (car ver) 3) (cadr ver)))))
-1)
"The minor version of dvisvgm, if dvisvgm 3 is installed. Otherwise -1.")
(defgroup org-latex-preview nil (defgroup org-latex-preview nil
"Options for generation of LaTeX previews in Org mode." "Options for generation of LaTeX previews in Org mode."
:tag "Org LaTeX Preview" :tag "Org LaTeX Preview"
@ -89,39 +99,39 @@ All available processes and theirs documents can be found in
;;;###autoload ;;;###autoload
(defcustom org-latex-preview-process-alist (defcustom org-latex-preview-process-alist
'((dvipng '((dvipng
:programs ("latex" "dvipng") :programs ("latex" "dvipng")
:description "dvi > png" :description "dvi > png"
:message "you need to install the programs: latex and dvipng." :message "you need to install the programs: latex and dvipng."
:image-input-type "dvi" :image-input-type "dvi"
:image-output-type "png" :image-output-type "png"
:latex-compiler ("%l -interaction nonstopmode -output-directory %o %f") :latex-compiler ("%l -interaction nonstopmode -output-directory %o %f")
:latex-precompiler ("%l -output-directory %o -ini -jobname=%b \"&%L\" mylatexformat.ltx %f") :latex-precompiler ("%l -output-directory %o -ini -jobname=%b \"&%L\" mylatexformat.ltx %f")
:image-converter ("dvipng --follow -D %D -T tight --depth --height -o %B-%%09d.png %f") :image-converter ("dvipng --follow -D %D -T tight --depth --height -o %B-%%09d.png %f")
:transparent-image-converter :transparent-image-converter
("dvipng --follow -D %D -T tight -bg Transparent --depth --height -o %B-%%09d.png %f")) ("dvipng --follow -D %D -T tight -bg Transparent --depth --height -o %B-%%09d.png %f"))
(dvisvgm (dvisvgm
:programs ("latex" "dvisvgm") :programs ("latex" "dvisvgm")
:description "dvi > svg" :description "dvi > svg"
:message "you need to install the programs: latex and dvisvgm." :message "you need to install the programs: latex and dvisvgm."
:image-input-type "dvi" :image-input-type "dvi"
:image-output-type "svg" :image-output-type "svg"
:latex-compiler ("%l -interaction nonstopmode -output-directory %o %f") :latex-compiler ("%l -interaction nonstopmode -output-directory %o %f")
:latex-precompiler ("%l -output-directory %o -ini -jobname=%b \"&%L\" mylatexformat.ltx %f") :latex-precompiler ("%l -output-directory %o -ini -jobname=%b \"&%L\" mylatexformat.ltx %f")
;; The --optimise, --clipjoin, and --relative flags cause dvisvgm to ;; The --optimise, --clipjoin, and --relative flags cause dvisvgm to
;; do some extra work to tidy up the SVG output, but barely add to ;; do some extra work to tidy up the SVG output, but barely add to
;; the overall dvisvgm runtime (<1% increace, from testing). ;; the overall dvisvgm runtime (<1% increace, from testing).
:image-converter ("dvisvgm --page=1- --optimize --clipjoin --relative --no-fonts --bbox=preview -o %B-%%9p.svg %f")) :image-converter determined-at-runtime)
(imagemagick (imagemagick
:programs ("pdflatex" "convert") :programs ("pdflatex" "convert")
:description "pdf > png" :description "pdf > png"
:message "you need to install the programs: latex and imagemagick." :message "you need to install the programs: latex and imagemagick."
:image-input-type "pdf" :image-input-type "pdf"
:image-output-type "png" :image-output-type "png"
:latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f") :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
:latex-precompiler ("pdftex -output-directory %o -ini -jobname=%b \"&pdflatex\" mylatexformat.ltx %f") :latex-precompiler ("pdftex -output-directory %o -ini -jobname=%b \"&pdflatex\" mylatexformat.ltx %f")
:image-converter :image-converter
("convert -density %D -trim -antialias %f -quality 100 %B-%%09d.png"))) ("convert -density %D -trim -antialias %f -quality 100 %B-%%09d.png")))
"Definitions of external processes for LaTeX previewing. "Definitions of external processes for LaTeX previewing.
Org mode can use some external commands to generate TeX snippet's images for Org mode can use some external commands to generate TeX snippet's images for
previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
@ -176,6 +186,22 @@ Place-holders only used by `:image-converter':
:type '(alist :tag "LaTeX to image backends" :type '(alist :tag "LaTeX to image backends"
:value-type (plist))) :value-type (plist)))
;; This is a bit hacky, but since we can't include reference to the
;; runtime value of `org-latex-preview--dvisvgm3-minor-version' in the
;; default value of `org-latex-preview-process-alist', we have to
;; resort to modifying the value at runtime like so.
;; Theoretically only the "load" condition is needed, but some people seemed
;; to have problems with this that are solved by adding "eval".
(cl-eval-when (load eval)
(when-let ((dvisvgm (alist-get 'dvisvgm org-latex-preview-process-alist)))
(when (eq (plist-get dvisvgm :image-converter) 'determined-at-runtime)
(plist-put dvisvgm :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}'" "")
" --bbox=preview -o %B-%%9p.svg %f"))))))
(defcustom org-latex-preview-compiler-command-map (defcustom org-latex-preview-compiler-command-map
'(("pdflatex" . "latex") '(("pdflatex" . "latex")
("xelatex" . "xelatex -no-pdf") ("xelatex" . "xelatex -no-pdf")
@ -2531,20 +2557,11 @@ fragments in EXTENDED-INFO."
(org-latex-preview--place-images (org-latex-preview--place-images
extended-info (nreverse fragments-to-show))))) extended-info (nreverse fragments-to-show)))))
(defvar org-latex-preview--dvisvgm-version>=3.1
(and (executable-find "dvisvgm")
(with-temp-buffer
(call-process "dvisvgm" nil t nil "--version")
(version<=
"3.1"
(string-trim (buffer-string) "dvisvgm "))))
"Whether dvisvgm >= 3.1 is installed.")
(defun org-latex-preview--dvisvgm-filter (_proc _string extended-info) (defun org-latex-preview--dvisvgm-filter (_proc _string extended-info)
"Look for newly created images in the dvisvgm stdout buffer. "Look for newly created images in the dvisvgm stdout buffer.
Any matches found will be matched against the fragments recorded in Any matches found will be matched against the fragments recorded in
EXTENDED-INFO, and displayed in the buffer." EXTENDED-INFO, and displayed in the buffer."
(let ((dvisvgm-processing-re "^processing page \\([0-9]+\\)\n") (let ((dvisvgm-processing-re "^processing page \\([0-9]+\\)[\n:]")
(fragments (plist-get extended-info :fragments)) (fragments (plist-get extended-info :fragments))
page-marks fragments-to-show) page-marks fragments-to-show)
(beginning-of-line) (beginning-of-line)
@ -2574,7 +2591,7 @@ EXTENDED-INFO, and displayed in the buffer."
(setq page-marks (cdr page-marks))) (setq page-marks (cdr page-marks)))
(when fragments-to-show (when fragments-to-show
(setq fragments-to-show (nreverse fragments-to-show)) (setq fragments-to-show (nreverse fragments-to-show))
(if org-latex-preview--dvisvgm-version>=3.1 (if (>= org-latex-preview--dvisvgm3-minor-version 1)
(mapc #'org-latex-preview--await-fragment-existance fragments-to-show) (mapc #'org-latex-preview--await-fragment-existance fragments-to-show)
(mapc #'org-latex-preview--svg-make-fg-currentColor fragments-to-show)) (mapc #'org-latex-preview--svg-make-fg-currentColor fragments-to-show))
(org-latex-preview--place-images extended-info fragments-to-show)))) (org-latex-preview--place-images extended-info fragments-to-show))))
@ -2927,7 +2944,7 @@ information for this run."
(and fg (format "\\color[rgb]{%s}" fg)) (and fg (format "\\color[rgb]{%s}" fg))
(and fg (and fg
(eq processing-type 'dvisvgm) (eq processing-type 'dvisvgm)
org-latex-preview--dvisvgm-version>=3.1 (>= org-latex-preview--dvisvgm3-minor-version 1)
"\\special{dvisvgm:currentcolor on}")))) "\\special{dvisvgm:currentcolor on}"))))
(and num (format "\\setcounter{equation}{%d}" (1- num))) (and num (format "\\setcounter{equation}{%d}" (1- num)))
"%\n" "%\n"