7

I write a lot of LaTeX. In plain text mode, sequences like 123 in are highlighted in red. That is a space, a digit string, a space, and then the specific word in. Such things as a1 in are not highlighted.

Since in plain text this string has no significance that I am aware of - I would like to know what Vim thinks it is doing. And also would like to know how to stop it.

(I suppose it has something to do with highlighting groups, but I have no idea which one is involved.)

muru
  • 24,838
  • 8
  • 82
  • 143
Bruce
  • 205
  • 1
  • 6

1 Answers1

10

You can find out what syntax item applies by positioning the cursor and using something like:

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

Because that's quite a mouthful it might be useful to map that to a command:

command! SynName echo synIDattr(synID(line("."), col("."), 1), "name")

For TeX files, this gives texLength as a result. You can remove this with:

:syn clear texLength

In your vimrc, you'd put that in an autocmd, to make sure it gets loaded after the tex syntax file is loaded:

autocmd Syntax tex syn clear texLength

Also see How can I add additional syntax highlighting rules in my local vimrc? for more details on adding local syntax rules.


As for why that is highlighted, I'm not familiar with (La)TeX, but judging from the syntax file it's used for some length measurements:

" TeX Lengths:
syn match texLength "\<\d\+\([.,]\d\+\)\=\s*\(true\)\=\s*\(bp\|cc\|cm\|dd\|em\|ex\|in\|mm\|pc\|pt\|sp\)\>"

This matches any number followed by a unit measurement such as "cm", "pt", and "in" (presumably for "inch"), with optional spaces between the number and unit.

I think this may be a bug in the syntax file that it matches anywhere in the document, and that it should have contained in the definition to make it only match in some places, but like I said: I'm not familiar with TeX at all so I don't know the syntax.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
  • 2
    Unfortunately, a length can indeed appear in lots of places: "foo \hskip 1 in bar" gives "foo", then a one-inch space, then "bar". It's probably not doable to perfectly tell the "a length" and "not a length" cases apart with no context, so the syntax file errs this way. – Wander Nauta Jul 13 '22 at 09:23
  • 2
    Rather than autocmd Syntax, use ~/.vim/after/syntax/tex.vim – D. Ben Knoble Jul 13 '22 at 13:56
  • Thanks. You definitely hit the mark as to where to look. And yes, definitely a bug - since it is highlighting in normal text. In fact, it is a bit of a toss up as to whether highlighting normal text or not highlighting the lengths is the bigger problem. But, I think I will go with not highlighting the lengths - as what is a length is usually obvious to me and highlighting the plain text is jarring. – Bruce Jul 14 '22 at 00:11
  • @D.BenKnoble I am not sure what you are getting at here partly because ~/.vim/ is empty in my installation. – Bruce Jul 14 '22 at 00:15
  • @Bruce the syntax directory is similar to the ftplugin directory; it provides a nice way to separate specific settings via standard runtimepath directories. The user’s .vim directory is usually on the runtimepath (along with .vim/after) making it possible to override anything. The file grouping is just nicer than an autocommand. You would normally create any needed directories and files· – D. Ben Knoble Jul 14 '22 at 01:03
  • 1
    Using the after/ directory is the same as an autocmd @Bruce; it just splits things up a bit in different files, which some people prefer. Personally, I prefer to put everything in my vimrc file – it's just a matter of personal preference. Also see How can I add additional syntax highlighting rules in my local vimrc? for more details – Martin Tournoij Jul 14 '22 at 02:20
  • @MartinTournoij thanks. I too prefer to put it all in my vimrc file. But, it is nice to know the options. – Bruce Jul 14 '22 at 06:06