Emacs+ LaTeX 快速上手

更新时间:2024-05-03 04:13:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Emacs + LaTeX 快速上手

?

本教程完全针对本校D215机房Ubuntu系统中的Emacs和LaTeX配置。关于如何配置,请看这里。

?

本教程中涉及的LaTeX源文件和图片都可以在这里找到。

目录

1 Emacs + AucTeX,60分钟快速入门

为什么非要推荐LaTeX?

这完全是出于个人喜好。从1996年开始接触计算机到现在,Windows、UNIX、MS-Word、LaTeX 我都用过了。我觉得我该把我认为优秀的东西推荐给你。即使你不感兴趣,但做为计科专业的学生你应该知道它的存在。

为什么非要推荐Emacs?

这不仅是出于个人喜好,也不仅是因为Emacs是最优秀的编辑器,我最基本的目的是,希望每一个计科专业的学生能熟练使用键盘。

1.1 放松心情

LaTeX很强大,但对于初学者来说,你不必关心它有多强大,因为最为常用的命令和环境不过就是那么几个。而且你也不必手工输

入这些命令,只要你用Emacs+AucTeX,几个简单的快捷键就足以满足你的基本需求了。对于格式复杂的需求,通常你只要套用模版就可以解决问题了。所以,大家只要把Emacs用熟,一切迎刃而解。

1.2 用LaTeX写文章就是在编程

我们先回忆一下用Emacs写一个 hello.c 的过程:

1. 打开Emacs;

2. 开始编辑一个新文件,名字叫 hello.c:

3. C-x C-f

在Emacs窗口的最下面(也就是 mini buffer 里)写上新文件的名字 hello.c:

hello.c C-j

4. 向文件里写东西:

5. #include

6. int main(int argc, char *argv[]) 7. {

8. printf (\); 9. return 0; 10.

}

保存:

C-x C-s

编译:

gcc hello.c

运行:

./a.out

再来看一下用Emacs写一个 hello.tex 的过程:

1. 打开Emacs;

2. 开始编辑一个新文件,名字叫 hello.tex:

3. C-x C-f

在Emacs窗口的最下面(也就是 mini buffer 里)写上新文件的名字 hello.tex:

hello.tex C-j C-j

4. 向文件里写东西:

5. \\documentclass{article} 6. \\begin{document} 7. Hello, world!

8. \\end{document}

保存:

C-x C-s

编译:

xelatex hello.tex

看结果:

evince hello.pdf

怎么样? hello.c 和 hello.tex 的编辑过程没什么分别吧。只要把Emacs用熟练,不管写什么程序,都是这么个过程。你

1. 不必学习VC去写C/C++, 2. 不必学习eclipse去写Java,

3. 不必学习MS-Word去写报告、幻灯片, 4. 不必学习……

一句话,“Everything Emacs”,可以省下大量不必要的学习时间。人生苦短,何必让你的生活被 VC/eclipse/MS-Word 搞得头昏脑胀呢? 简单而强大,本就是计科专业学生和非专业学生应有的不同 。

如果你对Emacs操作还很陌生,那么现在就打开Emacs

C-h t

重温一下那些基本操作吧。

1.2.1 什么是 C-x C-f ?

这么说,

1. 把你的双手在标准键盘上放好,

2. 左手小指稍向左移,按在 caps lock 键上。按住别动。(D215机房的 caps lock 键被我们改成 Control 键了) 3. 小指按在 caps lock 上别放开,左手无名指稍向下移,在 x 键上按一下就放开,这就是 C-x 。

4. 小指按在 caps lock 上别放开,左手食指在 f 键上按一下,这就是 C-f 。现在左手各指都可以放开了。

这就是 C-x C-f ,作用是要求打开一个文件, f 代表 file 。那么,告诉我

?

什么是 C-x C-s ?

什么是 C-x 2 ?什么是 C-x 3 ?什么是 C-x 0 ?什么是 C-x 1 ?什么是 C-x o ?

?

?

什么是 C-x h ?什么是 C-w ? 什么是 C-g ?

什么是 C-j ?什么是 C-i ?

?

?

注意, \\\\ 代表“强制换行”。 再新起一行,加上日期:

