Faster LaTeX part IV: Use a precompiled preamble

After the third part in this series on how to speed up the LaTeX compilation part I ran out of ideas. Fortunately, Stefan Kottwitz and Frakturfreund reminded me of one more thing: precompiled preambles. The idea is quite simple: Every time you compile a LaTeX document, it first reads all packages that are called in the preamble with the \usepackage{} command and this takes time. This process can be skipped because most of your preamble almost never changes; the so-called static part. In this post, mylatexpackage is used to create a precompiled preamble.

Static and dynamic parts of a preamble

First, split your preamble in a static and a dynamic part. With static part, I mean the part of the preamble that is not likely to change during the time you’re working on a certain LaTeX project. Think of: most packages, standard macros you always use, and so on. End your static preamble with an important command: \endofdump. The precompiled preamble will contain everything above this line.

In the dynamic part, stuff that might change every now and then should be put. Think of the \title, \author and \date commands, as well as \includeonly if you’ve followed the first post in this series. As a side note: if you’re using the minitoc package, include it in the dynamic part as well (mylatexformat doesn’t work with it).

So to summarize, your preamble should have the following layout:

% static part
...
\endofdump
%dynamic part
...
\begin{document}

How to precompile the preamble

Now for the hard part. For this example we’ll be using a LaTeX file called mydoc.tex.
One last note: I’ve tested this trick on both Mac OS X with TeXlive and Windows with MikTeX. For Linux, I guess the syntax is all the same. If not, I I hope the readers will comment the right syntax in the comments below ;-) Let’s start!

  1. Make sure you preamble as the upper mentioned distinction of static and dynamic part, separated by the \endofdump command.
  2. Open terminal. Put mydoc.tex either in the root folder of your computer or navigate in the Terminal command prompt the folder containing mydoc.tex.
  3. In Terminal, run the following command:
    pdftex -ini -jobname="mydoc" "&pdflatex" mylatexformat.ltx mydoc.tex

    You might get an LaTeX error message (File `mydoc.mtc0′ not found). Ignore that by pressing return. Now a file called mydoc.fmt should be created among others. This one contains the precompiled preamble. The command &pdflatex can be changed to &latex, &xetex, and so on (depending on with you’re using).

  4. In order to load the mydoc.fmt format, add %&mydoc to the very first line of mydoc.tex. Hence, it will look something like this:
    %&mydoc
    ...
    \begin{document}

    Notice that the static part of the preamble is removed, as that is all stored in the mydoc.fmt file. So if you haven’t done this already: comment the static part of the preamble (with %, off course)!

Saved time

I was interested in how much time I actually saved with this little trick, so I made a little comparison. I made two versions of mydoc.tex: mydoc-noformat.tex for the original file and mydoc-format.tex for the file with precompiled preamble and with the %&mydoc line added. The files are available for downloading below.

Without a precompiled preamble, the process took 1.864 seconds. With the format file created, compiling mydoc-format.tex only took 0.531 seconds. That is more than three times as fast!!

Download

A ZIP archive is made, containing to beamer documents; one with and one without precompiled preamble. Download it here. The beamer class was chosen to exaggerate the effect of precompiled preambles (as loading the beamer class requires quite some time).

8 Comments

  1. I simply could not leave your website prior to suggesting that I really enjoyed the standard info an individual supply on your visitors? Is gonna be again steadily to check out new posts

  2. Beamer miete says:

    My brother recommended I would possibly like this website. He was once totally right. This post truly made my day. You can not believe simply how a lot time I had spent for this information! Thank you!

  3. hpvd says:

    Many thanks for this series of articles !!

    Regarding this part (4), I got the following information directly from the package author Florent:

    You NEVER need to comment the \documentclass and \usepackage lines:
    Those lines will be ignored automatically if the “Speedup-hyper” format (that is the *.fmt file) is found.

    Besides, it is recommended to write:
    \csname endofdump\endcsname
    Instead of: \endofdump

    Because if the format is not loaded, \endofdump is an undefined cs, while \csname endofdump\endcsname expands to \relax.
    —-
    works like a charm for me :-)

    => maybe you like to update your great article…

  4. muthukan says:

    I try to compile the mydoc-format.tex file. The file not compiling and appear following error.

    This is pdfTeX, Version 3.1415926-1.40.10 (MiKTeX 2.8)
    initexmf: Unknown format name/engine: mydoc-format/pdfTeX
    (Fatal format file error; I’m stymied)
    pdflatex.exe: No error

  5. veemu says:

    I can try to compile a mydoc-format.tex file it will make a following error:

    This is e-TeX, Version 3.1415926-2.2 (BaKoMa TNS 8.0) (preloaded format=ETEX 2012.5.8) 8 MAY 2012 15:41
    entering extended mode
    **&eTeX mydoc-format.tex
    SRCINFO: open 1:”mydoc-format.tex”
    (mydoc-format.tex
    ! Undefined control sequence.
    l.9 \begin
    {document}
    ? r
    OK, entering \nonstopmode…
    ! Undefined control sequence.
    l.10 \frame
    {
    The control sequence at the end of the top line
    of your error message was never \def’ed. If you have
    misspelled it (e.g., `\hobx’), type `I’ and the correct
    spelling (e.g., `I\hbox’). Otherwise just continue,
    and I’ll forget about whatever was undefined.

    [1] )
    Output written on mydoc-format.dvi (1 page, 272 bytes).

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>