1

With this raw text:

This is a ligature: fi This is not a ligature: fi.

I would like to get the same output in LaTeX, but I get the following. However you notice it works perfectly in tex.stackexchange. You can see the ligature.

enter image description here

My goal is to declare all the symbols that LaTeX cannot display and tweak the font to make them appear:

\documentclass{article}
\usepackage{newunicodechar}
\usepackage{fontspec}
\usepackage{xeCJK}
\usepackage{FiraSans}
\setCJKmainfont{ipaexm.ttf}
\setmonofont{Fira Code}

\newfontfamily{\fira}{FiraCode-Regular}[Contextuals=Alternate]
\newunicodechar{fi}{{\fira fi fi}}

\begin{document}
\texttt{This is a ligature: fi. This is not a ligature: fi.}
\end{document}

Unfortunately I haven't find any way of doing it...

1 Answer 1

3

It’s a bit simpler than that.

The problem was that Fira code doesn’t have a 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 .

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 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}

Fira Code sample

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}

Fira Code sample

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .