# Encoding error when changing from LaTeX to XeTeX engine in Emacs AucTeX

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.

-

\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.

-
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 6 '12 at 17:18
@Ricardo It's not only redundant, it's actually wrong. :) – egreg May 6 '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 6 '12 at 18:02
lmodern.sty is for enabling the "old style" fonts, while fontspec, by default, loads the OpenType version of the Latin Modern fonts – egreg May 6 '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