步骤 1:修改 chap01.tex
% chap01.tex
% 条件判断:如果是单独编译,则加载完整结构;如果被主文档引入,则不加载
\ifx\maindocument\undefined % 检查是否被主文档引入(主文档会定义\maindocument)
% 单独编译时的文档结构
\documentclass{article} % 与主文档保持一致的文档类
\usepackage{amsmath, graphicx, subfig} % 与主文档保持一致的宏包
\begin{document}
\fi
% 以下是章节实际内容(被主文档引入的部分)
\chapter{第一章 引言} % 假设主文档是book类,子文档用\chapter
\section{研究背景}
...(你的内容)...
% 公式、图表等标签(确保不与其他章节重复)
\begin{equation}
E=mc^2 \label{eq:example}
\end{equation}
% 条件判断:单独编译时结束文档环境
\ifx\maindocument\undefined
\end{document}
\fi
步骤二:修改主文档 main.tex
% main.tex
\documentclass{book} % 主文档的文档类
\usepackage{amsmath, graphicx, subfig} % 主文档的宏包(子文档会复用这些)
\def\maindocument{} % 定义标志,告知子文档:现在是主文档引入模式
\begin{document}
\tableofcontents
% 引入子文档(此时子文档会跳过自己的\documentclass等命令)
\input{data/chap01}
% 其他章节
\input{data/chap02}
...
\end{document}</