Matching interval brackets

I'm using vim to edit LaTeX code, and I'm often writing intervals like (a,b] but this messes with other vim features. For instance,

• Jumping with %: if I have something like \phi((a,b]), the final ) will match the second ( and some closing parenthesis later in the code will match the first.
• There's a feature (I'm not sure if it's specific to vimtex or just a feature of vim) where ]] closes any open delimiters, and this makes that behave wrongly - it'll try to close the ) from many lines ago.
• The tsd/csd/dsd features of vimtex which toggle/change/delete surrounding delimiters don't work (I think csd and dsd are extensions of vim-surround but I'm not sure). In particular, either they select the wrong delimiters or do nothing at all.

Currently I'm putting )s in comments to mitigate this. This solves the problem of parentheses later in the file matching wrongly, but matching in the same line doesn't work (and things like tsd from vimtex don't work properly).

Is there any better solution, or a standard solution for this kind of problem? Ideally this would deal with [a,b) also.

• This is a very difficult problem, actually. It is very hard to know how to match parantheses if the start and end paranthesis is of different type. I remember we had a discussion about this at a vimtex issue once. You could try to search the issue threads and see if you find anything, or you could create a new issue. May 23 '18 at 6:29
• @KarlYngveLervåg I think this is the relevant issue? May 23 '18 at 19:31
• Yes, that's relevant. although the topic is indentation. The point is still, as mentioned above, that it is inherently very difficult to get this right for unbalanced parantheses. May 24 '18 at 6:01
• A workaround might be to use the \DeclarePairedDelimiter command from the mathtools package to then just write like \leftopenint{a, b}. I learned about this in this question
– Jan
May 28 '19 at 7:52
• @Jan I quite like this solution! If you'd like to write it as an answer I'm happy to accept it. May 28 '19 at 17:02

1 Answer

A workaround might be to use the \DeclarePairedDelimiter command from the mathtools package to then just write like \leftopenint{a, b}. See the accepted answer to this question.

\documentclass{minimal}
\usepackage{mathtools}
\DeclarePairedDelimiter\leftopenint{(}{]}
\DeclarePairedDelimiter\rghtopenint{[}{)}

\begin{document}
\begin{equation*}
\leftopenint{-\infty, \frac{1}{N} } \cup \rghtopenint*{\frac{1}{N}, \infty}
\end{equation*}
\end{document}


which gives me what is posted below. Note that the * makes the delimiters scale.

Just noted the asymmetric appearance of the brackets in the header also breaks syntax highlighting and the commands like closing of matching environments. I could fix this with outsourcing these definitions to a separate file and then use \include...