10

The following MWE works fine when compiling with the LaTeX engine (AUCTeX 11.86)

\documentclass{article}

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

\begin{document}
áéíóúñ
\end{document}

However, with the XeTeX engine I get the following error:

ERROR: Package inputenc Error: Unicode char \u8:áéí not set up for use with LaTeX

--- TeX said ---
.

See the inputenc package documentation for explanation.

If I change the input encoding to Latin-1 the document compiles correctly. Why is XeTeX rejecting the UTF-8 encoding?

I tried the suggestions given in Typeset directly in Spanish and in AUCTeX and XeTeX but neither help.

Ricardo
  • 3,186

2 Answers2

20

\usepackage[utf8]{inputenc} with XeLaTeX is wrong, because XeTeX directly reads UTF-8 and that package confuses the way it interprets the characters.

Files input to Xe(La)TeX should always be UTF-8 encoded (UTF-16 and UTF-32 work too, but UTF-8 seems preferable). There is the possibility to specify a different input encoding, but this is for legacy documents and is not what you want.

If you want a document that typesets both with pdflatex and xelatex, then you can try

\documentclass{article}

\usepackage{ifxetex}

\ifxetex
  \usepackage{fontspec}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{lmodern}
\fi

\begin{document}
áéíóúñ
\end{document}

and save the file as UTF-8. You can use babel in both cases, since Spanish is based on the Latin alphabet; however, switching to Polyglossia for XeLaTeX may be better:

\documentclass{article}

\usepackage{ifxetex}

\ifxetex
  \usepackage{fontspec}
  \usepackage{polyglossia}
  \setmainlanguage{spanish}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage[spanish]{babel}
  \usepackage{lmodern}
\fi

\begin{document}
áéíóúñ
\end{document}

Note that fontspec uses by default the OpenType version of the Latin Modern fonts.

egreg
  • 1,121,712
  • Thanks for clearing this up. I removed the inputenc package and now it works as I expected it to. I also saw in one of the log files that XeTeX begs the user not to use inputenc. On another note, would specifying \usepackage{lmodern} be redundant when using fonstpec? – Ricardo May 06 '12 at 17:18
  • 1
    @Ricardo It's not only redundant, it's actually wrong. :) – egreg May 06 '12 at 17:48
  • I've taken it out! Should I ask another question as to why it is wrong or can I ask here in the comment section? – Ricardo May 06 '12 at 18:02
  • 1
    lmodern.sty is for enabling the "old style" fonts, while fontspec, by default, loads the OpenType version of the Latin Modern fonts – egreg May 06 '12 at 20:48
  • @egreg hello professor. This question was asked two years ago, but currently, only \usepackage[T1]{fontenc} is needed with XeLaTeX – doed Aug 25 '14 at 17:07
  • 1
    @doed It's not necessary to load fontenc in XeLaTeX; it might be in some cases, but not usually. – egreg Aug 25 '14 at 18:00
  • @egreg right, it could be. Languages and their symbols is beyond this comment. I'm not ignoring the fact that XeLaTeX ignores inputenc. But I'm not ignoring either, that for some accented characters like in the op's question: áéíóú for example, t1enc.def must be loaded. – doed Aug 25 '14 at 18:27
1

This is an old post that is not longer true. With TeX Live 2020 the MWE of the question now works without problems because the package is now ignored, producing only a warning, but not a fatal error:

Package inputenc Warning: inputenc package ignored with utf8 based engines.

This does not mean that now use inputenc package everywhere is a good practice.

Fran
  • 80,769