- Separation of Style and Content
- A Simple LaTeX Document
- WYMIWYG (No, Not WYSIWYG)
A Simple LaTeX Document
Commands in LaTeX (and, indeed, TeX) start with a backslash (\) character. Then follows the command name and often some arguments. Typically, a document starts with a preamble of a few commands, and then some text. A minimal LaTeX document looks something like this:
\documentclass{article} \begin{document} \end{document}
The first line tells LaTeX what kind of document you’re writing—in this case, article. Other options include book and letter. This specification defines some basic information about the structure of the document, and the layout when it’s typeset. For example, a book has chapters, while a letter doesn’t. A letter has the concept of an address, while an article doesn’t.
Some optional arguments are permitted in the \documentclass command, specifying things such as page size, whether it’s double-sided, and the base font size. The text of the document then goes between the last two lines in the example.
LaTeX recognizes a blank line as signifying a break between paragraphs. This is a holdover from when most text editors didn’t handle word wrapping nicely, and so an author would insert a line break at the 40- or 80-column boundary, depending on the size of the author’s terminal. Two newlines meant a new paragraph.
Most common elements in a document have simple commands associated with them. A section title, for example, is defined using the \section command:
\section{This is a Section Heading}
Syntactically equivalent constructs exist for chapters, subsections, etc. For a pure-text document, such as a novel, this is all you need. Complex documents require more.
Floating
LaTeX works with boxes. Each character has a box. These are assembled into boxes containing words, and then paragraphs, etc. Sometimes it’s convenient to place a box on the page that floats around the text. This is used for things such as floating images and tables.
Inserting a floating figure into a LaTeX document would be done with something like the following section:
\begin{figure}[htp] \begin{center} \includegraphics{aFigure.pdf} \end{center} \caption{\label{fig:afig} This is a figure} \end{figure}
The brackets ([]) on the first line are used for optional arguments. Here, they specify the order in which LaTeX should try to place the picture:
- here—as close to between the preceding and following text as possible
- top—at the top of the page
- page—on a separate page of figures
The next begin-end block specifies that the figure should be centered on the page. Finally, a caption is added, and numbered automatically.
The \label command inside the caption associates a name with the number of this figure. If this is the first figure in a document, the following LaTeX code will produce the text Figure 1:
Figure \ref{fig:aFig}
This will be updated automatically if the figure is renumbered later. The same syntax can be used for chapter and section number labels and references.
Mathematics
One area where TeX shines is in typesetting mathematics. Simple mathematical insertions are added between dollar signs. The formula x2 would be added in a LaTeX paragraph as $x^2$. If you’ve used OpenOffice.org’s equation editor, this markup will look familiar to you; the command-entry mode there provides an almost identical input format.
Longer equations are generated using an equation environment:
\begin{equation} e^{\pi i} + 1 = 0 \end{equation}
By default, equations inserted this way are numbered, and therefore can also be labeled (with \label) and then referenced by number (with \ref).
Packages
Not every feature you would ever want is available in LaTeX. Fortunately, there’s an easy mechanism for extending it. If you include a lot of listings in your document, for example, you may decide that you want something a little more advanced than the verbatim environment, which inserts text as is. To the rescue comes the listings package, included this way:
\usepackage{listings}
This allows you to insert syntax-highlighted listings, with line numbers, captions, auto-generated tables of listing, etc.
Lots of things are available in packages for LaTeX at the Comprehensive TeX Archive Network (CTAN). These include fairly mundane things, such as a package with a set of underlining styles, and more complex things, such as packages for generating directed graphs.
Entering LaTeX
One advantage LaTeX has over SGML forms is that it’s very easy to type. On most keyboard layouts that I’ve used, the backslash (\) key is very easy to hit, while the angle brackets (<>) required by SGML can be a pain.
The other advantage when entering LaTeX is its terseness. In XHTML, for example, a paragraph is supposed to begin <p> and end </p>. This involves uncomfortable key presses. With LaTeX, you just hit one of the largest keys on your keyboard twice.
Standard File Formats
There’s a lot of discussion at the moment about the merits of open and standard file formats. If you create a lot of documents in a proprietary format, there’s a significant chance that you won’t be able to access them later. For example, I have quite a few documents in ClarisWorks version 1 format, and nothing that can read them.
LaTeX documents are in the most open of open formats—plain text (usually ASCII or UTF-8), with human-readable markup. Even someone with no knowledge of LaTeX could look at a LaTeX source file and read the text. He or she could make fairly educated guesses that \chapter signifies the start of a chapter, and possibly even make some shrewd guesses as to the structure of tables and other environments.
Automation
Since a LaTeX file is a simple text file, it’s very easy to machine-generate. It would be trivial, for example, to create a web interface that would generate invoices or corporate letters in LaTeX from data entered in web forms, and make them available for download as a high-quality PDF.
In many corporate environments, most documents are required to conform to a certain template structure. Someone who needs to write a letter, for example, could fill in a few boxes containing the recipient’s address and the text of the letter, and then have a PDF typeset automatically (or even printed in the mailroom, ready for sending).