Vim 语法高亮使用 $ 和 lstlisting 的 lstinline

发布于 2024-11-25 01:37:20 字数 194 浏览 1 评论 0原文

如果我使用 lstlisting 的内联版本,如图所示:

\lstinline{!$omp parallel for}

vim 中的语法突出显示会出错,并且 Latex 文件的其余部分会被涂成红色,就好像它是代码清单的所有部分一样。导致问题的是美元 $。我怎样才能避免这种情况?

If I use the inline version of lstlisting as shown:

\lstinline{!$omp parallel for}

the syntax highlighting in vim goes wrong, and the remainder of the latex file is coloured red as if it is all part of the code listing. It's the dollar $ which causes the problem. How can I avoid this?

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

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

发布评论

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

评论(8

我早已燃尽 2024-12-02 01:37:20

让我们最终一劳永逸地解决这个问题!

我给 tex.vim 的维护者 Charles E. Campbell 发了邮件,建议他为列表包添加突出显示规则。然而事实证明,在 tex.vim 中缺乏对列表包的支持实际上是故意的。推理可以参见:h tex-package。简而言之,您应该创建自己的扩展语法突出显示规则(请参阅帖子底部):

Tex:想要突出显示更多命令吗?

LaTeX 是一种可编程语言,因此有数千个完整的包
专门的 LaTeX 命令、语法和字体。如果你正在使用这样的
您经常希望分布式语法/tex.vim 支持的包
它。然而,显然这是不切实际的。所以请考虑使用
mysyntaxfile-add 中的技术用于扩展或修改提供的突出显示
通过语法/tex.vim。请考虑上传您编写的任何扩展,
通常会放在 $HOME/after/syntax/tex/[pkgname].vim 中,以
http://vim.sf.net/

就我个人而言,我认为它不会被包括在内有点不幸,因为它肯定会增加普通用户使用 Vim 编写他或她的 LaTeX 的门槛。查找并添加 lstlistinglstinline 等的语法突出显示并不容易。当查看 线程时,它看起来不会很快发生变化。

DevSolar 似乎已经找到了它(我很感谢您的认可!),但是 Campbell 提供了一些 LaTeX 示例包支持 vimballs。第一个 lstlisting.vba.gz 包含 lstlisting 的突出显示规则lstinputlisting。但它确实缺少本主题所涉及的 lstinline

最后,这是我的 listings.vim 位于 $HOME/.vim/after/syntax/tex/

syn region texZone start="\\begin{lstlisting}" end="\\end{lstlisting}\|%stopzone\>"
syn region texZone  start="\\lstinputlisting" end="{\s*[a-zA-Z/.0-9_^]\+\s*}"
syn match texInputFile "\\lstinline\s*\(\[.*\]\)\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt

-

正确突出显示逐字、lstlisting 和 lstinline 环境。

这似乎是首选解决方案。它不需要更改 /usr/share/vim/.. 中的系统范围文件,您不必下载和获取 vimball 或更改环境,而不是修复语法突出显示本身。我可能会考虑将其作为一个简单的插件发布,以使其更易于访问。

最后,请记住检查您是否确实运行的是 tex 文件类型,而不是 plaintex,后者缺乏太多功能而无法实现。已经在上面的评论中发布了此内容,但是可以在我添加到 LaTeX 的 票证中找到更多信息-框

Let's finally solve this issue once and for all!

I mailed Charles E. Campbell, the maintainer of tex.vim, suggesting he'd add highlighting rules for the listings package. However it turns out lacking support for the listings package in tex.vim is actually intentional. The reasoning can be found :h tex-package. In short, you're supposed to create your own extended syntax highlighting rules (see bottom of post):

Tex: Want To Highlight More Commands?

LaTeX is a programmable language, and so there are thousands of packages full
of specialized LaTeX commands, syntax, and fonts. If you're using such a
package you'll often wish that the distributed syntax/tex.vim would support
it. However, clearly this is impractical. So please consider using the
techniques in mysyntaxfile-add to extend or modify the highlighting provided
by syntax/tex.vim. Please consider uploading any extensions that you write,
which typically would go in $HOME/after/syntax/tex/[pkgname].vim, to
http://vim.sf.net/.

Personally I think it's a little unfortunate that it's not going to be included, as it surely increases the threshold for the average user to write his or hers LaTeX using Vim. Finding and adding syntax highlighting for lstlisting, lstinline etc. isn't too easy. It does not look like it's going to change anytime soon either when looking at this thread.

DevSolar seems to have already found it (and I appreciate the credit!), but Campbell offers a couple of example LaTeX package support vimballs. The first one, lstlisting.vba.gz, includes highlighting rules for lstlisting and lstinputlisting. It does however lack lstinline, which this topic is about.

Finally, here's my listings.vim resided in $HOME/.vim/after/syntax/tex/

syn region texZone start="\\begin{lstlisting}" end="\\end{lstlisting}\|%stopzone\>"
syn region texZone  start="\\lstinputlisting" end="{\s*[a-zA-Z/.0-9_^]\+\s*}"
syn match texInputFile "\\lstinline\s*\(\[.*\]\)\={.\{-}}" contains=texStatement,texInputCurlies,texInputFileOpt

-

Proper highlighting of verbatim, lstlisting and lstinline environments.

This seems to be the preferred solution. It does not require one to alter system wide files in /usr/share/vim/.., you don't have to download and source a vimball or alter environments rather than fixing the syntax highlighting itself. I might into look into releasing this as a simple plugin to make it more accessible.

Lastly, remember to check that you're actually running the tex filetype and not plaintex which lacks far too much to be viable. Already posted this in a comment above, but some more information can be found in a ticket I added to LaTeX-Box.

み青杉依旧 2024-12-02 01:37:20

在我看来,最初的问题是,tex 文件的其余部分显示为混乱的语法突出显示。因此,也许简单实用的解决方案可能是

%stopzone

在 lstlisting-Region 之后使用 a 。

The initial Problem seemed to me, that the rest of the tex-File is shown with messed-up syntax highlighting. So maybe the easy and practicable Solution could be to use a

%stopzone

after the lstlisting-Region.

瀞厅☆埖开 2024-12-02 01:37:20

这并不是真正的错误——这是设计使然。为了正确突出显示这一点,Vim 必须解析并解释整个 TeX 文档,直到出现此代码为止,因为 TeX 是一种上下文相关语言。对于语法突出显示插件来说,这太复杂且太耗时。

唯一可接受的修复方法是使用 TeX 的完整实现从头开始重写语法荧光笔,该 TeX 为源代码中的每个标记发出元信息。这是一个巨大的项目。据我所知,目前没有可用的 TeX 实现提供此类信息,这意味着人们实际上必须自己编写此信息。

This isn’t really a bug – it’s by design. In order to highlight this correctly, Vim would have to parse and interpret the whole TeX document up until the point where this code occurs, since TeX is a context-sensitive language. This is both too complex and too time-consuming for a syntax highlighting plugin.

The only acceptable fix would be to rewrite the syntax highlighter from scratch, using a complete implementation of TeX that emits meta information for each token in the source code. This is a huge project. As far as I know, no currently available TeX implementation gives such information, which means that one would really have to write this oneself.

花海 2024-12-02 01:37:20

这对我有用:

\lstinline[mathescape]{!$\$omp parallel for}

据我了解,它在数学模式下排版美元符号,但我看不到任何明显的差异。

This works for me:

\lstinline[mathescape]{!$\$omp parallel for}

As far as I understand, it typesets the dollar symbol in math mode, but I couldn't see any visible difference.

我的影子我的梦 2024-12-02 01:37:20

我在 tex 文件中插入 knitr 的 R 命令时遇到了类似的问题。我在 \rinline{} 中有很多 $ 和其他特殊字符。我修复了它,以便 Vim 忽略此命令中的文本。

您想要编辑 tex.vim 文件,在我的发行版上它是 sudo vim /usr/share/vim/vim73/syntax/tex.vim

在大约。第 228 行:

syn match texInputFile      "\\\(epsfig\|input\|usepackage\)\s*\(\[.*\]\)\={.\{-}}"     contains=texStatement,texInputCurlies,texInputFileOpt

更改为:

syn match texInputFile      "\\\(epsfig\|input\|usepackage\|rinline\)\s*\(\[.*\]\)\={.\{-}}"        contains=texStatement,texInputCurlies,texInputFileOpt

... 或 lstinline

syn match texInputFile      "\\\(epsfig\|input\|usepackage\|lstinline\)\s*\(\[.*\]\)\={.\{-}}"      contains=texStatement,texInputCurlies,texInputFileOpt

I had a similar problem with inserting R commands for knitr in tex files. I had a lot of $ and other special characters within \rinline{}. And I fixed it so that Vim ignores text within this command.

You want to edit tex.vim file, on my distribution it is sudo vim /usr/share/vim/vim73/syntax/tex.vim

At ca. line line 228:

syn match texInputFile      "\\\(epsfig\|input\|usepackage\)\s*\(\[.*\]\)\={.\{-}}"     contains=texStatement,texInputCurlies,texInputFileOpt

Change to:

syn match texInputFile      "\\\(epsfig\|input\|usepackage\|rinline\)\s*\(\[.*\]\)\={.\{-}}"        contains=texStatement,texInputCurlies,texInputFileOpt

... or for lstinline:

syn match texInputFile      "\\\(epsfig\|input\|usepackage\|lstinline\)\s*\(\[.*\]\)\={.\{-}}"      contains=texStatement,texInputCurlies,texInputFileOpt
凡间太子 2024-12-02 01:37:20

根据 timss' 的答案,创建一个名为 ~/.vim/after/syntax/tex/listings 的文件。 tex 并添加以下规则。将 YOURENV 替换为您想要突出显示的环境!

syn region texZone start="\\begin{YOURENV}" end="\\end{YOURENV}\|%stopzone\>"

现在,当您下次使用现有的 .tex 文件打开 Vim 时,应该会自动加载此规则。

屏幕截图添加规则之前和之后的突出显示

Based on timss' answer, create a file named ~/.vim/after/syntax/tex/listings.tex and add the following rule. Replace YOURENV with the environment you want to make the highlighting exception for!

syn region texZone start="\\begin{YOURENV}" end="\\end{YOURENV}\|%stopzone\>"

This rule should now automatically be loaded next time you open Vim with an existing .tex file.

screenshot of the highlighting before and after adding the rule

坏尐絯℡ 2024-12-02 01:37:20

我的没有这个问题我正在运行 Vim 7.3.46,文件类型=tex。 (我的默认为 .tex 文件上的“plaintex”;您可能想使用 :set ft 检查文件类型?)

I don't have this issue. I am running Vim 7.3.46 with filetype=tex. (Mine defaults to "plaintex" on .tex files; you might want to check filetype with :set ft?)

想你只要分分秒秒 2024-12-02 01:37:20

浏览 timss 在 user1174052 的答案上发布的链接,我发现 Dr Chip 的 Vim 页面 ,它提供了 lstlisting.vba.gz Vimball 插件。下载,在 Vim 中打开...

vim lstlisting.vba.gz

...执行 vimball...

:so %

...下次打开 LaTeX 文件时,语法高亮会完全忽略 lstlisting 环境的内容。

感谢用户 timss,他发布了 LaTeX-Box 票证的链接,同时他还发布了 Chip 博士页面的链接。我添加这个答案只是作为“直接链接”快捷方式。

Going through the link posted by timss on user1174052's answer, I found Dr Chip's Vim Page, which provides the lstlisting.vba.gz Vimball plugin. Download, open in Vim...

vim lstlisting.vba.gz

...execute the vimball...

:so %

...and the next time you open a LaTeX file, contents of lstlisting environments are completely ignored by the syntax highlighting.

Credit goes to user timss, who posted the link to the LaTeX-Box ticket, where he also posted the link to Dr Chip's page. I added this answer merely as a "direct link" shortcut.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文