大型文档源文件拆分编辑编译\include{filename}

本文详细介绍了如何使用LaTeX中的include命令将大型文档拆分为多个源文件进行编译,通过在主文档中定义导言区并包含子文档,实现结构化管理与高效编辑,同时展示了实例代码以辅助理解。
大型文档,如果把所有的文字都录入在同一个.tex文件中,那个文件的体积是不可估量的,文件的结构式混乱不堪的,文字的定位也是令人头疼的。幸亏latex提供了结构化的处理命令---include。
命令\include{filename}(filename不包含后缀名.tex)能够把那个文件filename的内容编译时插入到当前位置。

要点:

  • 在主文档(例如 main.tex)中书写导言区,包含哪些宏包,对整个文档的风格、字体、行距、引用进行定义。
  • 在主文档的正文区使用命令 \include{filename}来包含子文档。
  • 在子文档里直接以\chapter{} 、\section{}开头,不需要导言区、标题、作者等信息了。
  • 可以再任何一个文件中引用其他文件的标签。
请看下面的实例:
  • main.tex

   
  1. % !TeX:pdflatex,pdfTeXify
  2. % 测试大型文档拆分多个源文件进行编译。
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. %% main.tex
  5. %% 只在 main.tex 当中建立导言区,并include子文件
  6. %% 子文件不需要导言区、begin{document},只需
  7. %% \chapter{}、\section{}等等。
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. \documentclass[UTF8]{ctexbook}
  10. \usepackage{graphicx}
  11. \usepackage[CJKbookmarks=true,colorlinks,linkcolor=black,anchorcolor=blue,citecolor=green]{hyperref}
  12. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  13. %%%%%%%%%% Text body %%%%%%%%%%
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. \begin{document}
  16. \youyuan
  17. \sffamily
  18. \title{源文件拆分编译}
  19. \author{Lin\TeX9527}
  20. \date{2014--09--04}
  21. \maketitle
  22. \tableofcontents
  23. \include{file1}
  24. \include{file2}
  25. \end{document}

  • file1.tex

   
  1. \chapter{水果}
  2. \section{葡萄}
  3. 葡萄,紫黑紫黑的,甜,真甜。
  4. \section{西瓜}
  5. 西瓜,圆又圆。
  6. \section{狗狗和西瓜}
  7. 见第\pageref{fig:dog}页的图\ref{fig:dog}。

  • file2.tex

   
  1. \chapter{动物}
  2. \section{人}
  3. 人,人心隔肚皮。
  4. \section{狗}
  5. 狗是人类的好朋友。
  6. \begin{figure}[htbp]
  7. \centering
  8. % Requires \usepackage{graphicx}
  9. \includegraphics[scale=0.5]{dog}\\
  10. \caption{两只很萌的哈士奇}\label{fig:dog}
  11. \end{figure}

这时整个工程的结构文件:
261415494358284.png

可以再任何一个源文件中点击编译按钮。






转载于:https://www.cnblogs.com/LinTeX9527/p/3994804.html

#include <iostream> #include <fstream> #include <filesystem> #include <vector> #include <sstream> #include <algorithm> #include <cctype> #include <unordered_set> namespace fs = std::filesystem; // 辅助函数:修剪字符串两端的空白 std::string trim(const std::string& str) { size_t start = str.find_first_not_of(" \t\r\n"); size_t end = str.find_last_not_of(" \t\r\n"); if (start == std::string::npos || end == std::string::npos) return ""; return str.substr(start, end - start + 1); } // 辅助函数:分割字符串 std::vector<std::string> split(const std::string& str, char delimiter) { std::vector<std::string> tokens; std::string token; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { token = trim(token); if (!token.empty()) { tokens.push_back(token); } } return tokens; } int main(int argc, char* argv[]) { if (argc != 2) { std::cerr << "Usage: " << argv[0] << " <imakefile.mk>\n"; return 1; } fs::path imakefilePath = fs::absolute(argv[1]); if (!fs::exists(imakefilePath) || !fs::is_regular_file(imakefilePath)) { std::cerr << "Error: IMakefile not found: " << imakefilePath << "\n"; return 1; } // 获取项目名(父目录名) fs::path projectDir = imakefilePath.parent_path(); std::string projectName = projectDir.filename().string(); // 读取IMakefile内容 std::ifstream infile(imakefilePath); if (!infile.is_open()) { std::cerr << "Error: Unable to open file: " << imakefilePath << "\n"; return 1; } std::string line; bool osWinb64Found = false; std::string objectType; std::unordered_set<std::string> compileDefs; std::vector<std::string> sysLibs; // 解析IMakefile while (std::getline(infile, line)) { line = trim(line); // 检查OS=winb_64 if (line.find("OS=") == 0) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string value = trim(line.substr(pos + 1)); if (value == "winb_64") { osWinb64Found = true; } } } // 解析BUILT_OBJECT_TYPE if (line.find("BUILT_OBJECT_TYPE=") == 0) { size_t pos = line.find('='); if (pos != std::string::npos) { objectType = trim(line.substr(pos + 1)); } } // 解析ADDED_FLAGS if (line.find("ADDED_FLAGS=") == 0) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string flags = trim(line.substr(pos + 1)); // 移除引号并分割 flags.erase(std::remove(flags.begin(), flags.end(), '\"'), flags.end()); for (const auto& flag : split(flags, ' ')) { if (flag.find("-D") == 0) { compileDefs.insert(flag.substr(2)); } } } } // 解析GenericWincFlags if (line.find("GenericWincFlags=") == 0) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string flags = trim(line.substr(pos + 1)); flags.erase(std::remove(flags.begin(), flags.end(), '\"'), flags.end()); for (const auto& flag : split(flags, ' ')) { if (flag.find("-D") == 0) { compileDefs.insert(flag.substr(2)); } } } } // 解析SYS_LIBS if (line.find("SYS_LIBS=") == 0) { size_t pos = line.find('='); if (pos != std::string::npos) { std::string libs = trim(line.substr(pos + 1)); libs.erase(std::remove(libs.begin(), libs.end(), '\"'), libs.end()); for (const auto& lib : split(libs, ' ')) { sysLibs.push_back(lib); } } } } infile.close(); // 检查是否找到OS=winb_64 if (!osWinb64Found) { std::cerr << "Error: OS=winb_64 not found in IMakefile\n"; return 1; } // 创建CMakeLists.txt fs::path cmakePath = projectDir / "CMakeLists.txt"; std::ofstream outfile(cmakePath); if (!outfile.is_open()) { std::cerr << "Error: Unable to create CMakeLists.txt\n"; return 1; } // 写入CMake内容 outfile << "cmake_minimum_required(VERSION 3.13)\n"; outfile << "project(" << projectName << ")\n\n"; outfile << "set(CMAKE_CXX_STANDARD 11)\n"; outfile << "set(CMAKE_C_STANDARD 11)\n\n"; // 添加包含目录 outfile << "include_directories(\n"; outfile << " ${CMAKE_CURRENT_SOURCE_DIR}/LocalInterfaces\n"; outfile << ")\n\n"; // 添加编译定义 if (!compileDefs.empty()) { outfile << "add_compile_definitions(\n"; for (const auto& def : compileDefs) { outfile << " " << def << "\n"; } outfile << ")\n\n"; } // 收集源文件 outfile << "file(GLOB_RECURSE SOURCES\n"; outfile << " \"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp\"\n"; outfile << " \"${CMAKE_CURRENT_SOURCE_DIR}/src/*.c\"\n"; outfile << ")\n\n"; // 添加构建目标 if (objectType == "shared library") { outfile << "add_library(" << projectName << " SHARED ${SOURCES})\n"; } else if (objectType == "load module") { outfile << "add_executable(" << projectName << " ${SOURCES})\n"; } else if (objectType == "ARTIVE library") { outfile << "add_library(" << projectName << " STATIC ${SOURCES})\n"; } else { std::cerr << "Warning: Unknown BUILT_OBJECT_TYPE: " << objectType << "\n"; outfile << "# Unknown build type: " << objectType << "\n"; } outfile << "\n"; // 添加链接库 if (!sysLibs.empty()) { outfile << "target_link_libraries(" << projectName << "\n"; for (const auto& lib : sysLibs) { outfile << " " << lib << "\n"; } outfile << ")\n"; } outfile.close(); std::cout << "Successfully generated CMakeLists.txt at: " << cmakePath << "\n"; return 0; }将此代码拆分为一个cmaketransform.h和一个cmaketransform.cpp和一个main.cpp
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值