简体   繁体   English

在 Emacs Org-mode 中突出显示 LaTeX 方程时如何隐藏符号 $?

[英]How to hide the symbol $ when highlighting LaTeX equations in Emacs Org-mode?

I know that in Org-mode, the emphasis markers like *, ~, =, +, _ can be hidden by this setting: (setq org-hide-emphasis-markers t) .我知道在 Org-mode 中,像*, ~, =, +, _这样的强调标记可以被这个设置隐藏: (setq org-hide-emphasis-markers t)

I wonder if there is any option that enables hiding the markers like $ or \\( , \\) of LaTeX?我想知道是否有任何选项可以隐藏 LaTeX 的$\\( , \\)等标记?

This will be useful when LaTeX math equations can be highlighted by setting: (setq org-highlight-latex-and-related '(latex script entities))当 LaTeX 数学方程可以通过设置突出显示时,这将很有用: (setq org-highlight-latex-and-related '(latex script entities))


Update 1更新 1

I tried the solution proposed by @Thomas on a fresh emacs -q but somehow the symbol $ is still not hidden, while other markers like *,+ are.我在新的emacs -q上尝试了@Thomas 提出的解决方案,但不知何故,符号$仍未隐藏,而其他标记,如*,+则隐藏。 \\ \\

Not sure if there is something wrong with my Emacs?不确定我的 Emacs 是否有问题? I'm using Org 9.3 of Emacs 27.1我正在使用 Emacs 27.1 的 Org 9.3


Update 2更新 2

The solution of Thomas does work for Emacs 25.2! Thomas 的解决方案适用于 Emacs 25.2!

But somehow, there is a major change in Emacs 26.2, 26.3, 27.1 that breaks this feature... :(但不知何故,Emacs 26.2、26.3、27.1 中有一个重大变化打破了这个特性...... :(


Update 3更新 3

Since the solution suggested by Thomas doesn't work with recent Emacs (26 or newer), I finally came up with a quick solution by customizing the function org-do-latex-and-related of Org-mode.由于 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)))

If anyone interested in this feature, you can place the above function somewhere in your Emacs configuration file, after Org-mode is loaded, so that the new org-do-latex-and-related will override the original one of Org-mode.如果有人对此功能感兴趣,可以在加载 Org-mode 后,将上述功能放在 Emacs 配置文件中的某个位置,以便新的org-do-latex-and-related将覆盖原来的 Org-mode。

Here is what I obtained using the code above:这是我使用上面的代码获得的:

在此处输入图片说明

An alternative approach is to not hide the $ symbol at all and instead use org-fragtog .另一种方法是根本不隐藏 $ 符号,而是使用org-fragtog It automatically toggles org-mode latex fragment previews as the cursor enters and exits them.当光标进入和退出它们时,它会自动切换组织模式乳胶片段预览。

However, when you open a file with equations it will only toggle the preview of an equation as the cursor enters and leaves the equation.但是,当您打开带有方程式的文件时,它只会在光标进入和离开方程式时切换方程式的预览。 I also have a keybinding (f5) for org-latex-preview .我还有一个用于org-latex-preview的键绑定(f5)。 If I open an org-mode file with many equations I can type Cu Cu f5 to toggle latex preview on all equations in the buffer.如果我打开一个包含许多方程的组织模式文件,我可以输入Cu Cu f5来切换缓冲区中所有方程的乳胶预览。 If I want to change some equation I can just move the cursor there and org-fragtog-mode will turn-off the preview.如果我想改变一些公式,我可以将光标移动到那里, org-fragtog-mode将关闭预览。 Then I can edit the equation and the cursor leaves the equation the preview will be automatically enabled again.然后我可以编辑方程,光标离开方程,预览将再次自动启用。

This also works correctly when org-highlight-latex-and-related is set to any of the possible choices as well as when you use prettify-symbols-mode.org-highlight-latex-and-related设置为任何可能的选择以及使用 prettify-symbols-mode 时,这也能正常工作。

Customize the variable org-emphasis-alist and add $ as an additional maker with the default values for all customization options.自定义变量org-emphasis-alist并使用所有自定义选项的默认值添加$作为附加制造商。 To do so, type为此,请键入

M-x customize-variable [RET] org-emphasis-alist [RET]

Then go to the bottom of the customization buffer, click on the last INS , and insert a dollar sign under Marker character: .然后到自定义缓冲区的底部,点击最后一个INS ,在Marker character:下插入一个美元符号。

Finally, click on State to make this change permanent either for the current editing session only or also for all future sessions.最后,单击“ State以使此更改仅对当前编辑会话或所有未来会话永久生效。

NOTE that you have to restart org-mode by typing Mx org-mode again in order for this change to take effect.请注意,您必须通过再次键入Mx org-mode来重新启动 org-mode 以使此更改生效。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM