LaTeX for Swift中的代码突出显示

时间:2015-08-17 11:16:51

标签: swift latex syntax-highlighting

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

3 个答案:

答案 0 :(得分:6)

Swift开箱即用:

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{minted}{swift}
    let x = 42
    println("Hello, \(x)!")
\end{minted}
\end{document}
带有xelatex -shell-escape x

生成

screenshot

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

答案 1 :(得分:0)

查看listings包。

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

答案 2 :(得分:0)

我最近使用了以下listings配置(注意:该代码未被StackOverflow正确着色,但它是正确的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}
}