# Concealing the code for braket package for LaTeX with vimtex + tex-conceal

I am using vimtex with latexmk and using conceal setting provided by vim and extending it with an additional plug-in : KeitaNakamura/tex-conceal.vim. Additionally, I use a package braket to encode \langle, \rangle and | nicely.

The braket package comes with commands which are used like :

1. \bra{A} ( it will print into pdf as <A| and I want to conceal in my tex file so that it appears like : <A|)

2. \ket{A} ( it will print into pdf as |A> and I want to conceal in my tex file so that it appears like : |A>)

3. \bracket{A|B} ( it will print into pdf as <A|B> and I want to conceal in my tex file so that it appears like : <A|B>)

My aim is to use conceal feature of vim on the commands that come with the braket package which is not available by default or by the plug-in I am using.

Short of asking the developer of the plug-in to include support for braket package is there any other method by which I can achieve this? Writing some custom code or using some other plugin?

You can do something like the following:

" ~/.vim/after/syntax/tex.vim
syntax match bra /\\bra{\%([^}]*}\)\@=/ conceal cchar=<
syntax match bra /\%(\\bra{[^}]*\)\@<=}/ conceal cchar=|
syntax match ket /\\ket{\%([^}]*}\)\@=/ conceal cchar=|
syntax match ket /\%(\\ket{[^}]*\)\@<=}/ conceal cchar=>
syntax match braket /\\braket{\%([^}]*}\)\@=/ conceal cchar=<
syntax match braket /\%(\\braket{[^}]*\)\@<=}/ conceal cchar=>


If you want to keep the highlight group when not concealing, you’ll need to get the highlight group for commands like \langle (texStatement, I believe) and replace bra, ket, and braket with it.

For the curious, the weapons of choice here are

• \%(\) non-capturing groups (they're slightly more performant)
• \@= matches the preceding atom with zero-width and doesn't consume the match1
• \@<= matches the preceding atom with zero-width only if what follows also matches and doesn't consume the match1

1. We need this "non-consuming" behavior—\zs and \ze could be used to build the right patterns, but because we are creating overlaps we need to not consume anything. \zs and \ze will consume the matches they make which prevents vim's syntax engine from examining that text again.

We can't use region because we need two different conceal characters (one on each end of the region). This is not supported natively, so workarounds like double-matches are required.

• Sorry but this didn't work for me! – Nitin Apr 30 at 17:27
• This failed as well. Actually the command from braket package is \bra{%} with some expression in place of % and in compiled file it is displayed as : <%|. I am having concealing feature with langle whether I use this command or not and if I use the same command for \bra{} this doesn't work. Here's what I tried : syntax match braket /\\bra/ conceal cchar=< and a few variations (I didn't put any braces afterbra in this command and hence didn't do the same in my tex file as well.) – Nitin May 1 at 5:48
• @Nitin please edit your question to include this—it’s hard to answer a question that is underspecified, and I’m still not sure I follow. Please given some example usages and how you would like them to appear on screen. – D. Ben Knoble May 1 at 13:25
• @Nitin please try the update – D. Ben Knoble May 1 at 14:49
• Sorry, this didn't work either. Maybe I am doing something wrong. If you can kindly suggest some reference for this then I will read and try to do something. I tried :h syntax and it did give me an idea but not sufficient, perhaps. Thanks a lot for trying so many times. :) – Nitin May 1 at 23:12