# Ultrafast PDFLaTeX with precompiling

I try to improve the time pdflatex needs to compile my book.

# Really working example

### book.tex

%&preamble
\begin{document}
Hi
\end{document}


and

### preamble.tex

\documentclass{article}


I run the following commands on bash console:

$pdflatex -ini -jobname="preamble" "&pdflatex preamble.tex\dump"$ latexmk -pdf -pvc -e '\$latex=q/latex %O -shell-escape %S/' book.tex


A window opens with a (nearly) live preview that will be updated after each change in the book.tex file. The PDF Viewer evince reloads automatic when the .pdf changed.

# Open Questions

I had no success to compile with the preamble of my book in the preamble.tex file.

• How can I find out what I can precompile?
• Can I precompile \newcommand's, \usepackage's?
• Can I precompile a pure chapter without header?

# Other aproaches for speed up

(I do not understand how to combine this ideas for best result)

• pdflatex knows a -draftmode I measured 20% faster compilation with time pdflatex -draftmode 50pagetest.tex
• If you \include many chapters you can use \includeonly in your master file (book.tex) to compile just the chapters you are editing at the moment. – Ethan Bolker Oct 29 '12 at 0:56
• @Jonas Yes \include and \includeonly works only with separating the file's content on different pages. But for your final compilation you can change those \includeonly back to \inputs. • Also related: mylatexformat – Qrrbrbirlbel Oct 29 '12 at 1:22
• @JonasStein You can create a format (precompile) preloading packages, and \newcommand. With mylatexformat you can compile files with the same preamble, the files must have a \begin{document} and \end{document} – Guido Oct 29 '12 at 3:11
• – Peter Grill Oct 29 '12 at 4:39
• @JonasStein: Could you post a MnWE for your (failed) separation of your book document into preamble.tex and book.tex? Also, what is the exact wording of the error message? – krlmlr Oct 29 '12 at 19:51

"precompile" is probably a slightly confusing phrase to use as TeX is not a compiler but (mostly) a macro expansion language, but anyway...

In general you can dump most macro definitions and register assignments into a format. What you can't do is ship out pages. So in practice you can dump most LaTeX preambles.

Rather than having to edit the file so that it only works with the preloaded preamble format it is possible to leave the preamble as normal, but define the dumped format to skip the commands that were previously executed in the dump.

My truly ancient mylatex files on ctan do this or there is a newer version of that with additional features and better maintained: mylatexformat

The main thing you have to beware of (and which I suspect you are falling over) is if any of commands that you dump use \jobname (for example to open auxiliary files) then you have to ensure that the jobname when you dump the format is the same as the jobname when you produce the document. Also If the macros are assuming that files opened by commands in the preamble are still open when the document is processed then you will need to re-open them when you use your preloaded format loading the format file will re-set TeX's internal state with respect to its internal memory but it will not re-assign the file handles to the filesystem.

The first hit on searching for mylatex on this site shows an example discussing this in the context of tikz externalize.

TikZ's externalization and mylatex

For the benefit of people using a search engine arriving here looking for how to convert a never-changing but slow-loading preamble into a super-quick "precompiled" format.

The instructions here take you through a simple process, as follows. You need the mylatexformat package to make this work.

1. Split your preamble into "static" and "might change" stuff, with the static stuff first.
2. At the end of the static stuff add the command \endofdump
3. Run the following from the command line/terminal, replacing both instances of foo with the name of your document:

pdftex -ini -jobname="foo" "&pdflatex" mylatexformat.ltx foo.tex

1. Delete the static part of your preamble (might want to save it somewhere else for safe keeping!) (Note: it is suggested in the comments that this step isn't necessary. I haven't tested it but if someone does and confirms this, feel free to edit this answer and delete this step.)
2. Insert the code %&foo at the top of your .tex file, replacing foo with the name of your file.

Your compiles will now happen much faster.

Note: if you have any "might change" preamble, you need to keep the \endofdump command, otherwise compilation will start at begin{document} skipping all your preamble

• Step 4 (deleting the precompiled preamble) is not needed. The generated format loads mylatexformat which skips content until \endofdump. Viceversa, if you're ready to delete content, you needn't mylatexformat, you just need to use the \dump command as explained here: web.archive.org/web/20170718172440/http://… – Blaisorblade Jul 18 '17 at 17:26
• @Blaisorblade wow, this really makes the workflow much, MUCH simpler. Love it. – LondonRob Jul 18 '17 at 19:18
• Thanks! Meanwhile, I've taken tex.stackexchange.com/a/37730/1340, dropped mylatexformat, adapted it a bit (sorry, not published yet) and now get the prelude auto-recompiled by latexmk on changes (though it need be a separate file for that), which seems even better (YMMV)! – Blaisorblade Jul 18 '17 at 20:57