# Proper handling of special chars in verbatim/lstlisting, .. #124

Closed
opened this Issue Sep 15, 2013 · 15 comments

### 2 participants

Vim/LaTeX-Box doesn't properly handle the verbatim or lstlisting environments, $, _ etc. are still handled as special characters and thus breaks the syntax highlighting with text outside the environment. Can these environments be handled differently, and possibly be defined which environments to handle as code snippets? Hi! This is actually not a bug with LaTeX-Box, but rather with the built-in tex syntaxt for vim. What vim version are you on, and what version of syntax/tex.vim are you using? I am on vim 7.4 patch 16, with version 79 of the tex syntax plugin. You can check the syntax file with :e$VIMRUNTIME/syntax/tex.vim. The vim version is found with :version.

closed this Sep 18, 2013

Sorry, it might've been poorly worded, I didn't mean to say this was a bug in LaTeX-Box, but rather ask if there was a posibility to define your own environments in LaTeX-Box/an idea to add such a functionality that could improve the syntax highlighted of custom environments.

I'm running my configuration on multiple machines, but for example my laptop has Vim 7.3, patch 1-547. The version of tex.vim is 73 (Apr 24, 2012). It looks like syn match texSpecialChar isn't handled for the special case that is verbatim. Even if this was added I would like to handle other environments accordling, such as lstlisting or lstinline, but not sure if this is something LaTeX-Box could/should do or not.

Does the syntax highlighting of verbatim work in your version of Vim/tex.vim?

Yes, the syntax highlighting of verbatim environments work perfectly fine with me. I have no clue why it doesn't work with you. It might be that version 73 has some bugs, because I think I have a patched version of it that should handle the verbatims. I believe that the latest version (79) should fix your problems. It can be found here.

In any case, LaTeX-Box does not (as far as I understand) treat the tex syntax highlighting.

I see. I tested now with both the newer version and my old one and it's the same. Actually it seems like verbatim works sometimes, but not when for example added to a new file it doesn't work. Reopening it and it works. Additionally it works when I replace lstlisting (listings package) with verbatim in an already made file. I can't really see the pattern here or why it would fail.

Even if I get that to work, I would still like to be able to handle custom environments such as lstlisting or lstinline like verbatim, but you don't think that's something for LaTeX-Box (new feature or not)?

Ah, you first issue is actually easy to explain: When you start an empty tex-file, the filetype is set to plaintex, and so LaTeX-Box and other tex-stuff is not loaded. You can then do set ft=tex to load tex plugins.

I do not think syntax-stuff is to be handled by LaTeX-Box, and I think that it is handled pretty well by the standard tex syntax plugin. However, if you can send me an example file with a custom environment that is NOT supported by the standard syntax plugin, then I could see if it is easy to add support. Then I might add that in the same manner that cref has been added now (see after/syntax/tex.vim in LaTeX-Box plugin folder).

reopened this Sep 19, 2013

Oh, didn't notice that. Atleast that's atleast easily solveable by autocmd BufNewFile,BufRead *.tex set ft=tex.

I looked further into tex.vim and it actually has functionality for lstlisting, but only for spelling:

" listings package:
syn region texZone start="\\begin{lstlisting}" end="\\end{lstlisting}\|%stopzone\>" contains=@Spell


However the maintainer of tex.vim, Charles Campbell, also made a plugin called lstlisting. Search for lstlisting on this page or use the direct link to the vimball (open it in vim and do :so %).

As for lstinline this is configurable using texInputFile as is used for usepackage, see this answer in Vim syntax highlighting with $and lstlisting's lstinline on Stack Overflow. Either change $VIMRUNTIME/syntax/tex.vim (search for usepackage) or add this to for example $HOME/.vim/after/syntax/tex/lstinline.vim (or if you're using Vundle; $HOME/.vim/bundle/vundle/after/syntax/tex/):

syn match texInputFile "\\lstinline\s*$$$.*$$$\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt


I just combined the two into listings.vim.

Not sure if this is something that should be added to LaTeX-Box or not though. Seeing as there is already some handling of lstlisting in tex.vim (ftplugin) I think both handling lstlisting (properly) and lstinline should be done with tex.vim (ftplugin/syntax) since the listings package is so popular anyway. I might contact Charles Campbell and suggest it.

You can choose to close this issue or not, it's working for me now (still on 7.3, but with a newer tex.vim) so it's up to you if you need more info/looking to implement this to LaTeX-Box.

I think that in principle, this should be handled by the syntax script. However, if it is a simple one-liner as you've given above, then I won't mind adding it to after/syntax/tex.vim. A pull request would be awesome, as I have a little bit much on my hands right now, else it might have to wait for some days.

It's tested and working, but sure, I'll see if I can fix a pull request for it! :-)
(I might still contact Campbell later though, because I think it should be in the syntax script).

Great. And I agree it should be in the syntax script, so please do contact Campbell.

Hi again.

I completely forgot this issue and contacting Campbell all together, sorry!
However I finally sent a mail to him, so if he agrees that support for Listings should be in the official tex.vim it will hopefully be fixed at some point in the near future (it really is a small fix).

If you're curious, I'd reckon the change will look something like this:

$diff tex.vim tex2.vim 251c251 < syn match texInputFile "\\$$epsfig\|input\|usepackage$$\s*$$$.*$$$\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt --- > syn match texInputFile "\\$$epsfig\|input\|usepackage\|lstinline$$\s*$$$.*$$$\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt 590a591,592 > syn region texZone start="\\begin{lstlisting}" end="\\end{lstlisting}\|%stopzone\>" > syn region texZone start="\\lstinputlisting" end="{\s*[a-zA-Z/.0-9_^]\+\s*}"  I'll be sure to update you/this issue if/when I get a response. Got a response from Campbell. There is in fact a reason that tex.vim does not include support for the listings package, and it can be found in the tex-package help page: 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/.

So there's that, it does not seem that it will be included in tex.vim. Especially considering this thread where it's even stated that support was at some point included, but later removed.

Personally I think that's a little sad 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.

Given the outcome, would it now be interesting to add listings support to LaTeX-Box? I'd be happy to add a pull request. Here's my working ~/.vim/../after/syntax/tex/listings.vim

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


Great! Good work. And I agree, I don't see how this could ever be a problem, and I also think it would improve the experience for most people.

I would be happy to merge a pull request!

referenced this issue Feb 9, 2014
Merged

#### Fixed #124 - support for the listings package. #157

pushed a commit that closed this issue Feb 9, 2014
 timss Fixed #124 - support for the listings package. 31b6f85
closed this in 31b6f85 Feb 9, 2014

My first pull request, seems I went a little overboard with referencing the issue(s).
Thanks for accepting it :-)

No problem! And thanks for working on this issue. Let me know if you find more issues or ideas for improving the plugin.

Will do!

referenced this issue Jun 4, 2014
Merged

#### fix syntax #174

to join this conversation on GitHub. Already have an account? Sign in to comment