Learn LaTeX in 30 Minutes

2020-02-21

30 分钟入门 LaTeX

本文译自Overleaf

内容

[TOC]

什么是 LaTeX?

LaTeX 是用来创建专业外观 ( professional-looking ) 文档的工具。 它基于 WYSIWYM ( 所见即所得 ) 的理念,这意味着你在编辑时只需关注文档的内容,计算机会自动处理字符排版。和 Microsoft Word 或 LibreOffice 类似,用户可以直接输入纯文本并且让 LaTeX 处理后续排版工作。

为什么要学习 LaTeX?

在全球范围内,LaTeX 被广泛应用于科技文档,书记以及许多其他形式出版物的排版。 它不仅能够创建排版美观的文档,也能够让用户快速地处理排版过程中的的复杂部分,例如输入的数学公式,创建的表格,参考文献的引用和创建文献目录 ( biblographies )。而且,LeTeX 能够在所有部分间保持一致的布局。此外,因为还有大量的开源的程序包 ( packages ) 可供 LaTeX 使用, LaTeX 的潜力无穷。有了这些程序包的加持,用户能大大扩展 LaTeX 的功能。例如添加脚注 ( footnotes ), 绘制原理图 ( schematics ),创建表格等。

人们使用 LaTeX 的一个重要原因是它将文档的内容和文档的样式剥离开来。这意味着一旦你写好了文档的内容,我们可以简单地改变文档样式。 类似地,你也可以创建样式模板来应用到不同的文档,让它们有相同的现实效果。据此,科技论文可以借助模板来进行版面处理。这些模板 ( templates ) 拥有预置制的布局,这意味着你只需要在其中填入内容。实际上,有成百上千的模板可以供我们使用,从个人简历到PPT,几乎涵盖方方面面。

编写你的第一支 LaTeX 程序片段

第一步是创建一个新的 LaTeX 项目。你可以在本地计算机上通过新建.tex 文件或在 Overleaf中开始一个新项目。首先,让我们举一个简单的例子:

\documentclass{article}

\begin{document}
First document. This is a simple example, with no 
extra parameters or packages included.
\end{document}

我们可以看到第一行是有缩进的,这说明 LaTeX 已经对它进行了排版。让我们继续看看它的每一行代码都意味着什么。

第一行代码声明了文档类型,被称为“” (class). “类” 决定着文档的整体外观 ( overall appearance ). 不同类型的文档需要定义不同的“类”。例如,个人简历和科技文档需要定义不同的“类”。在此例中,“类”被定义为 article,这是 LaTeX 中最简单最常见的类。面对其他类型的文档,你也许需要为他们定义不同的类比如 book 或者 report.

在以第一行后,我们写下了文档的内容。内容被包含在 \begin{document}end{document} 标签中。它也被叫做文档的“体” ( body )。你可以尝试在这里进行修改。想要在 PDF 文档中查看结果你需要对文档进行编译。如果在 Overleaf 中,只用点击 Recompile。( 你也可以设置对文档进行更改后进行自动重新编译,点击 Recompile 旁的小箭头,点选 Auto CompileOn。)

如果你在使用比较基础的文本编辑器比如 gedit, emacs,vim,sublime,notepad 等。 你需要手动进行编译。在计算机终端或命令行界面下键入 pdflatex <your document>。更多手动编译的参考信息见

如果你使用的是专业的 LaTeX 编辑器例如 TeXmaker 或 TeXworks,简单地点击 Recompile 按钮即可完成重新编译。有关重新编译的更多内容,请见软件对应的文档。

现在你已经掌握如何向文档中添加内容,我们的下一步是给文档添加标题 ( title )。在此之前,我们先得简要介绍 preamble

文档的导言 ( Preamble )

在之前的例子里文本被囊括在\begin{document}命令之中。在 .tex 文件中,在 \begin{document} 之前的所有部分被称为“序” ( preamble )。 我们在序中定义文档的类型,语言,所要使用的程序包等。例如,一个普通的文档的序如下所以:

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}

以下是每行代码的细节:

\documentclass[12pt, letterpaper]{article}

如之前所说,该行定义了文档类型。中括号内可以填入不止一个参数,这些参数间用逗号分隔。在本例中,额外的参数是字号 ( 12pt ) 和 页面尺寸 ( letterpaper )。当然,你也可以使用其他字号 ( 9pt,11pt,12pt )。如果没有指定字号,默认值为 10pt。对于页面尺寸而言,我们还可以选择 a4paperlegalpaper; 更多关于页面尺寸的细节可以前往 Page size and margins

