"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
I wrote the above back in 2012, If writing it now I would stress two other classes of things that can not be dumped.
- OpenType fonts (so affecting any xelatex or lualatex preamble that loads system fonts), and
- Lua state, so affecting any package that uses
\directlua
if used with luatex. For any particular Lua code you can usually \dump
a macro definition that executes the code in \everyjob
or \AtBeginDocument
however this often requires substantial re-arrangement of the package code, so using mylatex
with an unchanged latex document is often not possible when using lualatex.
\include
many chapters you can use\includeonly
in your master file (book.tex
) to compile just the chapters you are editing at the moment.\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\input
s. • Also related:mylatexformat
\newcommand
. Withmylatexformat
you can compile files with the same preamble, the files must have a\begin{document}
and\end{document}
book
document intopreamble.tex
andbook.tex
? Also, what is the exact wording of the error message?