博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LaTeX多文件编译的方法总结
阅读量:7021 次
发布时间:2019-06-28

本文共 2845 字,大约阅读时间需要 9 分钟。

 

LaTeX多文件编译的方法总结

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

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

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

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

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

 

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

\documentclass[fleqn]{article}\usepackage{subfiles}  \usepackage{D:/My_Preamble} %必须是绝对路径,才能让各个section*.tex在单独编译时使用到\title{This is title}\author{Jay Chou}\date{\today}\begin{document}\maketitleHello World!\subfile{sections/section1}\subfile{sections/section2}\bibliographystyle{unsrt}\bibliography{D:/My_Reference} %必须是绝对路径。但各个section*.tex单独编译时使用不到(这是一个缺点)\end{document}

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

\documentclass[../main.tex]{subfiles} %两个点代表返回上一级菜单\newcommand{\AAA}{\textbf{abcdefg}} % 这个'\AAA'命令只在这个tex文件里起作用\begin{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}\newtheorem{thm}{Theorem} \setcounter{thm}{0}\newcommand{\defn}{\overset{\Delta}{=}} \newcommand{\st}{\textrm{~s.t.~}} \newcommand{\supp}{\mathop{\rm 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}\newcommand{\skip@preamble}{%    \let\document\relax\let\enddocument\relax%    \newenvironment{document}{}{}%    \renewcommand{\documentclass}[2][subfiles]{}}\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}\endinput%%%% End of file `subfiles.sty'.

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

 

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

 

【转载】

你可能感兴趣的文章
Ubuntu MATE 15.04 Vivid Vervet Beta 1
查看>>
程序员的年终总结,各种版本各种残
查看>>
《Spring Data官方文档》翻译邀请
查看>>
Jquery绑定事件(bind和live的区别)
查看>>
Python爬虫:用BeautifulSoup进行NBA数据爬取
查看>>
吉利汽车:数字化转型从上云开始
查看>>
bash模板
查看>>
【Spark Summit East 2017】使用Spark, Kafka和Elastic Search的大规模预测
查看>>
机器学习——决策树算法原理及案例
查看>>
关注上帝的所想所爱,创造无限财富
查看>>
Cocos2d-x3.2 Sprite精灵类的创建与设置
查看>>
Starting MySQL.Manager of pid-file quit without updating file.[FAILED]
查看>>
phalapi-进阶篇7(使用缓存以及用redis拓展解决实际问题)
查看>>
深入浅出PostgreSQL B-Tree索引结构
查看>>
读代码整洁之道
查看>>
Git@OSC代码托管(含Git教程!important非命令行)
查看>>
PostgreSQL 如何高效解决 按任意字段分词检索的问题 - case 1
查看>>
从零开始学_JavaScript_系列(18)——dojo(7)(dojo中类的继承)
查看>>
图像处理------基于像素的皮肤检测技术
查看>>
Java8 Foreach操作
查看>>