我需要突出显示LaTeX中的源代码。包清单似乎是大多数用例的最佳选择,对我来说也是如此,直到现在。
然而,现在我需要更多的灵活性。一般来说,我要找的是一个真正的lexer。特别是,我需要(对于自己的语言定义)定义(并突出显示!)自己的数字样式。清单不允许在代码中突出显示数字。然而,我需要产生这样的东西:
清单也不能处理字符串的任意分隔符。考虑以下有效的Ruby代码:
s = %q!this is a string.!
在这里,!几乎可以被任何分隔符替换。
(清单不能处理Unicode也很烦人,但这是另一个问题。)
理想情况下,我正在寻找清单的扩展,使我能够提供更复杂的词法规则。但除此之外,我也在寻找可行的替代方案。
其他线程建议使用可以产生LaTeX输出的py军政府。甚至还有一个软件包——文本——来缓解这种转变。
然而,这严重缺乏功能。我特别感兴趣的是清单风格的行编号、源代码行引用以及在源代码中嵌入LaTeX的可能性(在清单中选择texcl和mascape)。
作为一个例子,下面是一个带有清单的源代码排版,其中显示了替换应该提供的一些东西:
[“横向添加”修改自Bit Twiddling Hacks]
TeX is (famously) Turing-complete, but I'm pretty sure you're going to have to write this extension yourself. The documentation makes it clear that the original author of listings orphaned it in 2004, and that it has not been updated since 2006. The package wasn't designed to override the formatting of numeric literals, but you might be able to modify it by changing the definition of \lst@ProcessDigit. If that doesn't work, you'll have to understand in detail how the "identifier style" options work, and you'll have to duplicate that machinery for your numeric literals.
我不太理解为什么您如此不愿意将外部工具引入到您的工具链中,但因为您是这样做的,您将不得不做额外的工作。在查看源代码之后,我希望可以修改清单,但我个人会选择对LaTeX进行预处理。
把Norman的建议牢记在心,我已经拼凑出了一个解决方案,使用(打了补丁的)py雨季来突出显示,并在不爆裂的情况下推送尽可能多的功能;-)
我还创建了一个LateX包,一旦我的pyptions补丁在版本1.2中发布…
介绍铸造
mint是一个包,它使用pyments在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
(但薄荷也适用于乳胶和pdflatex…)
铸造。麦秆的工作类似于文本。但允许附加功能。
如何得到它
在CTAN上列出了mint(包信息)
文档当然包括在内。
现在由杰弗里·普尔管理。开发版本,包括最新的.sty文件,可以在github.com/gpoore/minted上获得,并且可以从那里克隆。
再次感谢诺曼激励我制作这个包装。