C-c C-m date C-j C-c C-m today C-j

其实,没有 \\date{\\today} 这一句系统会自动把今天的日期添加上的。而且 \\date{} 里面也不一定非要是当天的日期。 title, author, date 一般被叫做文章的 top matter 。 再新起一行,写

\\maketitle C-j

\\maketitle 自然是要排版 top matter 了。换句话说,不要标题的话可以省略掉这个命令。现在文章变成了这样:

1: \\documentclass{article} 2:

3: \\begin{document}

4: \\title{How to Structure a \\LaTeX{} Document} 5: \\author{Andrew Roberts\\\\ 6: School of Computing,\\\\ 7: University of Leeds,\\\\ 8: Leeds,\\\\

9: United Kingdom,\\\\ 10: LS2 1HE\\\\

11: \\emph{andyr@comp.leeds.ac.uk}} 12: \\date{\\today} 13: \\maketitle 14:

15: \\end{document}

好奇的话,现在可以编译一下,看看PDF文件的效果:

C-c C-c C-j C-c C-v

如果你的 xpdf 没出毛病的话,一个PDF文件应该显示在屏幕上了。如果 xpdf 不正常,那么就打开命令行,敲:

evince simple.pdf &

效果还满意吧?保持你的好奇心。在下面的操作中,你随时可以编译一下看看效果。

好了,回到Emacs。现在你的光标应该停在 \\maketitle 的下面一行。我们开始写“摘要”部分。敲键盘

C-c C-e

前面我们已经见到过 C-c C-e 了,就是要开始一个“环境”。接着来, mini buffer 里现在又有提示了:

Environment type: (default itemize)

是在问你要开始那种环境啊?默认是开始 itemize 环境,因为它是最常用的环境。但我们现在要写的是“摘要”,告诉它:

abstract C-j

abstract 就是“摘要”的意思。科技论文都是要有摘要的嘛。于是,你的文章变成了这样:

1: % 此处略去十数行 2:

3: \\maketitle 4:

5: \\begin{abstract} 6: |

7: \\end{abstract} 8:

9: \\end{document}

光标停在 \\begin{abstract} 和 \\end{abstract} 之间(第6行)。好,现在往摘要部分里填点东西:

1: % 此处略去十数行 2:

3: \\maketitle 4:

5: \\begin{abstract}

6: In this article, I shall discuss some of the

fundamental topics in

7: producing a structured document. This document itself does not go into

8: much depth, but is instead the output of an example of how to implement

9: structure. Its \\LaTeX{} source, when in used with my tutorial

10: provides all the relevant information. 11: \\end{abstract} 12: 13: |

现在,我们要接着上面的例子,写更多更长的东西了。为了编号方便,文章末尾的 \\end{document} 我也不再写出来了。

好, 按 C-n 把光标移到 \\end{abstract} 的下一行。让我们开始文章的第一节:

C-c C-s

s 代表 section, “节”的意思。 mini buffer 提示:

Level: (default section)

也就是问你,是不是要起一个新 section 啊?没错,我就是要起一个新的章节,于是直接 C-j 。 mini buffer 又提示:

Title:

也就是问你,章节标题是……?那就给它个标题吧,就叫“ Introduction ”。 C-j 之后, mini buffer 继续提示:

Label: sec:introduction

这是在问你,要不要给这个新章节打个标签,比如 sec:introduction, 以后也许要索引到它呢?这个暂时无关紧要, C-j 就是了。(第18行)

14: % 此处略去十数行 15:

16: \\end{abstract} 17:

18: \\section{Introduction} 19: \\label{sec:introduction} 20:

给这一节添加内容:

1: % 此处略去十数行 2:

3: \\section{Introduction} 4: \\label{sec:introduction} 5:

6: This small document is designed to illustrate how easy it is to create a well structured

7: document within \\LaTeX\\citee{lamport94}. You

should quickly be able to see how the article

8: looks very professional, despite the content being far from academic. Titles, section

9: headings, justified text, text formatting etc., is all there, and you would be surprised

10: when you see just how little markup was required to get this output.

11:

