The \include command in LaTeX is used to import the contents of another LaTeX file into your current document. It’s a powerful tool for organizing and structuring large documents, especially for projects like books, theses, or reports. Here’s a guide on how to use the \include command in LaTeX.
Basic Syntax
The basic syntax for the \include command is as follows.
\include{filename}Replace filename with the actual filename of the file you want to include (without the .tex extension). It can be used anywhere within the document body (between \begin{document} and \end{document}).
Note:
- The file extension (e.g.,
.tex) is optional with the\includecommand, as LaTeX assumes.texby default. Therefore, you only need to provide the base filename. - Ensure that the file you want to include is in the same directory as your main LaTeX document, or provide the correct relative or absolute path to the file.
Function of ‘\include’
- It includes the entire contents of another
.texfile in your current document. - It is useful for modularizing your code, separating reusable sections, and organizing large projects.
Behavior of ‘\include’
- Starts a new
.aux filefor the included file, used for cross-referencing. - Inserts a page break before the included content. In other words, the
\includecommand always starts a new page before including the content. If you want to include content without starting a new page, consider using\inputinstead. - Only includes the content after the first
\begin{document}in the included file.
Example
Assume you have a main LaTeX file named main.tex and two separate files, section1.tex and section2.tex, each containing the content for a specific section.
\documentclass{article}
\begin{document}
\title{My Document}
\author{John Doe}
\maketitle
\tableofcontents
\include{section1}
\include{section2}
\end{document}Important Points
- The
\includecommand is often used for chapters in larger documents. Each included file is considered a separate unit, and LaTeX creates separate auxiliary files for each included file. - You can use the
\includeonlycommand with a comma-separated list of file names in the preamble to selectively include certain files during compilation. It can be useful when working on large documents to speed up the compilation process. For example,\includeonly{filename1,filename2}will only include filename1 and filename2 during compilation. - Avoid cyclical includes (including a file that includes itself).
\include vs. \input
The key differences between \include and \input are as follows.
\includecreates a new .aux file for better cross-referencing.\includeinserts a page break by default, while\inputdoesn’t.\includeis recommended for large files and complex projects.
You can check the article on \include vs. \input to know the difference between the two.
By following these guidelines, you can efficiently use the \include command in LaTeX to manage and organize your documents.
