LaTeX多文件编译的方法总结

LaTeX多文件编译的方法总结

 

在编写LaTeX文档的时候,由于文档的section较多,或者section的编写时间各不相同,我们可能碰到如下问题:

1、由于想分开编写各个section

2、preamble太多,想专门弄一个文件放preamble

3、想用bibtex来生成参考文献

我分别参考了这个几个网页:111 222,再结合自己的上手体会,给出下面这个(还凑合的)解决方案。

我们首先建立一个文件夹“project”,在下面分别创建“main.tex”文件“sections”文件夹。在“main.tex”里面写入:

复制代码
documentclass[fleqn]{article}

usepackage{subfiles}  
usepackage{D:/My_Preamble} %必须是绝对路径,才能让各个section*.tex在单独编译时使用到

	itle{This is title}
author{Jay Chou}
date{	oday}

egin{document}

maketitle

Hello World!

subfile{sections/section1}

subfile{sections/section2}

ibliographystyle{unsrt}
ibliography{D:/My_Reference} %必须是绝对路径。但各个section*.tex单独编译时使用不到(这是一个缺点)
end{document}
复制代码

“section1.tex”文件这样编写,并放到“sections”文件夹里:

复制代码
documentclass[../main.tex]{subfiles} %两个点代表返回上一级菜单


ewcommand{AAA}{	extbf{abcdefg}} % 这个'AAA'命令只在这个tex文件里起作用

egin{document}

%From there on, type whatever you want.
section{This is section 1}

Hello, this is section 1.
end{document} 
复制代码

其他的section的tex文件照着这个格式写就行了。“My_preamble.sty”文件如下编写,并放到D盘目录下:

复制代码
ProvidesPackage{msqmypreamble}


usepackage{amsmath, amssymb, amsthm}
usepackage{cite}
%usepackage{graphicx, graphics} % Allows including images
% etc
% etc
% etc %setlength{voffset}{-2.0cm} %setlength{parskip}{0.2cm} ewtheorem{thm}{Theorem} setcounter{thm}{0} ewcommand{defn}{overset{Delta}{=}}
ewcommand{st}{ extrm{~s.t.~}}
ewcommand{supp}{mathop{ m supp}}
% etc
% etc
% etc
复制代码

现在,把各个section共同的preamble都写到“My_Preamble.sty”文件中,然后将这个文件放到一个路径下,然后在“main.tex”文件中像4行那样使用这个sty。为什么必须是绝对路径,这是为了让之后的各个“section*.tex”文件也能找到这个文件在哪,从而使用里面的命令。  在各个“section*.tex”文件中,你可以再附加一个本section独有的preamble,就想第三行的“AAA”那样。但这个“AAA”只能在本tex文件中使用。

然后是bibtex。各个“section*.tex”在单独编译的时候无法找到参考文件,及时在“main.tex”中bib文件的路径是绝对路径。这算是一个小瑕疵(很惭愧)。将bib文件单独拿出来放在一个地方的作用是使得每次收集bibtex信息可以集中在一起。

之后,我们可以编译这个“main.tex”文件,得到包含各个section的总文件,也可以单独编译各个“section*.tex”文件,并且各个“section*.tex”文件可以使用My_Preamble这个preamble,很方便。但不足之处是,单独编译各个“section*.tex”文件时,若文件中应用了bib文件中的条目,输出结果是显示不出来的,之后在编译总文件时才能正确显示。

关于label的引用:各个“section*.tex”内部最好不要使用同名的label标号(例如 label{eq:1} ),使用相同的标号会冲突的。同时,各个“section*.tex”之间可以相互应用对方的标号,当编译“main.tex”时,这些相互引用的标号就会显示出来(单独编译某个“section*.tex”显示不出来)。

最后是这个“subfiles.sty”文件,它的内容如下:

复制代码
%% This is file `subfiles.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% subfiles.dtx  (with options: `package')
%% 
%% Copyright 2002 Federico Garcia
%% 
NeedsTeXFormat{LaTeX2e}
ProvidesPackage{subfiles}[2002/06/08 Federico Garcia]
DeclareOption*{PackageWarning{CurrentOption ignored}}
ProcessOptions
RequirePackage{verbatim}

ewcommand{skip@preamble}{%
    letdocument
elaxletenddocument
elax%
    
ewenvironment{document}{}{}%
    
enewcommand{documentclass}[2][subfiles]{}}

ewcommandsubfile[1]{egingroupskip@preambleinput{#1}endgroup}
endinput
%%
%% End of file `subfiles.sty'.
复制代码

创建个txt文件,把如上代码复制进去,然后把txt文件的文件名改为“subfiles.sty”,放到装“main.tex”的文件就可以使用了。

写了这么多,应该能 +1s 吧,蛤蛤蛤!

【转载】http://www.cnblogs.com/mashiqi

原文地址:https://www.cnblogs.com/tsingke/p/6390900.html