1

When editing a .tex file, is it possible to change the syntax highlighting after a file is loaded so that @ is able to be recognized as part of a command? Normally, this is done at load-time when .sty, .cls, etc. is the extension, but there are times when .tex files have "special" syntax in which it would be nice to behave as though .sty were the extension.

I have been using the trick of making a .sty file that is a symbolic link to the .tex file, but I am wondering if there is another way, such as through a modeline.

3
  • 1
    @VivianDeSmedt Please do not edit posts in such a way that it changes the meaning of the question. The "after the file is loaded" part was important! Commented Mar 26 at 17:11
  • 1
    sorry for that. I tried to clarify the question but obviously I had the wrong understanding. Commented Mar 26 at 17:28
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the solutions using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Mar 30 at 17:28

2 Answers 2

2

You have the option to make all your TeX file to use the style highlighting by setting the g:tex_stylish:

let g:tex_stylish = 1 

More information with:

Otherwise you could add ~/.vim/syntax/tex.vim syntax that checks if the first line of your document contains a special string (e.g.: style)

~/.vim/syntax/tex.vim:

if getline(1) =~ 'style' let b:tex_stylish=1 endif 

If you document starts with:

% This document is a style 

it will be highlighted as a style (as it would have a .sty extension)

2
  • The "g:tex_stylish = 1" suggestion removes the error-checking, which is useful. I had not thought of the "if getline(1) =~ 'style'" suggestion---that would work, though only for me (not for anyone else opening the file). I like it, though. Commented Mar 26 at 17:16
  • Internally the syntax use the b:tex_stylish buffer variable that is set looking at the file extension. I see no other way than having something local to your configuration. The modeline only set options that have no impact to this aspect of the syntax :-| Commented Mar 26 at 17:30
1

A solution to how to enable it for files that are already open (which is, say, 1/3 of the original question) is to do something like this:

let b:tex_stylish=1 | set ft=tex 

or perhaps

let b:tex_stylish=1 | syntax on 

This effectively sets the variable b:tex_stylish and then reloads the file. As mentioned in other answers, doing it automatically on a file-by-file basis is not trivial.

1
  • 1
    filetype detect is the canonical way to reload a filetype script, I believe. Commented Mar 28 at 12:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.