文档基础知识
作为第一份 $\LaTeX$ 文件, 我们希望输出
Hello World!
通过这个简单的例子,让大家熟悉$\LaTeX$的语法。
\documentclass{report}
\begin{document}
% This is the first Latex document. More to be added.
Hello World!
\end{document}
- %代表评论,之后同一行的字都不会被输出。
- 第一行$\text{\documentclass{report}}$表示这份文件的类型是report. 其他的类型还有article, book, letter, IEEEtran等等。同时也可以标示出来一些诸如字体大小的选项,用[]。
比如说, $\text{\documentclass[12pt]{report}}$表示这篇文件的类型是report,正文的字体是12号字。
- $\text{\documentclass{report}}$ 和 $\text{\begin{document}}$ 之间的地方是preamble,用来声明使用哪些包(package),自定义方程,等等。
- $\text{\begin{document}}$ 标志着正文的开始, $\text{\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}
- \caption: 表格的题目
- \centering: 表格整体要居中
- \hline: 横线
- {l l} 表格内的第一和第二列左对齐