I read about precompiling the preamble in http://tex.stackexchange.com/a/15606/4918 and it worked fine with pdflatex but how can I do this for xelatex?

I tried

xelatex -ini -shell-escape -job-name="header" "&xelatex header.tex\dump"


but it ends with an error:

! Can't \dump a format with native fonts or font-mappings.

-

I had the same problem myself once. The first solution is of course, to take \usepackage{fontspec} out of the preamble document and put it right before \begin{document} in the main document.

But as you, just like me, want to load fontspec in a custom document class, you could use

\RequirePackage{etoolbox}
\AtEndPreamble{
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{STIXGeneral}
}


there. \AtEndPreamble works similar to the standard LaTeX command \AtBeginDocument, only that the code inside can itself use \AtBeginDocument like fontspec does.

Unfortunately this means that you can't cut the compile time of fontspec here. The same problem exists with math-unicode and polyglossia for example. (While I had no problems with babel.)

And here is an explanation as to why this is not possible, that I found on the XeTeX mailing list:

Jonathan Kew a écrit :

Right; this would present various technical challenges, and could be quite confusing for the user if the .fmt file contained references to fonts that meanwhile have been removed or modified in the host
system.

Ok. OTOH, I don't understand exactly how font preloading works for "classical" TeX, but the same could happen if the fonts were removed from the texmf tree (or even form the map files)? Or maybe TeX needs only the tfms while XeTeX is using "more" of the font?

Yes, that's basically the situation. With "classical" TeX, the .fmt
file contains the entire tfm data for the preloaded fonts, so TeX can then do its work without reference to the tfms or any other files. (If you remove the tfms, it won't even notice as it already has that data. If you remove the pfbs or map file entries, TeX won't care, it can
still typeset. Your output driver may have problems, though!)

But for the equivalent to work with xetex, we'd have to "embed" a
large portion of the entire OpenType font into the .fmt file
. This
doesn't seem like a good idea.

-
You should probably add explicitly to the format \RequirePackage{expl3} and other packages which fontspec loads. –  Bruno Le Floch Aug 1 '12 at 12:05
Thanks. I’ll accept your answer although it doesn’t really answer my question but explains why there can’t be a better answer … –  Tobi Aug 1 '12 at 12:08
Your preamble loads a ‘native’ (i.e. non TFM) font, which as the meessage says can't be dumped in the format. For example, just having \usepackage{fontspec} in your preamble will load Latin Modern OpenType fonts.
Do you mean that fontspec doesn’t work with precompiling headers? –  Tobi Mar 24 '12 at 20:44
Yes. fontspec used to have a cm-default option to disabling loading Latin Modern fonts, but that option is now gone. –  Khaled Hosny Mar 24 '12 at 21:21
I’m using other fonts than CM, so even this option won’t help :-(. So I get you right that there’s no way to precompile the preamble when using fontspec (fontspec is part of my own document class)? –  Tobi Mar 24 '12 at 21:37