如何在 LaTeX 中为与正则表达式匹配的线条着色

发布于 2024-08-16 15:53:54 字数 289 浏览 1 评论 0原文

我目前正在使用 LaTeX 列表包来显示代码块以及文件与先前版本的差异。

这两个块都按列表着色,就好像它们是代码(它们确实是代码)一样,但我想对差异进行着色,类似于 emacs diff-mode - 红色表示匹配 ^- 的行,绿色表示 ^\+ 等。

有谁知道是否有一个包可以实现这一点,列表是否可以做到这一点,或者是否可以编写一个 LaTeX 命令来做到这一点? (或者更确切地说 - 在哪里可以找到有关后者的良好信息来源?)

谢谢,

Hud

I'm currently using the LaTeX listings package to display a block of code, and a diff of the file against a previous version.

Both blocks are coloured by listings as if they are code (which they are) but I would like to colour the diff similarly to emacs diff-mode - red for lines matching ^-, green for ^\+ etc.

Does anyone know if there is a package to achieve this, whether listings can do it, or whether it is possible to write a LaTeX command to do it? (or rather - where a good source of information on the latter can be found?)

Thanks,

Hud

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

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

发布评论

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

评论(2

所谓喜欢 2024-08-23 15:53:54

如果你只想要简单的差异,Pygments 支持它们,所以 textments (pygments 的乳胶前端)可以满足您的需求。

但我想你想要的是让差异有颜色,同时适当地突出显示底层代码的语法。一般来说,您不能以通常的方式正确执行此操作,因为语法突出显示可能取决于前一行的状态,并且使用 udiffs 前一行可能会丢失,或者插入的行可能位于已删除的行之后,&c 。

为了做正确的事情,您需要对旧版本和新版本进行语法突出显示,然后将突出显示的版本混在一起以获得正确的输出。相当多的工作,而且我还没有听说有人这样做过。

您还可以尝试简单地修改语言的常用语法突出显示器,删除涉及多行状态的突出显示规则,以及使用 udiff 标记将规则插入到颜色行。比照。 Pygments' 编写您自己的词法分析器;你想要从 diff 中得到什么是比较棘手的,因为你想要突出显示颜色,所以你不能只将这些行放入 GenericTokens 中;我不知道这样做的正确方法是什么。

If you just want unadorned diffs, Pygments supports them, so texments (a latex front-end for pygments) does what you want.

But I guess that what you want is to have diff's coloured, while having the syntax of the underlying code highlighted appropriately. This you can't do properly the usual way, in general, because syntax highlighting may depend on the state from the previous line, and with udiffs the previous line may be missing, or an inserted line might follow a deleted line, &c.

To do the right thing, you'd need to syntax highlight the old and new versions, and then scramble the highlighted versions together to get the right output. Quite a bit of work, and I've not heard of anyone who's done that.

You could also try simply modifying the usual syntax highlighter for a language, removing highlighting rules that involve multiline state, and inserting rules to colour lines with udiff markup. Cf. Pygments' Write your own lexer; what you want from diff is trickier, since you want what is coloured to be highlighted, so you can't just make the lines into GenericTokens; I don't know what the right way to do this is.

野却迷人 2024-08-23 15:53:54

使用 sed、awk、Python [选择的语言] 等脚本工具生成 Latex 源代码。

Use a scripting tool such as sed, awk, Python [lang of choice]to produce the Latex source code.

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