Skip to main content
To split a LaTeX document across multiple .tex files, keep one main file (main.tex) and load the other files with \input{file} (inline, no page break) or \include{file} (starts and ends on a new page, supports selective builds with \includeonly). When individual chapters also need to compile on their own, use the subfiles package. For broader large-document workflows—including Git setup, build automation, and draft-mode configuration—see Managing large documents.

Choosing between \input, \include, and subfiles

Step 1: Share a preamble and load sections with \input

\input{filename} inserts the contents of filename.tex directly at that line, as if you had typed them in main.tex. It adds no page break, works in both the preamble and the document body, and can be nested inside other included files. The .tex extension is optional. Use \input for a shared preamble.tex, macros, individual sections, or large tables that do not need their own page.
main.tex
preamble.tex
introduction.tex
methods.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio
In LaTeX Cloud Studio, a project can hold several files, and files saved next to main.tex in the project are found by \input, \include, and \usepackage on TeX Live 2026 (full distribution). If you organize chapters in subfolders such as chapters/01-introduction.tex, pass the relative path from the main file (for example \input{chapters/01-introduction}).

Step 2: Split chapters with \include and compile selectively with \includeonly

For chapter-based report and book documents, \include{chapter1} does three things that \input does not:
  1. It runs \clearpage before and after the included file, starting each chapter on a fresh page.
  2. It writes a separate .aux file for each included file (such as chapter1.aux).
  3. It works with \includeonly{...} in the preamble so you can recompile only the chapters you are editing.
\include is valid only inside the document body (placing it in the preamble fails with ! LaTeX Error: Missing \begin{document}.) and cannot be nested inside another \include file (doing so fails with ! LaTeX Error: \include cannot be nested.). You can still call \input inside an \included chapter. By convention, omit the .tex extension.
main.tex
chapter1.tex
chapter2.tex
To compile only Chapter 2 during drafting without losing Chapter 1’s numbering or cross-references:
  1. Run a full compile of main.tex once so LaTeX writes chapter1.aux and chapter2.aux.
  2. Add \includeonly{chapter2} to the preamble of main.tex (list multiple files separated by commas, such as \includeonly{chapter1,chapter2}, matching the exact paths passed to \include).
  3. Recompile: LaTeX typesets only chapter2.tex while reading chapter1.aux to preserve chapter numbers, page numbers, and \ref{ch:intro}. Remove or comment out \includeonly for the final build.
When compiling locally with a separate output directory (such as -output-directory=build) and \include{chapters/chapter1}, the matching chapters/ subfolder must already exist inside the output directory so LaTeX can write chapters/chapter1.aux.

Step 3: Compile chapters independently with subfiles

Files loaded with \input or \include cannot compile on their own because they lack \documentclass and \begin{document}. The subfiles package solves this by letting each child file inherit the preamble of main.tex:
  1. Load \usepackage{subfiles} in main.tex and insert child files with \subfile{chapter1} (inline, like \input) or \subfileinclude{chapter1} (page break and \includeonly support, like \include).
  2. Start each child file with \documentclass[main.tex]{subfiles} (or \documentclass[../main.tex]{subfiles} if the child file lives one folder deeper) and wrap its text in \begin{document}...\end{document}.
When you compile main.tex, subfiles skips each child file’s preamble and \begin{document}/\end{document} and inserts its body. When you compile chapter1.tex directly (for example pdflatex chapter1.tex), it loads main.tex’s preamble and builds a standalone PDF of that chapter.
main.tex
chapter1.tex
chapter2.tex

Common multi-file errors

  • ! LaTeX Error: \include cannot be nested.: An \included file calls \include again. Change the inner call to \input.
  • ! LaTeX Error: Missing \begin{document}. on \include: \include was placed in the preamble. Use \input{preamble} for preamble files.
  • Unresolved ?? references under \includeonly: Compile the entire document once without \includeonly first so every chapter writes its .aux file, then re-enable \includeonly.

Try it in the editor

To test a multi-file setup in the browser, start from the Master’s Thesis template in LaTeX Cloud Studio, replace main.tex with the complete example, and save the companion .tex files next to main.tex in the project (TeX Live 2026 full distribution includes subfiles).