The two commands are not interchangeable
\input{file} is a text splice. TeX closes the current file, reads file.tex token by token from exactly the point where the command stood, and returns to the parent as if nothing had happened. No page break, no extra auxiliary file, no state saved. It works in the preamble, in the middle of a table, inside an environment — anywhere at all — and it nests without comment. This is the right tool for macro files, preamble fragments, title pages and a paragraph you are drafting in isolation.
\include{file} is a document-structuring command, and its job is to divide the document into units that are separately optional. Its kernel definition does three things \input does not: it issues a \clearpage first (so all pending floats are placed and the page geometry restarts), it writes \@input{file.aux} into the master auxiliary file and redirects label and citation writing into file.aux while the file is being read, and it consults the \includeonly list to decide whether to read the file at all.
The three consequences are the whole lesson. A chapter break becomes automatic and unavoidable — a document of \included chapters never begins a chapter mid-page. Labels can be tracked per file, which is what makes partial builds possible at all. And do not lean on nesting: an \include inside a file that was itself \included is documented against, and only the outer level reliably gets the selective treatment from \includeonly — the pattern that keeps working is \include for chapters and \input inside them. The same restriction in sharper form: \include cannot stand inside an argument or an environment, because it begins with \clearpage, and \clearpage in a tabular is an error.
Choose by unit of structure, not by size. Use \include for things that begin on a new page and can be switched off: chapters, articles in an edited volume, appendices. Use \input for everything that is really one file wearing a hat: the preamble split into preamble/packages.tex and preamble/macros.tex, a long table, the title page, the answer key of one exercise set.
\text{source read} = \sum_{f \in F} \text{lines}(f), \qquad \text{compile time} \propto \text{source read}
What the per-file .aux buys you, and what it costs
While \include{ch02} is being read, LaTeX writes labels and citations into ch02.aux instead of main.aux, and at the end of the run main.aux contains a line \@input{ch02.aux} which pulls each of those files back in. This is the same mechanism the .aux uses to record \bibdata and \bibstyle, and it is the reason a multi-file project has a nest of small auxiliary files beside main.aux.
The benefit: \includeonly{chapters/02-hilbert} makes LaTeX skip reading the other chapters while still reading their auxiliary files. Your working chapter compiles in seconds and every \ref into another chapter still resolves to something — which is exactly the property you want while revising one chapter of a 200-page book.
The cost has two parts, and both are quietly dangerous. Counters only advance for material that is actually read, so the excluded chapters do not contribute to the numbering: your working chapter becomes chapter 1, its equations 1.x, and the page count drops by a hundred. And because references to excluded files resolve from their last full build's auxiliary data, the numbers can be stale in a way that looks like correctness — \cref{fig:old} will happily print "figure 3.4" from a run that no longer matches the text. The discipline is to do a full build before anything is shared, reviewed or submitted, and to grep for ?? in the log rather than trusting the visible page.
Practical shape of the switch: keep \includeonly{} in the master file as a single commented line, so that "fast mode" is one edit, and never commit it uncommented. Some projects go further and put the decision in the build command (latexmk -pdf -e '$includeonly="ch02"' via a wrapper), which keeps the source honest; the simpler habit is a second root file, draft.tex, that includes only what you are writing.
\underbrace{4.2}_{\text{all chapters read}} \ne \underbrace{1.2}_{\text{only ch01 and ch04 read}}
% ============================================================
% main.tex — the only file the compiler is ever pointed at
% ============================================================
\documentclass[12pt,a4paper,twoside]{book}
\usepackage{subfiles} % lets a child also be compiled on its own
\input{preamble/packages} % \input, not \include: no page break, no own .aux
% (packages.tex loads amsmath, graphics, hyperref,
% and cleveref last of all)
\input{preamble/macros}
\input{preamble/theoremstyles}
\includeonly{chapters/01-banach} % selects among \include files; comment it
% out before building anything you share
\begin{document}
\frontmatter % book only: roman numbers, restarts at i
\input{frontmatter/titlepage} % a \input file with no \begin{document} of its own
\tableofcontents
\mainmatter % arabic again, restarting at 1
\include{chapters/01-banach} % plain child: \include forces \clearpage and
% writes chapters/01-banach.aux
\subfile{chapters/02-hilbert} % subfiles child: has its own \documentclass
% prologue, so it is read with \subfile
\include{chapters/03-operators}
\appendix
\include{appendix/notation}
\backmatter
\bibliographystyle{plainnat}
\bibliography{refs} % with biblatex instead: \printbibliography
\listoffigures
\end{document}
Letting a chapter compile by itself: subfiles
The demand is universal and the naive answer — copy the preamble into every chapter — rots within a week. The subfiles package gives the child file a two-line prologue instead: the master file stays an ordinary document that \includes or \subfiles its children, and each child begins with \documentclass[../main.tex]{subfiles} followed by a normal \begin{document}...\end{document} body. Compiling the child loads the parent's class and preamble through that path and then typesets only the child, so you get a one-chapter PDF with the real macros, fonts, colours and theorem styles.
Two mechanics make it usable rather than clever. First, when a subfile is compiled on its own, \jobname changes, and any path written relative to the master breaks — the package supplies \subfix{} for exactly this, so graphics and bibliography paths are written as \includegraphics{\subfix{figures/basis}} and resolve correctly in both modes. Second, cross-references into sibling chapters cannot resolve in a standalone child build unless the sibling auxiliary files are read, which is why a subfile build shows ?? for references outside the chapter — a limitation of the layout, not a bug to chase.
subfiles and subfile are two different packages and the names are almost the only thing they share. subfile (part of the caption bundle family) is the older, thinner one: a child wrapped in \begin{filecontents}-like markers plus an \endinput trick so the file can be compiled alone with a hand-written preamble. subfiles is the one that reuses the master preamble, and it is the sane default. The alternative for figures — the standalone class with \includestandalone — has its own preamble-reuse machinery (subpreambles=true) and is worth the extra package only when the figure is genuinely a document.
The decision in practice: for a thesis or book where chapters must be reviewable as separate PDFs, subfiles plus \include. For a paper where the only reason to split is readability, plain \input of sections — fewer moving parts, no path subtleties, and the whole document always compiles as one.
% ============================================================
% chapters/02-hilbert.tex
% Two ways to use this file:
% (a) read by main.tex through \subfile{chapters/02-hilbert}
% (b) compiled on its own, with the class and preamble of main.tex
% ============================================================
\documentclass[../main.tex]{subfiles} % path back to the root document
\begin{document}
\chapter{Hilbert spaces}\label{ch:hilbert}
\section{Definition}
A complete inner-product space:
\begin{equation}\label{eq:parallelogram}
\lVert x + y \rVert^{2} + \lVert x - y \rVert^{2}
= 2\lVert x \rVert^{2} + 2\lVert y \rVert^{2}.
\end{equation}
Cross-references to other chapters keep working in both modes, because the
auxiliary files of the excluded chapters are still read:
see \cref{ch:banach} and \cref{eq:projection} on
\cpageref{eq:projection}.
\begin{figure}[htbp]
\centering
\includegraphics[width=.6\textwidth]{\subfix{figures/basis}} % subfix makes the
\caption{An orthonormal basis.}\label{fig:basis} % path work in both modes
\end{figure}
\end{document}
Paths: import, subimport and the file that moved
LaTeX resolves every filename against the directory of the master file — the one you typed at the command line — not against the file that contains the command. That is the entire reason "it worked until I put the chapters in a folder" exists. Inside chapters/02.tex, the line \includegraphics{figures/x} looks for ./figures/x beside main.tex, and finds nothing.
Three fixes, in increasing order of robustness. Set an explicit path with the import package: \import{chapters/}{02-hilbert} (note the trailing slash — import takes a directory, not a filename) makes the child's own relative paths resolve from the master again, and \subimport{sub/}{file} resolves relative to the importing file. Second, keep graphics in one place and index them from a variable: \graphicspath{{figures/}{chapters/figs/}}, which needs no package and no change of command. Third, put the file-name prefix in a macro of your own (\newcommand{\figpath}{figures}) and use \figpath/name everywhere, which is the only approach that also survives a rename.
A separate class of trouble is the file that is read by two different masters — a common section shared by a paper and a thesis, or a chapter compiled standalone. Here \subimport is the right tool because it resolves relative to the importing file rather than to the root, and the standalone class with class=report-style options handles the figure case. Do not solve it with \input{../shared/x} and hope: relative parent paths work on your machine and break inside Overleaf's zip, in CI, and in a repository where the folder layout is one level different.
Figures as separate documents
A TikZ or pgfplots figure can cost more to compile than the text around it, and the way to stop paying for it every run is to compile it once, separately, and include the resulting PDF. Two routes. The blunt one: keep figures/circuit.tex as a complete standalone document, compile it to circuit.pdf, and \includegraphics{figures/circuit} in the master — fast, reproducible, and it requires the human step of recompiling the figure when it changes. The automatic one: \usepackage{standalone} in the master and \includestandalone[width=.6\linewidth]{figures/circuit}, which compiles the child when needed and inserts it, honouring border and varwidth options written in the child's class options.
The standalone class is also the answer to "my included figure carries an inch of white space": the child document has no page geometry to inherit, so \documentclass{standalone} plus border=2pt crops to the drawn object and \includegraphics scales it. For a plot, \documentclass[tikz,border=2pt]{standalone} and a single \begin{tikzpicture} is the smallest working shape.
The cost of \includestandalone is real and worth knowing before a deadline: the child is compiled with its own engine call, so a project of 40 included figures can be slower than one monolithic file, and the child's preamble is loaded into the master under subpreambles=true, which can produce double-definition errors when the child and the master both load the same package with different options. In a document where figures change as often as the text, this is the wrong tool; where they are stable, it is the one that makes a rebuild take seconds.
% ============================================================
% figures/circuit.tex — a figure that is a document in its own right
% compile it alone to get circuit.pdf, or let standalone load the
% parent preamble with [class=..., mode=packages]
% ============================================================
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{circuits.ee.IEC}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw (0,0) to [battery] (0,3) to [resistor] (3,3) -- (3,0) -- (0,0);
\end{tikzpicture}
\end{document}
% In the master file, either of:
% \includegraphics[width=.5\textwidth]{figures/circuit} % after compiling it once
% \usepackage{standalone} ... \includestandalone[width=.5\textwidth]{figures/circuit}
Which file is the root, and how your editor knows
With a multi-file project the compiler always receives exactly one filename, and everything else is derived: \jobname is that name without extension, and it is what the auxiliary, table-of-contents and index files are named after. That is also why building a child file directly in a project that uses \include produces a second, smaller family of auxiliary files beside the child and confuses your clean-up script.
Editors need the root declared, because a chapter file opened on its own has no class to compile with. The portable convention is a magic comment on the first line of the child: % !TeX root = ../main.tex, honoured by TeXstudio, Kile, VS Code's LaTeX Workshop and emacs AUCTeX (with TeX-default-master-file or its auto-detection); the related % !TeX program = xelatex comment fixes the engine. Overleaf ignores neither in spirit but uses a stored "Main document" setting, which is per-project server state and is lost when you download and re-upload the sources — one of the small reasons a project can come back from Overleaf refusing to build.
To branch on the root from inside the source, expand first and then compare, because \jobname is a TeX string register rather than a macro: \edef\@root{main}\edef\@now{\jobname}\ifx\@root\@now ...\fi does the job, while a bare \ifx\main\jobname is never true. The same test in a single line is \ifnum\pdfstrcmp{main}{\jobname}=0 on pdfTeX and LuaTeX. The currfile package gives the same information more comfortably (\currfilename, \currfilepath), and the filehook package lets you run code when a named file is read, which is the clean way to apply a per-chapter header or reset a counter at a chapter boundary.
A note on \input syntax that bites in scripts: the LaTeX command form is \input{file} with braces; the raw TeX primitive is \input file \relax with a space and no braces, and it gobbles spaces while reading the name. Mixing the two produces the obscure "file `main.tex ' error" style message, where the stray character in quotes is a space that ended up inside the filename.
A layout that survives a year
What experienced projects converge on is boring and consistent: one root file; a preamble/ directory of \input fragments (packages, macros, theorem styles, list settings, each under a hundred lines); a chapters/ directory of \included units, numbered so that alphabetical order is document order; figures/ beside the sources it belongs to, or one flat figures/ with subfolders mirroring chapter names; tables/ for generated .tex output; one refs.bib; and the class or style file for the venue in the root, where the submission system will look for it.
Two files make the difference between that layout and a pile of folders. .latexmkrc pins the engine, the Synctex flag, the bibliography tool and the clean-up list so the editor, the terminal and CI all build the same way — the same argument the compiling lesson makes, applied to a project instead of a file. .gitignore lists the build products (*.aux, *.log, *.toc, *.out, *.ind, *.ilg, *.idx? — careful: .idx is generated, so ignore it too, but keep any hand-written .bib) plus *.synctex.gz, *.bbl only if your venue does not freeze it.
And one habit: never move a file without compiling. A renamed directory is the most common way a working project dies at 11pm, and the log message it produces — LaTeX Error: File `chapters/02-hilbert.tex' not found — names the exact string to fix. Grep the project for the old path before you commit the move; in a \include-based layout the paths live in one file, which is a point in favour of keeping the master root as the only place where any path is written.
\mathtt{main.aux} + \sum_{i} \mathtt{ch}i\mathtt{.aux} \;\longrightarrow\; \{\,(\text{key},\ \text{number},\ \text{page})\,\}
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
\text{source read} = \sum_{f \in F} \text{lines}(f), \qquad \text{compile time} \propto \text{source read}
\documentclass[12pt,a4paper,twoside]{book}
\documentclass[../main.tex]{subfiles} % path back to the root document
\documentclass[border=2pt,tikz]{standalone}
\mathtt{main.aux} + \sum_{i} \mathtt{ch}i\mathtt{.aux} \;\longrightarrow\; \{\,(\text{key},\ \text{number},\ \text{page})\,\}
Common mistakes
- Using
\includefor a fragment you wanted spliced in — a preamble file, a table, a paragraph. The forced\clearpagesends the material to a new page and the extra.auxfile makes the next partial build misleading. - Shipping a document built with
\includeonly{}active. Excluded chapters do not advance counters, so your chapter is "Chapter 1" and every reference into an excluded file prints a number from the last full build. - Writing
\include{sub/file}inside an environment or an argument.\includestarts with\clearpage, which is illegal in those places; use\inputor\subimport. - Resolving paths against the child file:
\includegraphics{figures/x}insidechapters/02.texlooks besidemain.tex. Use\import{chapters/}{02},\graphicspath, or\subfix{}when children also compile standalone. - Committing
\includeonly{}, the.auxnest, or the child's\documentclass{subfiles}prologue to a shared repository, so that a co-author's build silently differs from yours. - Relying on
% !TeX rootcomments that your collaborator's editor ignores, then hearing "the chapter file does not compile". Keep one documented command line (latexmk -pdf main.tex) that works with no editor open.
Questions about this lesson
What is the difference between input and include in LaTeX?
\input is a plain text splice at the exact point it appears: no page break, no separate auxiliary file, usable anywhere including the preamble. \include issues \clearpage before and after, writes labels and citations into its own file.aux, and can be switched off with \includeonly. Use \include for chapters and \input for fragments.Why is includeonly a bad idea for the final build?
How do I compile a single chapter of a LaTeX book?
\includeonly{chapters/ch02} in the master and compile the master, or make the chapter a subfiles document that starts with \documentclass[../main.tex]{subfiles} and has its own \begin{document}. The first is faster and keeps one build; the second gives a standalone PDF of that chapter with the real preamble, and needs \subfix{} for graphics paths.My included file cannot find its images. Why?
\import{chapters/}{file}, declare the extra directories once with \graphicspath{{figures/}{chapters/figs/}}, or with subfiles wrap the path in \subfix{}.Can I nest \include inside \include?
\include gets the \clearpage treatment and the auxiliary-file redirection, and \includeonly will not select the inner one. Nest \input inside an included chapter instead, which is transparent and has no state of its own.Should figures be separate .tex files?
\includegraphics the result, or use the standalone package with \includestandalone to automate it. If they change while you write, include the drawing code directly with \input — \includestandalone recompiles the child on every run and can be slower than one file.Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.