# How to speed up pdflatex for a very large document on MacOS X?

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.

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}
\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.

• @romeovs: thank you for your help/explanation (sorry for my late reply)! Thant looks interesting, however I tried to copy all the preamble with begin and end document (but without the document) to a new file and compiled it once with pdflatex. That took 1 second. Does that mean I'll save only one second each latex run, if I follow the instructions above? When I launch latexmk, I assume the preamble is compiled only once, or is it compiled ad each pdflatex run? What stopped me from trying it diretcly was: I'm not sure which of my commands in the preamble could be dynamic - how do I find out? Jun 12, 2011 at 15:08
• @Martin I don't know if you can add up the compilation time of preamble and main to get the standard compilation time. Als, I don't use latexmk and therefore do not know what it does exactly, my guess is that it only compiles preamble.tex once. You could check this by constructing a very bulky preamble and running that several times, looking for speed changes. As for dynamic commands, I just meant the commands that you might alter a lot when working in main.tex (eg. a macro that might change). Macro's that edit auxiliary files should also be considered dynamic (as mentioned above). Jun 12, 2011 at 15:15
• @romeovs: thanks for your answer. So I assume, that it could really save me "only" 1 second per latex run? Considering the fact that I now spent about half an hour to figure out the effects of the precompiled preamble and draftmode, ... :-( The problem with the dynamic commands is: I'm not sure which of them might modify .aux files and before I spend much time for finding out, it might be the better choice to just go on writing my thesis. :-( Jun 12, 2011 at 15:24
• @Martin Yes, when you have a small preamble it won't actually save you much time. I use a large preamble with lot's of packages and self-defined macro's, package options etc. Using this method I can knock 7 to 8 seconds of my compiler time, which is quit neat. I use quite a lot of packages and none of them use auxiliary files in the preamble, so I'd say the chances are that your packages don't do this either. It's a safe bet to place none of them in the dynamic preamble. If something behaves strange you can always move them. Jun 12, 2011 at 18:15
• @romeovs: Thanks for this nice answer! It’s a nice feature but I had to replace "&latex preamble.tex\dump" width "&pdflatex preamble.tex\dump" because I have JPG-images. May you’d like to add a nota abut this to your answer.
– Tobi
Jan 11, 2012 at 16:09

for all except the last pdflatex run use -draftmode

pdflatex -draftmode file


taken from the documentation:

   -draftmode
Sets \pdfdraftmode so pdfTeX doesn't write  a  PDF  and  doesn't
read any included images, thus speeding up execution.

• I already use the option draft in the fixme-Package, is that the same? -- and. with "last run" do you mean the last run of those in latexmk each time, or only "the one" very last run to produce the final document? Apr 12, 2011 at 10:50
• @Herbert: Thanks! I just knew about the package options and wondered if they just might activate the corresponding option for pdflatex. For the "last run": I hope I did not misunderstand you, so how can I tell latexmk to use -draftmode for all runs except the last one? Apr 12, 2011 at 11:54
• pdflatex is already running when reading the package options and -draftmode is far different to a draft option. Look into the latexmk or write to the author, that he should take the option into account.
– user2478
Apr 12, 2011 at 12:07
• – user2478
Apr 12, 2011 at 12:28
• @Herbert: thanks again for your help! I tried it out: if I use -draftmode as an option for latexmk (always), it takes 33s to compile (always after removing all aux files etc.) my 260 pages thesis, instead of 46s without draftmode, but then in the first case, I have no PDF to preview. Unfortunately, it seems not possible to disable draftmode only in the last run, as latexmkdoes not know it will be the last run (that's what the author wrote me). If I do latexmk with draftmode and then add one "normal" pdflatex run, it also takes 45s. Jun 12, 2011 at 15:13

Setting in the preamble

\pdfcompresslevel=0
\pdfobjcompresslevel=0


reduced the compile time for my (current version of my) thesis from 2:05 to 1:01 (Minutes:Seconds), but also increased the size of the pdf from about 65 MB to 718 MB (seven hundred eighteen Megabyte). Therefore for the final pdflatex run probably

\pdfminorversion=4% or 5 or 6...
\pdfcompresslevel=9% which is really slooow
\pdfobjcompresslevel=3% maybe reduce to 2; 3 might not be supported by all readers


should be used. If you use a version control system making (uncompressed) backups also of the pdf, you should think about what to do with the 718 MB files...