[Tex/LaTex] fontenc vs fontspec with XeLaTeX

font-encodingsfontsfontspecxetex

I have encountered something I fail to understand. I have read that I should use fontspec and not fontenc when I use XeLaTeX, but this causes some problems for me when I use the cochineal font package.

When I use the following code:

\documentclass[a4paper, english]{article}

\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage{fontspec}
\usepackage[p,osf]{cochineal} 

\title{Some title}

\begin{document}
    \maketitle
    some text
\end{document}

I get the message in the log that Font shape TU/Cochineal-OsF/m/n undefined(Font) using TU/lmr/m/n instead and the font for the title defaults ot Latin Modern. But when I instead use:

\documentclass[a4paper, english]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[p,osf]{cochineal} 

\title{Some title}

\begin{document}
    \maketitle
    some text
\end{document}

I don't get that message and the title is in the Cochineal font. Am I setting something wrong when I use fontspec? Is there some drawback with using fontenc together with XeLaTex? I would really like to get Cochineal also in the title.

I'm using MikTex on a Windows 10 machine.

All the best,
Richard

Best Answer

The cochineal package is only for pdflatex. If you have installed the OpenType version of Cochineal as a system font you can emulate the package as follows

\documentclass[a4paper]{article}

\usepackage{fontspec}
\usepackage{polyglossia}

\setmainfont{Cochineal}[
  Numbers={Proportional,OldStyle},
]
\setmainlanguage{english}

\DeclareRobustCommand{\lfstyle}{\addfontfeatures{Numbers=Lining}}
\DeclareTextFontCommand{\textlf}{\lfstyle}
\DeclareRobustCommand{\tlfstyle}{\addfontfeatures{Numbers={Tabular,Lining}}}
\DeclareTextFontCommand{\texttlf}{\tlfstyle}

\title{Some title}

\begin{document}

\maketitle

some text 1234567890\par
some text {\lfstyle 1234567890} \textlf{1234567890}\par
some text {\tlfstyle 1234567890} \texttlf{1234567890}\par

\end{document}

enter image description here

If, for some reasons, you only rely on the TeX Live distribution, the font setup is slightly different.

\documentclass[a4paper]{article}

\usepackage{fontspec}
\usepackage{polyglossia}

\setmainfont{Cochineal}[
  Extension=.otf,
  UprightFont=*-Roman,
  ItalicFont=*-Italic,
  BoldFont=*-Bold,
  BoldItalicFont=*-BoldItalic,
  Numbers={Proportional,OldStyle},
]
\setmainlanguage{english}

\DeclareRobustCommand{\lfstyle}{\addfontfeatures{Numbers=Lining}}
\DeclareTextFontCommand{\textlf}{\lfstyle}
\DeclareRobustCommand{\tlfstyle}{\addfontfeatures{Numbers={Tabular,Lining}}}
\DeclareTextFontCommand{\texttlf}{\tlfstyle}

\title{Some title}

\begin{document}

\maketitle

some text 1234567890\par
some text {\lfstyle 1234567890} \textlf{1234567890}\par
some text {\tlfstyle 1234567890} \texttlf{1234567890}\par

\end{document}