Vim syntax and Latex math inside markdown
Asked Answered
B

2

19

I write documentation in markdown using ViM and I also put math using the latex $$ symbol (I compile using pandoc). The thing is that ViM syntax wouldn't ignore the underscores _ inside the dollar symbols and it is pretty annoying. For instance if I write this:

$$ a_1 = 0 $$

Then Vim will highlight all the following text as italics due to to the underscore used.

How can I change that?

Also it would be nice if I could highlight what's inside $ with a different format.

Bianchi answered 30/9, 2015 at 12:11 Comment(2)
Is it happening even if you use vim-pandoc-syntax?Flashover
Related: Syntax highlight Markdown formulas using LaTeX highlightingAllometry
Z
18

I have put these lines in my .vimrc. It works for inline math on the same line and block-mode math.

" This gets rid of the nasty _ italic bug in tpope's vim-markdown
" block $$...$$
syn region math start=/\$\$/ end=/\$\$/
" inline math
syn match math '\$[^$].\{-}\$'

" actually highlight the region we defined as "math"
hi link math Statement

Edit: I've since written a blog post called Vim syntax highlighting for Markdown, Liquid and MathJax.

Zany answered 7/1, 2016 at 1:1 Comment(4)
Thanks, works great! I've put the code in ~/.vim/after/ftplugin/markdown.vim to keep .vimrc clean.Snappy
I have to put it in ~/.vim/after/syntax/markdown.vim instead (however, errors are impossible to debug if you don't understand vim syntax commands well. The question on Vi.SE has a link to a plugin that can be used)Chub
Alternatively vim-pandoc-syntax and some other plugins also have support for Markdown syntaxChub
The blog post entry is worth a look. It is a more complete solution.Tangency
C
0

Just install https://github.com/sheerun/vim-polyglot

The latex in md file will display like this: enter image description here

Crucify answered 24/4, 2024 at 3:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.