注意到了吗?在这一节里有一个新命令 \\cite{} (我?笔误?成了 \\citee{}, 以避开 org->html 转换的一个小bug), 这是在引用一个参考文献。先不管它,后面再说。 如法炮制,再添加几个章节:

12: \\section{Structure} 13: \\label{sec:structure} 14:

15: One of the great advantages of \\LaTeX{} is that all it needs to know is

16: the structure of a document, and then it will take care of the layout

17: and presentation itself. So, here we shall begin looking at how exactly

18: you tell \\LaTeX{} what it needs to know about your document.

19:

20: \\subsection{Top Matter} 21: \\label{sec:top-matter} 22:

23: The first thing you normally have is a title of the document, as well as

24: information about the author and date of

publication. In \\LaTeX{} terms,

25: this is all generally referred to as \\emph{top

matter}.

26: 27: |

注意到 \\emph{} 了吗?它代表 emphasize ,“强调”。英文习惯用斜体字来表示强调的东西,那么自然, \\emph{top matter} 就是把 top matter 排版成 top matter 了。

注意到 \\subsection{} 了吗?一会儿,我们还会看到 \\subsubsection 。不用解释吧,文章的章节次序是这样:

1. chapter 2. section 3. subsection 4. subsubsection 5. paragraph 6. subparagraph

其中, chapter 是给 book 和 report 用的,而 article 是从 section 开始。

现在我们就来一个 \\subsubsection 。不出所料的话,光标应该在第27行。那么就:

C-c C-s

mini buffer 提示:

Level: (default subsection)

当然输入:

subsubsection C-j

mini buffer 提示:

Title:

输入:

Article Information C-j

mini buffer 提示:

Label: sec:article-information

似曾相识吧?敲 C-j, 那么,你看到的是:

28: \\subsubsection{Article Information} 29: \\label{sec:article-information} 30:

也就是说,我们有了一个 subsubsection (第28行) 。现在,我们再添加一个 environment:

C-c C-e

mini buffer 提示:

Environment type: (default abstract)

我们当然不再需要 abstract 了,现在我们要的是 itemize ,也就是“不带序号的列表”。那么当然输入:

itemize C-j

于是看到:

31: \\begin{itemize} 32: \\item

33: \\end{itemize} 34:

光标停在 \\item 的后面(第32行)。非常好,这正是我想要的。直接输入如下文字:

\\verb|\\title{}| --- The title of the article.

然后,

M-return

也就是,左手拇指按住 Alt 键,同时右手小指去敲回车键。你会看到这样的效果:

1: \\begin{itemize}

2: \\item \\verb|\\title{}| --- The title of the article. 3: \\item

4: \\end{itemize} 5:

也就是说,不仅换了行,而且自动有了 \\item 等待你输入新的东西(第3行)。

你一定注意到了 \\verb|| 这个新命令。它的作用和bash命令行的单引号 (’) 是一样的。还记得吧,在命令行,单引号里的东西是原样输出的。 \\verb|| 里的东西也一样。 verb 是 verbatim 一词的缩写,就是“原样引用”的意思。好奇的话,就 C-c C-c 一下,看看效果。 好,继续输入:

\\verb|\\date| --- The date. Use:

得到:

1: \\begin{itemize}

2: \\item \\verb|\\title{}| --- The title of the article. 3: \\item \\verb|\\date| --- The date. Use: | 4: \\end{itemize} 5:

没什么好说的。现在我们要在 itemize 环境里面再来一个 itemize 。光标现在应该在第3行的最后。敲:

C-c C-e C-j

于是得到:

1: \\begin{itemize}

2: \\item \\verb|\\title{}| --- The title of the article. 3: \\item \\verb|\\date| --- The date. Use: 4: \\begin{itemize} 5: \\item

6: \\end{itemize} 7:

8: \\end{itemize} 9:

简单吧?不用说了,你肯定知道下面这些是怎么来的了吧。

1: \\begin{itemize}

2: \\item \\verb|\\title{}| --- The title of the article. 3: \\item \\verb|\\date| --- The date. Use: 4: \\begin{itemize}

5: \\item \\verb|\\date{\\today}| --- to get the date that the document is typeset.

6: \\item \\verb|\\date{}| --- for no date. 7: \\end{itemize} 8: \\end{itemize} 9:

