[翻譯] 30分鐘上手 LaTeX

原  文:Learn LaTeX in 30 minutes
譯  者:Xovee
翻譯時間:2020年5月5日

30分鐘上手 LaTeX

本指南的主要內容是介紹 LaTeX\LaTeX 的基本使用。你不需要事先掌握關於 LaTeX\LaTeX 的任何知識,在你學習完本文後,你將會創建你的第一個 LaTeX\LaTeX 文檔,並且掌握關於 LaTeX\LaTeX 的一些基礎的用法。
PS:如果您還沒有註冊過 Overleaf,希望您能通過我的分享鏈接進行註冊:https://www.overleaf.com?r=969b656f&rm=d&rs=b

什麼是 LaTeX\LaTeX

LaTeX\LaTeX (發音類似於 LAY-tek 或者 LAH-tek,中文的話類似於雷坦克) 是一個創建專業文檔的工具。它基於 WYSIWYM (就是這個奇怪的名字)理念,也就是說,你只需要專注於文檔的內容,讓計算機來處理文檔的格式。與 Microsoft Word 或者 LibreOffice Writer 不同,LaTeX\LaTeX 用戶只需要輸入純文字而不需要關心文檔的格式。

爲什麼學習 LaTeX\LaTeX

因爲 LaTeX\LaTeX 在世界各地出版的各類科學文獻、書籍中得到了廣泛的使用。它不僅可以創建出有着漂亮排版的文檔,還可以讓用戶非常方便的處理排版中非常複雜的一些問題,例如輸入數學公式、創建表格、引用、參考文獻,以及全文統一的格式。而且,LaTeX\LaTeX 有着數不清的開源庫,它可以做到的事情可以說是有無限可能。這些開源庫允許用戶用 LaTeX\LaTeX 做許多事情,例如添加腳註、繪製綱要、創建表格,等等。

人們使用 LaTeX\LaTeX 的一個重要的原因是,它將文檔的內容和格式分開。這意味着當你完成了文檔的內容創作之後,你可以輕易地修改文檔的排版和格式。類似的,你可以創建一種可以讓許多文檔都使用的格式。這樣,許多學術期刊可以創建適用於它們論文的模版。這些模版已經定義好了排版和格式,你只需要添加論文的內容,即可創建出統一風格的論文文檔。事實上,你可以在網上找到數不清的模版,包括簡歷和幻燈片。

編輯你的第一個 LaTeX\LaTeX 文檔

第一步:創建一個新的 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\LaTeX 已經幫你處理了一些排版任務:對段落的第一行進行了縮進。讓我們看看代碼中的每一個部分都起了什麼作用。

在 Overleaf 中打開一個例子

代碼中的第一行聲明瞭文檔的類型,也稱爲 。類控制着文檔的所有樣式。不同類型的文檔需要定義不同類型的類,例如,簡歷和學術論文所需要的類是不一樣的。在這個例子中,文檔的類是article,它在 LaTeX\LaTeX 中是最簡單也是最常用的類。其他文檔類型還有bookreport等。

在定義了文檔的類型之後,你開始輸入文檔的內容,它包含在\begin{document}\end{document}這兩個標籤之間。這又被稱爲文檔的 正文。你可以在這裏開始輸入和修改文檔的內容。爲了在 PDF 中看到這些輸入的效果,你需要編譯這個文檔。在 Overleaf 中,你只需要點擊 Recompile 即可。(你還可以將你的項目設定爲在編輯時自動重編譯:點擊 ‘Recompile’ 按鈕旁邊的小箭頭,然後將 ‘Auto Compile’ 設定爲 ‘On’。)

如果你在使用基礎的文字編輯器,例如 gedit、emacs、vim、sublime、notepad 等,你需要手動地編譯文檔。首先在你的計算機的命令行中運行 pdflatex <your document> 指令。這裏有着更多的信息。

