# How do I get Vim to correctly highlight Math mode in LaTeX command options?

I adopted the bracket notation, e.g., x[i], for the discrete signal x where i is the time index. The problem occurs in a pgfplots figure but I figured, the syntax highlighting behavior is due to being math mode in a LaTeX command option, i.e., \begin{command}[option] ... \end{command}.

Here is a MWE originally named demonstration.tex which compiles correctly:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
Here, I show you a not so very interesting graph
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel=$i$,
ylabel={$x[i]$},
]
{
(1,1)
(2,2)
(3,1)
};
\end{axis}
\end{tikzpicture}
\caption{Example figure}
\end{figure}
\end{document}


The erroneous syntax highlighting occurs in the line with ylabel={$x[i]$}. There, the closing bracket ] is matched with the opening [ two lines above. Then everything after the closing $ is displayed as if in math mode. I assume the issue can be resolved by allowing math mode in command option mode (if these are the right terms) but I wasn't able to find or comprehend the right spot in the syntax file. ## 2 Answers To achieve what you ask is, I believe, quite difficult. I'm the author of vimtex, and I've added some custom syntax highlighting, see vimtex/after/syntax/tex.vim. As you might notice, the syntax highlighting syntax can become quite complex, and I think in this particular case, it will be very nontrivial. Therefore, instead of a direct answer to what you ask, I will instead suggest a workaround that I think should work well: Instead of adding the plot settings as an optional argument to the axis environment, you can add them with \pgfplotsset, \documentclass{article} \usepackage{pgfplots} \pgfplotsset{compat=1.12} \begin{document} Here, I show you a not so very interesting graph \begin{figure} \centering \begin{tikzpicture} \pgfplotsset{ xlabel=$i$, ylabel={$x[i]\$},
}
\begin{axis}
{
(1,1)
(2,2)
(3,1)
};
\end{axis}
\end{tikzpicture}
\caption{Example figure}
\end{figure}
\end{document}


• Yes, but the % key is easier; it relies on the searchpair functions, which allows to find nested pairs of delimiters. This functionality is not available for syntax pattern matching (as far as I know). Note: I do not claim that it is impossible to solve this, I only say that it is difficult, and that I, for instance, don't want to spend time trying to solve it, since the gain is not that high compared to the (expected) amount of work. – Karl Yngve Lervåg May 2 '16 at 18:47