4

I am trying to create a mini book with LaTeX on Windows in order to learn the basic thing with LaTeX.

I would like to divide my book into different projects but I have some sort of style defined for whole document. When I divide it into different section, I need to apply this design over and over again.

How can I achieve that without repeating myself? Is there something like Master Page on LaTeX?

2
  • 1
    The \input command is your friend. :)
    – Count Zero
    Oct 19, 2011 at 14:28
  • @CountZero if you can provide any sample code, you will be my friend ;)
    – tugberk
    Oct 19, 2011 at 14:29

4 Answers 4

6

Suppose you have a main file, in which you have the style definitions. At some point then you can insert the other chapters like this:

\documentclass{article}

\usepackage{<somepackage>}

\begin{document}

Your text

\input{chapter1}

More text

\input{chapter2}

etc.

\end{document}

The \input command will take as an argument the name of the LaTeX file you want to insert. The extension is required only if it is different from .tex. There is the 'brother' of this command, \include. The difference is explained here: When should I use \input vs \include ?

2

Besides keeping you style definitions and list of packages in a separate file and including them with \usepackage{<somepackage>}, I'd recommend you use the standalone package as well. This will allow you to compile each chapter as you go and and then use that file directly in the main book.

So, first create a somepackage.sty file to contain all your custom macros and packages such as:

\usepackage{amsmath}% or any others that you use
\usepackage{lipsum}% for dummy text
\usepackage{standalone}% Need standalone package

This file will become sort of you master page. I'd include the geometry information for the margins in here, watermarks, as well as any footers, headers, etc...

Then create separate files for each chapter. Here is an example for chapter1.tex which is a complete file and can be compiled by itself:

\documentclass{book}
\usepackage{somepackage}% your custom list of macros and packages
\begin{document}
\chapter{First Chapter}
    \section{}
    \lipsum[1]% ... contents of chapter 1 here...
\end{document}

Similarly for the other chapters. Then your main file would look like:

\documentclass{book}  
\usepackage{somepackage}% your custom list of macros and packages

\begin{document}
  \input{chapter1}
  \input{chapter2}
\end{document}

One important thing to keep in mind is that main file must include the standalone package as well as all ALL the packages required by the individual chapter files.

1

It is opssible too to include the definitions. Assuming you have a file called layout.tex contaning all your packages (and maybe the class too) and definitions you can use it with

\documentclass{article}
\input{layout}
\begin{document}
  Text
\end{document}

If you like to use layout.tex in many documents you can move it to your user texmf folder to make LaTeX find it like a package or class file. To find out the right file open the “Run-Program“ (Don’t know the english name it’s “Eingabeaufforderung“ in German windows) and type kpsewhich -var-value=TEXMFHOME.

1

You could also use the subfiles package, which would allow you to have the entire preamble in the master document, and then each separate file will inherit the preamble of the main file when compiled. It functions similarly to the standalone package mentioned above.

1
  • subfiles does not allow to use command like 'includeonly`, but it does however allow you nest files.
    – G. Bay
    Feb 20, 2016 at 17:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .