Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: highlight latex comment using a different file type.

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)

like image 923
sixtyfootersdude Avatar asked Mar 12 '26 15:03

sixtyfootersdude


1 Answers

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.

like image 54
Geoff Reedy Avatar answered Mar 15 '26 07:03

Geoff Reedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!