LaTeX/Modular Documents

From Wikibooks, open books for an open world
Jump to navigation Jump to search

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics
  5. How to get help

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Customizing Page Headers and Footers‎
  14. Importing Graphics
  15. Floats, Figures and Captions
  16. Footnotes and Margin Notes
  17. Hyperlinks
  18. Labels and Cross-referencing
  19. Initials

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Text

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
  2. Letters
  3. Presentations
  4. Teacher's Corner
  5. Curriculum Vitae
  6. Academic Journals (MLA, APA, etc.)

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Creating Package Documentation
  5. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC


During this guide we have seen what is possible to do and how this can be achieved, but the question is: I want to write a proper text with LaTeX, what to do then? Where should I start from? This is a short step-by-step guide about how to start a document properly, keeping a good high-level structure. This is all about organizing your files using the modular capabilities of LaTeX. This way it will be very easy to make modifications even when the document is almost finished. These are all just suggestions, but you might take inspiration from that to create your own document.

Project structure[edit | edit source]

Create a clear structure of the whole project this way:

  1. create a directory only for the project. We'll refer to that in the following parts as the root directory
  2. create two other directories inside the root, one for LaTeX documents, the other one for images. Since you'll have to write their name quite often, choose short names. A suggestion would be simply tex and img.
  3. create your document (we'll call it document.tex, but you can use the name you prefer) and your own package (for example mystyle.sty); this second file will help you keep the code cleaner.

If you followed all those steps, these files should be in your root directory, using "/" for each directory:

./document.tex
./mystyle.sty
./tex/
./img/

nothing else.

Getting LaTeX to process multiple files[edit | edit source]

As your work grows, your LaTeX file can become unwieldy and confusing, especially if you are writing a long article with substantial, discrete sections, or a full-length book. In such cases it is good practice to split your work into several files. For example, if you are writing a book, it makes a lot of sense to write each chapter in a separate .tex file. LaTeX makes this very easy thanks to two commands:

\input{filename}

and

\include{filename}

input vs include[edit | edit source]

What they have in common is that they process the contents of filename.tex before continuing with the rest of the base file (the file that contains these statements). Similar to preprocessor of C or C++ language.

When the compiler processes your base file and reaches one of the commands \input or \include, it reads filename.tex and processes its content in accordance with the formatting commands specified in the base file. This way you can put all the formatting options in your base file and write the contents using \input or \include in the files which contain the actual content of your work. This means that the important part of your working process, i.e. writing, is kept largely separate from formatting choices. This is one of the main reasons why LaTeX is so good for serious writing! You will thus be dealing solely with text and very basic commands such as \section, \emph etc. Your document will be uncluttered and much easier to work with.

The two methods differ from each other in some important ways:

  • \include{texfile}:
    • Cannot have nested statements of \include.
    • The file can contain \input statements however.
    • Forces a page break, which makes it ideal for a book's chapters.
    • Expects .tex file name without extension, otherwise the project won't compile the additional file.
  • \input{textfile}:
    • Can have nested \inputs. Consider it as concatenation across the files.
    • Does not forces page break - makes it ideal for use within a long article with discrete sections, which of course are not normally set on a new page).
    • .tex extension of the input file is optional.

Nesting files has to be done carefully, keeping the complexity of the project structure as simple as possible, having in mind the growth/scale of the content.

Working on discrete parts of your documents has consequences for how the base file is compiled; these will be dealt with below.

External file paths[edit | edit source]

Compiler references external files from the directory of the base tex file. You can use both absolute and relative paths. Per OS file paths will differ, keep that in mind for absolute addresses.

An absolute path is a full path- and filename with every element specified. So, filename.tex might have the full path,

\input{/home/user/texfiles/filename.tex}

If you had created the directory myfiles for your writing project, in your texfiles directory, its full path would be,

\input{/home/user/texfiles/myfiles/filename.tex}

Obviously, using absolute paths is inefficient if you are referring to a file in the current directory. If, however, you need to include a file which is always kept at a specific place in your system, you may refer to it with an absolute path, for example,

\input{/home/user/documents/useful/foo.tex}

