[Tex/LaTex] vim-spell: exclude custom LaTeX commands

spellingvim

I'm using Vim (with vim-latex) to write my LaTeX documents.

I made a bunch of custom commands to mark different kinds of "variables" (as in programming languages) I use in text, in formulas and in code listings.
I always wrap the variable names in my commands to ensure consistent text formatting and have the ability to conveniently change the format at a later point.

Usually a command—e.g. \somevariable{somename}—does not contain an orthographically correct name.
The problem is, Vim's spell checker marks all my variable names as wrong and this clutters the spell checking (e.g. when I want to step between errors).

Another example are TikZ pictures where the node names of my graph are marked as spelt wrong.

Is there a (preferably easy) way to exclude the contents (the stuff inside { and }) of custom commands from spell checking?

(I already found this question but this is not exactly what I want as it only deals with standard commands/environments and adding all my custom commands here is quite cumbersome)

Best Answer

You can do it manually by using syn match. For example, your problem with \somevariable can be solved with

syn match texSomevariable "\\somevariable{[^}]\{-}}"hs=s+14,he=e-1 containedin=texStatement contains=@NoSpell

This will disable spellcheking in between { and } after \somevariable. (It assumes that you do not use } inside the argument.)

Depending on how complicated the part of your document in which you do not want the spell checker on, it might get quite complicated to do something similar. Your example with Tikz seems to be dificult, but the solution above should work with somevariable changed to anything else.

EDIT: If you would like to have { and } highlighted as well, as per default, then you should include texDelimiter in contains, so that it becomes contains=@NoSpell,texDelimiter instead of contains=@NoSpell

EDIT: There can be problems if \somevariable matches something special, like \cite. Since \cite is highlighted with texRefZone, this should be added to containedin, so the command should be:

syn match texSomevariable "\\cite{[^}]\{-}}"hs=s+6,he=e-1 containedin=texStatement,texRefZone contains=@NoSpell