项目分享: Org Mode 中 LaTeX 预览与导出的优化 (Improved LaTeX Preview).

非常期待……

对 xelatex 和 pdflatex 处理起来的大小好像不一样,pdflatex 看起来正常,xelatex 会小很多,看起来不协调。scale 和 zoom 都设置成 1.25 了。然后那个displaymath 形式的公式,单个 alpha 看起来巨大一个。

latex_preview

这个是 dvisvgm 的问题,需要把参数从 --exact-bbox 改为 --bbox=preview.

正常的效果就像这样,不知道为什么序号对的不是很齐。

从 Discord 转来的已知问题,是开发者在9月1日发布的。

<@107836515278651392> State of OLP:

  1. xelatex previews are broken, it doesn’t look like there’s an easy way to fix scaling from org-latex->preview or from preview.sty.
  2. Scaling with dvisvgm is broken in general because of --exact-bbox. We need to go back to --bbox=preview. To fix the clipping on MacOS we can (conditionally) provide preview.sty with an option for a slightly larger margin.
  3. imagemagick support is broken, mostly because of API changes in the past few months. Will fix.
  4. HTML export is broken because of defmacro issues with org-html-format-latex.
  5. org-async needs to be tweaked to allow setting process parameters like pty/pipe and adaptive process reads (Dat 60% speed increase). It’s best if we do this semi-live instead of me waving a hammer at it.
  6. precompilation frequently blocking Emacs is a real pain in the neck. It’s a tall order to add it to the main org-async chain, but I was wondering if we can add an option to precompile ahead of time using a child Emacs process, or via wrapping org-compile-file with org-async somehow. I’ve tried the child Emacs method, but it’s tricky to update the org-persist-index this way.
  7. Centering images using the display engine is possible and close to free, but equations aren’t always centered within the images, so this gets tricky. In any case I’m not in favor of adding this feature to the library. I can post the code to do it on a wiki somewhere, or make an external package.
  8. Making live-previews robust requires a couple of changes to the general API. The problem is that fragments in invalid states are live-previewed all the time, and these cause all kinds of (inconsequential and transient) errors. We need a way of modifying the async process tree at declaration time so that failures are silently ignored.
  9. It looks like we’re making too many calls to org-element-context to check if we’re in a LaTeX env vs LaTeX fragment. I’m just going to store this boolean as an overlay property.
  10. Veteran/Prolific Org users (think John Kitchin) have all kinds of customizations in their org-latex-preview-process-alist. They’re not going to be happy about the “improvements” when their workflow breaks completely with Org 9.7. It might be worth reaching out to them ahead of time.
  11. Precompilation bug 1: All linked Org files are opened up when precompiling
  12. Precompilation bug 2 (with #+startup: latexpreview: Latex previews are generated in the org-export-with-buffer-copy buffer that’s created for precompilation. (i) This shouldn’t happen, and (ii) It fails anyway because the buffer is killed before any callbacks can run.

仓库最后更新在8月6日,大概可以认为这些问题都还存在。不过这些问题都不太影响日常使用,就我的体验除了公式对不太齐很难受以外都没感知。

1 个赞

感谢搬运。 那就先还是用 pdflatex 或 lualatex 吧。xelatex 那个 scale 问题还是挺大的,我上面只是个非常小的例子。公式多了之后有很多大小不一。。。然后就是 mac 下公式好像也不居中。

我今天更新了一下org,发现公式的序号能对的齐了

更nice的是,如果前面增加了align环境,后面的序号会一起跟着更新,而且速度很快

2 个赞

我的问题和 62 楼的一样, 但是相反, 我的 alpha 和 display math 相比特别小… 而且我用了 --bbox=preview 后依然没有任何改变

我在 org-latex-preview 代码里没看到这个 3.0 的 check, 请问一下这个 check 大概在什么位置? 另外我用 dvisvgm2 可以正常出 svg 而不是 png

@name8102 @A7R7

请问你们是如何做到行内公式和行间公式大小一致的? 公式居中对齐我都不追求了. 现在我最大的问题就是行间公式永远比行内公式大, 如果用 :zoom keyword 改大小, 永远只能两个一起改, 效果是行内公式能看清的时候, 行间公式过于巨大; 行间公式大小合适时, 行内公式又太小看不清. 效果如下图所示:

(图) 大小不一致的行内/行间公式 (org dev, emacs -Q, emacs-plus@29)

得到上图的 terminal 命令是:

emacs -Q -l test.el

其中 test.el 中的内容如下:

(add-to-list 'load-path "~/.emacs.d/elpa/org-mode/lisp") ;; 加入你的 org dev 文件夹所在路径
(with-eval-after-load 'org
  (setq org-latex-preview-numbered t
	org-latex-preview-precompile nil)
  (let ((pos (assoc 'dvisvgm org-latex-preview-process-alist)))
    (plist-put (cdr pos) :image-converter '("dvisvgm --page=1- --optimize --clipjoin --relative --no-fonts --bbox=preview -o %B-%%9p.svg %f"))))

(insert "#+latex_compiler: xelatex
#+latex_header: \\usepackage{ctex, amsmath}
\\begin{equation}
\\alpha + \\alpha =2\\alpha \\quad \\text{中文测试}
\\end{equation}

\\begin{equation}
\\omega \\cdot \\omega = \\omega^2
\\end{equation}

I am \\(\\alpha\\) I am \\(\\omega\\)
")
(org-mode)
(insert (concat "\nI am using org " org-version " " (org-git-version)))

我的 org dev 来源是 https://git.tecosaur.net/tec/org-mode.git, branch 是 dev. 所用的源码是目前(2023/12/08)最新的 commit 76468c

所有安装了 org dev 的人都可以尝试用上面的代码复现我图中的结果 (启动 emacs -Q 后只需要手动输入一下 M-x org-latex-preview 即可). 可以看到我在 test.el 中集成了本贴中的所有解决方案, 包括:

  • 用 amsmath
  • --bbox=preview

并且, 我尝试过 dvisvgm 2 和 3. 效果都一样.

在上面这些尝试后, 我依然无法在 emacs-plus@29 中得到上面某些用户的理想结果. 不知道什么地方还可以改进. 作为对比, 我最后在下图中给出我用目前的 stable org 9.6.12 得到的平凡结果

(图) 大小一致的行内/行间公式 (org 9.6.12, 自攒 emacs 配置, 非 doom, emacs-plus@29)

我尝试了一下使用你的 test.el 启动 emacs -q -l test.el,报错说找不到 org-latex-preview-process-alist,很奇怪,我还在debug


另外,你提到的问题我也遇到过,有时一些行间公式看上去很大,这个问题经常在刚打开org文件的时候出现。你有没有尝试org-latex-preview-clear-cache?我专门写了一个小函数来重新加载cursor下的latex预览

  (defun my/org-latex-preview-reload ()
    (interactive)
    (call-interactively 'org-latex-preview-clear-cache)
    (org-latex-preview)
    )

我检查了一下我的 test.el, 只有一个小问题(已更正), 是有一个 ,# 前的逗号不该有(是我 tangle 前 org 自己加上的, 是已知的 org 行为). 我测试了一下更正的 test.el, 可以复现我的结果.

我尝试过 org-latex-preview-clear-cache. 没有解决问题.

我在 discord 上看了一下, 这个大小不一致的行为貌似以前是存在的, karthink 已经修复了. 但是不知道我为什么现在还有, 我在 discord 上也在问.

感觉我这个问题在 discord 上把 karthink 都整不会了.

我换用了 emacs-mac 试了一下, 依然不正常. 不知道这个新的 org-latex-preview 能不能在 mac 上正常使用.

提醒一下,如果是使用Arch Linux

需要下载
texlive-basic
texlive-latex
texlive-latexrecommended
texlive-latexextra
几个包

详情请参考TeX Live - ArchWiki


试用了一下,效果还是不错的

可惜org-roam有这个问题:https://github.com/org-roam/org-roam/issues/2361

1 个赞

这个问题似乎是因为 org-element 的 api 在 9.7 中有了更新,如果你之前有过 byte compile 或者 native compile 的话可以重新编译一下 org 相关的包,我是这么解决这个问题的

谢谢你,我试一试

问个相关的问题,Algorithms - Overleaf, Online LaTeX Editor 这种算法,应该怎么让 org-latex-preview 正确渲染。

我试了下, 在我这里是可以正常渲染的

update: (我又测试了下 \begin{algorithm}… 的环境发现报错了:

 ■  Warning (org): Tried to cache "12eecf18ae20d89e4ebd012ca90f83b20cb1cce4" without a path, skipping. This should not happen, please report it as a bug to the Org mailing list (M-x org-submit-bug-report).

这似乎是个罕见的问题, 我在 discord 回复给 karthink 了: Discord


单独测试 algpseudocode\begin{algorithmic} 环境没发现异常:

检查一下你的 org-latex-packages-alist? 加上 ("" "algpseudocode" t).

我这边测试环境完整的头部和 org-latex-packages-alist 如下:

(setq org-latex-packages-alist
      '(("T1" "fontenc" t)
        ("" "amsmath" t)
        ("" "bm" t) ; Bold math required
        ("" "mathtools" t)
        ("" "siunitx" t)
        ("" "physics2" t)
	("" "algpseudocode" t)
        ("" "mlmodern" t)))

(setq org-latex-preview-preamble
      "\\documentclass{article}
[DEFAULT-PACKAGES]
[PACKAGES]
\\usepackage{xcolor}
\\usephysicsmodule{ab,ab.braket,diagmat,xmat}%
")
2 个赞

感谢 我都没发现这个错误,确实能够渲染了,我再看看我原来的内容。

1 个赞

image 这里的相关配置可以抄抄么

是指图标吗? 我使用了苹果的图标字体 SF Pro.

(defun my/iconify-org-buffer ()
  (progn
    (push '("#+begin_src" . ?􀃤) prettify-symbols-alist)
    (push '("#+end_src" . ?􀅽) prettify-symbols-alist)
    (push '("#+RESULTS:" . ?􀎚) prettify-symbols-alist)))
(add-hook 'org-mode-hook #'my/iconify-org-buffer)
1 个赞