Emacs 自带的 org mode 转 LaTeX 基本上是开箱即用,这让我完全没有装和 org mode 有关系的包。但实际上,我经常遇到令人恼火的情况,主要是因为 emacs 在不该 escape brace 时 escape brace。
常见的有:
-
\foo{bar}[]{baz}
变为\foo{bar}[]\{baz\}
; -
\foo<bar>{baz}
变为\foo<bar>\{baz\}
; - 多行的 latex fragments 。
不过添加如下 advice 的话就能解决前两个,第三个可以保全前括号在同一行的情况:
(advice-add 'org-element-latex-fragment-parser :filter-return
(lambda (latex-fragment)
(when latex-fragment
(let* ((plist (cadr latex-fragment))
(value (plist-get plist :value))
(begin (plist-get plist :begin))
(end (plist-get plist :end))
(post-blank (plist-get plist :post-blank)))
(if (or (not (eq ?\\ (char-after begin)))
(memq (char-after (1+ begin)) `(,?\[ ,?\()))
latex-fragment
(goto-char end)
(while
(and (memq (char-after (point)) `(,?\[ ,?\{ ,?<))
(let ((next (scan-sexps (point) 1)))
(when next (goto-char next)))))
(let* ((end (point))
(value (filter-buffer-substring begin end)))
(goto-char begin)
`(latex-fragment (:value
,value :begin ,begin :end ,end
:post-blank ,post-blank))))))))