Swift 的 LaTeX 代码高亮显示

Code highlighting in LaTeX for Swift

我找到了一些带有列表的解决方案或将源代码放入我的 LaTeX 文档中,但我希望突出显示 Swift 代码。有什么提示吗?

看看 listings 包。

虽然我不确定 Swift 是否已经是支持语言的一部分,但添加您自己的语言定义并不难。 参见例如this example.

Swift 开箱即用 minted:

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{minted}{swift}
    let x = 42
    println("Hello, \(x)!")
\end{minted}
\end{document}

xelatex -shell-escape x 产生

但是请注意,这需要安装 Pygments 2。

我最近使用了以下 listings 配置(注意:代码未被 Whosebug 正确着色,但它是正确的 LaTeX 片段):

% Swift syntax highlight definition for listings
% Source: https://gist.github.com/chriseidhof/18dbc1c4eef919eab2c7

\usepackage{xcolor}
\lstdefinelanguage{swift}
{
  morekeywords={
    open,catch,@escaping,nil,throws,func,if,then,else,for,in,while,do,switch,case,default,where,break,continue,fallthrough,return,
    typealias,struct,class,enum,protocol,var,func,let,get,set,willSet,didSet,inout,init,deinit,extension,
    subscript,prefix,operator,infix,postfix,precedence,associativity,left,right,none,convenience,dynamic,
    final,lazy,mutating,nonmutating,optional,override,required,static,unowned,safe,weak,internal,
    private,public,is,as,self,unsafe,dynamicType,true,false,nil,Type,Protocol,
  },
  morecomment=[l]{//}, % l is for line comment
  morecomment=[s]{/*}{*/}, % s is for start and end delimiter
  morestring=[b]", % defines that strings are enclosed in double quotes
  breaklines=true,
  escapeinside={\%*}{*)},
  numbers=left,
  captionpos=b,
  breakatwhitespace=true,
  basicstyle=\linespread{1.0}\ttfamily\footnotesize, % https://tex.stackexchange.com/a/102728/129441
}

\definecolor{keyword}{HTML}{BA2CA3}
\definecolor{string}{HTML}{D12F1B}
\definecolor{comment}{HTML}{008400}

\lstset{
explpreset={},
  language=swift,
  inputencoding=utf8x,
extendedchars=\true,
  basicstyle=\ttfamily\small,
  showstringspaces=false, % lets spaces in strings appear as real spaces
  columns=fixed,
  keepspaces=true,
  keywordstyle=\color{keyword},
  stringstyle=\color{string},
  commentstyle=\color{comment}
}