Trung Ta
Trung Ta

Reputation: 1732

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).

I wonder if there is any option that enables hiding the markers like $ or \(, \) of LaTeX?

This will be useful when LaTeX math equations can be highlighted by setting: (setq org-highlight-latex-and-related '(latex script entities))


Update 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. \

Not sure if there is something wrong with my Emacs? I'm using Org 9.3 of Emacs 27.1


Update 2

The solution of Thomas does work for Emacs 25.2!

But somehow, there is a major change in Emacs 26.2, 26.3, 27.1 that breaks this feature... :(


Update 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.

(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.

Here is what I obtained using the code above:

enter image description here

Upvotes: 2

Views: 848

Answers (2)

darcamo
darcamo

Reputation: 3493

An alternative approach is to not hide the $ symbol at all and instead use 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. If I open an org-mode file with many equations I can type C-u C-u f5 to toggle latex preview on all equations in the buffer. If I want to change some equation I can just move the cursor there and org-fragtog-mode will turn-off the preview. 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.

Upvotes: 2

Thomas
Thomas

Reputation: 17422

Customize the variable org-emphasis-alist and add $ as an additional maker with the default values for all customization options. 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:.

Finally, click on State to make this change permanent either for the current editing session only or also for all future sessions.

NOTE that you have to restart org-mode by typing M-x org-mode again in order for this change to take effect.

enter image description here

Upvotes: 1

Related Questions