[Tex/LaTex] How to speed up pdflatex for a very large document on MacOS X

macpdftex

I wonder how I can speed up the pdflatex compilation of a large document (thesis) with >200 pages and many images (however all imported PDFs or imported pixel graphics, no TikZor similar things yet!).
My current LaTeX project has several source files, combined with \include.

I read about latexdaemon which seems to be able to precompile the preamble to save time.

  • Is there anything similar I can use on MacOS X?
  • Are there ways to only process the things that have really changed? (I'm currently editing many chapters at the same time, so \includeonly seems not helpful.)

I use TexMakerX as editor and Skimas viewer.
I've configured latexmk to automatically do several pdflatex runs (see Automatically start the necessary pdflatex runs if .tex has changed (MacOS X)?).

Skim does autoupdate the PDF file, as soon as it has changed.

Best Answer

Take a look this description. It's a description of how to format the preamble yourself. Assuming your source file is main.tex, it all comes down to these steps (which were copied from the link and modified by me for my own use):

  1. Rearrange your preamble so that its static part precedes any dynamic part. What in the preamble can be “dynamic”? For example, if you use \includeonly, you probably want to change its argument rather frequently and so it is not static. Finally, some packages read and write auxiliary files when you issue a command that you should put in the preamble (eg, nomencl uses `\makenomenclature). Clearly, you have to run these commands in every run.

  2. Extract the static part of the preamble into, say, preamble.tex. At this point, main.tex no longer contains the static part. It should start with the dynamic part, or simply `\begin{document} if you have nothing dynamic.

  3. Execute:

    pdflatex -ini -jobname="preamble" "&pdflatex preamble.tex\dump"
    

in the command line. You will notice that a new file `preamble.fmt has been generated in the same directory, along with other auxiliary files.

  1. Edit the first line of main.tex so that it starts with %&preamble.

  2. Execute latex main.tex as usual:

    pdflatex -shell-escape main.tex
    

You may get an error about the class file immediately, or you may notice that latex finishes as usual. But if you have paid attention to the output, or if you inspect the the log file, then you will see that the preamble gets processed in “no time” at all. At this point, cheers!

To make things slightly fancier, here is an modification. First, add this to the end of preamble.tex:

\def\preambleloaded{Precompiled preamble loaded.}

Then, before the dynamic preamble (or \begin{document} if there is none) of `main.tex, add:

\def\ifundefined#1{\expandafter\ifx\csname#1\endcsname\relax} \ifundefined{preambleloaded}
\typeout{PRECOMILED PREAMBLE NOT LOADED}\input{preamble} \else
\typeout{\preambleloaded}
\fi

The idea is to define a macro in the precompiled preamble and use it to detect if the precompiled preamble has been loaded or not. If not, \input{preamble} will be used to pull the static preamble back in and you will not see any error at all.


Also, if you are using tikz to make figures, have a look at the external library. Check the Tikz and Pgf manual, section 32 describes how to use it.