# Enhance Latex Syntax without Rewriting everything

I want to extend the latex syntax for the given file:

\documentclass[a4paper,11pt]{book}
\begin{document}
\begin{figure}[t]
\begin{tikzpicture}
\node[alter] at (0,0) (v1) {typ0};
\end{tikzpicture}
\end{figure}
\end{document}


Right now, the name 'v1' gets spell-checked. I want to remove spell checking for it. Thus, I tried adding several commands to the .vim/ftplugin/tex_mappings.vim file. However, everything I can come up would require me to add a lot more additional rules than I want to. E.g. the line

syn region texRefZone       matchgroup=texStatement start="\\node" end=";\|%stopzone\>"


stops spell checking, but highlights everything from '[' to ';' in the 'Reference' color, which I don't want, plus it also disables spell checking for the stuff inside the curly brackets (typ0), but I want (the default) spell checking there. What is a nice way to disable spell checking for 'v1' without messing up everything else on this line? Thanks!

I run Arch Linux and vim version 'version 8.2.1704'. My .vimrc is

syntax enable
let g:tex_flavor = 'latex'

• Custom syntax should really go in ~/.vim/after/syntax/; then it happens after the usual syntax. Nov 7, 2020 at 13:09
• Thank you for the comment, but this still does not really help me for fixing my problem. Nov 7, 2020 at 19:02
• I think large part of the issue you've been having is that syntax rules depend on the order in which they were defined (last one matches stronger), so by redefining some of them you're probably shadowing some important ones that come earlier... It's unfortunate to have to redefine the syntax as a whole, but that's probably your best bet... See answer about vimtex, if you open an issue about it you might be able to get your requests in there. (If you're doing TeX or LaTeX, I strongly recommend you adopt vimtex!) Nov 9, 2020 at 7:52
• Thank you, but vimtex does slow down my whole vim experience a lot, but maybe I give it another try now. Nov 12, 2020 at 18:36

To extend the syntax for the tex filetype, you should specify your customizations in ~/.vim/after/syntax/tex.vim (or .config/nvim/after/syntax/tex.vim for neovim).

The rules that you are looking for are quite complex, because they need to define nested regions. You would need to add the contains= keyword, something like this:

syntax region texRefZone
\ matchgroup=texStatement
\ start="\\node\>" end=";\|%stopzone\>"
\ contains=@NoSpell,...


I'm currently working on a major PR for vimtex where I fully adopt syntax highlighting as an native feature. vimtex already has some minor improved support for Tikz pictures. When the PR is merged, I would be willing to consider adding more advanced support for the type of commands you're interested in; feel free to open a new issue/feature request.

• Thank you, I will add a new PR later. However, I still don't like using vimtex, because it makes editing latex files so slow. But thank you nevertheless. Nov 12, 2020 at 18:35
• I do not find vimtex slow myself, and I'm more than willing to look into such issues if you can give me concrete "minimal" examples and descriptions of what you are experiencing. If so, please open an issue on the Github repo. Nov 13, 2020 at 18:20