Split a document into multiple files in LaTeX (input, include)
\input{file} inserts the content of file.tex at that place, as if you had typed it there. \include{file} does the same for whole chapters: it starts a new page and lets you compile only some of the chapters with \includeonly.
Insert a file with \input
The inserted file contains only content, without \documentclass and without \begin{document}. The ending .tex can be left out. For a file in a folder, write the path with forward slashes: \input{chapters/intro}.
Complete document:
\documentclass{article}
\begin{document}
\input{chapter1}
\input{chapter2}
\end{document}Whole chapters with \include
\include starts a new page before and after the file and keeps a separate .aux file for it. \includeonly{chapter2} in the preamble then compiles only that chapter, while page numbers and references of the other chapters stay as they were in the last full run.
Complete document:
\documentclass{article}
\includeonly{chapter2}
\begin{document}
\include{chapter1}
\include{chapter2}
\end{document}Move the preamble to a file
A file with your \usepackage lines and definitions can be loaded with \input in the preamble and shared by several documents.
Complete document:
\documentclass{article}
\input{preamble}
\begin{document}
The set $\R$ is defined in the file with the preamble.
\end{document}Files that also compile alone
With the subfiles package a part is a document of its own. Its class line names the main file, and LaTeX takes the preamble from there when you compile the part alone.
Complete document:
\documentclass[../main.tex]{subfiles}
\begin{document}
\section{Method}
This file compiles alone and as a part of the main document.
\end{document}The main file for subfiles
The main file loads subfiles and inserts each part with \subfile. The \documentclass line and the document environment of the part are skipped then.
Complete document:
\documentclass{article}
\usepackage{subfiles}
\begin{document}
\section{Introduction}
Text of the main file.
\subfile{sections/method}
\end{document}File chapter1.tex:
\section{Introduction}
Text of the first file.File chapter2.tex:
\section{Method}
Text of the second file.File preamble.tex:
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{graphicx}
\newcommand{\R}{\mathbb{R}}File sections/method.tex:
\documentclass[../main.tex]{subfiles}
\begin{document}
\section{Method}
This file compiles alone and as a part of the main document.
\end{document}Pitfalls
\includeworks only in the document body and cannot be nested: an\includeinside an included file stops with "\include cannot be nested". Use\inputinside included files.- A wrong file name or path stops with the error "File not found", see File `boktabs.sty' not found.
- With
\inputand\include, paths count from the folder of the main file, also inside a file that lies in a subfolder.\subfileis the exception: paths inside a subfile count from the folder of that subfile.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
subfiles - Error File `boktabs.sty' not found
- How-to Set up a thesis in LaTeX
- How-to Set the image folder in LaTeX