Split a Large TeX Document with \input
Use TeX's built-in \input primitive to split a monolithic source file into maintainable parts without extra packages.
09 Jul 2025, 22:33 UTC

The problem: one file becomes hard to maintain
A single TeX source file grows difficult to navigate as a document expands. Error messages reference line numbers in that one file, and version control diffs show unrelated changes together. TeX includes a built-in primitive, \input, that reads another source file as if its contents were typed at that spot. It works in plain TeX, LaTeX, and common engines such as pdfTeX, XeTeX, and LuaTeX without loading extra packages.
Useful takeaway: split chapters, sections, or appendices into separate .tex files and pull them into a main file with \input. Compilation behaves mostly as if the content lived in the main file, while editing and error diagnosis become more local.
Prerequisites
- A TeX engine installed: pdfTeX, XeTeX, or LuaTeX. The examples assume
pdflatex. - A plain-text editor and a terminal or command prompt.
- Read and write permissions for the project directory, because TeX writes the log and output PDF there.
- All part files saved with the
.texextension or without one; TeX appends.texby default when the file is not found.
Procedure: create a main file and include parts
- Create the main file. In your project directory, create
main.tex:
\documentclass{article}
\begin{document}
\input{introduction}
\input{methods}
\input{conclusion}
\end{document}
Run this command in the project directory:
pdflatex main.tex
Where to run: a terminal opened in the folder containing main.tex. Required permissions: read access to main.tex and any input files, plus write access for the generated .log, .aux, and PDF. Risk: if a part file is missing, TeX stops with I can't find file \`filename.tex'.
- Create part files. Create
introduction.texwith content that belongs inside the document body:
\section{Introduction}
This text lives in a separate file. TeX inserts it where \input appears.
Repeat for methods.tex and conclusion.tex. Each file should contain only document-body content or preamble-safe fragments; do not repeat \documentclass or \begin{document} inside a part file.
- Compile and check. After running
pdflatex main.tex, confirm:
- The output PDF contains text from every included file.
- The transcript (
main.log) lists each part file when TeX opens it. - If TeX reports an error, the line number refers to the part file currently being read, not
main.tex.
Concrete example: nested includes
You can nest \input calls. For example, methods.tex can contain:
\section{Methods}
\input{setup}
\input{procedure}
When main.tex inputs methods.tex, TeX then opens setup.tex and procedure.tex in sequence. Each nested file must have balanced braces and environments; an unmatched brace in setup.tex can leak into the surrounding file and produce confusing errors.
Diagnostic decision: where is the error?
| Observation | Likely cause | Check |
|---|---|---|
| PDF missing text from one part | File not found or typo in \input name | Look for I can't find file in main.log |
Error line points to chapter1.tex | Syntax mistake inside the included file | Open the named part file at the reported line |
| Fatal error about input stack depth | Circular \input dependency | Search part files for \input{main} or mutual includes |
| Text runs together after an included file | Space after filename was swallowed | Use braces: \input{chapter1} instead of \input chapter1 |
Limitations and cautions
\inputdoes not create a scope. Macro definitions and register assignments made in a part file persist after TeX returns to the main file unless you wrap them in a group such as\begingroup ... \endgroup.- TeX ignores spaces after a filename when the filename is not wrapped in braces. Write
\input{chapter1}, not\input chapter1, if text follows on the same line. - Circular includes exhaust TeX's input stack and abort compilation. TeX does not detect the cycle gracefully in most engines.
- File resolution depends on the engine and kpathsea configuration. Relative paths usually resolve from the current working directory; keep part files alongside
main.texor use explicit relative paths such as\input{parts/chapter1}.
Practical way to verify the result
Compile main.tex and open the produced PDF. Then search main.log for the names of your part files; TeX normally logs each file it opens. To confirm that errors localize correctly, temporarily introduce a brace mismatch in one part file, compile, and check that the log identifies that part file rather than main.tex. Remove the test mismatch afterward.
Recovery
Adding \input changes only source files, not system state. If the modular layout causes problems, remove the \input lines from main.tex and paste the part contents back into the main file, or restore the previous version from your version-control system. No package uninstallation or engine rollback is required.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.