LaTeX入门(二)

LaTeX入门(二)

ZYX

参考文章:Learn LaTeX in 30 minitues

学会使用LaTeX

在LaTeX中加入数学

LaTeX的优势之一是可以轻松编写数学表达式。LaTeX提供了两种写作模式以排版数学公式:

  • 内联数学模式,用于撰写处于段落中的数学公式;
  • 展示数学模式,用于撰写不是文本或段落中一部分的表达式,并排版在独立的行中。

内联数学模式

示例:

1
2
3
4
5
\documentclass[12pt, letterpaper]{article}
\begin{document}
In physics, the mass-energy equivalence is started
by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Inline math mode

为了排版内联数学模式,可以使用这些分隔符对:\( … \), $ … $ 或者\begin{math}… \end{math},如下面的例子所示:

1
2
3
4
5
6
\documentclass[12pt, letterpaper]{article}
\begin{document}
\begin{math}
E=mc^2
\end{math} is typeset in a paragraph using inline math mode —— as is $E=mc^2$, and so too is \(E=mc^2\).
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Inline math mode 2

展示数学模式

在展示模式下的数学排版可以被编号,或者不编号,如下面的例子所示:

1
2
3
4
5
6
7
8
9
10
\documentclass[12pt, letterpaper]{article}
\begin{document}
The mass-energy equivalence is described by the famous equation
\[ E=mc^2 \] discovered in 1905 by Albert Einstein.

In natural units ($c = 1$), the formula expresses the identity
\begin{euqation}
E=m
\end{equation}
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Display math mode

为了排版展示模式数学,可以使用这些分隔符对:\[ … \], \begin{displaymath} … \end{displaymath} 或者\begin{euqation} … \end{equation}。历史遗留地,排版展示模式数学需要使用 $$ 符号分隔符,如:$$ …display math here …$$,但这个方法不再被推荐使用,使用\[ … \]以替代这种方式。

更完整的例子

以下的例子演示了很多使用LaTeX排版的数学内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\documentclass{article}
\begin{document}
Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can
be combined and nested to write expressions such as

\[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q})\]

We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscipts and subscripts:

\[ \int_0^1 \frac{dx}{e^x} = \frac{e-1}{e} \]

Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are writtern as $\Omega$ $\Delta$.

Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc.
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

More complete math example

下一个例子使用equation*环境,由amsmath包提供,所以需要再preamble中加入以下行:

1
\usepackage{amsmath} % For the equation* environment

使用amsmath的更多信息见我们的帮助文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
\documentclass{article}
\usepackage{amsmath} % For the equation* environment
\begin{document}
\section{First example}

The well-known Pythagorean theorem \(x^2 + y^2 = z^2\) was proved to be invalid for other
exponents, meaning the next equation has no integer solutions for \(n>2\):

\[ x^n + y^n = z^n \]

\section{Second example}

This is a simple math expression \(\sqrt{x^2+1} \) inside text.
And this is also the same:
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.

This is a simple math expression without numbering
\[\sqrt{x^2+1}\]
separated from text.

This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

\ldots and this:
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

amsmath

LaTeX的数学可能性无穷无尽,见以下文章了解更多:

基本文档结构

摘要

科学论文通常会提供摘要以总结/概括它们的核心主题或论点。下面的例子展示了使用LaTeX的abstract环境排版一个摘要:

1
2
3
4
5
6
7
\documentclass{article}
\begin{document}
\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Abstract

段落和新行

有了摘要之后,我们可以开始写我们的第一个段落了。接下来的例子演示:

  • 如何通过按两次回车键来创建一个新段落,结束当前行并插入后续空白行。
  • 如何通过使用\\命令(双反斜杠)插入手动换行符来开始新行而不开始新段落;或者,使用\newline命令。

这个例子中的第三段演示了使用指令\\和\newline:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}
\begin{document}

\begin{abstract}
This is a simple paragraph at the beginning of the
document. A brief introduction about the main subject.
\end{abstract}

After our abstract we can begin the first paragraph, the press ''enter'' twice to start the second one.

This line will start a second paragraph.

I will start the paragraph and then add \\ a manual line break which causes this text to start on a new line but remains part of the same paragraph. Alternatively, I can use the \verb|\newline|\newline command to start a new line, which is also part of the same paragraph.
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Paragraph and newline