如果你使用一個特定的 LaTeX 編輯器,例如 TeXmaker 或者 TeXworks,點擊 Recompile 按鈕即可。查閱這些程序的文檔,如果你對過程還不太清楚的話。

現在你已經學習瞭如何給你的文檔添加內容,下一步是給文檔添加一個標題,爲了做到這一點,我們必須簡單地介紹一下 序言(preamble)。

文檔的序言

在上一個例子中,文字是在指令\begin{document}之後輸入的。而在這個指令之前 .tex 文件中的所有內容被稱爲序言(preamble)。在序言中,你可以定義文檔的類型,你所使用的語言,你想引入的包,以及許多其他的模塊。下面展示了一個普通文檔的序言:

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

下面介紹每一行的作用:

\documentclass[12pt, letterpaper]{article}
像之前說的那樣,這行命令定義了文檔的類型。它通常還有一些額外的參數,定義在方括號之中。這些參數必須用逗號來分割。在這個例子中,這些額外的參數設定了文檔的字體大小(12pt),和文檔的大小(letterpaper)。當然,你可以設定其他類型的字體大小(例如9pt、10pt、11pt),如果沒有指定這個參數,那麼文檔的字體大小將設定爲默認的10pt。對於文檔的大小來說,常用的選項包括 letterpaper、a4paper、legalpaper 等;頁的大小和邊距中詳細介紹了這些參數。

\usepackage[utf8]{inputenc}
這行命令定義了文檔的編碼形式。你可以簡單地忽略它,或者設定爲其他的值,當然,utf-8 是比較推薦的選項。除非你真的需要另外一種編碼方式,或者你不確定該用哪種編碼方式,在序言中添加這一行。

添加標題、作者和日期

爲了在文檔中添加標題、作者以及日期,你需要在序言中添加如下三行命令(不要在文檔的正文中添加)。這些命令是:

\title{First document}
這是標題。

\author{Hubert Farnsworth}
這裏你定義文檔的作者,以及,你可以添加下面一行可選的命令:

\thanks{funded by the Overleaf team}
這行命令可以在作者的名字之後添加,也就是在作者指令的花括號之內。它會添加一個上標和一個腳註。當你需要在文檔中致謝機構的時候,這個命令很有用。

\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指令來讓這些命令生效。這個命令應該出現文檔的正文裏。

\begin{document}

\maketitle

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

\end{document}

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

輸入註釋

在你寫代碼的時候,添加一些註釋是非常有用的。註釋是一種不會出現在文檔最終輸出之中的文字,它不會對文檔造成任何影響。註釋對你組織文檔、添加筆記、或者調試代碼有着很大的幫助。在 LaTeX\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}

在這裏插入圖片描述

在 Overleaf 中打開這個例子。

粗體、斜體、下劃線

我們現在來學習一些基礎的文字格式指令。

  • 粗體:在 LaTeX\LaTeX 中,粗體文字定義在 \textbf{...} 命令中。
  • 斜體:在 LaTeX\LaTeX 中,斜體文字定義在 \textit{...} 命令中。
  • 下劃線:在 LaTeX\LaTeX 中,下劃線文字定義在 \underline{...} 命令中。

下面是一些例子:

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

在這裏插入圖片描述
另外一個非常有用的指令是強調\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指令的行爲。

在 Overleaf 中打開這個例子。

添加圖片

我們現在來看如何在 LaTeX\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}

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

LaTeX\LaTeX 本身不能管理圖片,所以你需要導入一個包。包可以被用來改變 LaTeX\LaTeX 文檔默認的外觀,或者添加更多特定的功能。在這個例子中,你需要在文檔中添加一個圖片,所以你需要用到 graphicx 包。這個包引入了一個新的命令,\includegraphics{...}\graphicspath{...}。爲了使用 graphicx 包,在文檔的序言中添加如下命令:\usepackage{graphicx}

