# Fix syntax highlighting in LaTeX

I'm using vim 8.0.94. In a TeX file (specifically LaTeX), the three occurrences of  in


{}
{}

should be highlighted in the same way, but they are not. Curiously, in the second, but not the third, the $$is marked as plain text, and the$$ is marked as an error. I used to be able to find my way around syntax.vim files, but the TeX one is too complicated for me to figure out what's causing this. (I don't even know whether to look in the command section or the math-mode section.)

You can get the name of the syntax item under the cursor with:

:echo synIDattr(synID(line('.'), col('.'), 1), 'name')

In this case, it's apparently texBadMath.

Looking at /usr/share/vim/vim80/syntax/tex.vim I see there are three syn match commands for this:

if !exists("g:tex_no_math") && !s:tex_no_error
syn match texBadMath           "\\end\s*{\s*array\|gathered\|bBpvV]matrix\|split\|subequations\|smallmatrix\|xxalignat\s*}"
syn match texBadMath           "\\end\s*{\s*align\|alignat\|displaymath\|displaymath\|eqnarray\|equation\|flalign\|gather\|math\|multline\|xalignat\*\=\s*}"
syn match texBadMath           "\)]"
endif

The s:tex_no_error is set from:

if exists("g:tex_no_error") && g:tex_no_error
let s:tex_no_error= 1
else
let s:tex_no_error= 0
endif

And is documented:

" 5. If you have the variable "g:tex_no_error" defined then none of the
"    lexical error-checking will be done.
"
"    ie. let g:tex_no_error=1

So adding this to your vimrc is the easiest option. This should result in:

A more difficult route is to fix this syntax file. The line that causes this error is the last one:

syn match texBadMath          "\)]"

The \s make it a bit confusing, but a literal \ followed by a ] or ) is considered an error.

I'm not really familiar with TeX, but this should be comprativly easy to fix. You could contact the author of the file, which is listed at the top.

• Thank you! It can't always be true that \) is an error, because, for example, the first instance is highlighted just fine. I'll have a look with these suggestions in mind, and see if I can track down why that one instance is highlighted incorrectly. Dec 7 '16 at 20:41
• @LSpice The reason the first line works is because that's not inside braces ({ and }), and it looks like the textBadMath only gets applies in certain places (like inside braces). All of the last line is highlighted wrong (entire line is purple) probably because the syntax highlighter is thrown off by the (perceived) error on the second line. Dec 7 '16 at 20:57
• Do you know where to look for g:tex_no_math? That sure sounds like the place to look for why it doesn't think there should be any math …. Dec 8 '16 at 17:44
• Well, so I've had another look at the file, and, even with your great suggestions, it's still beyond me to figure out the fix. I've e-mailed the maintainer as you suggested, but, in the meantime, this seems like the best answer, so I will accept it. Dec 8 '16 at 17:58