Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim syntax highlighting with $ and lstlisting's lstinline

If I use the inline version of lstlisting as shown:

\lstinline{!$omp parallel for}

the syntax highlighting in vim goes wrong, and the remainder of the latex file is coloured red as if it is all part of the code listing. It's the dollar $ which causes the problem. How can I avoid this?

like image 696
user2023370 Avatar asked Jul 18 '11 20:07

user2023370


4 Answers

Let's finally solve this issue once and for all!

I mailed Charles E. Campbell, the maintainer of tex.vim, suggesting he'd add highlighting rules for the listings package. However it turns out lacking support for the listings package in tex.vim is actually intentional. The reasoning can be found :h tex-package. In short, you're supposed to create your own extended syntax highlighting rules (see bottom of post):

Tex: Want To Highlight More Commands?

LaTeX is a programmable language, and so there are thousands of packages full of specialized LaTeX commands, syntax, and fonts. If you're using such a package you'll often wish that the distributed syntax/tex.vim would support it. However, clearly this is impractical. So please consider using the techniques in mysyntaxfile-add to extend or modify the highlighting provided by syntax/tex.vim. Please consider uploading any extensions that you write, which typically would go in $HOME/after/syntax/tex/[pkgname].vim, to http://vim.sf.net/.

Personally I think it's a little unfortunate that it's not going to be included, as it surely increases the threshold for the average user to write his or hers LaTeX using Vim. Finding and adding syntax highlighting for lstlisting, lstinline etc. isn't too easy. It does not look like it's going to change anytime soon either when looking at this thread.

DevSolar seems to have already found it (and I appreciate the credit!), but Campbell offers a couple of example LaTeX package support vimballs. The first one, lstlisting.vba.gz, includes highlighting rules for lstlisting and lstinputlisting. It does however lack lstinline, which this topic is about.

Finally, here's my listings.vim resided in $HOME/.vim/after/syntax/tex/

syn region texZone start="\\begin{lstlisting}" end="\\end{lstlisting}\|%stopzone\>"
syn region texZone  start="\\lstinputlisting" end="{\s*[a-zA-Z/.0-9_^]\+\s*}"
syn match texInputFile "\\lstinline\s*\(\[.*\]\)\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt

-

Proper highlighting of verbatim, lstlisting and lstinline environments.

This seems to be the preferred solution. It does not require one to alter system wide files in /usr/share/vim/.., you don't have to download and source a vimball or alter environments rather than fixing the syntax highlighting itself. I might into look into releasing this as a simple plugin to make it more accessible.

Lastly, remember to check that you're actually running the tex filetype and not plaintex which lacks far too much to be viable. Already posted this in a comment above, but some more information can be found in a ticket I added to LaTeX-Box.

like image 82
timss Avatar answered Nov 19 '22 22:11

timss


The initial Problem seemed to me, that the rest of the tex-File is shown with messed-up syntax highlighting. So maybe the easy and practicable Solution could be to use a

%stopzone

after the lstlisting-Region.

like image 7
Frank Scherrer Avatar answered Nov 20 '22 00:11

Frank Scherrer


This isn’t really a bug – it’s by design. In order to highlight this correctly, Vim would have to parse and interpret the whole TeX document up until the point where this code occurs, since TeX is a context-sensitive language. This is both too complex and too time-consuming for a syntax highlighting plugin.

The only acceptable fix would be to rewrite the syntax highlighter from scratch, using a complete implementation of TeX that emits meta information for each token in the source code. This is a huge project. As far as I know, no currently available TeX implementation gives such information, which means that one would really have to write this oneself.

like image 6
Konrad Rudolph Avatar answered Nov 20 '22 00:11

Konrad Rudolph


This works for me:

\lstinline[mathescape]{!$\$$omp parallel for}

As far as I understand, it typesets the dollar symbol in math mode, but I couldn't see any visible difference.

like image 4
Jonas Due Vesterheden Avatar answered Nov 19 '22 23:11

Jonas Due Vesterheden