3

I use the latex-suite vim plugin, which shows me errors inline in my vim editor. However, I get false positives for certain environments like lstlistings. How can I configure latex-suite to ignore these environments?

1 Answer 1

3
+50

The way I would go about doing this is to make the lstlisting environment (and any other environments which also have this problem) have the same syntax highlighting as the verbatim environment: a single colored block, with special TeX symbols like $ and % treated like ordinary text.

This means that you can accomplish this without worrying about latex-suite at all: latex-suite doesn't change your syntax highlighting. The first step is to make sure your .tex files are syntax highlighted, which you may have done already. If not, put the following in your .vimrc:

filetype on
au BufRead,BufNewFile *.tex,*.sty,*.cls set filetype=tex
au FileType tex syntax on

It turns out that for vim's LaTeX syntax highlighter, a verbatim environment defines a texZone region, so all we need to do is ensure that the lstlisting environment also defines a texZone region:

au filetype tex syntax region texZone start='\\begin{lstlisting}' end='\\end{lstlisting}'

This incantation means "whenever we're working with a TeX file, append to the texZone definition the region between \begin{lstlisting} and \end{lstlisting}."

Then, reload your file, and verbatim and lstlisting environments will look the same.

enter image description here

And of course you can replace lstlisting with other environments that have this problem.

Caveats

  • If you use a non-default syntax highlighting language definition for TeX, this may not work. (Different colorschemes are okay.) In that case, though, you can still determine what syntax item the verbatim environment represents by typing :echom synIDattr(synID(line("."), col("."),1),"name") while in command mode in a verbatim environment, and then use the returned syntax group instead of texZone.
  • This solution does not handle the optional environment argument to lstlistings; since this is LaTeX code rather than part of the listing, it would be nice for the syntax highlighting to reflect that. I do not know how to do that, and it might become a difficult parsing/regex problem (since it could contain nested brackets).
  • Apparently, the vim consensus is that latex-suite is not a good plugin; this link recommends a few alternatives.
4
  • Can this also apply to \lstlisting{}? Jan 13, 2016 at 19:55
  • @RaffiKhatchadourian Can \lstlisting be used as a command instead of an environment? I've never heard of that, couldn't get it to work just now when I tried it, and couldn't find it anywhere in the documentation. Jan 13, 2016 at 20:31
  • 1
    Sorry, I meant \lstinline{}. Jan 13, 2016 at 20:35
  • I think you can, but it would be a regex that I don't know how to make: like \verb, \lstinline can accept many different characters as delimiters (e.g. \lstinline_foo_ or \lstinline+foo+), and I don't know how to make a vim regex with arbitrary delimiters. Jan 14, 2016 at 0:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.