注意LaTeX会自动缩进段落——除了紧挨着标题的段落,例如章节或者小节,我们将在接下来看到这一点。

新用户被建议多重\\或者\newline不应该被用来“模拟”间距较大的段落,因为这有可能干扰LaTeX的排版算法。推荐的方法是继续使用空白行来创建新的段落,不要用任何的\\,然后通过在preamble增加\usepackage{parskip}加载parskip包

更多段落相关的信息可以看如下的文章,也给出了使用\\的建议:

章节和小节(Chapters and sections)

更长的文章,无论(irrespective of)使用什么创作软件,通常被分为parts(部分)、chapters(章节)、sections(节)、subsections(小节)等等。LaTeX也提供文档结构命令,但是可用的命令以及他们的实现(以及他们的作用)可能取决于使用的文档类(class)。举例来说(By way of example),使用 book 类创建的文档可以被划分为parts、chapters、sections、subsections等等,但letter类不支持任何指令来做到这些。

接下来的例子演示了用于基于book类构建文档的一些命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
\document{book}
\begin{document}

\chapter{First Chapter}

\section{Introduction}

This is the first section.

Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam lobortisfacilisis sem. Nullam nec mi et
neque pharetra sollicitudin. Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...

\section{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...

\subsection{First Subsection}
Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...

\section*{Unnumbered Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem...
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Chapters and sections

分段命令的名称大部分是不言自明(self-explanatory)的;例如,\chapter{First Chapter}创建了一个新的章节,标题为First Chapter,\section{Introduction}创建了一个名为Introduction的节,等等。节可以进一步被分为子节\subsection{…},甚至子子节\subsubsection{…}。节、子节的编号是自动的,但是可以通过使用相应的带星号版本(末尾带有星号)来禁用自动编号。例如\section*{…}和\subsection*{…}。

总的来说(Collectively),LaTeX文档类提供了以下的分节命令,每一个特定类都支持相应的子集。

  • \part{part}
  • \chapter{chapter}
  • \section{section}
  • \subsection{subsection}
  • \subsubsection{subsubsection}
  • \paragraph{paragraph}
  • \subparagraph{subparagraph}

特别的,\part和\chapter指令只在report和book文档类中可用。见该Sections and chapters文档以了解更多相关文档结构命令。

创建表

Overleaf同样提供三个选项来创建表:

  1. 使用可视化编辑器或代码编辑器工具栏中的 Insert Table 按钮。
  2. 当使用可视化编辑器时从另一个文档里复制并粘贴一个表。
  3. 在代码编辑器中撰写LaTeX代码来创建表。

如果是LaTeX新手,那么第一种方法,即使用可视化编辑器工具栏是一种开始的好方法,可以在可视化编辑器和代码编辑器中切换来查看表背后的代码。

这里,我们专注于选项3——创建表的最灵活的方法——并提供示例以演示如何在LaTeX中创建表格,包括添加行(规则)和标题。随着你获得经验,看我们这里的详细指导关于如何使用LaTeX创建表

在LaTeX中创建一个基本表

从一个例子开始演示如何排版一个基本表。

1
2
3
4
5
6
7
\begin{center}
\begin{tabular}{c c c}
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9
\end{tabular}
\end{center}

可以在Overleaf中查看效果。

示例输出如下:

Basic table

这个tabular环境是LaTeX的默认的创建表的方法。你必须指定一个参数给这个环境,这里是{c c c},告知LaTeX将会有三列,并且里面的文本会居中(c)。也可以使用r来右对齐文本,或者使用l来左对齐文本。对齐符号&被用来在一个表行中划定独立的表单元。为了结束一个表行可以使用\\指令。我们的表被包裹在一个center环境中,使表在页面的文本宽度内居中。

增加边界

tabular环境支持在表中增加水平和垂直线(规则)作为表的一部分:

  • 为了增加水平规则,在行的上方和下方,使用\hline指令
  • 为了增加垂直规则,在列之间,使用垂直线参数|

在接下来的例子里,参数是{|c|c|c|},声明了3个居中的列(centerd),每一列由一个竖线分隔开;另外,我们使用\hline来放置一个垂直规则在第一行的上方和最后一行的下方。

1
2
3
4
5
6
7
8
9
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{center}

可以在Overleaf中查看效果。

示例输出如下:

Tabular with lines

以下是一个更深远的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\begin{center}
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
\hline
2 & 7 & 78 & 5415 \\
\hline
3 & 545 & 778 & 7507 \\
\hline
4 & 545 & 18744 & 7560 \\
\hline
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\end{center}

可以在Overleaf中查看效果。

示例输出如下:

Further  tabular example

其中的[…ex]控制每列文字距离底部的距离。

标题、标签和引用

可以使用与图片大部分相同的方式起标题和引用表。唯一的区别是不是在figure环境下,而应该使用table环境。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Table \ref{table:data} shows how to add a table caption and reference a table
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col3 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
2 & 7 & 78 & 5415 \\
3 & 545 & 778 & 7507 \\
4 & 545 & 18744 & 7560 \\
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\caption{Table to test captions and labels.}
\label{table:data}
\end{table}

可以在Overleaf中查看效果。

示例输出如下:

Caption label and reference

增加目录表

创建目录表是很直接的,因为命令\tableofcontents几乎为你做了全部的工作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
\documentclass{article}
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{August 2022}
\begin{document}

\maketitle

\tableofcontents

\section{Introduction}

This is the first section.

Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam lobortisfacilisis sem. Nullam nec mi et
neque pharetra sollicitudin. Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...

\section*{Unnumbered Section}
\addcontentsline{toc}{section}{Unnumbered Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...

\section{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

Table of contents

节、子节和章节会自动被包含在目录表中。为了手动增加条目,例如一个没有编号的节,如示例中那样使用指令\addcontentsline。

下载你完成的文档

更多见Export your work from Overleaf

寻找并使用LaTeX包

LaTeX不仅提供了重要的排版功能,还通过使用附加包提供了可扩展性框架。不是试图提供“尝试做任何事”的指令和特征,LaTeX被设计成可扩展的,允许用户加载外部的代码体(package),提供更专业的排版能力,或者扩展LaTeX的内置功能,例如排版表。如上面的节添加图片,graphicx包通过提供指令扩展了LaTeX以引入图像文件,通过在preamble中使用以下指令加载:

1
\usepackage{graphicx}
加载包

如上文提到的那样,在preamble中使用\usepackage命令来加载包,但是由于许多LaTeX包提供了一组选项,可以用于配置它们的行为,使用\usepackage指令通常看上去像这样:

1
\usepackage[options]{somepackage}

方括号告知LaTeX当加载某些包时应该应用哪一组选项。在用户请求的选项集中,单个选项或设置通常用逗号分隔;例如,geometry包提供了许多选项来配置LaTeX中的页面布局。所以一个典型的geometry包的使用看上去可以是如下的样子:

1
2
\usepackage[total={6.75,8.75in},
top=1.2in, left=0.9in, includefoot]{geometry}

这个geometry包是全球LaTeX社区成员编写和贡献的软件包的一个例子,任何想使用它的人都可以免费使用。

如果一个LaTeX包不提供任何选项,或者用户想要使用默认的选项值,可以如下加载:

1
\usepackage{somepackage}

当使用\usepackage[…]{somepackage},LaTeX寻找相应的文件叫somepackage.sty,需要加载和处理它——来使package指令可用。如果LaTeX不能找到somepackage.sty,它会报错,如下面的例子展示的那样:

1
2
3
4
5
\documentclass[12pt, letterpaper]{article}
\usepackage{somepackage}% a NON-EXISTENT package
\begin{document}
This will fail!
\end{document}

可以在Overleaf中查看效果。

示例输出如下:

No somepackage error

在CTAN找包的信息

各种包在Comprehensive TeX Archive Network发行,通常被称为CTAN。

可以浏览CTAN查找有用的包:

  • Title: LaTeX入门(二)
  • Author: ZYX
  • Created at : 2025-09-11 11:25:56
  • Updated at : 2025-09-11 11:25:56
  • Link: https://zyxzyx.top/LaTeX入门(二)/
  • License: This work is licensed under CC BY-NC-SA 4.0.