\usepackage[utf8]{inputenc}

该行代码制定了文档的编码方式。推荐设置为 utf-8。 除非要特意设置为其他编码方式,否则只要你不清楚该设置什么编码方式,最好将此行添加到序中。

添加标题,作者和日期

想要给文档添加标题,作者和日期。我们可以在 preamble ( 不是在文档的主体 )中,添加三行。它们是:

\title{First document}

这是标题

\author{Hubert Farnsworth}

把作者信息添加在此处。如果需要,也可以在下一行添加致谢信息:

thanks{funded by the Overleaf team}

致谢信息可以被添加在 title 大括号内,显示在作者姓名之后。它将为大括号内的文本添加上角标和脚注。如果你要在文章中感谢某个机构,可以添加此项。

date{February 2014}

你可以手动添加日期。或者使用 \today 来添加,这样日期可以在编译文档时自动更新。

添加完以上各行后,你的序会像下面这样:

\documentclass[12pt, letterpaper, twoside]{article}
\usepackage[utf8]{inputenc}

\title{First document}
\author{Hubert Farnsworth \thanks{funded by the Overleaf team}}
\date{February 2017}

现在,你已经为你的文档添加了标题,作者和日期,你可以通过 maketitle 命令将以上信息在文件中显示出来。 \maketitle 命令应该出现在在文档 body 内任何需要显示标题的地方。

\begin{document}

\maketitle

We have now added a title, author and date to our first \LaTeX{} document!

\end{document}

添加注释 ( comments )

在许多情况下,添加注释 ( comments ) 是十分必要且有用的。 注释不会显示在最终文档中,它不会影响文档的最终内容。在 LaTeX 中,注释的前缀是%

\begin{document}

\maketitle

We have now added a title, author and date to our first \LaTeX{} document!

% This line here is a comment. It will not be printed in the document.

\end{document}

文字的粗体,斜体和下划线

现在,我们来了解一些简单的文字格式命令。

  • Bold:LaTeX 中,要显示为粗体文本应被写进 \textbf{...} 命令中。
  • 斜体:斜体文本应被写进 \textit{...} 命令中。
  • 下划线 :下划线文本被写进 \underline{...} 命令中。

例如:

Some of the \textbf{greatest}
discoveries in \underline{science} 
were made by \textbf{\textit{accident}}.

另一个有用的命令是 emph{...} 命令。emph 命令是否使文本显示为斜体,取决于被emph 包含的文本之外的上下文字体是否是斜体。如果上下文都是斜体,那么谨慎使用 emph 命令。请看以下示例:

Some of the greatest \emph{discoveries} 
in science 
were made by accident.

\textit{Some of the greatest \emph{discoveries} 
in science 
were made by accident.}

\textbf{Some of the greatest \emph{discoveries} 
in science 
were made by accident.}

此外,一些程序包比如 Beamer, 可以更改 \emph 命令的行为。

添加图片

我们现在将在 LaTeX 文档中添加图片。在 Overleaf中,我们需要先上传图片。

以下是插入图片的一个实例:

\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {images/} }

\begin{document}
The universe is immense and it seems to be homogeneous, 
in a large scale, everywhere we look at.

\includegraphics{universe}

There's a picture of a galaxy above
\end{document}

LaTeX 本身不能管理图片,你需要借助程序包来进行管理。程序包可以被用来改变 LaTeX 文档的默认外观或者为 LaTeX 添加更多功能性的内容。在本例中,你需要在文档中引用一张图片,所以你需要用到 graphicx 这个程序包。 该程序包提供了以下命令,\includegraphics{...}\graphicspath{...}。想要使用 graphicx 程序包,只需要将以下命令插入到 preamble 中:\usepackage{graphicx}

\graphicspath{ {images/} } 命令告诉 LaTeX 图片保存在名为当前目录下的 images 文件夹中。

\includegraphics{universe} 是向文档插入图片的命令。其中,universe 是不带拓展名的图片的文件名 ( universe 的实际格式是 PNG )。图片的文件名不应该包含空格和多个西文点号。

注意:文件的扩展名可以被引用,但是我们最好省略扩展名。如果我们省略扩展名,LaTeX 会寻找所有它支持的图片格式。另外,我们也建议文件后缀名都是用使用小写英文字符。更多系列请移步generating high resolution and low resolution images

