LaTeX
项目结构
创建清晰的项目结构如下:
-
创建一个仅用于该项目的目录。我们将在以下部分称之为根目录。
-
在根目录下创建两个子目录,一个用于 LaTeX 文档,另一个用于图像。由于你将频繁引用它们,建议使用简短的名称。建议分别命名为
tex
和img
。 -
创建你的文档(我们称之为
document.tex
,但你可以使用自己喜欢的名称)和你的自定义包(例如mystyle.sty
);第二个文件将帮助你保持代码的简洁。 -
如果你按照上述步骤操作,这些文件应位于根目录中,并使用
/
表示各个目录:-
./document.tex
-
./mystyle.sty
-
./tex/
-
./img/
-
其他无关文件。
-
让 LaTeX 处理多个文件
随着工作量的增加,LaTeX 文件可能会变得庞大且难以管理,尤其是当你写的是长篇文章或书籍时。在这种情况下,分割你的工作到多个文件是一个好习惯。例如,如果你写的是一本书,将每一章写在单独的 .tex
文件中是非常合理的。LaTeX 使得这变得非常简单,通过以下两个命令:
-
\input{filename}
-
\include{filename}
input 与 include 的区别
它们的共同点是,在编译器处理基础文件并遇到 \input
或 \include
命令时,会读取 filename.tex
文件并根据基础文件中指定的格式命令处理其内容。这意味着你可以将所有格式选项放在基础文件中,然后在包含实际内容的文件中使用 \input
或 \include
。
两者的不同之处:
-
\include{texfile}
:-
不能嵌套
\include
。 -
适合章节切换,因为它会强制分页。
-
需要不带扩展名的
.tex
文件名。
-
-
\input{textfile}
:-
可以嵌套多个
\input
,类似文件的串联。 -
不强制分页,适合长文章的段落。
-
.tex
扩展名可选。
-
外部文件路径
编译器从基础 .tex
文件所在的目录引用外部文件。你可以使用绝对路径或相对路径。不同操作系统的文件路径会有所不同,使用绝对路径时要注意这一点。
-
绝对路径是指每个元素都明确指定的完整路径和文件名。例如:
\input{/home/user/texfiles/filename.tex}
-
如果你的写作项目在
texfiles
目录下创建了myfiles
目录,完整路径会是:\input{/home/user/texfiles/myfiles/filename.tex}
相对路径适用于便于在不同计算机间移动或避免重写路径名的情况。相对路径是指相对于当前目录定义的路径。LaTeX 使用标准的 UNIX 表示法:
-
./
表示当前目录 -
../
表示上一级目录 -
../../
表示上两级目录
例如:
\input{./filename.tex}
完全相同的效果。但是,如果您发现将所有文件放在当前目录的子目录中,名为 myfiles 更方便,您可以通过指定
\input{./myfiles/filename.tex}
来引用该文件。实际上,在我们上面的绝对路径示例中,您也可以相对引用该文件:
\input{../../documents/useful/foo.tex}
当然,所有常见的文件系统——Linux、Mac OS X 和 Windows——也都支持上面提到的 UNIX ./、../ 功能。请注意,LaTeX 即使在 Microsoft Windows 平台上(该平台使用反斜杠 \ 作为路径分隔符)也使用正斜杠 /。Windows 系统上的 LaTeX 实现会为您执行此转换,确保您的文档在所有安装中都是有效的。
LaTeX 与现代文件系统集成的这种灵活性使得您可以以适合您特定设置的方式输入文件。
在使用通过 \input 或 \include 导入的 LaTeX 文件时,重要的是要注意,路径是相对于主 .tex 文件所在的目录,而不是包含(或输入)文件所在的目录。如果您使用每章一个文件夹,并将每章的图形放在该章节的文件夹中,并使用 \include 将章节源文件读入主 LaTeX 文件的父文件夹中,这可能会成为一个问题。通过使用 import 包(见下文)可以解决这个问题。该包添加了命令 \subimport,允许相对于命令所在文件导入文件。
编译主文件
当您编译文档时,页面引用等内容将根据您使用 \input 和 \include 命令而变化。通常,LaTeX 用户只会对文档的部分进行编译,以检查某个章节的语法是否正确,并确保它看起来如作者所愿。完整的编译通常仅在生成完整草稿或最终版本时进行。在这种情况下,通常需要运行 LaTeX 两次或更多次,以解决所有的页面编号、引用等(尤其是如果您使用诸如 BiBTeX 的参考文献管理软件)。
检查您的工作中一个或多个组件的语法健全性的最简单方法是用百分号注释掉命令,例如:
\documentclass{article}
\begin{document}
%\input{Section_1}
%\input{Section_2}
%\input{Section_3}
\input{Section_4}
%\input{Section_5}
\end{document}
这段代码将以文章格式处理您的主文件,但只有文件 Section_4.tex 中的内容会被处理。如果那是您在发送给主要期刊之前需要检查的最后一部分,您只需删除所有的百分号,并重新运行 LaTeX,重复编译过程,以解决所有的引用、页面编号等。
使用 includeonly
使用此命令提供了更复杂的,因此更有用的功能。如果您在前言中包含以下命令,即在 \begin{document} 之前,
\includeonly{filename1,filename2,...}
只有大括号中指定的文件会被包括。请注意,您可以为此命令提供一个或多个文件作为参数:用逗号分隔,不能有空格。如果您使用绝对路径或相对路径来引用文件,请输入完整的路径。
这要求文档中有 \include 命令来指定这些文件。文件名应不带 .tex 扩展名:
\documentclass{book}
\includeonly{Chapter_1,Chapter_4} % 仅编译第 1 章和第 4 章,不允许使用空格
\begin{document}
\include{Chapter_1} % 省略 '.tex' 扩展名
\include{Chapter_2}
\include{Chapter_3}
\include{Chapter_4}
\end{document}
这段代码会处理主文件,但仅包括作者的第一章和第四章的内容(Chapter_1.tex 和 Chapter_4.tex)。重要的是,这种方式尽可能保留了之前运行的 .aux 信息,因此比上面临时建议要少干扰您的交叉引用。
Import
import 包提供了两个命令 \import 和 \subimport,它们非常类似于内置的 \input 和 \include 命令,但有两个附加功能:1. 允许对子文档进行嵌套导入,2. 允许相对目录的导入。
类似于 \input 和 \include,它在主文档的前言中为所有子文档提供支持。
示例:
我们有以下目录结构
main.tex
paragraphs
|
-– paragraph1.tex
paragraph2.tex
subparagraphs
|
-– subparagraph1.tex
文件 paragraph1.tex 通过命令 \input{paragraphs/paragraph1.tex} 被包含到主文件 main.tex 中,当文件 paragraph1.tex 再次包含另一个相对路径的文件时,例如 \input{subparagraphs/subparagraph1.tex},会出现错误,因为路径 subparagraphs/ 是相对于子文件夹 paragraphs 的,而 paragraphs 又是主文件的子文件夹。
我们可以通过在主文件 main.tex 中添加 package import 和使用命令 \subimport{paragraphs/}{paragraph1.tex} 来解决该错误,而不是使用 \input{paragraphs/paragraph1.tex}。这样,我们可以在包含该文件时指定相对路径。
子文件的单独编译
仅使用 \input 和 \include 的一个缺点是只能编译主文档。但是,您可能决定在单独的文本部分上工作,并希望将它们与主文件分开编辑和编译。有一些可用的包来解决这个问题。
Subfiles
subfiles 包提供了一种使用与主文档相同的前言编译文档各部分的方法。
在主文档中,必须加载该包:
\usepackage{subfiles}
替代使用 \input 和 \include,子文档必须如下加载:
\subfile{filename}
子文档必须以以下语句开始:
\documentclass[main.tex]{subfiles}
\begin{document}
并以以下语句结束:
\end{document}
可以通过在主文档中定义一个“标识”命令 \newcommand{\onlyinsubfile}[1]{#1} 来添加仅在子文档单独编译时应用的部分,然后在 \begin{document} 后使用 \renewcommand{\onlyinsubfile}[1]{} 来重写它。同样,您也可以为仅在主文档编译时出现的部分进行相同操作。
总结来说,主文档(main.tex)如下所示:
\documentclass{book}
\usepackage{subfiles}
\newcommand{\onlyinsubfile}[1]{#1}
\newcommand{\notinsubfile}[1]{}
\begin{document}
\renewcommand{\onlyinsubfile}[1]{}
\renewcommand{\notinsubfile}[1]{#1}
%% 我的文档内容
\subfile{chapter1}
%% 更多的文档内容
\end{document}
而第一章(chapter1.tex)如下所示:
\documentclass[main.tex]{subfiles}
\begin{document}
%% 我的第一章内容
\onlyinsubfile{这只会在编译 chapter1.tex 时出现(而非 main.tex 编译时)}
\notinsubfile{这只会在编译 main.tex 时出现(而非 chapter1.tex 编译时)}
%% 更多的第一章内容
%%
\end{document}
一些 Linux 发行版没有将 subfiles 包包含在它们的 LaTeX 发行版中,因为直到 TeXLive 2012 才包括该包。您可以从 CTAN 下载 subfiles.tds.zip。该包将包含两个文件 subfiles.cls 和 subfiles.sty。将这些文件移动到路径 /usr/share/texmf/tex/latex 下的名为 subfiles 的目录中。这样仍然无法使该包可用;必须首先执行 texhash 程序。现在您就可以使用了!
虽然 subfiles 无法使子文件的引用相对于其自身目录进行,但 \subimport 命令提供了此功能。
Subfiles 和 bibtex
在主文件和子文件中包含 bibtex 参考文献有点棘手。以下是一个解决方案:
主文件:
\documentclass[11pt,reqno]{amsart}
\usepackage{subfiles}
\def\biblio{\bibliographystyle{amsalpha}\bibliography{bibgraf}} % *修改:添加 \main/
来指定相对文件位置。
\begin{document}
\def\biblio{}
\subfile{sub.tex}
\bibliographystyle{amsalpha}
\bibliography{bibgraf}
\end{document}
子文件:
\documentclass[main.tex]{subfiles}
\begin{document}
这是一个测试 \cite{kato88:_commut_euler_navier_stokes% Kato & Ponce 1988, Comm. Pure Appl. Math. 41, 891
}
\biblio
\end{document}
Subfiles:主文件和子文件在不同子目录下
解决此问题的方案如下:主文件
\documentclass[12pt]{article}
\usepackage{subfiles}
\begin{document}
Hallo
\subfile{BlattAnalysis/blatt1}
\subfile{BlattAnalysis/blatt2}
\end{document}
BlattAnalysis 中的子文件如下:
\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../main.tex]{subfiles}
\begin{document}
Hallo
这是一个测试
\begin{equation}
\label{eq:sec1:1}
\int dx =0
\end{equation}
\end{document}
Subfiles:子文件和图片在不同子目录下
为了能够设置特定于子文件的图形路径,并且还能够将每个子文件构建为独立文档,图形路径需要提供两次。对于使用与上面相同设置的子文件 X,这将导致:
\makeatletter
\def\input@path{{../}}
\makeatother
\documentclass[../main.tex]{subfiles}
\graphicspath{
{"../myfiles/Blatt_X/Pictures/"}
{"../../myfiles/Blatt_X/Pictures/"}
}
\begin{document}
Hallo
这是一个测试
\begin{figure}[H]
\includegraphics{example.png}
\end{figure}
\end{document}
Standalone
The standalone
package is designed to move more in the opposite direction than subfiles
. It provides a way to import the preamble of child documents into the main document, allowing for a flexible way to include text or images in multiple documents (e.g., an article and a presentation).
In the main document, the package must be loaded as:
\usepackage{standalone}
Child documents are loaded using \input
or \include
.
The child documents contain, for example, the following statements:
\documentclass{standalone}
% Load any packages needed for this document
\begin{document}
% Your document or picture
\end{document}
In summary, the base document (main.tex
) looks like:
\documentclass{book}
\usepackage{standalone}
\begin{document}
%% my document content
\input{chapter1}
%% more of my document content
\end{document}
And Chapter 1 (chapter1.tex
) looks like:
\documentclass{standalone}
% Preamble
\begin{document}
%% my chapter 1 content
%%
%% more of my chapter 1 content
\end{document}
Including Complete PDF Documents or Single Pages
The pdfpages
package can be used to insert ready PDF files or separate pages and more pages per one page in any layout (e.g., 2×3).
The package has several options:
\usepackage[ options ]{pdfpages}
Options:
-
final
: Inserts pages. This is the default.
-
draft
: Does not insert pages, but prints a box and the filename instead.
-
enable-survey
: Activates survey functionalities (Experimental, subject to change).
Including a PDF Document:
\includepdf[ key=val ]{ filename }
Options for key=val
– a comma-separated list of options using the key=value
syntax. An up-to-date list of parameters can be found in the package documentation. Here are some commonly used parameters:
-
pages
: Selects pages to insert. The argument is a comma-separated list containing page numbers (pages={3,5,6,8}
), ranges of page numbers (pages={4-9}
), or any combination. To insert empty pages, use {}
. For instance, pages={3,{},8-11,15}
will insert page 3, an empty page, and pages 8, 9, 10, 11, and 15.
-
angle
: You can use the angle
option to rotate the included page, for example, for rotating a landscape document when the LaTeX document is in portrait. Example: angle=90
.
-
addtolist
: Adds an entry to the list of figures, the list of tables, or any other list (e.g., from float.sty
). This option requires four arguments, separated by commas:
-
page number
: The page number of the inserted page.
-
type
: Name of a floating environment (figure, table, etc.).
-
heading
: Title inserted into LoF, LoT, etc.
-
label
: Name of the label. This label can be referred to with \ref
and \pageref
.
For example:
\newcounter{inclPDFpage}
\includepdf[pages=-,pagecommand={\refstepcounter{inclPDFpage}\label{test.\theinclPDFpage}}]{toInclude.pdf}
The fourth page of toInclude.pdf is page \pageref{test.4} in the compiled document
You can also insert pages from several external PDF documents:
\includepdfmerge[ key=val ]{ file-page-list }
Several PDFs can be placed table-like on one page. See more information in the documentation.
从模块化文档生成单个 .tex 文件
如果您需要从模块化文档生成单个 tex 文件,可以使用以下几个脚本 [1]:
- 一个 Perl 脚本:latexpand
- 一个 C 脚本:flatex
- 该脚本的一个 Python 版本:flatex
- 另一个 C 脚本:flatten
文件 mystyle.sty
与其像通常那样将所有需要的宏包都放在文档的开头,最好的方法是将所有需要的宏包加载到一个名为 mystyle
的虚拟宏包中,这个宏包是您专门为您的文档创建的。这样做的好处是,您只需要在您的 main.tex
文档中添加一个 \usepackage
命令,从而保持代码的整洁。此外,所有关于您样式的信息都将保存在一个文件中,这样当您开始另一个文档时,您只需复制该文件并正确地包含它,您将拥有与之前完全相同的样式。
创建您自己的样式非常简单:创建一个名为 mystyle.sty
的文件(您可以随意命名,但必须以 ".sty" 结尾)。在 mystyle.sty
文件的开头写入:
代码段
\ProvidesPackage{mystyle}
然后像往常一样使用标准的 \usepackage{...}
命令添加所有您想要的宏包,更改所有您想要的变量的值等等。它就像将您在此处放置的代码复制并粘贴到您的文档中一样工作。
在写作过程中,每当您需要决定格式时,请为您自己的格式定义一个命令并将其添加到您的 mystyle.sty
文件中:让 LaTeX 为您工作。如果您这样做,当您改变主意时,更改它将非常容易。
这实际上是编写宏包过程的开始。有关更多详细信息,请参阅 LaTeX/宏。
有关您可以使用的几个宏包的列表,请参阅宏包列表部分。
主文档 document.tex
然后创建一个名为 document.tex
的文件;这将是主文件,您将编译它,即使您不应该经常需要编辑它,因为您将在其他文件中工作。它应该看起来像这样(这是 report
文档类的示例代码,但您可以轻松地将其更改为 article
或其他任何文档类):
代码段
\documentclass[12pt,a4paper]{report}
\usepackage{graphicx}% 在这里放置所有其他宏包:
\usepackage{mystyle}
\usepackage{hyperref}
\begin{document}
\input{./tex/title.tex}
%\maketitle
\tableofcontents
\listoffigures
\listoftables
\input{./tex/intro.tex}
\input{./tex/main_part.tex}
\input{./tex/conclusions.tex}
\appendix
\input{./tex/myappendix.tex}
% 参考文献:
\clearpage
\input{./tex/mybibliography.tex}
\end{document}
这里使用了前面章节中表达的许多代码。您导入唯一需要的宏包,即您的 mystyle.sty
(请注意,在代码中导入时不需要扩展名),然后您的文档就开始了。然后它插入标题:我们不喜欢 \maketitle
的输出,所以我们创建了自己的标题,它的代码将放在我们之前创建的名为 tex
的文件夹中的一个名为 title.tex
的文件中。如何在其中编写将在标题创建部分中解释。然后插入目录、图表目录和表格目录。如果您不需要它们,只需注释掉这些行。然后插入文档的主要部分。正如您所见,document.tex
中没有任何文本:所有内容都在 tex
目录中的其他文件中,这样您就可以轻松地编辑它们。我们将文本与结构代码分开,从而提高了 LaTeX 的“所见即意”的特性。然后我们可以看到附录,最后是参考文献。它位于一个单独的文件中。
一旦您创建了 document.tex
,您就不再需要编辑它了,除非您想在 tex
目录中添加其他文件,但这不会经常发生。现在您可以编写您的文档,将其分成任意数量的文件,并添加许多图片而不会感到困惑:由于您为项目设置了严格的结构,您将能够清晰地跟踪所有编辑。
一个建议:不要给您的文件命名为 "chapter_01.tex" 或 "figure_03.png",即尽量避免在文件名中使用数字:如果 LaTeX 自动给出的编号与您给出的编号不同(这很可能会发生),您会非常困惑。在命名文件时,停顿一下,想一个简短且能完全解释文件内容而又不会产生歧义的名称,当文档变得更大时,这将为您节省大量时间。
外部链接
- subfiles 宏包文档
- standalone 宏包文档
- pdfpages 宏包文档