Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created October 8, 2014 13:44
Show Gist options
  • Save chriseidhof/18dbc1c4eef919eab2c7 to your computer and use it in GitHub Desktop.
Save chriseidhof/18dbc1c4eef919eab2c7 to your computer and use it in GitHub Desktop.
Listings Swift config
\lstdefinelanguage{swift}
{
morekeywords={
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
}
\definecolor{keyword}{HTML}{BA2CA3}
\definecolor{string}{HTML}{D12F1B}
\definecolor{comment}{HTML}{008400}
\lstset{
language=swift,
basicstyle=\ttfamily,
showstringspaces=false, % lets spaces in strings appear as real spaces
columns=fixed,
keepspaces=true,
keywordstyle=\color{keyword},
stringstyle=\color{string},
commentstyle=\color{comment},
}
@carstenhag
Copy link

Not sure if anybody cares, but this is my setup, adding a few more keywords:

% https://gist.github.com/chriseidhof/18dbc1c4eef919eab2c7
% Swift syntax highlight definition for listings
\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{
  language=swift,
  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},
}

@sfktrifork
Copy link

sfktrifork commented Jun 1, 2019

I think you're a few keywords short: try, catch, guard

@ftraxxler
Copy link

ftraxxler commented Mar 31, 2020

"Test message: \(message)" why is the variable also the string color can someone help me pls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment