开发者

Vim: highlight latex comment using a different file type.

I am u开发者_如何学Csing 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)


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.


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