## LaTeX: It's Not Just for Academia, Part 2

by Kevin O'Malley
03/05/2004

In the first article of this two-part series, you learned about LaTeX's most popular implementations for Mac OS X, and that it's not a word processor, but rather a document preparation and typesetting system. Under Mac OS X, there are many high-quality writing environments that simplify the process of composing LaTeX documents.

Before going any further, let me address some corrections/omissions from the first article. As a few readers pointed out, TeXShop and OzTeX can be setup to use Fink's UNIX TeX package. Saying "X11-based LaTeX implementations" may have mislead readers. TeX and X11 are not coupled in any way. You use X11-based DVI or PDF previewers to view LaTeX output. The file II2.dmg is not from the TeXShop folks, but rather is linked from their page. The program is available from the i-Installer Home Page. Finally, many readers have asked for more complete LaTeX sources. I have included these in the Resources section.

Now, let's see how you can use LaTeX to accomplish some common writing tasks. As you can imagine, LaTeX has many commands and features, and can be somewhat complicated to learn. Today, I'll keep things simple and concentrate on the basics.

This article introduces you to the LaTeX language, the basic structure of a LaTeX document, and shows some common writing tasks and how to accomplish them using LaTeX. Finally, I will give a few examples of common LaTeX documents and demonstrate how to generate these documents in different output formats, including formatting your documents for the Web.

For these examples, you can use any LaTeX environment you wish -- TeXShop, OzTeX, or the command-line version. At this point it may be helpful to briefly discuss the environment I use. I use TeXShop if I wish to use a Mac OS X Aqua-based program. Other programs have more feature, but I like the simplicity and stability of TeXShop.

For writing, I use GNU Emacs (21.2.1 from Fink) under Apple's X11. I do not rely on document previewing much, but when I need it, I do the following. I use a Perl script that looks at my LaTeX document every few seconds and calls an output generation program when the document changes; when I save the LaTeX document, the preview is automatically generated. I also have the script generate and display the current word count for the document.

Recently, I have been using Enrico Franconi's Enhanced Carbon Emacs. This implementation was pointed out by a reader in the Trackback section. This Emacs implementation is based on the emacs-21.3.50 CVS distribution. Overall, I really like this version, which includes LaTeX tools such as AucTeX and RefTeX. In addition, it has good integration with Aqua-based previewing programs like Acrobat Reader and TeXShop.

The script generates output in PDF. To view the PDF file, I use TeXShop's external editor feature, which permits TeXShop to redisplay the PDF when it has changed.

I also use the Emacs' Flyspell mode to automatically check spelling as I type, and suggest corrections.

Another way to generate your output is by using Makefile.

Take a look at the Perl script and Makefile. These will give you examples of how to process a LaTeX document and generate various output formats.

### LaTeX Preliminaries -- Commands, Comments, and Whitespace

Before we begin composing a document, it's helpful to know a bit about how LaTeX handles some common document elements. Like most programming languages, LaTeX commands are case sensitive. A LaTeX command is formatted as follows:

\command-name[optional-parameters]{parameter}

A LaTeX command begins with a backslash, followed by the command name. Commands can have parameters, which are enclosed by curly braces ({}) as well as optional parameters, which are surrounded by square brackets ([]).

Any line beginning with a % is a comment. When LaTeX reads the % character in a file, it treats the rest of that line as a comment. In addition to the comment token, you can use the comment command.

\begin{comment}
This is a comment and is not displayed in the final document.
\end{comment}


### LaTeX Document Structure

Now that you know some basics, let's move on to the structure of a LaTeX document. When you program in C++, Python, or Java, you usually structure source files in a certain way. Same holds for LaTeX. Here is the format of a LaTeX document.

% -- Begin LaTeX document.
% Begin document preamble.
\documentclass[options]{document-class-name}
\usepackage{package-name}
...
% End document preamble.
\begin{document}
% -- Your text and LaTeX commands go here.
\end{document}
% -- End LaTeX document.


A LaTeX document begins with the document class command (\documentclass). This command specifies the class of your document, and consequently, what commands and environments are supported and how LaTeX formats the document. LaTeX document classes include article, report, and book, to name a few. Think of the document class as defining the type of writing you are doing as well as its appearance. For example, if you are going to write an article, use the article class; a book, use the book class.

Here are the most common LaTeX document classes.

