Latex 和列表:突出显示代码的某些部分
我使用 Latex 和 listings
包在文档中显示一些 C++ 代码(带有语法突出显示)。
我需要突出显示代码的某些部分。这个特定的突出显示与语法突出显示无关,它只是我想突出显示的代码的某些部分,以便读者可以关注它。
我希望我可以制作一些变量名称,例如以粗体显示并在黄色背景上。这是我用 MSWord 做的事情,我想用 Latex 重现(当然,不是红色和绿色下划线):
我还没有找到使用 listings
包来做到这一点的方法。是否可以?
I'm using Latex and the listings
package to display some C++ code (with syntax highlighting) inside a document.
I need to highlight some parts of the code. This specific highlight has nothing to do with syntax highlighting, it's just some parts of the code I'd like to highlight so that the reader can focus on it.
I wish I could make some variable name, for instance, displaying in bold, and on a yellow background. Here is something I did with MSWord I'd like to reproduce with Latex (of course, not the red and green underlining):
I haven't found a way to do it with the listings
package. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是 @Konrad 的方法确实逃脱了完全来自
listings
格式。如果突出显示的文本包含任何程序关键字,那么这些关键字将不会被listings
包突出显示,因为它们将位于非突出显示的行上。理想的情况可能是使用\highlight{\lstinline{...}}
,但似乎您无法在listings\lstinline
宏代码>环境。TeX StackExchange 站点有本质上是相同的问题< /a>,带有 基于 tikz 包的好答案和 进一步细化。它覆盖突出显示,同时仍然让
listings
自动格式化突出显示的文本。这整个基于
tikz
的方法 有了自己的生命,最终导致良好、强大的解决方案。这是目前(对我来说)最著名的方法,用于突出显示选定的列表行,同时保持自动语法突出显示。实际上,它对于在给定页面上的任何位置对之间描边突出显示效果一样好。Unfortunately @Konrad's approach truly escapes from
listings
formatting entirely. If the highlighted text contains any program keywords, then those keywords will not be highlighted by thelistings
package as they would be on non-highlighted lines. The ideal might be to use\highlight{\lstinline{...}}
, but it seems you cannot nest a\lstinline
macro inside alistings
environment.The TeX StackExchange site has essentially the same question, with a nice answer based on the tikz package and a further refinement thereof. It overlays highlighting while still letting
listings
automatically format highlighted text.This whole
tikz
-based approach took on a life of its own, eventually leading to a good, robust solution. That is currently the best known (to me) approach to highlighting selectedlistings
lines while keeping automatic syntax highlighting. It will actually work just as well for stroking highlights between any pair of locations on a given page.您可以在
listings
区域内启用任意 LaTeX 命令:\highlight
是您的突出显示宏(您需要自己编写)。有关更多详细信息,请参阅列表文档第 4.14 节(“转义为 LaTeX”)。请注意,您现在需要转义代码中所有其他出现的特殊字符 - 因此对于 C++ 代码,{}
可能是一个糟糕的选择。You can enable arbitrary LaTeX commands inside your
listings
region:\highlight
is your highlighting macro (you need to write it yourself). See the listings documentation, section 4.14 (“Escaping to LaTeX”) for further details. Notice that you now need to escape every other occurrence of the special characters in your code – so for C++ code,{}
is probably a bad choice.在C或C++代码中,我认为字符`可以自由地作为转义字符。
然后,您可以像使用
单词
exception
一样使用它,然后成为 exception。 BTW,字符 ` 是 Markdown 格式字符代表代码,因此在 C 或 C++ 中使用它非常容易,就像代码列表一样。
In C or C++ code, I think the character ` is free to be the escape character.
then, you can use it like
the word
exception
then become exception. BTW, character ` is the Markdown formatcharacter for code, so its very easy to use it in C or C++ like code listing.