图片的标题,标签和引用 ( Captions, labels and references )

图片可以通过 figure 命令来设置图片的标题,标签和引用:

\begin{figure}[h]
    \centering
    \includegraphics[width=0.25\textwidth]{mesh}
    \caption{a nice plot}
    \label{fig:mesh1}
\end{figure}

As you can see in the figure \ref{fig:mesh1}, the 
function grows near 0. Also, in the page \pageref{fig:mesh1} 
is the same example.

在例子中包含了许多重要的命令:

  • \caption{a nice plot}: 该命令设置图片的标题。标题可以在图片的上方,也可在图片下方
  • \label{fig:mesh}:如果你要在文档中引用该图片,那么通过该命令给图片设置一个标签。标签命令将为图片编号并且与下面要介绍的命令一起搭配后可使图片能够被你引用
  • \ref{fig:mesh1}: 该代码将被与被引用图片相关的数字所替代。

当我们将图片放置到 LaTeX 文档中后,我们应该始终将其放置在 figure 或其他相似环境下。这样, LaTeX 才能更好的对图片及后续文本进行排版。

注意:如果你在本机上调用了图片标题和引用,你需要对文档编译两次使得文件成功引用图片。但在 Overleaf 上会自动完成这一过程。

在 LaTeX 中创建列表

在 LaTeX 中创建列表很简单。你可以使用不同的列表环境 ( environments ) 来创建列表。环境是文档中的一个片段,片段中的显示效果不同于剩余文本。环境开始于 \begin{...} , 结束于 \end{...}

LaTeX 中有两类列表,有序列表和无序列表。两种类型的列表使用不同的环境。

无序列表

无序列表通过 itemize 环境来产生。列表中每个条目前必须有前缀 \item

\begin{itemize}
  \item The individual entries are indicated with a black dot, a so-called bullet.
  \item The text in the entries may be of any length.
\end{itemize}

默认情况下列表的每个条目都以黑点开头,这就是所谓的 bullet。条目中,文本的长度任意。

有序列表

有序列表在不同的环境中有相同的语法。我们通过使用 enumerate 环境来生成有序列表:

\begin{enumerate}
  \item This is the first entry in our list
  \item The list numbers increase with each entry we add
\end{enumerate}

同无序列表一样,有序列表每个条目前需要以 \item 开头。有序列表的序号从 1 开始。

向 LaTeX 添加数学模块

LaTeX 的主要优点之一就是简便的公式输入。 LaTeX 提供了数学公式的两种输入模式:“内联模式” ( inline ) 和“外显模式” ( display )。第一个模式中,我们把公式写入文本中的一部分。第二种模式中,公式不作为文本中的一部分,它们会被置于单独的行中,让我们来看看内联模式:

In physics, the mass-energy equivalence is stated 
by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.

我们可以使用以下三种分隔符在内联模式下引入公式:\( ... \)$ ... $ 或者 \begin{math} ... \end{math}。三种方法都可以,选哪个取决于个人喜好。

外显模式 ( displayed mode ) 有两种版本:有序号版和无序号版。

choice is a matter of taste

我们可以使用以下三种分隔符在外显模式下引入公式:\[ ... \]\begin{displaymath} ... \end{displaymath} 或者 \begin{equation} ... \end{equation}不推荐使用 $$ ... $$, 该命令会产生不一致的空格,在某些数学程序包中可能出问题。

重要提示:equation* 环境由外部程序包提供,详见amsmath article

许多数学相关的命令都需要 amsmath 程序包,在写数学相关内容前务必记得引用该程序包。下面给出一个基本数学模块命令的例子。

Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined an 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 superscripts and subscripts:

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

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

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

LaTeX 的数学模块蕴含无限可能。我们尽可能列出其中一些常用模块。想要了解更多可以点击以下链接。

基本编排 ( Formatting )

我们现在来看如何写摘要 ( abstracts ) 和如何将 LaTeX 文档编排成不同章,小节和段落。

摘要 ( Abstracts )

在科技文档中,将文章主题进行简要的概括是常见的做法。在 LaTeX 中,abstract 环境专门用来处理摘要。abstract 环境将会把特定格式的文本置于文档的顶部。

\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}

Abstractsmall.PNG

段落 ( Paragraphs ) 和新行 ( newlines )

\begin{document}

