$\LaTeX$ 教程

安装

对于这个教程,建议大家自己练习一下,适当地修改例子,看不同的指令产生的影响,在线(推荐)或者离线。

回到页面顶端 ↑

文档基础知识

作为第一份 $\LaTeX$ 文件, 我们希望输出
Hello World!
通过这个简单的例子,让大家熟悉$\LaTeX$的语法。
\documentclass{report}
\begin{document}
% This is the first Latex document. More to be added. 
Hello World! 
\end{document}
作为简单的练习,建议读者输入一些被不规律空格分隔开的字母(比如说a 三个空格 b 两个空格 c),输出的结果和你想象的一样么?
接下来我们为文件加一些框架。如果你在写文章需要不同的章节,如何在 $\LaTeX$中实现?
\documentclass{report}
% preamble 
\begin{document}
% This is my first Latex document. More to be added. 
\section{Section 1}
This is section 1.  
\subsection{subsection 1.1}
This is subsection 1.1. Notice the differences in the font headings. 
\section{Section 2}
This is section 2. 
\section*{Section 3}
But I don't want my section to be numbered. 
\end{document}
注意到如果不希望章节有编码,可以用$\text{\section*{}}$. 现在我们的文章有内容,有结构,如何增加目录和标题呢?
\documentclass{report}
\begin{document}
\title{First Document}
\author{your name}
\maketitle

% This will generate a table of contents
\tableofcontents
\section{Section 1}
This is section 1.  
\subsection{subsection 1.1}
This is subsection 1.1. Notice the differences in the font headings. 
\section{Section 2}
This is section 2. 
\section*{Section 3}
But I don't want my section to be numbered. 
\end{document}
如果你用离线的软件,刚刚更改完章节的标题后点击 Build,也许你会注意到目录上章节的名字并没有改变。只需要再Build一次就好了。有些读者可能注意到一些文档的目录是可以直接点的,那是怎么做到的呢?通过使用package hyperref, 我们也可以制作有超链接的目录。
\documentclass{report}
\usepackage{hyperref}
\begin{document}
\title{First Document}
\author{your name}
\maketitle
% This will generate a table of contents
\tableofcontents
\section{Section 1}
This is section 1.  
\subsection{subsection 1.1}
This is subsection 1.1. Notice the differences in the font headings. 
\section{Section 2}
This is section 2. 
\section*{Section 3}
But I don't want my section to be numbered. 
\end{document}
因为我们的内容太短,你会看到目录和正文在同一页。如果我们要另起一页呢?$\text{\newpage}$.
\documentclass{report}
\usepackage{hyperref}
\begin{document}
\title{First Document}
\author{your name}
\maketitle
% This will generate a table of contents
\tableofcontents
\newpage 
\section{Section 1}
This is section 1.  
\subsection{subsection 1.1}
This is subsection 1.1. Notice the differences in the font headings. 
\section{Section 2}
This is section 2. 
\section*{Section 3}
But I don't want my section to be numbered. 
\end{document}
至此,你已经掌握了$\LaTeX$文件的基本要素了。

回到页面顶端 ↑

列举和表格

在文档中我们常常会用到列举。请在你的编译器里输入下面的例子。
\documentclass{report}
\begin{document}
\begin{itemize}
\item Item 1. 
\item Item 2.
\item \ldots 
\item Item n. 
\end{list}
\end{document}
	
enumerate是另外一个常用的列举方法,当我们希望列举的项有编码时。 仅需要把上述代码中的itemize换做enumerate就可以了。 List还有很多用途,请参考 list_structures
\documentclass{report}
\begin{document}

\begin{table}
\caption{The Number of Iterations}
\centering 
% Each element in the tabular is left aligned
\begin{tabular}{l l}
\hline
iter1 & iter2	\\
\hline
31 & 25	\\
20 & 17	\\
45 & 37	\\
23 & 19	\\
\hline
\end{tabular}
\end{table}

\end{document}

回到页面顶端 ↑

符号和公式

数学公式, 希腊字母,以及特殊符号都需要在数学环境中使用。如果是在同一行,使用$\text{\$表达式\$}$。 如果是另起一行, 使用$\text{\[表达式\]}$ 或者 $\text{\$\$表达式\$\$}$。
同一行的公式:$\frac{1}{2}(3x+5)$ 还是同一行
另起一行的公式: $$\frac{1}{2}(3x+5)$$

回到页面顶端 ↑

插图

如果想在文件中引入插图,需要先知道图片的位置和名称。
\documentclass{report}
\usepackage{graphicx}
\begin{document}
\includegraphics[scale=0.5]{tmp.png}
\end{document}
需要把例子中图片名称tmp.png换做是你的插图的名字,如果不在同一文件夹需要说明位置,scale是原图尺寸的百分比,也可以指定图的大小,把[scale=0.5]换成[width= cm, height= cm]空白处填入你想要的图片大小。 如果你需要为图片增加标题,那么需要放在figure的环境中,参考下面的例子。
\documentclass{report}
\usepackage{graphicx}
\begin{figure}
\includegraphics[scale=.5]{tmp.png}
\caption{fig: tmp}
\end{figure}

\end{document}

回到页面顶端 ↑