Reduce Repeated LaTeX Code with Commands, Environments, and Input Files
Reduce repeated LaTeX in three ways: commands for repeated formatting, environments for repeated structures, and \input for shared files. Start with material that really is identical and likely to change together; not every repeated passage needs an abstraction.
Repeated Formatting: Define a Command
This complete example defines \vect, \code, and \R once, then reuses them:
\documentclass[11pt]{article}
\usepackage{amsmath,amssymb}
\newcommand{\vect}[1]{\boldsymbol{#1}}
\newcommand{\code}[1]{\texttt{#1}}
\newcommand{\R}{\mathbb{R}}
\begin{document}
For $\vect{x}\in\R^d$, open \code{main.tex}.
\end{document}
Definitions in the preamble are available throughout the body; a definition in the body is available only after it is executed and within its scope. A name collision can produce Command \R already defined. Rename it, use \renewcommand for an intentional redefinition, or \providecommand when a fallback definition is appropriate.
\newcommand supports up to nine parameters. Even four or five can make calls difficult to read without consulting the definition.
Repeated Structures: Define an Environment
Tables with the same structure can share an environment. Here, resulttable accepts a column specification and a header row:
\documentclass[11pt]{article}
\usepackage{booktabs}
\newenvironment{resulttable}[2]{%
\begin{table}[htbp]\centering
\begin{tabular}{#1}\toprule
#2\\\midrule
}{%
\bottomrule\end{tabular}
\end{table}
}
\begin{document}
\begin{resulttable}{lcc}{Model & Accuracy & F1}
Baseline & 0.812 & 0.794 \\
Ours & 0.867 & 0.851 \\
\end{resulttable}
\end{document}
Each table supplies data rows; the environment supplies rules, columns, and float placement. Errors inside a definition may be reported at its expansion or use. Test the definition with one minimal call to isolate the problem.
Shared Definitions: Move Them to a File
Use \input rather than copying the same preamble into several files.
preamble.tex:
\usepackage{amsmath,amssymb}
\newcommand{\vect}[1]{\boldsymbol{#1}}
main.tex:
\documentclass{article}
\input{preamble}
\begin{document}
$\vect{x}$
\end{document}
In this example, main.tex is at the project root. Input paths resolve from the compilation working directory, not from the chapter containing the \input command. From chapters/intro.tex, use \input{chapters/intro2} for the sibling file when compiling from that root.
\include suits independent chapters: it inserts page breaks and creates separate auxiliary files. \includeonly can limit previews, but references to omitted chapters depend on auxiliary information. Always compile the full project for final checks. \include cannot be nested.
Decide Whether an Abstraction Is Worthwhile
Look for three conditions: repeated several times, truly identical rather than merely similar, and likely to be revised together. These are practical guidelines, not a strict numerical rule.
An abstraction used twice may force unnecessary navigation. Slightly different passages can require so many switches that the call becomes harder to read than the original.
Compile and Check the Critical Points
Check that definitions are loaded before the body, paths match the root-based layout, and equations, tables, and references still point to the intended objects.
| Error | Cause | Action |
|---|---|---|
Command \foo already defined. | A name already exists | Rename it or intentionally redefine it |
Environment foo undefined. | Definition appears too late or was not loaded | Move it to the preamble and check \input |
File 'chapters/intro2.tex' not found. | Path was written relative to the chapter instead of the compilation root | Correct the project-relative path |
| Log location does not match the apparent source location | Failure occurs during macro/environment expansion | Test only the definition and a minimal call |
Takeaways
- Put recurring formatting in commands and recurring structures in environments.
- Keep shared definitions in input files and chapter boundaries in the main file.
- Abstract only when it improves readability and maintenance.
Import the full directory into EasyLaTeX and select main.tex as the compilation entry point.