如何在 LaTeX 中为与正则表达式匹配的线条着色
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你只想要简单的差异,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.
使用 sed、awk、Python [选择的语言] 等脚本工具生成 Latex 源代码。
Use a scripting tool such as sed, awk, Python [lang of choice]to produce the Latex source code.