编译一下,看看效果吧。

好了,请你现在照猫画虎,再来一个 subsubsection ,标题叫 Author Information 。模仿上面的东西,来得到下面的东西:

10: \\subsubsection{Author Information} 11: \\label{sec:author-information} 12:

13: The basic article class only provides the one

command:

14: \\begin{itemize}

15: \\item \\verb|\\author{}| --- The author of the

document.

16: \\end{itemize} 17:

18: It is common to not only include the author name, but to insert new lines (\\verb|\\\\|)

19: after and add things such as address and email

details. For a slightly more logical

20: approach, use the AMS article class (\\emph{amsart}) and you have the following extra

21: commands: 22:

23: \\begin{itemize}

24: \\item \\verb|address| --- The author's address. Use the new line command (\\verb|\\\\|) for line

25: breaks.

26: \\item \\verb|thanks| --- Where you put any

acknowledgments.

27: \\item \\verb|email| --- The author's email address. 28: \\item \\verb|urladdr| --- The URL for the author's web page.

29: \\end{itemize} 30:

怎么样,不太困难吧? 目前为止,我们用到的无非是下面这几个快捷键操作而已:

C-j C-c C-m C-c C-s C-c C-e M-return 下面让我们再起一个小节:

C-c C-s subsection C-j

Sectioning Commands C-j

C-j

得到:

31: % 此处略去数十行 32:

33: \\subsection{Sectioning Commands} 34: \\label{sec:sectioning-commands} 35:

添加一些文字:

1: % 此处略去数十行 2:

3: \\subsection{Sectioning Commands} 4: \\label{sec:sectioning-commands} 5:

6: The commands for inserting sections are fairly

intuitive. Of course,

7: certain commands are appropriate to different

document classes.

8: For example, a book has chapters but a article

doesn't.

9:

10: %A simple table. The center environment is first

set up, otherwise the

11: %table is left aligned. The tabular environment

is what tells Latex

12: %that the data within is data for the table. 13:

你应该能猜到,凡是跟在 % 后面的都是注释。 在这一小节,我们来尝试一下表格的输入。 先起一个新“环境”,叫 center :

C-c C-e center C-j

得到:

1: % 此处略去数十行 2:

3: \\subsection{Sectioning Commands} 4: \\label{sec:sectioning-commands}

5: The commands for inserting sections are fairly

intuitive. Of course,

6: certain commands are appropriate to different

document classes.

7: For example, a book has chapters but a article

doesn't.

8:

9: %A simple table. The center environment is first

set up, otherwise the

10: %table is left aligned. The tabular environment

is what tells Latex

11: %that the data within is data for the table. 12:

13: \\begin{center} 14:

15: \\end{center} 16:

center 当然是“居中”的意思了。在 center 环境里面,我们添加一个 tabular 环境:

C-c C-e tabular C-j

这时你会看到这样的提示:

(Optional) Position:

直接 C-j ,又看到提示了:

Format:

这是问你,表格的格式……有几列?每列之间要不要有竖线分割?……我的答案是这样:

|l|l|

也就是:竖线(|)小写L(l)竖线(|)小写L(l)竖线(|)。小写L代表 left ,也就是“左对齐”的意思。那么,你应该恍然大悟了,不就是……竖线-左对齐-竖线-左对齐-竖线嘛。那么,举一反三,除了小写L,我们还会见到r(右对齐)和c(居中)。现在,

C-j

得到如下结果:

1: % 此处略去数十行 2:

3: \\subsection{Sectioning Commands} 4: \\label{sec:sectioning-commands}

5: The commands for inserting sections are fairly

intuitive. Of course,

6: certain commands are appropriate to different

document classes.

7: For example, a book has chapters but a article

doesn't.

8:

9: %A simple table. The center environment is first

set up, otherwise the

10: %table is left aligned. The tabular environment

is what tells Latex

11: %that the data within is data for the table. 12:

13: \\begin{center}

14: \\begin{tabular}{|l|l|} 15:

16: \\end{tabular} 17: \\end{center} 18:

表格环境里也是可以有注释的:

1: % 此处略去数十行 2:

3: \\subsection{Sectioning Commands} 4: \\label{sec:sectioning-commands}

