×
Pro použití ShareLaTeXu v Angličtina klikněte zde

[edit] ShareLaTeX guides

[edit] LaTeX Basics

[edit] Mathematics

[edit] Figures and tables

[edit] References and Citations

[edit] Languages

[edit] Document structure

[edit] Formatting

[edit] Fonts

[edit] Presentations

[edit] Commands

[edit] Field specific

[edit] Class files

In large projects, such as books, keeping parts of your document in several .tex files makes the task of correcting errors and making further changes easier. It's simpler to locate a specific word or element in a short file. For this purpose this article explains how to manage big projects.

Contents

[edit] Introduction

Below is an example of a book whose sections and user-defined commands are stored in separate files.

\documentclass[a4paper,11pt]{book}
\usepackage{import}
\usepackage{example}
 
\usepackage{makeidx}
\makeindex
 
\begin{document}
 
\frontmatter
\import{./}{title.tex}
 
\clearpage
\thispagestyle{empty}
 
\tableofcontents
 
\mainmatter
\chapter{First chapter}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}
 
\chapter{Additional chapter}
\import{sections/}{section2-1.tex}
 
\chapter{Last chapter}
\import{sections/}{section3-1.tex}
 
\backmatter
 
\import{./}{bibliography.tex}
 
\end{document}

As you see, the example is a book with three chapters and several sections in a neat main file that pulls external files to generate the final document. The command \frontmatter in the book document class is used for the first pages of the document, the page numbering style is set to Roman numerals by this command; the command \mainmatter resets the page numbering and changes the style to Arabic, \backmatter disables the chapter numbering (suitable for the bibliography and appendices).

In the subsequent sections each part will be more clearly explained.

  Open an example of the import package in ShareLaTeX

[edit] Preamble in a separate file

If the preamble of your document has many user-defined commands or term definitions for the glossary, you can put it in a separate file. The right way is to create a file with the .sty extension. Let's see an example:

\ProvidesPackage{example}
 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[latin1]{inputenc}
\usepackage[spanish, english]{babel}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{textcomp}
\usepackage{pgfplots}
 
\pgfplotsset{width=10cm,compat=1.9}
 
%Header styles
\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\textbf{\textit{\nouppercase{\leftmark}}}}
\fancyhead[LO]{\textbf{\textit{\nouppercase{\rightmark}}}}
\fancypagestyle{plain}{ %
\fancyhf{} % remove everything
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}}
 
%makes available the commands \proof, \qedsymbol and \theoremstyle
\usepackage{amsthm}
 
%Ruler
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}}
 
%Lemma definition and lemma counter
\newtheorem{lemma}{Lemma}[section]
 
%Definition counter
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
 
%Corolary counter
\newtheorem{corolary}{Corolary}[section]
 
%Commands for naturals, integers, topology, hull, Ball, Disc, Dimension, boundary and a few more
\newcommand{\E}{{\mathcal{E}}}
\newcommand{\F}{{\mathcal{F}}}
...
 
%Example environment
\theoremstyle{remark}
\newtheorem{examle}{Example}
 
%Example counter
\newcommand{\reiniciar}{\setcounter{example}{0}}

All the commands in this file could have been put in the preamble, but the main file would have become confusing because of this large amount of code, and to locate the actual body of the document in such large file would be a cumbersome task.

This file could also be put in a normal .tex file and imported with the command import (see the next section), but a .sty file prevents possible errors if the file is accidentally imported more than once.

Notice that the first line of the example is

ProvidesPackage{example}


this means that we have to import this package as example in the main file, i.e. with the command

\usepackage{example}


as shown in the introduction.

Note: A .sty file is far more flexible, it can be used to define your own macros and optional parameters can be passed, see Writing your own package.

  Open an example of the import package in ShareLaTeX

[edit] Importing files

The standard tool to insert a latex file into another are \input and \include (see the reference guide), but these are prone to errors if nested file importing is needed. For this reason the recommended option is the package import.

\chapter{First chapter}
\import{sections/}{section1-1.tex}
\import{sections/}{section1-2.tex}

First, add this line to the preamble of your document:

\usepackage{import}


Then use \import{ }{ }. The first parameter inside braces is the directory where the file is located, it can be relative to the current working directory or absolute. The second parameter is the name of the file to be imported

There is also available the command \subimport that has the same syntax, but if used in one of the files that are imported in the main file, the path will be relative to that sub-file. For instance, below is the contents of the file "section1-1.tex" that was imported in the previous example:

\section{First section}
 
Below is a simple 3d plot
 
\begin{figure}[h]
\centering
\subimport{../img/}{plot1.tex}
\caption{Caption}
\label{fig:my_label}
\end{figure}
 
[...]

As you see, this file imports a pgf plot file called "plot1.tex" that creates a 3d plot. This file is imported by

\subimport{../img/}{plot1.tex}


If \import were used instead, the path ../img would be relative to the main file, instead of the folder "sections" where "section1-1.tex" is saved.

  Open an example of the import package in ShareLaTeX

[edit] Reference guide

Short description of the commands include and input.

include command
\include{filename}

Use this command in the document body to insert the contents of another file named filename.tex. Note that LaTeX will start a new page before processing the material input from filename.tex.

input command
\input{filename}

Use this command in the document body to insert the contents of another file named filename.tex. LaTeX wont start a new page before processing the material in filename.tex

  Open an example of the import package in ShareLaTeX

[edit] Further reading

For more information see: