Both \input
and \include
are commands in LaTeX used to include the contents of another file into your main document. However, they have some differences in their behavior. Here’s a guide to help you understand when to use \include
and \input
in LaTeX.
\input vs. \include
1. Processing
\input
: It reads the complete source code of the included file and processes it directly in the current document. This means any LaTeX commands inside the included file will be executed as if they were written directly in the main document.
This command is written as follows:
\input{filename}
\include
: It only reads the output of the included file, meaning any LaTeX commands inside are already compiled, and only the resulting text and formatting are inserted. This can be faster for large files.
This command is written as follows:
\include{filename}
2. Page breaks
\input
: Does not force a page break before the included content. The content is inserted at the current point in the document.
\include
: Always starts the included content on a new page. This can be helpful for chapters, sections, or other major divisions of your document.
3. Nesting
\input
: Can be nested, meaning you can use \input inside an already included file to include another file further.
\include
: Cannot be nested. Each included file must be directly included in the main document, or another included file.
4. Preamble
\input
: Can be used anywhere in the document, including the preamble.
\include
: Cannot be used in the preamble.
5. Speed
\input
: Generally faster, as it only processes the commands, not the full typesetting.
\include
: Slower due to the additional page break calculations.
6. Usage
\input
: Generally used for smaller files or when accessing the LaTeX commands inside the included file.
\include
: Generally used for larger files or to ensure a clean page break before the included content.
Difference between ‘\input’ and ‘\include’ in tabular form
Here’s a table summarizing the key differences:
Feature | \input | \include |
Processing | Reads and executes source | Reads and inserts output |
Page breaks | No forced page break | Starts on a new page |
Nesting | It can be nested | Cannot be nested |
Preamble | Can be used in the preamble | It can be used in the preamble |
Speed | Generally faster | Slower |
Usage | Smaller files, access commands | Larger files, clean page breaks |
Important Points
- Both commands require the included file to have a
.tex
extension. - Make sure the included file doesn’t contain a full LaTeX document structure (e.g.,
\documentclass
,\begin{document}
,\end{document}
). - Use
\includeonly
with\include
to include specific files during compilation selectively. - You can use the
.aux
file to manage dependencies between included files and labels.
Choosing the right command
Use ‘\input’ for:
- Small files or snippets of code.
- When you don’t want a page break before the included content.
- Nesting within other included files.
- Including content in the preamble.
Use ‘\include’ for:
- Large files or sections with their own logical flow.
- When you want a page break before the included content (e.g., chapters in a book).
- Ensuring page breaks in
\includeonly
sections.
Ultimately, the best choice between \input
and \include
depends on your specific needs and the characteristics of the files you’re including. I hope this information helps you decide which command to use in your LaTeX documents!