[Tex/LaTex] Vim does not recognize math environment (but handles other math fine)

editorsmath-modesyntax highlightingvim

So on one of my pc's the vim install dos not recognize math environments such as align and equation

Is there a way to turn this on?

Best Answer

Check if the syntax highlighting rules are up to date. Those should be located under /usr/share/vim/vim80/syntax/tex.vim. In this file there should be a block name Standard Math Zones:. Below that you'll find some lines stating something like:

call TexNewMathZone("A","displaymath",1)

To this block add the environments you find missing. E.g., for align use:

call TexNewMathZone("E","align",1)

If there is no starred form of the environment you want to add, use

call TexNewMathZone(suffix,name,0)

instead.

But perhaps you shouldn't make those changes system wide but on a per user basis. So create a copy of the file in /usr/share/... paste it in a folder in $HOME where vim finds it and make those changes there.

If the syntax highlighting is correct on the other machines, maybe copy the files from there to your machine where it fails to highlight correctly.

The default folder for the per-user-basis syntax highlighting would be something like ~/.vim/syntax/. If it doesn't exist, create it and everything should work.

EDIT: Perhaps a better approach is to create a short custom syntax file which sources the system wide one in the beginning and add some stuff there. The setup would then be the following file ~/.vim/syntax/tex.vim:

so $VIMRUNTIME/syntax/tex.vim

" adapt to match your system wide variable in $VIMRUNTIME/syntax/tex.vim
let s:tex_fast= "bcmMprsSvV"

if !exists("g:tex_no_math")
 call TexNewMathZone("E","align",1)
endif

if s:tex_fast =~# 'r'
  syn region texRefZone     matchgroup=texStatement start="\\autoref{"  end="}\|%stopzone\>"    contains=@texRefGroup
endif

In the above I've also added \autoref to the list of known commands for referencing stuff as an example.

I think this should be a better approach since you get bugfixes and new features from updates in your system wide file this way.

Alternatively (but with the same effects) put the file in ~/.vim/after/syntax/ and remove the first line (that's what is suggested in :help latex-syntax).