views:

71

answers:

4

I am using vim to write latex. I would like to highlight the latex comments using a different file type. (For example I would like to highlight the latex comments using c++ formatting).

Is there a way to do this?


(Edit)

Example:

\section{Introduction}

% This is a comment.  I would like to higlight comments using the 
% syntax highlighting from c++ files (so that keywords are higlighted)

bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ...

(Note: the end goal is not to use c++ higlighting but this makes the example more straight forward)

A: 

Take a look at :h contained. It looks like you could set something up in a custom syn file that highlights C++ keywords only if they're inside a comment.

:syntax keyword Test    int   contained
:syntax match   Comment "^%"  contains=Test

Does that help?

Kristo
@Kristo, I already have a syntax file that has that in it. I want to use it to highlight latex comments.
sixtyfootersdude
@sixtyfootersdude, `tex.vim` has `texCommentGroup` that contains keywords under the title `texTodo`. You'd have to add another `syn keyword` entry for the C++ keywords and add it to the `contains` list for the group. Then you can add your own highlighting for the new keywords.
Kristo
Ok that is great but is there some way that I can set it to be an entire syntax file?
sixtyfootersdude
@sixtyfootersdude, you can put it in a file in `.vim/after/syntax`. See http://stackoverflow.com/questions/3149958/vim-highlight-latex-comment-using-a-differnt-file-type/3159611#3159611 (Geoff's answer) for details.
Kristo
A: 

This isn't an exact solution, but it might be adaptable to what your doing. Haskell can be programmed in a "literate" style, literally mixing LaTeX and Haskell code in one file. This is then separated when compiled. There is a vim plugin that will highlight the LaTeX and Haskell portions individually.

rcollyer
A: 

You may need to check if you have Latex syntax file installed on your computer or not. If not, you have to install the syntax file.

If you are not satisfied with the syntax definition, you may google for the syntax file and installed. However, I normally don't replace any syntax file from web. What I do is to load the new syntax file only for my account's VIM.

I have some blogs on VIM. Not sure this one would help.

David.Chu.ca
+2  A: 

This is actually pretty easy to do, just create ~/.vim/after/syntax/plaintex.vim with the content:

let s:saved_syntax = b:current_syntax
unlet b:current_syntax

syntax include @Cpp syntax/cpp.vim

syntax match cppInComment /.*/ contained containedin=initexComment contains=@Cpp transparent

let b:current_syntax = s:saved_syntax

and ~/.vim/after/syntax/tex.vim with:

let s:saved_syntax = b:current_syntax
unlet b:current_syntax

syntax include @Cpp syntax/cpp.vim

syntax match cppInComment /.*/ contained containedin=texComment contains=@Cpp transparent

let b:current_syntax = s:saved_syntax

This includes c++ syntax as a sub-syntax of the TeX syntax and just says that C++ code should be highlighted within comments.

Geoff Reedy