# How do I configure fonts and package options for different engines?

I would like to be able to rely on the code in my document class to respond correctly to whether it is being rendered using pdfLaTeX or XeTeX engines. The editor I'm using (LyX) does this using several settings established through the document properties dialog, but I'd like to have a document class that configures itself correctly in response the the active engine alone, without requiring additional (editor specific) settings.

For example, I would like to be able to write something like:

``````\@ifundefined{XeTeXversion}{
% (1) Ensure that fontspec is not loaded
\usepackage[T1]{fontenc}    % (*) In LyX, this is set in document properties, by un-checking Fonts > Use non-Tex fonts
\usepackage[latin9]{inputenc}
%...
\usepackage{tgpagella}
%...
\usepackage[protrusion=true]{microtype}
% (2) Possible additional engine-sepcifc packages and options
}{%
% (1) Ensure that fontenc is not loaded
\usepackage{fontspec}      % (*) In LyX, this is set in document properties, by checking Fonts > Use non-Tex fonts
%...
\defaultfontfeatures{Mapping=tex-text}
\setromanfont[Ligatures=TeX]{Palatino}
%...
\usepackage{xunicode}
% (2) Possible additional engine-sepcifc packages and options
}
``````

While, I can get this to work if I explicitly switch between `fontspec` and `fontenc` using LyX's settings in the LyX UI (*), I'm left with two questions:

1. Is checking `XeTeXversion` a reliable way to determine that XeTeX is in use?
2. Is there a way to (1) disable or "unload" `fontspec` or `fontenc` in code like the example above to override what LyX may be doing in the absence of specific (*) settings in the UI?
3. Are there common engine-specific packages or package options I should be using (or avoiding) when switching between pdfLaTeX and XeTeX (2).
-

First of all, to get documents that are processable either by LaTeX and XeLaTeX, use only the UTF-8 encoding.

``````\RequirePackage{ifxetex}
\ifxetex
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\else
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tgpagella}
\usepackage[protrusion=true]{microtype}
\fi
``````

How to make this digestible to LyX is another matter.

Note that `xunicode` should not be loaded (it's loaded automatically by `fontspec`) and that the option `Mapping=tex-text` has been changed into `Ligatures=TeX`, so you're specifying it twice.

Commonly used packages such as `color`, `xcolor`, `graphicx`, `geometry` and `hyperref`, that must adapt their behavior according to the engine used, automatically detect the engine and so the special driver to be used.

Note: as of writing this answer, `microtype` is not available for XeLaTeX. A beta version that only supports character protrusion can be installed at

http://tlcontrib.metatex.org/cgi-bin/package.cgi/action=view/id=569

-
Are you recommending not to use `microtype` with XeTeX? –  Brent.Longborough Feb 20 '12 at 1:18
@Brent.Longborough: Good question. –  raxacoricofallapatorius Feb 20 '12 at 1:21
@Brent.Longborough: Version 2.4 of `microtype` -- still the "official" version -- does not support XeTeX. Version 2.5 of `microtype`, currently in advanced beta, does support protrusion but none of the other features of the package when run under XeTeX. (Under LuaLaTeX, the new version supports protrusion, font expansion, and letterspacing.) Version 2.5 (beta 8) is available online at tlcontrib.metatex.org/cgi-bin/package.cgi/action=view/id=569. –  Mico Feb 20 '12 at 2:17