使用者:雷王硯渤/sandbox

我們的第一份文件

編輯

現在我們可以創建我們的第一個文檔。我們將會用儘可能少的代碼來進行最簡單的輸出:典型的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