It’s a bit simpler than that.
The problem was that Fira code doesn’t have a fl
character. If you used a font that did have it, XeTeX or LuaTeX can handle it just fine, but the \fira
font you were defining didn’t help you because you selected the same font as before, which still did not have the character.
By default, TeX silently logs this somewhere in the middle of your .log
file. You can get it to at least print a warning message to the console by giving it the command \tracinglostchars=2
. That should really be the default.
Therefore, there are two solutions. You can map the character to Fira Code’s fl
ligature with
\newunicodechar{fi}{fl}
That substitues fl
, which gives you the font’s double-width fl
ligature so long as Ligatures=TeX
is active (which is the default). It will copy-and-paste from the PDF as fl
rather than fl
.
You could also load another font family that does have this character as a ligature and whose metrics are reasonably compatible: Fira Sans, which is already loaded as \sffamily
, will work This will give you the Unicode fl
character in your output, which might be what you want in a verbatim source listing.
\documentclass{article}
\tracinglostchars=2
\usepackage{newunicodechar}
\usepackage{fontspec}
\usepackage{ctex}
\usepackage{FiraSans}
\setCJKmainfont{ipaexm.ttf}
\setmonofont{Fira Code}
\newunicodechar{fi}{{\sffamily fi}}
%% To format the MWE for TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}
\begin{document}
\texttt{This is a ligature: fi. This is not a ligature: fl fi.}
\texttt{These are ligatures in Fira Code: -> =>}
\end{document}
I took the liberty of updating XeCJK
to ctex
.
The Fira Mono font would also work. This version gets you a monospace fl
even if you use it outside of \texttt
.
\documentclass{article}
\tracinglostchars=2
\usepackage{newunicodechar}
\usepackage{fontspec}
\usepackage{ctex}
\usepackage{FiraSans}
\setCJKmainfont{ipaexm.ttf}
\setmonofont{Fira Code}
\newfontfamily\firamono{Fira Mono}[Ligatures={Common,Discretionary,TeX}]
\newunicodechar{fi}{{\firamono fi}}
%% To format the MWE for TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}
\begin{document}
\texttt{This is a ligature: fi. This is not a ligature: fl fi.}
\texttt{These are ligatures in Fira Code: -> =>}
\end{document}