vim 中的 LaTeX 部分突出显示

发布于 2024-12-03 19:51:16 字数 828 浏览 3 评论 0原文

在 LaTeX 中,一个部分看起来像:

\section{Section Title}

我想突出显示这些部分或部分标题。我尝试将以下内容放入 ~/.vim/bundle/latexrc/after/syntax/tex.vim 中:(

syn match texSectioning "\\section\>" skipwhite nextgroup=texSectioningTitle
syn region texSectioningTitle       contained matchgroup=Delimiter start='{'    end='}' contains=@texSectioningGroup
syn cluster texSectioningGroup      contains=texMatcher,texComment,texDelimiter

请注意,默认的 tex.vim 不处理这种语法 语法文件。它只定义了“部分区域”,这对我来说几乎毫无价值。)

然后我在我的配色方案中定义了以下内容:

hi texSectioning gui=bold guifg=red

什么也没有发生;也就是说,部分标题在我的 LaTeX 代码中不会显示为红色(即使在我完全重新加载文件之后)。

我对 vim 的语法如何工作以及如何调试它完全感到困惑。

编辑 更多信息:它有时有效,有时无效。 完全不可预测。可能是什么问题?病原?还有别的事吗?我完全困惑了。

In LaTeX, a section looks like:

\section{Section Title}

I would like to highlight such sections, or section titles. I tried to put the following in ~/.vim/bundle/latexrc/after/syntax/tex.vim:

syn match texSectioning "\\section\>" skipwhite nextgroup=texSectioningTitle
syn region texSectioningTitle       contained matchgroup=Delimiter start='{'    end='}' contains=@texSectioningGroup
syn cluster texSectioningGroup      contains=texMatcher,texComment,texDelimiter

(Note that this kind of syntax is not handled by the default tex.vim syntax file. It only defines "section zones", which are pretty much worthless for me.)

I then define the following in my color scheme:

hi texSectioning gui=bold guifg=red

And nothing happens; that is, section titles do not appear in red in my LaTeX code (even after I reloaded the file completely).

I am totally confused as to how vim's syntax work, and how to debug it.

Edit
Some more information: it sometimes works and sometimes not. Completely unpredictable. What could be the problem? Pathogen? Something else? I'm completely puzzled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-12-10 19:51:16

您已经定义了新的语法项 texSectioningtexSectioningTitletexSectioningGroup,但尚未将它们链接到突出显示组,因此 Vim 不会知道如何显示它们。尝试添加这些行:

hi def link texSectioning Statement
hi def link texSectioningTitle String
hi def link texSectioningGroup Comment

StatementStringComment 颜色由您正在使用的颜色方案定义。这些只是示例:您可以将它们替换为在 colorscheme 文件中定义的任何组。

You have defined the new syntax items texSectioning, texSectioningTitle and texSectioningGroup, but you have not linked them to a highlighting group, so Vim doesn't know how to display them. Try adding these lines:

hi def link texSectioning Statement
hi def link texSectioningTitle String
hi def link texSectioningGroup Comment

The Statement, String and Comment colourings are defining by the colourscheme you are using. These are just examples: you can replace them with any group defined in the colourscheme file.

泪痕残 2024-12-10 19:51:16

答案如下:tex.vim 将文本划分为多个区域,其中的语法必须显式允许。关键元素是该命令:

syn cluster texChapterGroup contains=@texSectioningGroup

这对 vim 来说,在 texChapterGroup 内,允许使用语法簇 texSectioningGroup 。接下来要做的就是照常定义该集群。

另一个细节是区域 texSectioningTitle 必须被包含,否则它将匹配 LaTeX 中的任意对 {}

所以一个完整的解决方案是这样的:

syn match texSectioningCommand '\\section\>' skipwhite     nextgroup=texSectioningTitle contains=@texSectioningGroup
syn region texSectioningTitle        start='{'  end='}' contained
syn cluster texSectioningGroup contains=texSectioningCommand
syn cluster texChapterGroup contains=@texSectioningGroup

编辑 这就是为什么行为显然是不可预测的:vim 不会读取整个文件来找出语法。因此,在足够大的章节中,我的节语法将起作用,因为 vim 没有走得足够远,无法看到它位于章节区域中。

Here is the answer: the tex.vim divides the text in zones, in which the syntax must be explicitly allowed. The key element is that command:

syn cluster texChapterGroup contains=@texSectioningGroup

This says to vim that inside a texChapterGroup, the syntax cluster texSectioningGroup is allowed. The next thing to do is simply to define that cluster as usual.

Another detail is that the region texSectioningTitle must be contained, otherwise it will match arbitrary pairs of {} in LaTeX.

So a complete solution goes like this:

syn match texSectioningCommand '\\section\>' skipwhite     nextgroup=texSectioningTitle contains=@texSectioningGroup
syn region texSectioningTitle        start='{'  end='}' contained
syn cluster texSectioningGroup contains=texSectioningCommand
syn cluster texChapterGroup contains=@texSectioningGroup

Edit Here is why the behaviour was apparently unpredictable: vim does not read the entire file to figure out the syntax. So in a big enough chapter, my section syntax would work because vim did not go far enough to see it was in a chapter zone.

暮年 2024-12-10 19:51:16

只是为了更新信息以轻松突出显示部分。使用 containsin 意味着所有其他语法匹配都包含此新语法匹配。然后只需定义您想要的颜色即可。

syn match texSectioningCommand '\\section\>' containedin=ALLBUT,texComment
hi texSectioningCommand guifg=#ec5f67 ctermfg=203

或者,可以将简单的新语法匹配添加到 texFoldGroup 中,以便在块文档内进行评估。

syn match texSectioningCommand '\\section\>'
syn cluster texFoldGroup add=texSectioningCommand
hi texSectioningCommand guifg=#ec5f67 ctermfg=203

Just to update the information to highlight section easily. Using containedin means that all the other syntax matches contains this new syntax match. Then just define the color you want.

syn match texSectioningCommand '\\section\>' containedin=ALLBUT,texComment
hi texSectioningCommand guifg=#ec5f67 ctermfg=203

Alternatively, a simple new syntax match could be added to the texFoldGroup in order to be evaluated inside the the block document.

syn match texSectioningCommand '\\section\>'
syn cluster texFoldGroup add=texSectioningCommand
hi texSectioningCommand guifg=#ec5f67 ctermfg=203
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文