用于各种语言代码语法高亮显示的 LaTeX 包
我正在寻找一个可以在代码上进行语法突出显示的 LaTeX 包。 例如,现在我使用逐字块来编写代码:
\begin{verbatim}
<html>
<head>
<title>Hello</title>
</head>
<body>Hello</body>
</html>
\end{verbatim}
这可以很好地在我的文档上显示代码。 但是,假设我想像 IDE 在输出文档中那样突出显示 HTML 标记? 是否有一个包可以提供帮助?
我想对各种语言(例如 Java、C#、HTML、CSS 等)执行相同的操作。
I am looking for a LaTeX package that does syntax highlighting on code. For example, right now I use the verbatim block to write code:
\begin{verbatim}
<html>
<head>
<title>Hello</title>
</head>
<body>Hello</body>
</html>
\end{verbatim}
And this works fine to display the code on my document. But, suppose I wanted to highlight the HTML markup the way an IDE would in the output document? Is there a package that could help?
I would like to do the same for various languages such as Java, C#, HTML, CSS and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 listings 包。 它支持许多不同的语言,并且有很多用于自定义输出的选项。
You can use the listings package. It supports many different languages and there are lots of options for customising the output.
在提出类似问题后,我创建了另一个使用 Pygments 的包,并提供了相当的功能比
textments
多了一些选项。 它被称为minted
并且相当稳定且可用。只是为了炫耀一下,这里有一段用 minted 突出显示的代码:
After asking a similar question I’ve created another package which uses Pygments, and offers quite a few more options than
texments
. It’s calledminted
and is quite stable and usable.Just to show it off, here’s a code highlighted with minted:
我推荐 Pygments。 它接受任何语言的一段代码并输出语法突出显示的 LaTeX 代码。 它使用 fancyvrb 和 color 包来生成其输出。 我个人更喜欢它而不是列表包。 我认为 fancyvrb 会产生更漂亮的结果。
I recommend Pygments. It accepts a piece of code in any language and outputs syntax highlighted LaTeX code. It uses fancyvrb and color packages to produce its output. I personally prefer it to the listing package. I think fancyvrb creates much prettier results.
我会使用开发者 Konrad 提到的 minted 包 Rudolph 而不是列表包。 原因如下:
列表包
默认情况下,列表包不支持颜色。 要使用颜色,您需要包含颜色包并使用 \lstset 命令自行定义颜色规则,如 matlab 代码所述 此处。
另外,列表包不能很好地与 unicode 配合使用,但您可以按照解释解决这些问题 此处和此处。
以下代码
生成以下图像:
minted 包
minted 包支持颜色、unicode,看起来棒极了。 但是,为了使用它,您需要有 python 2.6 和 pygments。 在 Ubuntu 中,你可以在终端中检查你的 python 版本,
然后你可以安装 pygments 然后
,因为 minted 调用 pygments,你需要像这样用 -shell-escape 编译它
如果你使用像 TexMaker 这样的乳胶编辑器或者其他东西,我建议添加一个用户命令,这样你仍然可以在编辑器中编译它。
以下代码
生成以下图像:
I would use the minted package as mentioned from the developer Konrad Rudolph instead of the listing package. Here is why:
listing package
The listing package does not support colors by default. To use colors you would need to include the color package and define color-rules by yourself with the \lstset command as explained for matlab code here.
Also, the listing package doesn't work well with unicode, but you can fix those problems as explained here and here.
The following code
produces the following image:
minted package
The minted package supports colors, unicode and looks awesome. However, in order to use it, you need to have python 2.6 and pygments. In Ubuntu, you can check your python version in the terminal with
and you can install pygments with
Then, since minted makes calls to pygments, you need to compile it with -shell-escape like this
If you use a latex editor like TexMaker or something, I would recommend to add a user-command, so that you can still compile it in the editor.
The following code
produces the following image:
LGrind 就是这样做的。 它是一个成熟的 LaTeX 包,从 adam 是个牛仔时就已经存在了,并且支持多种编程语言。
LGrind does this. It's a mature LaTeX package that's been around since adam was a cowboy and has support for many programming languages.
我主要在论文中使用 lstlistings,但对于彩色输出(用于幻灯片),我使用 pygments 代替。
I mostly use lstlistings in papers, but for coloured output (for slides) I use pygments instead.
另一个专门用于此目的的 LaTeX 包是
highlightlatex
。无论如何,可以在 CTAN 的此页面< /a>.
Another LaTeX package dedicated to this purpose is
highlightlatex
.Anyway, an up to date list of packages for code displaying and syntax highlighting can be found on this page of CTAN.
我建议根据以下 tex 代码定义您自己的包; 这给了你完全的自由。 http://ubuntuforums.org/archive/index.php/t-331602。 html
I would suggest defining your own package based on the following tex code; this gives you complete freedom. http://ubuntuforums.org/archive/index.php/t-331602.html