2

The $ symbol opens inline math environment, unless it is used in verbatim enviromnent.

However Vim can’t seem to detect that it’s in a verbatim environment, and as a result the dollar breaks the syntax highlighting, since Vim starts the math mode highlighting until it encounters a new dollar symbol.

Is there a way to fix this ?

2 Answers 2

6

As the other answer says, if you are writing LaTeX you should have let g:tex_flavor="latex" set in the .vimrc. With that, Vim will skip highlighting verbatim environments but it will still not recognize others like lstlisting or minted (or other custom environments).

After such an environment containing dollar signs, just add the following:

%stopzone

LaTeX will ignore this, but Vim will take this cue to reset its syntax highlighting.

Vim will still color your included listing in a strange way when there are dollar signs, but it won't have any effect on the outcome.

3
  • Unfortunately %stopzone is not supported when Vim considers the file as plaintex, see my answer. Oct 7, 2021 at 10:10
  • 2
    %stopzone must be used without space, otherwise it does not work Oct 27, 2021 at 14:04
  • 1
    @DmitryMikushin thanks - edited. Oct 27, 2021 at 15:06
5

If Vim fails to detect that the dollar sign is used in a verbatim environment, it probably means it is not using the right syntax highlighting flavour.

Use :set syntax to find out which syntax highlighting flavor is used.

The tex flavour, meant for LaTeX, should behave correctly. In addition, it features a %stopzone keyword for the cases where it should fail : inserting this keyword in the code will reset the highlighting from where it is placed.

On the other hand, the plaintex flavour, meant for TeX, and used by default when Vim can’t decide which flavour to use, doesn’t support such subtilities.

To switch to the correct syntax highlighting flavour for this file, use :set syntax=tex or add %&latex as the first line of the file.

For more permanent solutions, you can add let g:tex_flavor = "latex" in your .vimrc (not tested).

NB : Note that the dollar sign also breaks the listings package environments. See this answer for a fix and comments therein for its drawbacks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .