17

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.

sharp
  • 171
  • 1
  • 4
  • 1
    Is it happening even if you use [vim-pandoc-syntax](https://github.com/vim-pandoc/vim-pandoc-syntax/)? – ryuichiro Sep 30 '15 at 15:23
  • Related: [Syntax highlight Markdown formulas using LaTeX highlighting](https://vi.stackexchange.com/q/16376/5324) – Tom Hale Jun 02 '18 at 09:47

1 Answers1

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.

Atcold
  • 683
  • 2
  • 10
  • 31
Scott
  • 2,568
  • 1
  • 27
  • 39
  • 2
    Thanks, works great! I've put the code in ~/.vim/after/ftplugin/markdown.vim to keep .vimrc clean. – Erwin411 Feb 22 '17 at 21:10
  • 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](https://vi.stackexchange.com/q/16376) has a link to a plugin that can be used) – user202729 Aug 29 '21 at 04:39
  • Alternatively `vim-pandoc-syntax` and some other plugins also have support for Markdown syntax – user202729 Aug 29 '21 at 06:38
  • The blog post entry is worth a look. It is a more complete solution. – Seth Aug 01 '23 at 16:42