我们的第一份文件

编辑

现在我们可以创建我们的第一个文档。我们将会用尽可能少的代码来进行最简单的输出:典型的Hello,World!程序。

  • 打开你喜爱的编辑器。如果你使用 vim, emacs 或 Notepad++, LaTeX代码将会自动高亮标记。
  • 输入以下LaTeX代码
% hello.tex - Our first LaTeX example!
\documentclass{article}
\begin{document}
Hello World!
\end{document}
  • 保存为hello.tex.

它代表什麼意思?

编辑
% hello.tex - Our first LaTeX example! 第一行是注释,因为它以百分号 (%) 开始; LaTeX 会将这样的一行忽略. 注释能够让作者更加方便地标注源文件的各个部分, 比如标出作者或是年份.
\documentclass{article} 这一行是命令, 指示 LaTeX 这个文档类型是 article. 文档类型文件对应格式, 在这个例子中 article 类型对应普遍的文章. 在很多情况下, 只需替换文档类型即可使相同的文章符合不同的格式要求.
\begin{document} 这一行标识了 document 环境的开始, 提示接下来是文档的内容部分. 在这行代码以上的所有代码被称为 preamble.
Hello World! 这是这个例子中唯一会真正输出的内容——我们希望在页面上显示的内容.
\end{document} document 环境至此结束. 这行代码提示 LaTeX 文档内容已经结束, 在其后的代码将会被忽略.

正如前文中已经提到的, LaTeX 命令以反斜杠开始 (±). 这就是说, LaTeX 一见到反斜杠, 就会期望一个命令. 注释不会被视为命令, 因为 LaTeX 在任何时候都会忽略注释.

生成文件

编辑

It is clearly not going to be the most exciting document you have ever seen, but we want to see it nonetheless. I am assuming that you are at a command prompt, already in the directory where hello.tex is stored.

  1. Type the command: latex hello (the .tex extension is not required, although you can include it if you wish)
  2. Various bits of info about LaTeX and its progress will be displayed. If all went well, the last two lines displayed in the console will be:
Output written on hello.dvi (1 page, 232 bytes).
Transcript written on hello.log.

This means that your source file has been processed and the resulting document is called hello.dvi, which takes up 1 page and 232 bytes of space. This way you created the DVI file, but with the same source file you can create a PDF document. The steps are exactly the same as before, but you have to replace the command latex with pdflatex:

  1. Type the command: pdflatex hello (as before, the .tex extension is not required)
  2. Various bits of info about LaTeX and its progress will be displayed. If all went well, the last two lines displayed in the console will be:
Output written on hello.pdf (1 page, 5548 bytes).
Transcript written on hello.log.

you can notice that the PDF document is bigger than the DVI, even if it contains exactly the same information. The main differences between the DVI and PDF formats are:

  • DVI 需要更少的磁盘空间,创建起来更快。但它不包括文档中的字体,因此如果你想在另一台计算机上正确查看文档,则必须安装所有必需的字体。它不支持任何交互性,如超链接或动画图像。 DVI查看器也不常见,因此你可以考虑在排版时使用它来预览文档。
  • PDF需要更多的磁盘空间并且创建速度较慢,但​​它包含文档中所有必需的字体,因此你不会遇到任何可移植性问题。它支持内部和外部超链接。它还支持高级印刷功能: 悬挂标点符号,字体扩展和边距字距调整,从而为TeX引擎提供更大的灵活性和更好的输出效果。如今它是共享和发布文档的事实标准,因此你可以考虑将其用于文档的最终版本。

About now, you saw you can create both DVI and PDF document from the same source. This is true, but it gets a bit more complicated if you want to introduce images or links. This will be explained in detail in the next chapters, but for now assume you can compile in both DVI and PDF without any problem.

Note, in this instance, due to the simplicity of the file, you only need to run the LaTeX command once. However, if you begin to create complex documents, including bibliographies and cross-references, etc, LaTeX needs to be executed multiple times to resolve the references. But this will be discussed in the future when it comes up.


Previous: Introduction Index Next: Basics