【发布时间】:2020-09-13 12:52:20
【问题描述】:
我知道在 Org 模式下,*, ~, =, +, _ 等强调标记可以通过以下设置隐藏:(setq org-hide-emphasis-markers t)。
我想知道是否有任何选项可以隐藏 LaTeX 的 $ 或 \(、\) 等标记?
当 LaTeX 数学方程可以通过设置突出显示时,这将很有用:
(setq org-highlight-latex-and-related '(latex script entities))
更新 1
我在新的emacs -q 上尝试了@Thomas 提出的解决方案,但不知何故,符号$ 仍然没有隐藏,而其他标记如*,+ 是。 \
不确定我的 Emacs 是否有问题?我正在使用 Emacs 27.1 的 Org 9.3
更新 2
Thomas 的解决方案确实适用于 Emacs 25.2!
但不知何故,Emacs 26.2、26.3、27.1 中有一个重大变化破坏了这个特性...... :(
更新 3
由于 Thomas 建议的解决方案不适用于最近的 Emacs(26 或更新),我终于通过自定义 Org-mode 的函数 org-do-latex-and-related 想出了一个快速的解决方案。
(defun org-do-latex-and-related (_limit)
"Highlight LaTeX snippets and environments, entities and sub/superscript.
Stop at first highlighted object, if any. Return t if some
highlighting was done, nil otherwise."
(when (org-string-nw-p org-latex-and-related-regexp)
(catch 'found
(while (re-search-forward org-latex-and-related-regexp
nil t) ;; on purpose, we ignore LIMIT
(unless (cl-some (lambda (f) (memq f '(org-code org-verbatim underline
org-special-keyword)))
(save-excursion
(goto-char (1+ (match-beginning 0)))
(face-at-point nil t)))
(let* ((offset (if (memq (char-after (1+ (match-beginning 0)))
'(?_ ?^))
1
0))
(start (+ offset (match-beginning 0)))
(end (match-end 0)))
(if (memq 'native org-highlight-latex-and-related)
(org-src-font-lock-fontify-block "latex" start end)
(font-lock-prepend-text-property start end
'face 'org-latex-and-related))
;;<<<<<<<<<<<<<<<<<<<<<
;; my code starts here
(when (and org-hide-emphasis-markers (< (+ start 4) end))
(cond ((member (buffer-substring start (+ start 2)) '("$$" "\\("))
(add-text-properties start (+ start 2) '(invisible org-link)))
((string= (buffer-substring (1+ start) (+ start 2)) "$")
(add-text-properties (1+ start) (+ start 2) '(invisible org-link))))
(cond ((member (buffer-substring end (- end 2)) '("$$" "\\)"))
(add-text-properties end (- end 2) '(invisible org-link)))
((string= (buffer-substring (1- end) (- end 2)) "$")
(add-text-properties (1- end) (- end 2) '(invisible org-link)))))
;; my code ends here
;;>>>>>>>>>>>>>>>>>>>>>
(add-text-properties (+ offset (match-beginning 0)) (match-end 0)
'(font-lock-multiline t)))
(throw 'found t)))
nil)))
如果有人对此功能感兴趣,可以在加载 Org-mode 后将上述函数放在 Emacs 配置文件中的某个位置,这样新的 org-do-latex-and-related 将覆盖原来的 Org-mode。
这是我使用上面的代码获得的:
【问题讨论】: