[Tex/LaTex] Use Fira Code font with ligatures in code listings

ligaturesxetex

I would like to use the Fira Code OTF font for code listings and verbatim blocks, which has beautiful ligatures. The font loads correctly using the following line:

\setmonofont{FiraCode-Regular}

But I cannot get the ligatures to work, no matter how many combinations of [Ligatures={X}] or [RawFeatures={X}] I try.

Is there any way to make them work with xelatex?

Best Answer

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}

output

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:

reference

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}

enter image description here

Related Question