您的位置:首页 > 运维架构 > Linux

如何使用 Emacs 创建 LaTeX 文档 | Linux 中国

2018-11-24 12:30 881 查看
这篇教程将带你遍历在 Emacs 使用强大的开源排版系统 LaTex 来创建文档的全过程。
-- Sachin Patil



致谢
编译自 | 
https://opensource.com/article/18/4/how-create-latex-documents-emacs
 
 作者 | Sachin Patil
 译者 | Yuqi Liu (oneforalone) 🌟共计翻译:1.0 篇 贡献时间:1 天

这篇教程将带你遍历在 Emacs 使用强大的开源排版系统 LaTex 来创建文档的全过程。

一篇由 Aaron Cocker 写的很棒的文章 “在 LaTeX 中创建文件的介绍[1]” 中,介绍了 LaTeX 排版系统[2] 并描述了如何使用 TeXstudio[3] 来创建 LaTeX 文档。同时,他也列举了一些很多用户觉得创建 LaTeX 文档很方便的编辑器。

Greg Pittman[4] 对这篇文章的评论吸引了我:“当你第一次开始使用 LaTeX 时,他似乎是个很差劲的排版……” 事实也确实如此。LaTeX 包含了多种排版字体和调试,如果你漏了一个特殊的字符比如说感叹号,这会让很多用户感到沮丧,尤其是新手。在本文中,我将介绍如何使用 GNU Emacs[5] 来创建 LaTeX 文档。

创建你的第一个文档

启动 Emacs:

  1. emacs -q --no-splash helloworld.org

参数 -q 确保 Emacs 不会加载其他的初始化配置。参数 --no-splash-screen 防止 Emacs 打开多个窗口,确保只打开一个窗口,最后的参数 helloworld.org 表示你要创建的文件名为 helloworld.org 。

GNU Emacs 打开文件名为 helloworld.org 的窗口时的样子。

现在让我们用 Emacs 添加一些 LaTeX 的标题吧:在菜单栏找到 “Org” 选项并选择 “Export/Publish”。

导入一个默认的模板

在下一个窗口中,Emacs 同时提供了导入和导出一个模板。输入 #(“[#] Insert template”)来导入一个模板。这将会使光标跳转到一个带有 “Options category:” 提示的 mini-buffer 中。第一次你可能不知道这个类型的名字,但是你可以使用 Tab 键来查看所有的补全。输入 “default” 然后按回车,之后你就能看到如下的内容被插入了:

  1. #+TITLE: helloworld

  2. #+DATE: <2018-03-12 Mon>

  3. #+AUTHOR:

  4. #+EMAIL: makerpm@nubia

  5. #+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline

  6. #+OPTIONS: author:t c:nil creator:comment d:(not "LOGBOOK") date:t

  7. #+OPTIONS: e:t email:nil f:t inline:t num:t p:nil pri:nil stat:t

  8. #+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:t todo:t |:t

  9. #+CREATOR: Emacs 25.3.1 (Org mode 8.2.10)

  10. #+DESCRIPTION:

  11. #+EXCLUDE_TAGS: noexport

  12. #+KEYWORDS:

  13. #+LANGUAGE: en

  14. #+SELECT_TAGS: export

根据自己的需求修改标题、日期、作者和 email。我自己的话是下面这样的:

  1. #+TITLE: Hello World! My first LaTeX document

  2. #+DATE: \today

  3. #+AUTHOR: Sachin Patil

  4. #+EMAIL: psachin@redhat.com

我们目前还不想创建一个目录,所以要将 toc 的值由 t 改为 nil,具体如下:

  1. #+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:nil todo:t |:t

现在让我们添加一个章节和段落吧。章节是由一个星号(*)开头。我们从 Aaron 的贴子(来自 Lipsum Lorem Ipsum 生成器[6])复制一些文本过来:

  1. * Introduction

  2.  \paragraph{}

  3.  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lorem

  4.  nisi, tincidunt tempus sem nec, elementum feugiat ipsum. Nulla in

  5.  diam libero. Nunc tristique ex a nibh egestas sollicitudin.

  6.  \paragraph{}

  7.  Mauris efficitur vitae ex id egestas. Vestibulum ligula felis,

  8.  pulvinar a posuere id, luctus vitae leo. Sed ac imperdiet orci, non

  9.  elementum leo. Nullam molestie congue placerat. Phasellus tempor et

  10.  libero maximus commodo.

