我想知道如何在Vim中匹配一个有多个参数的Latex命令。例如,我想匹配的东西的形式。
\command{SOME LATEX}{SOME LATEX}{SOME LATEX}
其中 "SOME LATEX "部分可以有任何正常的乳胶在里面高亮,例如:
\command{\anothercommand{a}}{\anothercommand{b}}{\command{a}{b}{c}}
我对匹配以下的Latex命令特别感兴趣 \command{
,每个 }{
,和最后的 }
. 在Vim中可以这样做吗,如果可以,怎么做?
好吧,这可不是件容易的事,它的复杂程度很大程度上取决于你希望它有多强......
我只是写了这些函数,它为你建立了一个正则表达式。
function! RecursiveBuild(nb)
if a:nb <= 0
return '[^}]\{-}'
endif
return '\([^{]\|{'.RecursiveBuild(a:nb-1).'}\)\{-}'
endfunction
function! BuildRegex(command, nb)
return '\\'.a:command.'\(\zs{\ze' . RecursiveBuild(5) . '}\)\{'.a:nb.'}'
endfunction
然后,你只需要运行。
:let @/ = BuildRegex('command', 2)
突出显示第二组的开端曲折括号。
如果你想配上其他的东西,你只需要将 \zs
(开始拍摄)和 \ze
采集到的 BuildRegex
功能
下面是一些例子。
这只能处理5个级别的调用,也就是说,你可以通过改变参数来增加支持的级别。
# This works:
\command1{\command2{\command3{\command4{\command5{Hey there}}}}}
# This doesn't works:
\command1{\command2{\command3{\command4{\command5{\command6{Hey there}}}}}}
你可以通过改变参数来增加支持的级别。RecursiveBuild
函数,但在某些时候,regex会太大,vim会不喜欢。我个人可以到8个。
这里是处理两个参数情况的答案,从这个答案可以扩展到更高的数组情况。它处理的是显示 \braket{a}{b}
作为 <a|b>
通过使用Vim的隐藏功能。
" This matches the '\braket' portion and displays it as '<'
syn match texStatement "\\braket\s*{\@=" conceal cchar=< contained containedin=@texmathzones
" This matches the first set of braces and hides the first, displaying the second as '|'.
syn region braketInner matchgroup=Delimiter start=+\%(\\braket\)\@<={+ end="}" contained transparent containedin=@texMathZones concealends cchar=|
" This matches the second set of braces and hides the first, displaying the second as '>'.
syn region braketInner matchgroup=Delimiter start=+\%(\\braket.*}\)\@<={+ end="}" contained transparent containedin=@texMathZones concealends cchar=>
遗憾的是,这意味着braket命令本身不能包含在其中;我找不到解决这个问题的办法。