LaTeX中突出显示的源代码 [英] Source code highlighting in LaTeX
问题描述
我需要在LaTeX中突出显示源代码.对于大多数用例来说,listings
软件包似乎是最好的选择,对我而言,直到现在.
但是,现在我需要更多的灵活性.通常,我要查找的是一个 real 词法分析器.特别是,我需要(用于自己的语言定义)来定义(并突出显示!)自己的数字样式. listings
不允许突出显示代码中的数字.但是,我需要产生以下内容:
listings
也不能处理字符串的任意定界符.考虑以下有效的Ruby代码:
s = %q!this is a string.!
在这里,!
可以用几乎任何分隔符代替.
(listings
无法处理Unicode也很烦人,但这是另一个问题.)
理想情况下,我正在寻找listings
的扩展名,该扩展名允许我提供更复杂的词汇规则.但是除非如此,我还在寻找可行的替代方案.
其他线程建议使用 Pygments ,它可以产生LaTeX输出.甚至还有一个软件包- texments
–可以简化过渡.
但是,这非常缺乏功能.特别是,我对listings
样式的行编号,源代码行引用以及将LaTeX嵌入源代码(在listings
中的选项texcl
和mathescape
)感兴趣.
例如,这是带有listings
的源代码排版,其中显示了替换项还应提供的一些内容:
[由Bit Twiddling Hacks修改的"Sideways加法"]
我听从诺曼(Norman)的建议,整理出了一个使用(a 版本1.2 中发布了我的Pygments补丁后,我还创建了LateX软件包. >
表示记念
有魅力的 是使用Pygments在LaTeX中提供一流语法突出显示的软件包.例如,它允许以下输出.
这里是复制上述代码的最小文件(注意,包括Unicode字符可能需要XeTeX)!
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{minted}
\setsansfont{Calibri}
\setmonofont{Consolas}
\begin{document}
\renewcommand{\theFancyVerbLine}{
\sffamily\textcolor[rgb]{0.5,0.5,0.5}{\scriptsize\arabic{FancyVerbLine}}}
\begin{minted}[mathescape,
linenos,
numbersep=5pt,
gobble=2,
frame=lines,
framesep=2mm]{csharp}
string title = "This is a Unicode π in the sky"
/*
Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
of an $n$-sided regular polygon circumscribing a
circle of diameter $d$.
*/
const double pi = 3.1415926535
\end{minted}
\end{document}
可以使用以下命令来排版:
xelatex -shell-escape test.tex
(但是 minted 也可以用于latex
和pdflatex
…)
minted.sty
与texments.sty
相似,但允许附加功能.
如何获取
- 当然会包括
-
文档.
-
记念的现在由Geoffrey Poore维护.开发版本包括最新的
.sty
文件.可以从 github.com/gpoore/minted 获取,也可以从那里克隆.
再次感谢Norman激励我生产此包装.
I need to highlight source code in LaTeX. The package listings
seems to be the best choice for most use-cases and for me it was, until now.
However, now I need more flexibility. Generally, what I’m looking for is a real lexer. In particular, I need (for an own language definition) to define (and highlight!) own number styles. listings
does not allow highlighting numbers in code. However, I need to produce something like this:
listings
also cannot cope with arbitrary delimiters for strings. Consider the following valid Ruby code:
s = %q!this is a string.!
Here, !
can be replaced by almost any delimiter.
(That listings
cannot handle Unicode is also quite vexing, but that’s another issue.)
Ideally, I am looking for an extension of listings
that allows me to provide more complex lexing rules. But barring that, I am also searching for viable alternatives.
Other threads have suggested using Pygments which can produce LaTeX output. There’s even a package – texments
– to ease the transition.
However, this sorely lacks features. In particular, I am interested in listings
-style line numbering, source code line references, and the possibility of embedding LaTeX in source code (options texcl
and mathescape
in listings
).
As an example, here’s a source code typeset with listings
which shows some of the things that a replacement should also provide:
["Sideways addition" modified from Bit Twiddling Hacks]
Taking Norman’s advice to heart, I’ve hacked together a solution that used (a patched) Pygments for highlighting and pushed in as many features as possible without bursting ;-)
I’ve also created a LateX package, once my Pygments patch was released in version 1.2 …
Presenting minted
minted is a package that uses Pygments to provide top-notch syntax highlighting in LaTeX. For example, it allows the following output.
Here’s a minimal file to reproduce the above code (notice that including Unicode characters might require XeTeX)!
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{minted}
\setsansfont{Calibri}
\setmonofont{Consolas}
\begin{document}
\renewcommand{\theFancyVerbLine}{
\sffamily\textcolor[rgb]{0.5,0.5,0.5}{\scriptsize\arabic{FancyVerbLine}}}
\begin{minted}[mathescape,
linenos,
numbersep=5pt,
gobble=2,
frame=lines,
framesep=2mm]{csharp}
string title = "This is a Unicode π in the sky"
/*
Defined as $\pi=\lim_{n\to\infty}\frac{P_n}{d}$ where $P$ is the perimeter
of an $n$-sided regular polygon circumscribing a
circle of diameter $d$.
*/
const double pi = 3.1415926535
\end{minted}
\end{document}
This can be typeset using the following command:
xelatex -shell-escape test.tex
(But minted also works with latex
and pdflatex
…)
minted.sty
works similar to texments.sty
but allows additional features.
How to get it
minted is listed on CTAN (package info)
documentation is of course included.
minted is now maintained by Geoffrey Poore. The development version, including the latest
.sty
file, is available at github.com/gpoore/minted, and can be cloned from there.
Once again, thanks to Norman for motivating me to produce this package.
这篇关于LaTeX中突出显示的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!