org-mode buffer latex syntax highlighting
Asked Answered
A

4

13

I have been using org-mode for publishing scientific notes. Most of what I publish is compiled latex code and I am looking for a way to highlight latex syntax in the org-mode buffer. I came across many solutions for displaying published latex code with syntax highlighting using listed, minted, pygments, etc. Is there a way to display highlighted latex syntax in the org-buffer?

Avrilavrit answered 21/6, 2013 at 15:44 Comment(3)
Can you provide an example of what you mean within an Org document?Cherianne
I think he wants in an org-mode buffer, to highlight all the embedded latex code (for instance \alpha, S^3, \dots).Frayne
Isn't C-c ' not enough while editing a LaTeX block? Do you want to see it highlighted at all times?Candide
S
14

The variable org-highlight-latex-and-related allows you to highlight inline math. However, LaTeX fragments will only be highlighted with a single face.

From the documentation of org-highligh-latex-and-related:

Non-nil means highlight LaTeX related syntax in the buffer.
When non nil, the value should be a list containing any of the
following symbols:
  `latex'    Highlight LaTeX snippets and environments.
  `script'   Highlight subscript and superscript.
  `entities' Highlight entities.

E.g.:

(setq org-highlight-latex-and-related '(latex script entities))

The face org-latex-and-related is used to fontify LaTeX fragments.

Saddletree answered 28/4, 2015 at 12:3 Comment(5)
This does not do highlighting for inline math, such as $v\in V$. Is there any way to achieve such highlighting?Nicholson
@Nicholson I do observe that $v \in V$ gets highlighted. Which Org mode version are you using?Saddletree
Version 9.0.7. Is there anything else I need in setup besides setting org-highlight-latex-and-related? Also, could there be things in conflict?Nicholson
I just checked my config and I do not change any other latex related Org mode options. My version is 9.0.9 but this has been working for a long time. Do you have any other font lock options enabled that could shade the behavior of Org mode? If not, you may want to post your question at the Org mode mailing list.Saddletree
I am having this problem even when opening an .org file with emacs -q and then evaluating (setq org-highlight-latex-and-related '(latex script entities)).Nicholson
R
6

You can enclose your latex fragments in#+BEGIN_LaTeX latex and #+END_LaTex latex to have them highlighted. It is different from source blocs (#+BEGIN_SRC LaTeX....) that are exported verbatim (with potential highlighting via pygments as you mentioned).

It works well for environments (equations,aligns...):

emacs screenshot

As far as I know it does not work for inline math (well it does if you enclose the whole paragraph but you loose the point of using org-mode...)

Source: this answer in emacs.stackexchange and Reddit.

Rostand answered 13/4, 2017 at 12:36 Comment(0)
N
2

Put the following in your .emacs config file:

;; fontify code in code blocks
(setq org-src-fontify-natively t)

PS- I don't understand it's not enabled by default.

Necessitate answered 22/6, 2013 at 7:31 Comment(1)
I have that variable already set as true, and when my latex code is enclosed in #+BEGIN_SRC latex #+END_SRC latex it is properly highlighted. However I want to compile the latex code, not display it as source code.Avrilavrit
F
1

You may want to try something like this.

(font-lock-add-keywords 'org-mode
        '(("\\(\\\\cite\\)" . font-lock-keyword-face)
          ("\\[[0-9]+]" . font-lock-type-face)
          ("\\s-*[a-zA-Z]+[0-9]+[a-z]" . font-lock-constant-face)))
(font-lock-add-keywords 'org-mode
        '(("\\(\\\\citep\\)" . font-lock-keyword-face)))
(font-lock-add-keywords 'org-mode
        '(("\\(\\\\citet\\)" . font-lock-keyword-face)))
(font-lock-add-keywords 'org-mode
        '(("\\(\\\\citealp\\)" . font-lock-keyword-face)))
(font-lock-add-keywords 'org-mode
        '(("\\(\\\\citeauthor\\)" . font-lock-keyword-face)))
(font-lock-add-keywords 'org-mode
        '(("\\(\\\\citeyear\\)" . font-lock-keyword-face)))

http://www.emacswiki.org/emacs/AddKeywords http://www.emacswiki.org/emacs/RegularExpression

Flickertail answered 30/7, 2014 at 22:54 Comment(2)
if you evaluate these commands when you already have a buffer open that you want to fontify, you must do M-x revert-buffer or M-x normal-mode in order for them to take effect in the already-open buffer.Dianndianna
While this is generally how you would add fontification, this is utterly unnecessary since org-mode already allows you to do so.Signalment

© 2022 - 2024 — McMap. All rights reserved.