LaTeX 中的源代码高亮显示 [英] Source code highlighting in LaTeX

查看:16
本文介绍了LaTeX 中的源代码高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 LaTeX 中突出显示源代码.listings 包似乎是大多数用例的最佳选择,对我来说,直到现在.

但是,现在我需要更大的灵活性.一般来说,我正在寻找的是一个真正的词法分析器.特别是,我需要(对于自己的语言定义)定义(并突出显示!)自己的数字样式.listings 不允许在代码中突出显示数字.但是,我需要制作这样的东西:

listings 也无法处理字符串的任意分隔符.考虑以下有效的 Ruby 代码:

s = %q!这是一个字符串.!

这里,! 几乎可以替换为 any 分隔符.

(listings 无法处理 Unicode 也很麻烦,但这是另一个问题.)

理想情况下,我正在寻找 listings 的扩展,它允许我提供更复杂的词法规则.但除此之外,我还在寻找可行的替代方案.

) Pygments 用于突出显示和推送尽可能多的功能而不会爆裂;-)

我还创建了一个 LateX 包,一旦我的 Pygments 补丁在 (包裹信息)

  • 文档当然也包括在内.

  • minted 现在由 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}
    
    egin{document}
    
    enewcommand{	heFancyVerbLine}{
      sffamily	extcolor[rgb]{0.5,0.5,0.5}{scriptsizearabic{FancyVerbLine}}}
    
    egin{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	oinfty}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

    Once again, thanks to Norman for motivating me to produce this package.

    这篇关于LaTeX 中的源代码高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