helloworld.org 文件

将内容修改好后,我们要把它导出为 PDF 格式。再次在 “Org” 的菜单选项中选择 “Export/Publish”,但是这次,要输入 l(“export to LaTeX”),紧跟着输入 o(“as PDF file and open”)。这次操作不止会打开 PDF 文件让你浏览,同时也会将文件保存为 helloworld.pdf,并保存在与 helloworld.org 的同一个目录下。

将 helloworld.org 导出为 helloworld.pdf

打开 helloworld.pdf 文件

你也可以按下 Alt + x 键,然后输入 org-latex-export-to-pdf 来将 org 文件导出为 PDF 文件。可以使用 Tab 键来自动补全命令。

Emacs 也会创建 helloworld.tex 文件来让你控制具体的内容。

Emacs 在三个不同的窗口中分别打开 LaTeX,org 和 PDF 文档。

你可以使用命令来将 .tex 文件转换为 .pdf 文件:

  1. pdflatex helloworld.tex

你也可以将 .org 文件输出为 HTML 或是一个简单的文本格式的文件。我最喜欢 .org 文件的原因是他们可以被推送到 GitHub[7] 上,然后同 markdown 一样被渲染。

创建一个 LaTeX 的 Beamer 简报

现在让我们更进一步,通过少量的修改上面的文档来创建一个 LaTeX Beamer[8] 简报,如下所示:

  1. #+TITLE: LaTeX Beamer presentation

  2. #+DATE: \today

  3. #+AUTHOR: Sachin Patil

  4. #+EMAIL: psachin@redhat.com

  5. #+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline

  6. #+OPTIONS: author:t c:nil creator:comment d:(not "LOGBOOK") date:t

  7. #+OPTIONS: e:t email:nil f:t inline:t num:t p:nil pri:nil stat:t

  8. #+OPTIONS: tags:t tasks:t tex:t timestamp:t toc:nil todo:t |:t

  9. #+CREATOR: Emacs 25.3.1 (Org mode 8.2.10)

  10. #+DESCRIPTION:

  11. #+EXCLUDE_TAGS: noexport

  12. #+KEYWORDS:

  13. #+LANGUAGE: en

  14. #+SELECT_TAGS: export

  15. #+LATEX_CLASS: beamer

  16. #+BEAMER_THEME: Frankfurt

  17. #+BEAMER_INNER_THEME: rounded

  18. * Introduction

  19. *** Programming

  20.    - Python

  21.    - Ruby

  22. *** Paragraph one

  23.    Lorem ipsum dolor sit amet, consectetur adipiscing

  24.    elit. Cras lorem nisi, tincidunt tempus sem nec, elementum feugiat

  25.    ipsum. Nulla in diam libero. Nunc tristique ex a nibh egestas

  26.    sollicitudin.

  27. *** Paragraph two

  28.    Mauris efficitur vitae ex id egestas. Vestibulum

  29.    ligula felis, pulvinar a posuere id, luctus vitae leo. Sed ac

  30.    imperdiet orci, non elementum leo. Nullam molestie congue

  31.    placerat. Phasellus tempor et libero maximus commodo.

  32. * Thanks

  33. *** Links

  34.    - Link one

  35.    - Link two

我们给标题增加了三行:

  1. #+LATEX_CLASS: beamer

  2. #+BEAMER_THEME: Frankfurt

  3. #+BEAMER_INNER_THEME: rounded

导出为 PDF,按下 Alt + x 键后输入 org-beamer-export-to-pdf

用 Emacs 和 Org 模式创建的 Latex Beamer 简报

希望你会爱上使用 Emacs 来创建 LaTex 和 Beamer 文档(注意:使用快捷键比用鼠标更快些)。Emacs 的 Org 模式提供了比我在这篇文章中说的更多的功能,你可以在 orgmode.org[9] 获取更多的信息.


via: https://opensource.com/article/18/4/how-create-latex-documents-emacs

作者:Sachin Patil[11] 选题:lujun9972 译者:oneforalone 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: