Latex之文件拆分及独立编译


发布日期 : 2025-12-23 07:22:36 UTC

访问量: 185 次浏览

最近使用Latex编写长文档,对于文件的组织有些困扰,找了一些资料。 如果LaTeX文档比较大,可以考虑拆分为几个部分。比如编辑一本书的时候可以将各章独立为 chap1.tex,chap2.tex,chap3.tex,然后在主文件main.tex中包含进来:

documentclass{book}
begin{document}
title{A LaTeX Book}
author{cohomo@blogbus}
date{}maketitle
input{chap1}
input{chap2}
input{chap3}
end{document}

上面的input命令可以改为include,区别在于,input可以放在导言区和正文区,包含的内容不另起一页;而include只能放在正文区,包含的内容另起一页。另外CJK中还有CJKinput和CJKinclude命令。还有个问题就是,如何使得各章既可以被包含在另一个文件中也可以独立编译呢?方法是将main.tex和chap1.tex作如下修改:

% main.tex
documentclass{book}
defallfiles{}
begin{document}
title{A LaTeX Book}
author{cohomo@blogbus}
date{}maketitle
input{chap1}
input{chap2}
input{chap3}
end{document}
% chap1.tex
ifx allfiles undefined
documentclass{article}
begin{document}
title{Something in Title}
author{cohomo@blogbus}
date{}maketitle
else
chapter{Chap1's Title}
fi
section{First Section}
section{Second Section}
ifx allfiles undefined
end{document}
fi

这样编写长文档就很方便和灵活了。