In practice, an absolute file path is generally used when one has to refer to a file which is quite some way away in the file system (or perhaps even on a different server!). One word of warning: do not leave empty spaces in the filenames, they can cause ambiguous behaviour. Either leave no spaces or use underscores _ instead.

You may, however, need to make your source portable (to another computer or to a different location of your hard disk), in which case relative paths should be used if you wish to avoid unnecessary rewriting of pathnames. Or, a relative path may simply be a more efficient and elegant way of referring to a file. A relative path is one which is defined in relation to the current directory, in our case the one which contains the base file. LaTeX uses the standard UNIX notation: with a simple dot . you refer to the current directory and by two dots .. you refer to the previous directory, that is the one above the current directory in the file system tree. The slash / is used to separate the different components of a pathname: directories and filenames. So by ./ you refer to the current directory, by ../ you refer to the previous directory, by ../../ you refer to a directory which is two steps upwards in the filesystem tree. Writing

\input{./filename.tex}

will have exactly the same effect as writing

\input{filename.tex}

but if you found it more convenient to put all your files in a sub-directory of your current directory, called myfiles, you would refer to that file by specifying

\input{./myfiles/filename.tex}

Indeed, in our example of the absolute path above, you could refer to that file relatively, too:

\input{../../documents/useful/foo.tex}

Of course, all commonly used file systems – Linux, Mac OS X and Windows – also feature the UNIX ./, ../ facility outlined above. Do note, however, that LaTeX uses forward slashes / even on Microsoft Windows platforms, which use backslashes \ in pathnames. LaTeX implementations for Windows systems perform this conversion for you, which ensures that your document will be valid across all installations.

This flexibility, inherent in the way in which LaTeX is integrated with modern file systems, lets you input files in a way which suits your particular set-up.

When using relative paths within a LaTeX file imported by \input or \include, it is important to note that the paths are relative to the directory in which the main .tex file resides, not to the directory in which the included (or input) file is found. This is likely to be an issue if using a folder per chapter, with the figures in each chapter's folder, and using \include to read the chapter source into the main LaTeX file in a parent folder. This problem can be solved by using the package import (See below). This package adds the command \subimport, which allows importing files relative to the file where the command is.

Compiling the base file[edit | edit source]

When you compile your document, page references and the like will change according to your use of the \input and \include commands. Normally LaTeX users only run the compiler on parts of the document to check that an individual chapter is syntactically correct and looks as the writer intended. A full run is generally only performed for producing a full draft or the final version. In such cases, it is invariably necessary to run LaTeX twice or more to resolve all the page numbers, references, etc. (especially if you are using bibliographic software such as BiBTeX, too).

The simplest way to check that one or more of the various components of your work is syntactically robust, is to comment out the command with a percentage sign, for example:

\documentclass{article}
\begin{document}
%\input{Section_1}
%\input{Section_2}
%\input{Section_3}
\input{Section_4}
%\input{Section_5}
\end{document}

This code will process your base file with the article conventions but only the material in the file Section_4.tex will be processed. If that was, say, the last thing you needed to check before sending off to that major journal, you would then simply remove all the percentage signs and rerun LaTeX, repeating the compiling process as necessary to resolve all references, page numbers and so on.

Using \includeonly[edit | edit source]

Using this command provides more complex, and hence more useful possibilities. If you include the following command in your preamble, i.e. before \begin{document},

\includeonly{filename1,filename2,...}

only the files specified between the curly braces will be included. Note that you can have one or more files as the argument to this command: separate them with a comma, no spaces. If you are using absolute or relative paths to the files, type in the complete reference.

This requires that there are \include commands in the document which specify these files. The filename should be written without the .tex file extension:

\documentclass{book}
\includeonly{Chapter_1,Chapter_4}  % compile just chapters 1 and 4, space characters not permitted
\begin{document}
\include{Chapter_1}                % omit the '.tex' extension
\include{Chapter_2}
\include{Chapter_3}
\include{Chapter_4}
\end{document}

This code would process the base file but only include the content of the author's first and fourth chapters (Chapter_1.tex and Chapter_4.tex). Importantly, this alternative retains as much of the .aux information as possible from the previous run, so messes up your cross-references much less than the makeshift suggestion above.

