Vim 语法高亮使用 $ 和 lstlisting 的 lstinline
如果我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
让我们最终一劳永逸地解决这个问题!
我给
tex.vim
的维护者 Charles E. Campbell 发了邮件,建议他为列表包添加突出显示规则。然而事实证明,在 tex.vim 中缺乏对列表包的支持实际上是故意的。推理可以参见:h tex-package
。简而言之,您应该创建自己的扩展语法突出显示规则(请参阅帖子底部):就我个人而言,我认为它不会被包括在内有点不幸,因为它肯定会增加普通用户使用 Vim 编写他或她的 LaTeX 的门槛。查找并添加
lstlisting
、lstinline
等的语法突出显示并不容易。当查看 此 线程时,它看起来不会很快发生变化。DevSolar 似乎已经找到了它(我很感谢您的认可!),但是 Campbell 提供了一些 LaTeX 示例包支持 vimballs。第一个 lstlisting.vba.gz 包含
lstlisting 的突出显示规则
和lstinputlisting
。但它确实缺少本主题所涉及的lstinline
。最后,这是我的
listings.vim
位于$HOME/.vim/after/syntax/tex/
-
这似乎是首选解决方案。它不需要更改
/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 intex.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):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
andlstinputlisting
. It does however lacklstinline
, which this topic is about.Finally, here's my
listings.vim
resided in$HOME/.vim/after/syntax/tex/
-
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 notplaintex
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.在我看来,最初的问题是,tex 文件的其余部分显示为混乱的语法突出显示。因此,也许简单实用的解决方案可能是
在 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
after the lstlisting-Region.
这并不是真正的错误——这是设计使然。为了正确突出显示这一点,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.
这对我有用:
据我了解,它在数学模式下排版美元符号,但我看不到任何明显的差异。
This works for me:
As far as I understand, it typesets the dollar symbol in math mode, but I couldn't see any visible difference.
我在 tex 文件中插入 knitr 的 R 命令时遇到了类似的问题。我在
\rinline{}
中有很多$
和其他特殊字符。我修复了它,以便 Vim 忽略此命令中的文本。您想要编辑 tex.vim 文件,在我的发行版上它是
sudo vim /usr/share/vim/vim73/syntax/tex.vim
在大约。第 228 行:
更改为:
... 或
lstinline
: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:
Change to:
... or for
lstinline
:根据 timss' 的答案,创建一个名为
~/.vim/after/syntax/tex/listings 的文件。 tex
并添加以下规则。将YOURENV
替换为您想要突出显示的环境!现在,当您下次使用现有的
.tex
文件打开 Vim 时,应该会自动加载此规则。Based on timss' answer, create a file named
~/.vim/after/syntax/tex/listings.tex
and add the following rule. ReplaceYOURENV
with the environment you want to make the highlighting exception for!This rule should now automatically be loaded next time you open Vim with an existing
.tex
file.我的没有这个问题我正在运行 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?)
浏览 timss 在 user1174052 的答案上发布的链接,我发现 Dr Chip 的 Vim 页面 ,它提供了 lstlisting.vba.gz Vimball 插件。下载,在 Vim 中打开...
...执行 vimball...
...下次打开 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...
...execute the vimball...
...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.