# Many documents, same preamble

I work with many documents which I hope will have the same style throughout.. When making a decision about the design, I want that to be applied to all of the documents uniformly. I've created 1 file that has the start and ending code. Then, all of my other files just have the text, and some minimal code (defining things like chapters and sections). E.g.:

\documentclass{article}

\title{The First Document}
%\title{The Second Document}
%\title{The Third Document}

\begin{document}

\input{document01}
%\input{document02}
%\input{document03}

\end{document}


To compile a different document, I just move the %'s to another line and run pdflatex on this file.

• Is there a better system for managing these?
• Can I put some details, such as the \title information in the beginning of each document (document01.tex, etc.)
• Is there any way to compile all of the documents at once as separate PDFs?
-
You could work the opposite way: defining a LaTeX package. In the package you can write the code that specifies the style. When you want to format a document that way you only have to import your LaTeX package. –  CommuSoft Nov 7 '11 at 1:54
I would imagine that this is much easier to do using some sort of scripting, and compile the main document layout iteratively. It is possible to execute, from the command line, pdflatex \documentclass{article} \title{<title>} \begin{document} \input{<document>} \end{document}. –  Werner Nov 7 '11 at 1:58

It is not necessary to include the \title{<title>} command in the main document preamble. That is, you might just as well include \title{<title>} as part of your document?.tex file2. As such, I have the following suggestion or sequence of steps:

1. Create your style by inserting everything into a style file, say mystyle.sty1. This should include everything that you need, like all the packages and formatting that you want to apply to your separate documents.

2. Create your documents using a common filename structure, like document??.tex where ?? represents some identifier that separates the document.

3. Write a script that cycles through document??.tex in the current folder, and executes

pdflatex \documentclass{article}\usepackage{mystyle}\begin{document}\input{<doc>}\end{document}


where <doc> is the current file being processed. The output of each processing will be stores in article.pdf, which you can then rename/move to <doc>.pdf. Since I am running Windows, here is a minimal batch file that would accomplish this:

@echo off
for /f %%a in ('dir /b document??.tex') do (
echo %%a
pdflatex \documentclass{article}\usepackage{mystyle}\begin{document}\input{%%a}\end{document}
move /y article.pdf %%~na.pdf
)


The list of files being processed is given by dir /b document??.tex, where ?? represents a wildcard (anything). /b strips the dir command from the headers, footers and all other file-related contents, thereby only leaving the file name. The above script starts out by called the "currently processed file" %%a. Then it prints out the file name (echo %%a) and runs pdflatex on the following generic document structure:

\documentclass{article}
\begin{document}
\input{document??}
\end{document}


Finally, the article.pdf output is moved to document??.pdf. A similar approach could be followed on other operating systems.

This procedure would generalise your procedure of creating multiple documents at once3.

-

Is there a better system for managing these?

What you are looking for is style files.

Create your own style file, put it in a location where your TeX distribution can find it and load it in every documents' preamble.

Can I put some details, such as the \title information in the beginning […]

Sure, since it belongs to the document (and not to the style), you can include it in your document as usual.

Is there any way to compile all of the documents at once as separate PDFs?

Since you are talking about independent documents that just follow a common style, there's no built-in way of compiling all at once. Write a shell script for that purpose.

-

Like Werner already wrote, you may put \title{<title>} into the document files itself instead of the preamble. If you are using, e.g. babel shorthand at the title, this is even a very good idea. Otherwise you'd have to activate the shorthand before the title and deactivate it after the title. So moving \title{<title>} (and \author{<author>}) after \begin{document} should be recommended.

Instead of creating a package (aka style) you can even create a wrapper class myclass.cls. To do so begin with:

\ProvideClass{myclass}[2011/11/07 v0.1 my first class]


With this, your class is similar to article, because it simply loads class article with all the options given to your class. We call such a class wrapper class.

Now copy everything in the preamble of your document to myclass.cls. Next you should replace \usepackage by \RequirePackage at myclass.cls. And you should remove loading of the package inputenc because the encoding is not an attribute of a class but of a document. Because inputenc has been removed from your class you should make it 7-bit-clean. This means you should e.g. replace ä by \"a. Maybe \RequirePackage[<language>]{babel} should be removed from your class too. But if the class supports only the languages you've given as option there, this is not a must.

At last you should add \endinput at the end of the class file. This is not a must, but it's nice to have.

After this, all your files may be:

\documentclass{myclass}
\usepackage{selinput}
\SelectInputMappings{
germandbls={ß},
Euro={€},
}
\usepackage[english]{babel}% use every language you want
% You need nothing else, because everything else is at myclass.cls.
\begin{document}
\title{Title <nr>}
\maketitle
Text of document <nr>% or \input{document<nr>}
\end{document}


I've used selinput instead of inputenc, because it's very nice even if you do not know the encoding of your editor.

-

This is a modified version of my answer to an earlier question: How can I divide my book into different project without repeating myself?

1. As others have mentioned place all your style definitions and list of packages in a separate file MyPackages.sty and include them with \usepackage{MyPackages}. This ensures that there is one specific place where these settings need to be changed, and will affect all the documents and ensure consistency.

In MyPackages.sty, I would also recommend you include the standalone package as well. This will allow you to compile each chapter as you go and then use that file directly in the main book.

Besides the packages, the MyPackages.sty file should also contain all your custom macros and might look something like this:

\usepackage{standalone}% Need standalone package
\usepackage{…}% other packages that you use



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

The custom macros here should include your style definitions as well. For example, if you want to have keywords in bold, instead of using \textbf{someKeyWord} you should define \newcommand*{\keyword}[1]{\textbf{#1} and use \keyword{someKeyWord} in your document. This serves two purposes: Someone reading your document will know that you are identifying someKeyWord as a keyword, and second if you decide to ever change the formatting of keywords, you only need to do that in one place.

2. Create separate files for each chapter (each documentXX file). Here is an example for document01.tex. Note that is a complete file and can be compiled by itself:

\documentclass{article}
\usepackage{MyPackages}% your custom list of macros and packages

% include any title information that is specific to this chapter
\begin{document}
\chapter{First Chapter Title}
\section{First Section Title}
... contents of chapter 1 here...
\end{document}


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

\documentclass{article}
\usepackage{MyPackages}% your custom list of macros and packages

\begin{document}
\input{document01}
\input{document02}
...
\end{document}


Assuming you use a very well defined naming structure you could automate this further using Can i automatically load chapters and sections based on the filesystem?. It probably is not worth the trouble if you only have a few files, but if you plan to keep adding new chapters, using this approach would not require you to update the main file.

One important thing to keep in mind is that main file must include the standalone package as well as ALL the packages required by the individual chapter files, and the above structure does that for you automatically.

3. And as others have mentioned, it is probably best to use an external script to compile the documents.

-