Use Fira Code font with ligatures in code listings
Firstly, you'll need to make sure that Fira Code is installed on your computer (which I'm sure you already do).
The font features that Fira Code uses aren't actually listed as ligatures within the OTF, but seems to be fall under Contextuals. In order to use these font features, it is just a matter of enabling the appropriate contextuals:
\documentclass{article}
\usepackage{fontspec}
\setmonofont[
Contextuals={Alternate}
]{Fira Code}
\begin{document}
\begin{verbatim}
.= .- := =:=
== != === !== =/=
<<- <-- <- <-> -> --> ->>
<=< <<= <== <=> => ==> =>> >=>
>>= >>- >- -< -<< =<<
<~~ <~ ~~~ ~> ~~>
<<< << <= <> >= >> >>>
<| <|> |>
<$ <$> $>
<+ <+> +>
<* <*> *>
\\ \\\ {- -} // ///
/* /** **/ */
</ <!-- www --> />
0xF 9:45 m-x *ptr
;; ;;; :: ::: !! !!!
?? ??? %% %%% && &&&
|| ||| .. ... ..< []
-- --- ++ +++ ** ***
~= ~- -~ =~ ~@
^= ?= /= /== |= ||=
## ### ####
#{ #[ #( #? #_ #_(
a*b a*A B*b A*B *a *A a* A*
a-b a-A B-b A-B -a -A a- A-
a+b a+A B+b A+B +a +A a+ A+
a:b a:A B:b A:B :a :A a: A:
\end{verbatim}
\end{document}
Note that I'm still trying to figure out why certain substitutions work (such as .=
) but some others don't (such as .-
). I suspect it may be do -
being converted from the ASCII dash to some other dash.
Just for reference, the ligatures from Fira Code are:
The “missing” ligatures can be obtained by removing the characters verbatim
wants to treat in a special way because traditional TeX fonts have ligatures; by default, fontspec
doesn't apply Ligatures=TeX
to the monospaced font, so there's no risk in redefining \verbatim@noligs@list
to empty.
\documentclass{article}
\usepackage{fontspec}
\setmonofont[
Contextuals={Alternate}
]{Fira Code}
\makeatletter
\def\verbatim@nolig@list{}
\makeatother
\begin{document}
\begin{verbatim}
.= .- := =:=
== != === !== =/=
<<- <-- <- <-> -> --> ->>
<=< <<= <== <=> => ==> =>> >=>
>>= >>- >- -< -<< =<<
<~~ <~ ~~~ ~> ~~>
<<< << <= <> >= >> >>>
<| <|> |>
<$ <$> $>
<+ <+> +>
<* <*> *>
\\ \\\ {- -} // ///
/* /** **/ */
</ <!-- www --> />
0xF 9:45 m-x *ptr
;; ;;; :: ::: !! !!!
?? ??? %% %%% && &&&
|| ||| .. ... ..< []
-- --- ++ +++ ** ***
~= ~- -~ =~ ~@
^= ?= /= /== |= ||=
## ### ####
#{ #[ #( #? #_ #_(
a*b a*A B*b A*B *a *A a* A*
a-b a-A B-b A-B -a -A a- A-
a+b a+A B+b A+B +a +A a+ A+
a:b a:A B:b A:B :a :A a: A:
\end{verbatim}
\end{document}
It is also possible to use Fira Code in ConTeXt. However, not all ligatures seem to work there. Unfortunately I have no idea why.
\definefontfeature
[firacode-ligs]
[mode=node,
calt=yes]
\starttypescriptcollection[firacode]
\starttypescript [mono] [firacode]
\definefontsynonym [FiraCodeBold] [file:FiraCode-Bold.otf] [features=firacode-ligs]
\definefontsynonym [FiraCodeLight] [file:FiraCode-Light.otf] [features=firacode-ligs]
\definefontsynonym [FiraCodeMedium] [file:FiraCode-Medium.otf] [features=firacode-ligs]
\definefontsynonym [FiraCodeRegular] [file:FiraCode-Regular.otf] [features=firacode-ligs]
\definefontsynonym [FiraCodeRetina] [file:FiraCode-Retina.otf] [features=firacode-ligs]
\stoptypescript
\starttypescript [mono] [firacode] [name]
\setups[font:fallback:mono]
\definefontsynonym [Mono] [FiraCodeRegular]
\definefontsynonym [MonoBold] [FiraCodeBold]
\stoptypescript
\starttypescript [firacode]
\definetypeface [\typescriptone] [rm] [serif] [modern] [default] [features=default]
\definetypeface [\typescriptone] [ss] [sans] [modern] [default] [features=default]
\definetypeface [\typescriptone] [tt] [mono] [firacode] [default]
\definetypeface [\typescriptone] [mm] [math] [modern] [default] [features=default]
\stoptypescript
\stoptypescriptcollection
\setupbodyfont[firacode]
\starttext
\starttyping
.= .- := =:=
== != === !== =/=
<<- <-- <- <-> -> --> ->>
<=< <<= <== <=> => ==> =>> >=>
>>= >>- >- -< -<< =<<
<~~ <~ ~~~ ~> ~~>
<<< << <= <> >= >> >>>
<| <|> |>
<$ <$> $>
<+ <+> +>
<* <*> *>
\\ \\\ {- -} // ///
/* /** **/ */
</ <!-- www --> />
0xF 9:45 m-x *ptr
;; ;;; :: ::: !! !!!
?? ??? %% %%% && &&&
|| ||| .. ... ..< []
-- --- ++ +++ ** ***
~= ~- -~ =~ ~@
^= ?= /= /== |= ||=
## ### ####
#{ #[ #( #? #_ #_(
a*b a*A B*b A*B *a *A a* A*
a-b a-A B-b A-B -a -A a- A-
a+b a+A B+b A+B +a +A a+ A+
a:b a:A B:b A:B :a :A a: A:
\stoptyping
\stoptext