• Letter -- for writing letters
• Article -- for writing short reports, papers, and documentation
• Report -- for larger writing tasks such as large reports having several chapters, or a theses
• Book -- for writing books
• Slides -- for preparing slides

The options parameter is used to change the settings for a document class. For example, most classes accept the typeface size options 10pt, 11pt, 12pt -- 10pt is the default. Another option is the paper size, which includes a4paper, a5paper, b5paper, letterpaper, legalpaper, executivepaper -- letter is the default. You can also specify the documents page orientation. For example,landscape tells LaTeX to display the document in landscape mode.

Here are some examples of common document classes.

\documentclass{letter}
\documentclass[11pt]{report}
\documentclass[12pt,landscape] {article}
\documentclass{slide}

You add extra functionality and commands through packages (\usepackage). Many packages come with LaTeX, such as color, graphicx, and makeidx. In addition, you can add packages written by others, or even write your own for specific writing needs. Following the use package command, you can define other commands that alter the formatting of your document.

The section of your document from document class to the begin document (\begin{document}) command is called the Document Preamble. The document preamble contains commands, packages, and other commands that set values and change the appearance of your document. LaTeX documents end with the end document command (\end{document}). Any text after this command is ignored.

The begin document command signals the beginning of your actual writing. Between the beginning and ending document commands is where you add your text and various LaTeX commands. What commands you can use is determined by the document class and the packages you include.

Before concluding this discussion, let's look at one more thing -- the environment command. The environment command enables you to control the formatting and appearance of your document's content. For example, imagine you need to create a list of text, add an abstract, or display a table. For these cases, LaTeX provides environments that make it easy to accomplish these goals. You can even define your own environments with the new environment command.

### Common LaTeX Operations

No matter what kind of document you write, there are always common operations you need to perform. For example, when writing a report or an article, you need to divide your writing into sections, add tables, include footnotes, or possibly include graphics.

This section shows you how to perform many common operations in LaTeX. These suggestions are in no way inclusive, or enumerate all of the possibilities. The goal here is to get you started with some simple versions of commands. This discussion should be augmented with a good LaTeX reference.

The output for each example is displayed in a single output document. As you read about these commands, it's a good idea to take a look at the example document's source file and output file. Also, see the LaTeX source file for this article.

#### Sections

LaTeX sections provide a mechanism for structuring your document. LaTeX supports several commands that you use to organize your document into sections.

• Part
• Chapter
• Section
• Paragraph
• Subparagraph

To see sections in action, see the example and source files for this article.

#### Including Files

When working on large documents, it can be difficult to maneuver and edit a single file that contains all your text and LaTeX commands. Fortunately, LaTeX provides a mechanism for breaking large documents into smaller pieces. To accomplish this, you use the include and input commands.

The include command generates a page break and inserts the named file at the location of the command. The input command also inserts the file, but with no page break. The named file does not include the file extension, just the file name. For example, to include the file chapter.tex you would just specify the name chapter.

\include{file-name}
\input{file-name}

There are certain rules for doing this, so consult the LaTeX documentation for more information.

#### Page Numbers

You control the appearance of page numbers with the page numbering command (\pagenumbering). LaTeX offers several options for numbering pages, including Arabic, roman, Roman, alpha, and Alpha.

\pagenumbering{roman}

Page numbers are one of several commands for changing the appearance of pages. You can also change the margins, number of columns, add headers and footers to a page, and create a title page and abstract. See the Page Styles section of a LaTeX reference for more information.

You create a table of contents in your document with the table of contents command (\tableofcontents). This command places a table of contents at the location of the command. LaTeX generates the table of contents entries from the section commands of the document.

LaTeX will automatically handle the numbering of sections and chapters for you. You can suppress adding sections to the table of contents, as well as section numbers, by using the starred version of the command.

\subsection*{My Title}

This is also useful if you are writing an article with no table of contents, and do not want numbers to appear for each section.

The list of figures and the list of tables commands place a list of all figures and tables in your document. The list is inserted at the location of the command. If the caption command appears in the figure environment, that figure is added to the list of figures. If the caption command appears in the table environment, the table is added to the table list. The text of the caption is what's displayed in figure or table list.

To generate the initial list of figures, tables, and table of contents, you need to run the LaTeX command twice.

% latex latex-file
% latex latex-file
 Pages: 1, 2, 3