\begin{abstract}
This is a simple paragraph at the beginning of the 
document. A brief introduction about the main subject.
\end{abstract}
 
Now that we have written our abstract, we can begin writing our first paragraph.
 
This line will start a second Paragraph.
\end{document}

当我们撰写文档内容的时候,如果你需要新起一个段落,你必须按两下 “Enter” 键 ( 来插入一个双空白行 )。值得一提的是 LaTeX 会自动处理段落缩进。

要新起一行而不新起一段,插入一个 break line 点,我们可以通过两根反斜杠 \\ 或者 \newline 命令来实现。

⚠️ 不能通过 \\\newlines 并且在之间添加大量空格来“模拟”段落,因为这可能与 LaTeX 排版算法相冲突。推荐做法是使用双空白行来建立新段落,并且最好不要包含任何 \\,然后在 preamble 中添加 \usepackage{parskip}

你可以在 Paragraphs and new lines 中得到更多信息。

章节 ( Chapters and Sections )

用来组织文档的命令随着文档格式的变化而变化,组织文档的最简单形式是分节 ( sectioning ),分节功能在所有文档格式中都可用。

\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

\section{} 命令标记着新章节的开始,章节名要写在大括号中。章节编号会自动进行,你可以通过在命令和大括号之间中添加 * 来关闭章节自动编号功能,就像这样 \section*{}。我们也可以添加子章节 \subsection{} ,子子章节也是可行的 \subsubsection{}。基本的章节层级划分如下:

-1 \part{part}
0 \chapter{chapter}
1 \section{section}
2 \subsection{subsection}
3 \subsubsection{subsubsection}
4 \paragraph{paragraph}
5 \subparagraph{subparagraph}

⚠️ \part\chapter 只在 reportbook 中生效。

更多关于文档结构的讨论详见article about sections and chapters

创建表格

在 LaTeX 中创建一个简单的表格

下面是一个最简单的表格的例子:

\begin{center}
\begin{tabular}{ c c c }
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\  
 cell7 & cell8 & cell9    
\end{tabular}
\end{center}

LaTeX 中,默认的创建表格的方法是利用 tabular 环境。你必须为它指定参数,在本例中是 {c c c}。这个参数告诉 LaTeX 表格将会是 3 列,表格中每个项目的内容都会居中。可以使用 r 来对文本进行右对齐,使用 l 来左对齐。符号 & 用来每个表格项的分隔符。每行的对齐符号必须少于列数。我们使用 \ 作为表格的换行符。我们将整个表格打包在 center 环境中因此我们的表格能够显示在页面中部。

添加边界

tabular 环境非常灵活,你可以在每列之间放许多分割线

\begin{center}
\begin{tabular}{ |c|c|c| } 
 \hline
 cell1 & cell2 & cell3 \\ 
 cell4 & cell5 & cell6 \\ 
 cell7 & cell8 & cell9 \\ 
 \hline
\end{tabular}
\end{center}

TablesEx2.png

你可以通过 \hline 为列添加水平边界,通过 | 添加数值边界。

  • { |c|c|c| }: 这声明了列表有三列,三列被垂直线分隔。
  • \hline: 该命令将插入水平线。我们已经在顶部和底部设置了水平分隔符。事实上,你可以多次使用 \hline

以下是第二个例子:

\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}

有时候,创建表格非常需要技巧。你可以去 TablesGenerator.com 创建并导出 LaTeX 中 tabulars值的代码。

表标题,标签和引用 ( Captions, labels and references )

你能为表设置标题和引用。与对图片的处理相似。唯一的不同是我们使用 table 环境而非 figure

Table \ref{table:data} is an example of referenced \LaTeX{} elements.

\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||} 
 \hline
 Col1 & Col2 & Col2 & 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}

⚠️ If you are using captions and references on your own computer, you will have to compile the document twice for the references to work. Overleaf will do this for you automatically.

添加目录 ( Table of Contents )

要创建一个有内容的表是非常简单直观的,命令是\tableofcontents

\documentclass{article}
\usepackage[utf8]{inputenc}
 
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{ }
  
\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...
       
\addcontentsline{toc}{section}{Unnumbered Section}
\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}

所有的章节,子章节会自动进行编排并把页面显示在目录中。手动添加目录,比如你要添加一个未编号的章节,可以使用 \addcontentsline就像实例中那样。

Homebrew 更换镜像源

利用双栈VPS搭建6in4隧道

comments powered by Disqus