From f206164ef4cc8fa36dfb8544446b1fdc36ad73cf Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Sun, 8 Oct 2023 16:04:12 -0700 Subject: [PATCH] org-latex-preview: Fix xelatex preview image sizes * lisp/org-latex-preview.el (org-latex-preview--include-preview-string, org-latex-preview--latex-preview-filter): when xetex is used, preview.sty assumes PDF output by default and does not write any postscript data to the output XDV file. This makes dvisvgm unable to correctly determine the preview geometry. Fix by specifying the `dvips' option to preview, which adds (apparently as a side effect) this geometry information to the XDV file. Remove a conditional in `org-latex-preview--latex-preview-filter' that was previously used to attempt to work around this issue. --- lisp/org-latex-preview.el | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lisp/org-latex-preview.el b/lisp/org-latex-preview.el index c9afff8e3..6bc4552c1 100644 --- a/lisp/org-latex-preview.el +++ b/lisp/org-latex-preview.el @@ -1475,8 +1475,24 @@ previews." org-latex-preview--single-eqn-format))))))) (defconst org-latex-preview--include-preview-string - "\n\\usepackage[active,tightpage,auctex]{preview}\n" - "A LaTeX preamble snippet that includes preview.sty for previews.") + "\n\\usepackage[active,tightpage,auctex,dvips]{preview}\n" + "A LaTeX preamble snippet that includes preview.sty for previews. + +The options passed to preview.sty are: +- active: activate preview package + +- auctex: Produce fake error messages at the start and end of + every preview environment. We use this to help with parsing + the tex compilation stdout using regexes. + +- tightpage: (effectively) shrink page sizes to fit each + previewed math environment. + +- dvips: Only required for xelatex runs. Add (as a side effect) + preview geometry data to the output XDV file. + +This is an abridged summary. See the documentation of +preview.sty for more details.") (defun org-latex-preview--create-tex-file (processing-info fragments) "Create a LaTeX file based on PROCESSING-INFO and FRAGMENTS. @@ -1812,11 +1828,7 @@ fragments in EXTENDED-INFO." (setq preview-marks (nreverse preview-marks)) (while preview-marks (goto-char (caar preview-marks)) - ;; Check for tightpage-info, as long as XeLaTeX is not being used, - ;; as it seems to behave differently to pdfLaTeX and luaLaTeX and - ;; produces an image without the margins that tightpage reports. - (unless (or tightpage-info (equal (plist-get extended-info :latex-processor) - "xelatex")) + (unless tightpage-info (save-excursion (when (re-search-forward "^Preview: Tightpage \\(-?[0-9]+\\) *\\(-?[0-9]+\\) *\\(-?[0-9]+\\) *\\(-?[0-9]+\\)"