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?
4 Answers
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.
-
1This does not do highlighting for inline math, such as $v\in V$. Is there any way to achieve such highlighting? Commented Aug 16, 2017 at 14:42
-
@AlwaysLearning I do observe that $v \in V$ gets highlighted. Which Org mode version are you using? Commented Aug 16, 2017 at 19:20
-
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? Commented Aug 17, 2017 at 6:02 -
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. Commented Aug 17, 2017 at 8:14
-
I am having this problem even when opening an
.org
file withemacs -q
and then evaluating(setq org-highlight-latex-and-related '(latex script entities))
. Commented Aug 17, 2017 at 15:35
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...):
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.
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.
-
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. Commented Jun 24, 2013 at 22:20
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
-
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.– MarkCommented Jun 5, 2015 at 20:46
-
While this is generally how you would add fontification, this is utterly unnecessary since org-mode already allows you to do so. Commented Mar 27, 2020 at 14:03
org-mode
buffer, to highlight all the embedded latex code (for instance\alpha
,S^3
,\dots
).C-c '
not enough while editing a LaTeX block? Do you want to see it highlighted at all times?