5: The commands for inserting sections are fairly

intuitive. Of course,

6: certain commands are appropriate to different

document classes.

7: For example, a book has chapters but a article

doesn't.

8:

9: %A simple table. The center environment is first

set up, otherwise the

10: %table is left aligned. The tabular environment

is what tells Latex

11: %that the data within is data for the table. 12:

13: \\begin{center}

14: \\begin{tabular}{|l|l|}

15: % The tabular environment is what tells Latex

that the data within is

16: % data for the table. The arguments say that

there will be two

17: % columns, both left justified (indicated by

the 'l', you could also

18: % have 'c' or 'r'. The bars '|' indicate

vertical lines throughout

19: % the table. 20: | (hline) 21: \\end{tabular} 22: \\end{center} 23:

现在我们开始画表格,边画边体会一下上面几行注释的意思吧。光标现在应该在第(hline)行,先画一条横线:

\\hline C-j

所谓 \\hline ,顾名思义,就是 horizontal line 。 开始第一行:

Command & Level \\\\ \\hline C-j

那个 & 就是两列之间的分隔符, \\\\ 我们见过,表示换行。 照猫画虎,把所有的行都加上,得到如下结果:

1: % 此处略去数十行 2:

3: \\subsection{Sectioning Commands} 4: \\label{sec:sectioning-commands}

5: The commands for inserting sections are fairly

intuitive. Of course,

6: certain commands are appropriate to different

document classes.

7: For example, a book has chapters but a article

doesn't.

8:

9: %A simple table. The center environment is first

set up, otherwise the

10: %table is left aligned. The tabular environment

is what tells Latex

11: %that the data within is data for the table. 12:

13: \\begin{center}

14: \\begin{tabular}{|l|l|}

15: % The tabular environment is what tells Latex

that the data within is

16: % data for the table. The arguments say that

there will be two

17: % columns, both left justified (indicated by

the 'l', you could also

18: % have 'c' or 'r'. The bars '|' indicate

vertical lines throughout

19: % the table. 20: \\hline

21: Command & Level \\\\ \\hline 22: \\verb|\\part{}| & -1 \\\\ 23: \\verb|\\chapter{}| & 0 \\\\ 24: \\verb|\\section{}| & 1 \\\\ 25: \\verb|\\subsection{}| & 2 \\\\ 26: \\verb|\\subsubsection{}| & 3 \\\\ 27: \\verb|\\paragraph{}| & 4 \\\\ 28: \\verb|\\subparagraph{}| & 5 \\\\

29: \\hline 30: \\end{tabular} 31: \\end{center} 32:

这张表格的效果应该类似于下面这样:

Command Level -1 0 1 2 3 4 5 \\part{} \\chapter{} \\section{} \\subsection{} \\subsubsection{} \\paragraph{} \\subparagraph{} 现在编译一下,看看效果,还过得去吧? 好了,表格画完了。再添加点文字:

1: % 此处删去数十行…… 2:

3: \\begin{center}

4: \\begin{tabular}{|l|l|}

5: % The tabular environment is what tells Latex

that the data within is

6: % data for the table. The arguments say that

there will be two

7: % columns, both left justified (indicated by

the 'l', you could also

8: % have 'c' or 'r'. The bars '|' indicate

vertical lines throughout

9: % the table. 10: \\hline

11: Command & Level \\\\ \\hline 12: \\verb|\\part{}| & -1 \\\\ 13: \\verb|\\chapter{}| & 0 \\\\ 14: \\verb|\\section{}| & 1 \\\\ 15: \\verb|\\subsection{}| & 2 \\\\ 16: \\verb|\\subsubsection{}| & 3 \\\\ 17: \\verb|\\paragraph{}| & 4 \\\\ 18: \\verb|\\subparagraph{}| & 5 \\\\ 19: \\hline 20: \\end{tabular} 21: \\end{center} 22:

23: Numbering of the sections is performed

automatically by \\LaTeX{}, so don't bother adding

24: them explicitly, just insert the heading you want between the curly braces. If you don't

25: want sections number, then add an asterisk (*)

after the section command, but before the

26: first curly brace, e.g., \\verb|section*{A Title

Without Numbers}|.

27:

现在我们讲讲“参考文献”。其实还是个 environment ,叫 thebibliography 。试试吧,

C-c C-e

thebibliography C-j

mini buffer 提示:

Label for BibItem: 99

这是问你,在引用参考文献时,采用两个字符宽的标签是否合适?你知道, 参考文献在文章中被引用到的时候,不都是以 [1],[2]...[99] 这样的形式出现吗?所谓“两个字符的宽度”,也就是说方括号里的数字不超过两个,也就是说你的文章中最多可以有 [1]...[99] 99个文献索引。这对于一篇普通的文章来说肯定是足够多了。

如果你还是不知道这合不合适,那么就假装合适, 按 C-j, mini buffer 提示:

(Optional) Bibitem label:

这是问你,要不要给每个参考文献条目加个标签?不理它, 按 C-j, mini buffer 提示:

Add key (default none):

这是必须要理的。所谓 key ,实际上就是一条参考文献的“标识号”,它是和前面我们见到的 \\cite{} 命令配合使用的。在引用一条参考文献时,就必然要通过它的标识号来唯一地找到它。比如 \\citee{lamport94} 就是要从参考文献列表中找到 lamport94 所对应的那一条。没明白?那么我们先给它一个 key ,等会儿编译一下,看看效果就明白了。输入:

lamport94 C-j

得到如下效果:

28: \\begin{thebibliography}{99} 29: \\bibitem{lamport94} 30: \\end{thebibliography} 31:

现在把参考文献写进去就行了:

1: % 此处略去数十行…… 2:

3: \\begin{thebibliography}{99} 4: \\bibitem{lamport94} 5: Leslie Lamport,

6: \\emph{\\LaTeX: A Document Preparation System}. 7: Addison Wesley, Massachusetts, 8: 2nd Edition, 9: 1994.

10: \\end{thebibliography} 11:

再加一条:

M-return C-j wikibooks C-j

http://en.wikibooks.org/wiki/LaTeX/simple.tex

得到:

1: % 此处略去数十行…… 2:

3: \\begin{thebibliography}{99} 4: \\bibitem{lamport94} 5: Leslie Lamport,

6: \\emph{\\LaTeX: A Document Preparation System}. 7: Addison Wesley, Massachusetts, 8: 2nd Edition, 9: 1994.

10: \\bibitem{wikibooks}

11: http://en.wikibooks.org/wiki/LaTeX/simple.tex 12: \\end{thebibliography} 13:

一篇像模像样的科技论文到此就算是大功告成了。

\\documentclass{article} \%usepackage{times}

\\begin{document}

% Article top matter

\\title{How to Structure a \\LaTeX{} Document} %\\LaTeX is a macro for printing the Latex logo \\author{Andrew Roberts\\\\ School of Computing,\\\\

University of Leeds,\\\\ Leeds,\\\\

United Kingdom,\\\\ LS2 1HE\\\\

\\emph{andyr@comp.leeds.ac.uk}} %\\emph formats the text to a typewriter style font

\\date{\\today} %\\today is replaced with the current date \\maketitle

\\begin{abstract}

In this article, I shall discuss some of the fundamental topics in

producing a structured document. This document itself does not go into

much depth, but is instead the output of an example of how to implement

structure. Its \\LaTeX{} source, when in used with my tutorial

provides all the relevant information. \\end{abstract}

\\section{Introduction}

This small document is designed to illustrate how easy it is to create a

well structured document within \\LaTeX\\cite{lamport94}. You should quickly be able to see how the article looks very professional, despite the content being

far from academic. Titles, section headings, justified text, text

formatting etc., is all there, and you would be surprised when you see

just how little markup was required to get this output.

\\section{Structure}

One of the great advantages of \\LaTeX{} is that all it needs to know is

the structure of a document, and then it will take care of the layout

and presentation itself. So, here we shall begin looking at how exactly

you tell \\LaTeX{} what it needs to know about your document.

\\subsection{Top Matter}

The first thing you normally have is a title of the document, as well as

information about the author and date of publication. In \\LaTeX{} terms,

本文来源:https://www.bwwdw.com/article/i5mg.html

Top