\graphicspath{ {images/} } 命令告訴 LaTeX\LaTeX 當前目錄下存儲圖片的文件夾的名字。

\includegraphics{universe} 命令是實際上用來在文檔中添加圖片的命令。在這裏 universe 是圖片的文件名(沒有文件名的後綴),所以 universe.PNG 就是 universe。文件名不能包含空格,或者多個點(.....)。

注意:文件名的後綴是可以使用的,但是不推薦那麼做。當文件名沒有後綴時,LaTeX\LaTeX 會搜索所有支持的文件格式。在上傳文件的時候,推薦使用小寫的文件名後綴。更多的信息請查看生成高清或低清的圖片

Captions、標籤、引用

你還可以給圖片添加標題、標籤,以及引用。請看下面的例子:

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

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

在這個例子中有三個重要的指令:

  • \caption{a nice plot}:顯然,這個命令設定了圖片的標題。如果你創建了多個圖片,那麼這個命令就應該在那裏使用。你可以將其放在圖片的上面,也可以放在圖片的下面。
  • \label{fig:mesh1}:如果你需要在文檔中引用你所添加的圖片,那麼用這個指令給圖片設定一個標籤。這個標籤會給圖片們進行數字排列,使用下面的這個命令可以讓你引用這些圖片。
  • \ref{fig:mesh1}:這個命令會生成圖片所代表的數字。

當你在 LaTeX\LaTeX 文檔中放置圖片時,把命令放在一個 figure 環境中,從而使 LaTeX\LaTeX 恰當地在文檔中設定這個圖片出現的位置。

注意:如果你在你的本地計算機中使用了 captions 和引用,你需要編譯你的文件兩次來讓引用正確地工作。在 Overleaf 中,你不需要這樣做。

LaTeX\LaTeX 中創建列表

LaTeX\LaTeX 中創建列表是非常簡單點。你可以使用不同的列表環境。環境是文檔中特殊的章節,它們的表現方式各不相同。它們一般開始於\begin{...},結束於\end{...}

一般來說,列表可以分爲兩大類:有序列表和無序列表。它們有着不同的環境。

無序列表

無序列表由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}

在這裏插入圖片描述
默認情況下,每一個列表項都由一個黑色的點開始,也被稱爲子彈。列表項中的文字一般沒有長度限制。

在 Overleaf 中打開這個例子。

有序列表

有序列表的語法與無序列表相同。我們使用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來輸入,這些列表項會自動地進行數字排序。這些數字從一開始計數。

在 Overleaf 中打開這個例子。

LaTeX\LaTeX 中輸入數學公式

使用 LaTeX\LaTeX 的一大好處就是它可以方便的輸入和展示數學公式。LaTeX\LaTeX 支持兩種公式輸入方法:行內模式和展示模式。行內模式下的數學公式是文本中的一部分。展示模式下的公式則不屬於文本的一部分,它們會獨立地進行展示。下面是一些行內公式的例子:

One of the main advantages of LATEX is the ease at which mathematical expressions can be written. LATEX allows two writing modes for mathematical expressions: the inline mode and the display mode. The first one is used to write formulas that are part of a text. The second one is used to write expressions that are not part of a text or paragraph, and are therefore put on separate lines. Let’s see an example of the inline mode:

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

在這裏插入圖片描述
爲了使用行內公式,你可以選用以下三種定義符之一:

  • \(...\)
  • $...$
  • \begin{math}...\end{math}

它們的功能都是一樣的,到底使用哪種定義符則取決於使用者的喜好了。

展示模式 下的公式則有兩種:有公式編號的,以及沒有公式編號的。

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{equation}
E=m
\end{equation}

在這裏插入圖片描述
爲了在展示模式中輸入公式,你可以使用如下幾種定義符:

  • \[...\]
  • \begin{displaymath} ... \end{displaymath}
  • \begin{equation} ... \end{equation}

