In which order should I load inputenc and fontenc?

When I’m using pdfLaTeX, my preamble usually contains

\usepackage[utf8]{inputenc}


and

\usepackage[T1]{fontenc}


Should these two be loaded in any particular order? What problems could arise if they’re loaded in the “wrong” order?

On a related topic, I know that fontenc should come after font packages, as described in Should the fontenc package be loaded before or after font packages?, so I’d move font packages along with inputenc – unless there’s a catch as well, which would indicate something like
1) font package(s) 2) inputenc 3) fontenc?

-

No the order doesn't matter as inputenc contains some code to avoid "order problems".

Some background: inputenc with option utf8 doesn't set up definitions for every unicode character as everyone knows who got once the message ! Package inputenc Error: Unicode char \u8:... not set up for use with LaTeX.

Instead it loads a .dfu for each font encoding declared by fontenc (naturally only if the dfu exists) so that there exist input definitions for the chars that can be printed with this font encodings.

fontenc puts all the font encodings it has been called with or which have been loaded by default in a list \cdp@list. inputenc process this list and loads the corresponding .dfu (e.g. T1.dfu) and then redefines \DeclareFontEncoding@ so that from now on every \DeclareFontEncoding will load the .dfu directly.

-

Before trying to answer the question I would like to make a general statement that font encoding and keyboard mappings is an area which is still in a mess. This is not only with LaTeX but also with the web. How many pages still come up with funny characters? The nearest thing to a solution is the use of UTF-8. For the TeX/LaTeX family the answer is XeLaTeX or LuaLaTeX.

For texts that their language is predominantly English, I would stick with the extended ASCII encoding at the cost of typing a few commands for accented letters here and there. This in my opinion is a safer way and ensures that journal archives have an easier time in the longer run.

As to the question, I would place \inputenc first as shown in the below minimal.

\documentclass{article}
\usepackage[utf8]{inputenc}
\def\testa{Umlauts: äöüß}
%\usepackage[T1]{fontenc}
%\usepackage{textcomp}
%\usepackage{lmodern}
%\usepackage[utf8]{inputenc}
\begin{document}

\testa

äöüß

\end{document}


Reasoning, defensive programming means I want to catch errors as soon as possible. Contrary to what one would expect, this works with and without \fontenc. (Since LaTeX loads both the T1 and OT1 encoding files on its own).

-
pdflatex can use UTF-8... –  Paul Gaborit Feb 8 '13 at 0:07
I use UTF-8 exclusively (except for BIBTeX files...), and write mostly in Spanish (ASCII with a few accented characters, my text has occasional "weird characters" thrown in, mostly names in Hungarian or German). pdflatex handles that just fine, and has for a long time now. Even hyphenation seems to work OK (TeX hyphenates so little it is hard to be sure...). –  vonbrand Feb 8 '13 at 2:44
@YiannisLazarides The fact that T1 is known to the format doesn't mean that it is used for typesetting characters: you have to request it explicitly (or some package has to); for instance, Ð will throw Command \DH unavailable in encoding OT1 without a \usepackage[T1]{fontenc} call. This is what I mean by "T1 is not loaded automatically". –  egreg Feb 8 '13 at 6:39
@YiannisLazarides: font encodings are a bit like babel languages: You can load more than one and you can switch between them but only one is active at some point. At the begin of the document the last fontenc option (again similar to babel) is active (or the default OT1). –  Ulrike Fischer Feb 8 '13 at 9:04
@YiannisLazarides: Because even with OT1 you can print umlauts - as you can use \chaptername in english and in german. But the output is different. To see the difference: Copy and paste the umlauts from the pdf and you will get "äöü" with T1 but "aou" with OT1 as OT1 doesn't use real glyph but prints accents over the chars. –  Ulrike Fischer Feb 13 '13 at 9:42