22

I read about precompiling the preamble in https://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.
<*> &xelatex header.tex\dump
11

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.

  • 1
    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
  • Maybe I'm too late to be here and gonna somewhat digress: I want to ask what is the difference between using mylatex.ltx (or mylatexformat.ltx) and not using it as in this post? Could anyone answer me? – Eric Feb 21 at 16:30
8

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
  • 1
    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
  • 1
    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
0

As a matter of fact, you can ignore the errors, and all will work fine.

Here is an example file, to be saved as test.tex

%&test
\documentclass[a4paper]{article}
\usepackage{amsmath,amssymb,amsthm,graphicx,tikz}
\usepackage{fontspec,unicode-math}
\endofdump
\setmainfont{Liberation Serif}
% this is a mistake in the map
%% http://tex.stackexchange.com/questions/55204/remapping-latex-symbol-to-another-unicode-value/55205
\AtBeginDocument{\let\setminus\smallsetminus}
\setmathfont{Latin Modern Math}
\begin{document}
\section{Test}
This is a test
\[ A\setminus B =  ∞ \]
\end{document}

then there is a Makefile

test.pdf: test.tex
    time xelatex -interaction=nonstopmode test.tex

test.fmt: test.tex
    time xetex -ini -interaction=nonstopmode -jobname="test" "&xelatex" mylatexformat.ltx test.tex

If you make test.fmt you will indeed see a lot of errors, but eventually the format file will be written; then make test.pdf will compile the test.tex without reloading all packages.

In my computer, compiling test.tex without using this trick takes ~3.8seconds, with this trick ~1.4seconds.

  • But this must have some drawbacks … the error couldn’t be there just for fun. – Tobi Nov 25 '19 at 22:20
  • Yes Tobi, you are right, this does not work everytime, I am investigating... – am70 Nov 26 '19 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.