$$ ... $$定義符是不推薦大家使用的,因爲它有時候會造成不一致的間距,或者在某些數學包中不能正常工作。

重要提示:equation*環境是由一個外部包提供的,請參考amsmath

在 Overleaf 中打開這個例子。

許多數學模式命令要求引入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{dx}{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.

在這裏插入圖片描述

在 Overleaf 中打開這個例子。

LaTeX\LaTeX 中有着數不清的支持輸入數學公式的功能和外部包。你可以閱讀我們提供的一些其他文章:

基礎排版

我們現在來看看如何寫摘要,以及如何將一個 LaTeX\LaTeX 文檔分爲不同的章節和段落。

摘要

在學術論文中,使用摘要是常見的做法。在 LaTeX\LaTeX 中有一個專門的 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}

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

段落

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

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

在寫文檔的時候,如果你需要開始一個新的段落,你必須輸入Enter鍵兩次(來插入一個空白行)。需要注意的是,LaTeX\LaTeX 會自動給段落首行進行縮進。

如果你想在直接從下一行中輸入內容,而不是創建一個新的段落,那麼你可以使用 \\ 命令(兩個反斜槓)或者 \newline 命令。

使用這兩個命令的時候要注意,它們不應當被用來模擬段落之間的大段空白,這樣做會擾亂 LaTeX\LaTeX 的排版算法。推薦的做法是使用雙空行來創建新的段落,然後在序言中使用\usepackage{parskip}包。

你可以在這篇文章中找到更多的信息。

章節

組織文檔的命令依賴於文檔的類型,最簡單的組織方法就是使用章節。

\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

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

\section{}命令標記着一個新節的開始,花括號之中的文字設定了節的標題。節會自動地用數字排序,你也可以在指令中使用*來創建沒有數字排序的節。除此之外,還有更多的節的命令:\subsection{}\subsubseciton{}等等。它們之間的等級如下所示:

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

需要注意的是:\part\chapter只在reportbook類型的文檔中可用。

更多的信息請參考這篇文章

創建表格

創建一個簡單的表格

請看下面的例子:

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

在這裏插入圖片描述

tabular環境是 LaTeX\LaTeX 中默認創建表格的環境。你必須給它指定一個參數,在這個例子中是{c c c}。這個參數告訴 LaTeX\LaTeX 這個表格有着三個列,每個列中的文字都是居中對齊的。你還可以使用r或者l來向右或者向左對齊。對齊符號&用來分割不同的單元格。每一行中&的數目必須比列數少一個。\\命令用來切換到表格的下一行。我們使用center環境來放置表格,使其在頁面中居中顯示。

在 Overleaf 中打開這個例子。

添加邊框

你還可以在tabular環境中給每個列或行添加邊框。

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

在這裏插入圖片描述

你可以使用水平線指令\hline和垂直線指令|來給表格添加邊框。

  • { |c|c|c| }:這個命令定義了三個列,它們由垂直線分割開來。
  • \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}

在這裏插入圖片描述
LaTeX\LaTeX 中創建表格有時候會比較麻煩,你可以使用一個方便的在線工具來創建表格:TablesGenerator.com。其中 File > Paste table data 選項允許你從表格應用中複製和粘貼數據。

在 Overleaf 中打開這個例子。

表題、標籤,以及引用

與圖片相同,你可以給表格添加標題、標籤、引用。唯一不同的地方是圖片中你使用figure環境,表格使用table環境。

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}

在這裏插入圖片描述
在 Overleaf 中打開這個例子。

注意:如果你在本地計算機上使用表題和引用,你需要對文檔進行兩次編譯來使其正常工作。在 Overleaf 則不需要這樣做。

添加目錄

創建文檔的目錄是非常直接的,使用\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命令。

在 Overleaf 中打開這個例子。

下載文檔

你可以在左上角的菜單中點擊 PDF 來下載文檔。或者在 PDF 預覽框的上面點擊 Download PDF 來快速下載文檔。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章