2

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]$},
            ]
            \addplot+[ycomb] plot coordinates
            {
                (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 2

2

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}
            \addplot+[ycomb] plot coordinates
            {
                (1,1)
                (2,2)
                (3,1)
            };
        \end{axis}
    \end{tikzpicture}
    \caption{Example figure}
\end{figure}
\end{document}

For more information about this, see the pgfplots manual, section 4.3.

2
  • It is however interesting that with the %-key Vim does correctly jump to the matching bracket. I also note that I had to encapsulate the mathmode label in curly braces. Hence, if these curly braces would be acknowlegded by Vim's syntax as something special might be a solution. If you can elaborate on this thought, please do.
    – apfel
    May 2, 2016 at 9:42
  • 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. May 2, 2016 at 18:47
0

If you use syntax/tex.vim v96 (available at http://www.drchip.org/astronaut/vim/index.html#SYNTAX_TEX) then the errant highlighting is fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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