When I am composing a large document in LaTeX, I often compose the chapters in separate documents. When I do this I have to copy and paste a large block of header text to the chapter I am editing so I can make sure the formatting is correct while I am writing it.

How can I import a header file of formatting information with a single line?

-

Solution 1: Put everything up to your \begin{document} in a separate file, say, preamble.tex. You can load this file in your document by saying \input{preamble}.

Solution 2: Almost the other way around: put your chapters in separate files, and have a main file like this

\documentclass...
.
.
.
\includeonly{chapter2}
\begin{document}
\include{chapter1}
\include{chapter2}
.
.
\end{document}


which will cause LaTeX to skip all the other chapters and only use chapter2.tex when you compile main.tex. (You can also list multiple chapters like so: \includeonly{chapter1,chapter7}.)

-
 I have a problem: what should and what should not be present in chapters - those "\include"d files? It is so obvious maybe that noone writes about it. And I have mistakes. Maybe because I repeated the same preamble in parent and every child files? – user13568 Apr 13 '12 at 17:47 @Kotek Welcome to TeX.sx! Your question won't be seen by many people here, so it would be best to repost it as a fresh question. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. – egreg Apr 13 '12 at 17:49

Put it in a file called header.tex and in your main chapter put \input{header.tex}.

But, you might be better off creating a master file that has the header text in it, then \includes the chapters.

\documentclass{book}
\title{my awesome book}
\author{me}
\date{\today}

\includeonly{ch1}
\begin{document}
\include{ch1.tex}
\include{ch2.tex}
...
\end{document}


Changing the \includeonly line allows you to switch on and off what gets included. So you could only include the chapter you were working on to make compiling quicker.

-
Also, calling them ch1 and not chapter1 will let you be 20 seconds faster in posting answers. :D – Ulrich Schwarz Nov 19 '10 at 21:03
@Ulrich: lol. I think ch01-<subject-of-chapter-1>.tex, etc., would be even better, but slower. – Matthew Leingang Nov 19 '10 at 21:11

You can also embed the command \input if you want something to appear irrespective of the \include commands:

\documentclass{book}
\title{my awesome book}
\author{me}
\date{\today}

\includeonly{ch1}
\begin{document}
\include{ch1.tex}
\include{ch2.tex}
...
\input{appendix}
\end{document}

-
 @user1999: good point. I took the liberty of putting backticks around your inline code so it looks like code. – Matthew Leingang Nov 20 '10 at 14:24 Good, I never understood the difference between the two... – Cedric H. Nov 27 '10 at 22:10