3

I have a few custom LaTeX commands like this one, for instance:

\newcommand{\oeuvre}[1]{\textit{#1}}

I use this for titles of books and other works. Sometimes I want them to be italicized, sometimes I want them to be in quotes, so I just use this command and I can change it all at once when necessary.

Now I want Vim to highlight that command and its contents as it does with \emph or \textit, and I want to disable spelling inside it.

(So in \oeuvre{whatver} "oeuvre" should be highlighted the same way as "emph" or "textit", and "whatver" should be in italics and it shouldn't count as a spelling mistake.)

Based on Vim's default TeX highlighting file, I tried this in my after/syntax/tex.vim:

syn match texTypeStyle      "\\oeuvre\>"

syn region texOeuvreStyle   matchgroup=texTypeStyle start="\\oeuvre\s*{" matchgroup=texTypeStyle  end="}" concealends contains=@texItalGroup,@NoSpell

hi texOeuvreStyle       gui=italic  cterm=italic

But it doesn't work. The command itself is highlighted properly, but not its contents, and spelling is still enabled.

So how can I get that to work?

(I know there are similar questions here, but I couldn't adapt them to my needs, I find this syntax highlighting stuff really confusing, sorry.)


Edit: Ok, I figured out that if I use syn region texItalStyle it'll work like I want it to… I'm still curious as to why I can't define my own texOeuvreStyle in what seems to me is the same way it's done for texItalStyle in the default syntax file though.

3

The problem is that the TeX syntax is hierarchical; the default texItalStyle is contained in two clusters, texFoldGroup and texItalGroup. The former cluster is contained in the top-level regions (texDocZone, texPartZone, and so on). Without that containment, your syntax rule never has a chance to match; the toplevel regions slice up your entire document, and they only allow contained members to match subsets of them.

By choosing the same syntax group for your extension, you automatically fit in. If you absolutely want to define a custom syntax group, you'd have to add them into the clusters (untested):

syntax cluster texFoldGroup add=texOeuvreStyle
syntax cluster texItalGroupGroup add=texOeuvreStyle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.