[Tex/LaTex] Are there cases where fontenc + luatex (or xetex) cause problems

fontsluatexxetex

According to the luatex docs, you shouldn't use fontenc with luatex. People swear up and down that fontenc is incompatible, but I haven't been able to find an example where loading the package causes problems.
(I'm curious about this because it can be easier to load the same base set of packages for pdftex/luatex/xetex, and add fontspec only when handling the latter two.)

I know fontenc is not the right way to deal with fonts in luatex or xelatex, but I'm specifically looking for cases where it's detrimental to load the fontenc package. Do you know of any?

Best Answer

fontenc is loaded by fontspec (you can check this in the log). So in itself the package is not a problem.

But fontenc is a special package: You can load it more than once with different options without getting option clash errors. It will then load font encoding definitions for all the options. E.g.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[LGR]{fontenc}
\usepackage[T2A]{fontenc}
\begin{document}
\encodingdefault, \makeatletter \f@encoding\makeatother

\end{document}

will load t1enc.def, lgrenc.def and t2enc.def.

This also is not problematic with lualatex and xelatex.

But fontenc will also set the last encoding option as the encoding default. And quite a number of encodings are not suitable for lualatex and xelatex. These engines need the TU encoding (fontspec sets this encoding). Other encodings can lead to quite wrong outputs:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[LGR]{fontenc}
\usepackage[T2A]{fontenc}
\usepackage{fontspec}
\setmainfont{DejaVuSans}
\begin{document}
\encodingdefault, \makeatletter \f@encoding\makeatother

Grüße, αβγ, Ҍҋ

{\fontencoding{T1}\selectfont
 Grüße, αβγ, Ҍҋ}

{\fontencoding{LGR}\selectfont
 Grüße, αβγ, Ҍҋ}

{\fontencoding{T2A}\selectfont
 Grüße, αβγ, Ҍҋ}

\end{document}

enter image description here

So you can use fontenc in your document (I need it to use chessfonts), but you should be careful to load it so that TU remains the default encoding. This here e.g. is wrong:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{DejaVuSans}
\usepackage[T1]{fontenc}
\usepackage[LGR]{fontenc}
\usepackage[T2A]{fontenc}
%
\begin{document}% wrong, encoding is T2A

Moving the \setmainfont resolves the problem:

\documentclass{article}
\usepackage{fontspec}
\usepackage[T1]{fontenc}
\usepackage[LGR]{fontenc}
\usepackage[T2A]{fontenc}
\setmainfont{DejaVuSans}
\begin{document} %encoding is TU now