Vim 匹配多参数 Latex 命令
Vim match multiple argument Latex commands
我想知道如何在 Vim 中匹配带有多个参数的 Latex 命令。例如,我想匹配以下形式的内容:
\command{SOME LATEX}{SOME LATEX}{SOME LATEX}
其中 'SOME LATEX' 部分可以在内部突出显示任何普通乳胶。例如,
\command{\anothercommand{a}}{\anothercommand{b}}{\command{a}{b}{c}}
我对匹配 \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)
突出显示第二组的左曲括号
如果要匹配其他的,只需要移动BuildRegex
函数中的\zs
(捕获开始)和\ze
(捕获结束)
截图
这里有一些例子:
免责声明
这只会处理 5 个级别的调用,意思是:
# This works:
\command1{\command2{\command3{\command4{\command5{Hey there}}}}}
# This doesn't works:
\command1{\command2{\command3{\command4{\command5{\command6{Hey there}}}}}}
您可以通过更改提供给 RecursiveBuild
函数的参数来增加支持级别,但在某些时候,正则表达式会太大并且 vim 不喜欢它。我个人最多可以达到8。
这是处理两个参数案例的答案,从中可以扩展到更高数量的案例。它通过使用 Vim 的隐藏功能将 \braket{a}{b}
显示为 <a|b>
。
" 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 命令不能包含在它们自身中;我找不到解决方法。
我想知道如何在 Vim 中匹配带有多个参数的 Latex 命令。例如,我想匹配以下形式的内容:
\command{SOME LATEX}{SOME LATEX}{SOME LATEX}
其中 'SOME LATEX' 部分可以在内部突出显示任何普通乳胶。例如,
\command{\anothercommand{a}}{\anothercommand{b}}{\command{a}{b}{c}}
我对匹配 \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)
突出显示第二组的左曲括号
如果要匹配其他的,只需要移动BuildRegex
函数中的\zs
(捕获开始)和\ze
(捕获结束)
截图
这里有一些例子:
免责声明
这只会处理 5 个级别的调用,意思是:
# This works:
\command1{\command2{\command3{\command4{\command5{Hey there}}}}}
# This doesn't works:
\command1{\command2{\command3{\command4{\command5{\command6{Hey there}}}}}}
您可以通过更改提供给 RecursiveBuild
函数的参数来增加支持级别,但在某些时候,正则表达式会太大并且 vim 不喜欢它。我个人最多可以达到8。
这是处理两个参数案例的答案,从中可以扩展到更高数量的案例。它通过使用 Vim 的隐藏功能将 \braket{a}{b}
显示为 <a|b>
。
" 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 命令不能包含在它们自身中;我找不到解决方法。