Import[edit | edit source]

The import package provides two commands, \import and \subimport that are very similar to the built-in \input and \include commands but has two additional features: 1. allows for nested importing of sub-documents and 2. allows for relative directories of imported documents.

Similar to \input and \include, it uses the preamble in the main document for all sub-documents.

Example:

We have the following directory tree

   main.tex
   paragraphs
     |
     -– paragraph1.tex
        paragraph2.tex
        subparagraphs
              |
              -– subparagraph1.tex

The file paragraph1.tex file is included in the main file main.tex by means of the command \input{paragraphs/paragraph1.tex}, and a problem arises when the file paragraph1.tex includes another file with a relative path, for example, \input{subparagraphs/subparagraph1.tex. In that case, we will become an error, because the path subparagraphs/ is relative to the subfolder paragraphs, which is a subfolder of the main file.

We can solve the error by adding the package import, \usepackage{import} in the main file, main.tex, and the command \subimport{paragraphs/}{paragraph1.tex} instead of \input{paragraphs/paragraph1.tex} in order to include the file paragraph1.tex. In that way, we can specify relatives paths when including files in that file.

Separate compilation of child documents[edit | edit source]

A disadvantage of solely using \input and \include is that only the base document can be compiled. However, you may decide that you work better on individual sections of text and wish to edit and compile those separate from the main file. There are a few packages available to address this problem.

Subfiles[edit | edit source]

The subfiles package provides a way to compile sections of a document using the same preamble as the main document.

In the main document, the package must be loaded as:

\usepackage{subfiles}

Instead of using \input and \include, child documents must be loaded as follows:

\subfile{filename}

The child documents must start with the following statements:

\documentclass[main.tex]{subfiles}
\begin{document}

and end with:

\end{document}

It is possible to add parts that will only be applied if the child document is compiled by its own, by defining an "identity" command \newcommand{\onlyinsubfile}[1]{#1} in the main document and then overwriting it after \begin{document} using \renewcommand{\onlyinsubfile}[1]{}. Similarly, the same can be done for parts to appear only if compiled by the main document.

In summary, the base document (main.tex) looks like:

\documentclass{book}
\usepackage{subfiles}
\newcommand{\onlyinsubfile}[1]{#1}
\newcommand{\notinsubfile}[1]{}

\begin{document}
\renewcommand{\onlyinsubfile}[1]{}
\renewcommand{\notinsubfile}[1]{#1}
%% my document content
\subfile{chapter1}
%% more of my document content
\end{document}

and Chapter 1 (chapter1.tex) looks like:

\documentclass[main.tex]{subfiles}
\begin{document}
%% my chapter 1 content
\onlyinsubfile{this only appears if chapter1.tex is compiled (not when main.tex is compiled)}
\notinsubfile{this only appears if main.tex is compiled (not when chapter1.tex is compiled)}
%% more of my chapter 1 content
%% 
\end{document}

Some linux distributions don't have subfiles package in their latex distributions, since it was not included until TeXLive 2012. You can download subfiles.tds.zip from CTAN. This package will contain two files subfiles.cls and subfiles.sty. Move these files to a directory under the name subfiles in the path /usr/share/texmf/tex/latex. This still won't make the package available; the texhash program must be executed first. Now you are good to go!

While subfiles fails to have a way of a subfile itself having references relative to its own directory, the \subimport command provides this functionality.

Subfiles and bibtex[edit | edit source]

It is a bit tricky to include a bibtex bibliography in the main and the subfiles. Here is a solution:

Main file:

\documentclass[11pt,reqno]{amsart}
\usepackage{subfiles}
\def\biblio{\bibliographystyle{amsalpha}\bibliography{bibgraf}}  % *Modification: added `\main/` to specify relative file location.
\begin{document}
\def\biblio{}

\subfile{sub.tex}
\bibliographystyle{amsalpha} 
\bibliography{bibgraf}
\end{document}

Sub file:

\documentclass[main.tex]{subfiles}
\begin{document}
This is a test \cite{kato88:_commut_euler_navier_stokes% Kato \& Ponce 1988, Comm. Pure Appl. Math. 41, 891
}
\biblio
\end{document}
Subfiles: Main and sub files in different subdirectories[edit | edit source]

The solution to that problem is as follows: Main file

\documentclass[12pt]{article}
\usepackage{subfiles}
\begin{document}
Hallo
\subfile{BlattAnalysis/blatt1}
\subfile{BlattAnalysis/blatt2}
\end{document}

and the sub file in BlattAnalysis looks like:

\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../main.tex]{subfiles}

\begin{document}
Hallo
this is a test
\begin{equation}
\label{eq:sec1:1}
\int dx =0 
\end{equation}
\end{document}
Subfiles: subfiles and pictures in different subdirectories[edit | edit source]

In order to be able to set a subfile-specific graphics path, and also to be able to build each subfile as standalone document graphicspath has to be provided twice. For the subfile number X using the same setup as above this would result in

\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../main.tex]{subfiles}

\graphicspath{
  {"../myfiles/Blatt_X/Pictures/"}
  {"../../myfiles/Blatt_X/Pictures/"}
}

\begin{document}
Hallo
this is a test

\begin{figure}[H]
    \includegraphics{example.png}
\end{figure}

\end{document}

Standalone[edit | edit source]

The standalone package is designed for moving more of the opposite direction than subfiles. It provides a means for importing the preamble of child documents into the main document, allowing for a flexible way to include text or images in multiple documents (e.g. an article and a presentation).

In the main document, the package must be loaded as:

\usepackage{standalone}

Child documents are loaded using \input or \include.

The child documents contain, for example, the following statements:

\documentclass{standalone}
% Load any packages needed for this document
\begin{document}
% Your document or picture
\end{document}

In summary, the base document (main.tex) looks like:

\documentclass{book}
\usepackage{standalone}
\begin{document}
%% my document content
\input{chapter1}
%% more of my document content
\end{document}

and Chapter 1 (chapter1.tex) looks like:

\documentclass{standalone}
% Preamble
\begin{document}
%% my chapter 1 content
%%
%% more of my chapter 1 content
\end{document}

Including complete pdf documents or single pages[edit | edit source]

pdfpages can be used for inserting ready PDF files or separate pages and more pages per one page in any layout (e.g. ).

The package has several options:

\usepackage[ options ]{pdfpages}

Options:

  • final: Inserts pages. This is the default.
  • draft: Does not insert pages, but prints a box and the filename instead.
  • enable-survey: Activates survey functionalities. (Experimental, subject to change.)

Including pdf document:

\includepdf[ key=val ]{ filename }

Options for key=val -- comma separated list of options using the key = value syntax. Up to date, comlete list of the key/value parameters can be found in the package documentation. Below follows few commonly used parameters:

pages Selects pages to insert. The argument is a comma separated list, containing page numbers (pages={3,5,6,8}), ranges of page numbers (pages={4-9}) or any combination. To insert empty pages use {}. For instance pages={3,{},8-11,15} will insert page 3, an empty page, and pages 8, 9, 10, 11, and 15.

Actually not only links but all kinds of PDF annotations will get lost. Page ranges are specified by the following syntax: m - n. This selects all pages from m to n. Omitting m defaults to the first page; omitting n defaults to the last page of the document. Another way to select the last page of the document, is to use the keyword last. (This is only permitted in a page range.) E.g.: pages=- will insert all pages of the document, and pages=last-1 will insert all pages in reverse order. (Default: pages=1)

angle You can use the angle-option for turning the included page, for exampe for turning a landscape document when the latex-document is portrait. Example: angle=90
addtolist Adds an entry to the list of figures, the list of tables, or any other list (e.g. from float.sty). This option requires four arguments, separated by commas:

addtolist={ page number , type , heading , label }

  • page number : Page number of the inserted page.
  • type: Name of a floating environment. (figure, table, etc.)
  • heading: Title inserted into LoF, LoT, etc.
  • label: Name of the label. This label can be referred to with \ref and \pageref.

Like addtotoc, addtolist accepts multiple sets of the above mentioned four arguments, all separated by commas. The proper recursive definition is: addtolist={ page number , type , heading , label [, lof-list ] }

pagecommand Declares LaTeX-commands, which are executed on each sheet of paper. (Default: pagecommand={\thispagestyle{empty}}

For example to include a pdf and refer to the page number of some of its pages, you can increase a counter and create a new label for each page of the included pdf:

\newcounter{inclPDFpage}
\includepdf[pages=-,pagecommand={\refstepcounter{inclPDFpage}\label{test.\theinclPDFpage}}]{toInclude.pdf}
The fourth page of toInclude.pdf is page \pageref{test.4} in the compiled document

You can also insert pages of several external PDF documents.

\includepdfmerge[ key=val ]{ file-page-list }

Several PDFs can be placed table-like on one page. See more information in its documentation.

Produce a single .tex from modular documents[edit | edit source]

If you need to produce a single tex file from a modular document, several scripts are available [1]

The file mystyle.sty[edit | edit source]

Instead of putting all the packages you need at the beginning of your document as you could, the best way is to load all the packages you need inside another dummy package called mystyle you will create just for your document. The good point of doing this is that you will just have to add one single \usepackage in your main.tex document, keeping your code much cleaner. Moreover, all the info about your style will be within one file, so when you will start another document you'll just have to copy that file and include it properly, so you'll have exactly the same style you have used.

Creating your own style is very simple: create a file called mystyle.sty (you could name it as you wish, but it has to end with ".sty"). Write at the beginning of the mystyle.sty file:

\ProvidesPackage{mystyle}

Then add all the packages you want with the standard command \usepackage{...} as you would do normally, change the value of all the variables you want, etc. It will work like the code you put here would be copied and pasted within your document.

While writing, whenever you have to take a decision about formatting, define your own command for it and add it to your mystyle.sty:let LaTeX work for you. If you do so, it will be very easy to change it if you change your mind.

This is actually the beginning of the process of writing a package. See LaTeX/Macros for more details.

For a list of several packages you can use, see the List of Packages section.

The main document document.tex[edit | edit source]

Then create a file called document.tex; this will be the main file, the one you will compile, even if you shouldn't need to edit it very often because you will be working on other files. It should look like this (it's the sample code for a report, but you might easily change it to article or whatever else):

\documentclass[12pt,a4paper]{report}
\usepackage{graphicx}
% put all the other packages here:

\usepackage{mystyle}
\usepackage{hyperref} 
\begin{document}

\input{./tex/title.tex}
%\maketitle
\tableofcontents
\listoffigures
\listoftables

\input{./tex/intro.tex}
\input{./tex/main_part.tex}
\input{./tex/conclusions.tex}

\appendix
\input{./tex/myappendix.tex}


% Bibliography:
\clearpage
\input{./tex/mybibliography.tex}

\end{document}

Here a lot of code expressed in previous sections has been used. You import the only package you need, that is your mystyle.sty (note that in the code it has to be imported without the extension), then your document starts. Then it inserts the title: we don't like the output of \maketitle so we created our own, the code for it will be in a file called title.tex in the folder called tex we created before. How to write it is explained in the Title Creation section. Then tables of contents, figure and tables are inserted. If you don't want them, just comment out those lines. Then the main part of the document is inserted. As you can see, there is no text in document.tex: everything is in other files in the tex directory so that you can easily edit them. We are separating our text from the structural code, so we are improving the "What You See is What You Mean" nature of LaTeX. Then we can see the appendix and finally the Bibliography. It is in a separate file.

Once you have created your document.tex you won't need to edit it anymore, unless you want to add other files in the tex directory, but this is not going to happen very often. Now you can write your document, separating it into as many files as you want and adding many pictures without getting confused: thanks to the rigid structure you gave to the project, you will be able to keep track of all your edits clearly.

A suggestion: do not give your files names like "chapter_01.tex" or "figure_03.png", i. e. try to avoid using numbers in file-names: if the numbering LaTeX gives them automatically, is different from the one you gave (and this will likely happen) you will get really confused. When naming a file, stop for a second, think about a short name that can fully explain what is inside the file without being ambiguous, it will let you save a lot of time as soon as the document gets larger.

External Links[edit | edit source]


Previous: Themes Index Next: Collaborative Writing of LaTeX Documents