11

When producing documents with pdflatex that may contain special utf-8 characters, it is recommended to add the following to the preamble

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

I know that with lualatex the inputenc line is not necessary, since everything is set up to use utf-8 encoding by default. But I don't know what to do about that fontenc line. Do I need to include it or not if I am using lualatex?

Aradnix
  • 3,735
  • 2
  • 20
  • 43
hugomg
  • 1,071
  • 1
    See @egreg's answer here. Spoiler alert, the answer is no. But I don't know exactly why, so I'll not answer the question. – Phelype Oleinik Jan 29 '18 at 21:18
  • 1
    no you should not have either of those lines for luatex – David Carlisle Jan 29 '18 at 21:19
  • 4
    Short answer: no. Long answer: NO. –  Jan 29 '18 at 22:47
  • You can if you use the luainputenc package instead of inputenc. But it is mainly for backward compatibility with old (PDF)LaTeX documents. If you really want to stick to the old input and font encodings, better use (PDF)LaTeX instead of LuaLaTeX. – Franck Pastor Jan 29 '18 at 23:09
  • As I understand it, somebody might want that line. However, that's only true if you know precisely why you want it and what the consequences are. If you aren't sure whether you need it, not only don't you need it, you shouldn't have it. Consider \usepackage{fontspec} instead. – cfr Jan 30 '18 at 00:17
  • @cfr since it arranges that the the unicode characters in the input will be mapped straight to the same numbered slots in a T1 encoded font and produce different characters with no warning. someone has to be really sure they want that. – David Carlisle Jan 30 '18 at 01:43
  • @DavidCarlisle Yes, I know. I do remember one question where that turned out to be what was wanted, though. I can't now remember why. (I don't mean somebody wanted 'different characters with no warning', but there was some font-specific issue which could be worked around this way.) – cfr Jan 30 '18 at 02:09
  • I've scoured this site and can't find any explanation of what these two lines actually DO. I understand that with LuaLaTeX and XeLaTeX they aren't necessary, but I can't find an explanation that doesn't involve something about unicode. Is there an explanation for font novices about what these lines DO? I see no difference in my compiled output when I use them and when I don't. – LaTeXereXeTaL Jul 14 '20 at 21:35

3 Answers3

9

If you want to use pdflatex, you should use in your preamble:

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

If you want to use lualatex or xelatex, you should use in your preamble:

\usepackage{fontspec}
\setsansfont{CMU Sans Serif}%{Arial}
\setmainfont{CMU Serif}%{Times New Roman}
\setmonofont{CMU Typewriter Text}%{Consolas}

If you want to compile your document time to time with lualatex or with pdflatex without changing preamble, you can use ifluatex package. Typical preamble for this purpose can looks like:

%%============================ Compiler Directives =======================%%
%%                                                                        %%
% !TeX program = lualatex                                   
% !TeX encoding = utf8
% !TeX spellcheck = english
%%                                                                        %%
%%============================== Document Class ==========================%%
%%                                                                        %%
\documentclass[14pt]{article}
\usepackage{ifluatex}
%%                                                                        %%
%%========================================================================%%

\ifluatex 
    \usepackage{fontspec}
    \setsansfont{CMU Sans Serif}%{Arial}
    \setmainfont{CMU Serif}%{Times New Roman}
    \setmonofont{CMU Typewriter Text}%{Consolas}
    \defaultfontfeatures{Ligatures={TeX}}
\else
    \usepackage[utf8]{inputenc}
    \usepackage[T2A,T1]{fontenc}
\fi

\usepackage[english]{babel}

\begin{document}



\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39
  • 1
    Why is the \setsansfont{CMU Sans Serif}, etc. necessary? And what is CMU, isn't Lualatex using Latin Modern fonts by default? What's the difference? Thanks! – Gerard Bosch Nov 21 '21 at 20:27
1

No, Luatex does not require using the fontenc package.

hugomg
  • 1,071
0

I still like to use 8 bit fonts for IPA (T3 encoding) and non-math Greek (LGR encoding)

I use them via the "substitutefont" package, and I believe that still requires that fontenc be loaded.

Anyway one thing I noticed is that if you load fontenc after fontspec then things break so if you do use fontenc, load it before fontspec.

Loading it before fontspec doesn't seem to harm anything.

EDIT:

I did a test. When loading fontenc after fontspec stuff, as long as TU is last specified encoding, things work. Take away the TU in this code and you'll see how it breaks.

\documentclass[letterpaper, fontsize=14pt]{scrbook}
\usepackage{fontspec}
\defaultfontfeatures{
  Ligatures = TeX,
  Extension = .ttf}
\setmainfont
  [ UprightFont = *-Regular ,
    ItalicFont  = *-Italic ,
    BoldFont    = *-Medium ,
    BoldItalicFont = *-MediumItalic ]
  {ClearSans}
\setsansfont
  [ UprightFont = *-Regular ,
    ItalicFont  = *-Italic ,
    BoldFont    = *-Bold ,
    BoldItalicFont = *-BoldItalic ]
  {ClearSans}
\usepackage[LGR,T1,TU]{fontenc}
\begin{document}
\chapter{Chapter One}
This is content.
\section{Section One}
This is more content.
\end{document}
  • Note that you do not need 8-bit fonts for either: every character and font that the T3 or LGR encodings support is also available in Unicode. – Davislor Jul 19 '21 at 00:09
  • Yes, they are, but it is a lot easier to type them from my Latin keyboard using Latin characters that then get mapped to the correct glyph at compile time. And it's also incredibly useful when I want to ensure the document compiles with both pdflatex and luatex. My understanding is that fontspec actually calls fontenc itself - the problem is that the LAST encoding is the default encoding so when calling fontenc after fontspec, T1 (or whatever) is last, breaking fontspec. – AnymouseProphet Jul 19 '21 at 00:55
  • You can use the TIPA mappings with the unitipa package, and I’ve seen a few methods to enable Beta Code mappings for Greek in LuaTeX. – Davislor Jul 19 '21 at 01:00
  • Sure, there is always more than one way to do things. For me personally, the way that works in both pdfLaTeX and LuaLaTeX is the better way because it is more portable between engines. As fontspec calls fontenc anyway, there isn't a conflict with the package itself and fontspec, just a conflict with calling it after fontspec has loaded. – AnymouseProphet Jul 19 '21 at 01:07
  • I think as long as TU is the last argument given to fontenc then it will work even after having called fontspec - just like (for western Latin users) we always had to make sure T1 was last called with pdflatex. But I haven't tested. – AnymouseProphet Jul 19 '21 at 01:13
  • I’m not saying you’re wrong or anything, but advantages of using Unicode include: much simpler code; the ability to copy, paste and search from the PDF; and human-readable source. It does require an input method that works for you, though. – Davislor Jul 19 '21 at 01:17