# 19

## TeX

This chapter looks at the following topics:

• What TeX is and why you would want to use it

• The differences between typesetting and writing

• The enhanced version of TeX called LaTeX

• What VirTeX and IniTeX are

### What Is TeX?

TeX (pronounced tech) is a text formatting system invented by Donald Knuth. It lets you produce professionally typeset documents by embedding TeX commands within a normal ASCII text file. This text file can then be converted to what is known as a DVI (device-independent file), which can be either previewed on-screen using an X Window program called xdvi or converted to a printer-specific file format, such as PostScript, HP LaserJet, or for other popular printers.

TeX is a powerful program in that it enables you to define specific typesetting commands (such as font size, page size, or space between lines). It also works as a programming language that enables you to create macros for defining more abstract units of text such as documents, headings, and paragraphs. The benefit of these high-level macros is that they enable you to concentrate on the authoring of a document, not the typesetting. The key appeal of TeX for engineers and scientists is that it supports the typesetting of complex mathematical formulas.

### Typesetting Versus Writing

The usefulness of a document can be limited by its appearance. Consider two documents: one that is well-organized with clearly defined units of text such as chapters, headings, and paragraphs, and another that has no paragraph breaks and no space between lines. The first document is much more appealing to the reader, whereas the second document is downright painful to read. So, despite the best efforts of an author to create a magnum opus, or even a recipe for strawberry jam, the meaning behind the words may get lost in a typographical abyss.

In guide publishing, authors aren't usually responsible for anything beyond the genius of their words. They usually leave the design and crafting of the guide to a guide designer. This person then hands the design template to page layout technicians. TeX performs this guide design and typesetting role for you, enabling you, the author, to be your own publisher. It gives you control over the publication of your own material while still permitting you to concentrate on what you're supposed to be writing about!

### TeX

A TeX file can be created with any Linux text editor such as vi or Emacs. You can enter text into a file called arkana.tex like this:


Do you suppose that Alfred Hitchcock would have had as successful a directing

career if he did not have the considerable talents of actors Cary Grant and

James Stewart in his most popular films? That's a tough one to answer... \bye

After you have saved your file, use the TeX program to convert it to a DVI file using this command:


$tex arkana The resulting arkana.dvi file that is created contains your text. This file can be used by different output devices (hence the name) for viewing or printing. For example, if you want to print your DVI file to a PostScript printer, convert it to a ps format, and print it using the dvi2ps utility: $ dvi2ps arkana.ps | lp

This assumes that the default printer is PostScript-capable. If you want to just preview how the text looks, use the X application xdvi:


$xdvi arkana.dvi & The TeX command also produces a log file entitled arkana.log, containing any error and warning messages, and other information such as the number of pages of output. The beauty of all this indirect representation of TeX output is that the TeX source file and its resulting DVI are very portable, particularly from Linux to its ancestor UNIX. #### Simple Text Formatting Most of the work in creating a TeX document is putting in the words that discuss whatever you're writing about. As shown earlier, it is fairly simple to create an unadorned TeX file: The only special command you used was \bye. This command tells the TeX program that it has reached the end of the document. The \bye command uses one of several characters that TeX treats with special interest, specifically the backslash or escape character. Here is the set of special characters that TeX recognizes: \, {, }, ~, #,$, %, ^, &, and the space character. The meaning behind these characters will be discussed as you progress.

One of the main conveniences of TeX is the intelligent way it deals with text. Words are any sequence of characters separated by whitespace characters. The number of whitespace characters between words is immaterial because TeX treats them as one character. Sentences are recognized by the last word preceding a ., ?, !, or :. Paragraphs are distinguished by a blank line following a sentence. Much like the spaces between words, TeX treats excess blank lines as redundant and ignores them. Thus, the text


How do you compare

All-American charm mixed

with a surprising element of vulnerability, uncommon

among other major Hollywood actors.

Cary Grant, on the other

hand, was versatile enough to play the villain as well as the suave hero in many films.

is formatted by TeX as follows:


How do you compare these two terrific leading men?
James Stewart had that good-natured, All-American charm mixed with a
surprising element of vulnerability, uncommon among other major
Hollywood actors.

Cary Grant, on the other hand, was versatile enough to play the villain
as well as the suave hero in many films.

You can also insert comments into your TeX file using the % character. Text following a % character is treated as a comment and not made part of the TeX output. The text


From her% Nothing to do with Hitchcock

% ...nothing at all

e to there

is formatted as


From here to there

TeX has several commands for manipulating paragraphs. The \par command starts a new paragraph, which has the same effect as inserting a blank line.


From here \par to there

The preceding line is formatted as follows:


From here

to there

The \noindent command tells TeX not to indent the paragraph:


I grew up on Newcastle Street.

\noindent That was close to Hazlehurst.

This is output as follows:


I grew up on Newcastle Street.

That was close to Hazlehurst.

You can also use the escape character before a space in order to force the insertion of an extra space:


I think that I need an extra\ \ \ space or two.

I'm sure of it.

This becomes


I think that I need an extra space or two.

I'm sure of it.

#### Fonts

Fonts are representations of characters that share similar size and style. The default font that TeX uses is Roman. You can override this by using the internal names that TeX associates with fonts that are externally loaded. You can also add new font definitions. The definitions that TeX knows about by default are: \rm (Roman), \tt (typewriter), \bf (bold), \sl (slanted), and \it (italic). TeX continues using whatever font was last specified (including the default) until it is instructed to do otherwise. Therefore, the text


This is roman, but I think I will switch to \tt typewriter for a while;
then again, maybe \it italic would be nice. Now back to \rm roman.

appears as follows:

This is roman, but I think I will switch to typewriter for a while; then again, maybe italic would be nice. Now back to Roman.

You can add a font and change its size using a command like this:


\font \fontname=auxiliary font

To use a 12-point roman font, redefine the \rm definition to use the cmr12 auxiliary font, like this:


\font\rm=cmr12

We are changing from this font \rm to that font.

This formats as follows:

We are changing from this font to that font.

Fonts have up to 256 different symbols including the standard numeric, uppercase, and lowercase character symbols that you use most frequently. Symbols that are not represented on a standard keyboard can be accessed using the \char command. This command uses the integer that follows it as a character code index into a font's character table. For example, the text


TeX would interpret \char 37 as a comment symbol

but it would not

care about a \char 43 sign.

is processed by TeX as follows:


TeX would interpret % as a comment symbol but it would not

care about a + sign.

#### Controlling Spacing

You've seen how you can insert individual extra spaces in TeX files. Now, let's examine how you can have more control over the spacing of larger portions of text. TeX has a series of commands that recognize the following units of measurement:

Unit Meaning

em Approximately the width of the character M, depending on the font in use

in Inches

pt Points (1 inch equals 72 points)

mm Millimeters (1 inch equals 25.4 millimeters)

These units are used with decimal numbers to specify the amount of spacing that you need. The \hskip command can insert a horizontal space on a line, like this:


\tt From here \hskip 0.5in to there

This produces the following output:


From here to there

You can also supply a negative number, which moves the text following the \hskip command to the left (the negative direction). The \hfil command distributes horizontal space in a paragraph when space is available. The interesting thing about the \hfil command is the fact that TeX inserts one implicitly for each paragraph. Bearing this detail in mind, you can use this command to flush text left or right, or center it on a line, like this:


\noindent \hfil Some centered text. \par

This is output as follows:


Some centered text.

The \vskip command can insert a vertical space between paragraphs using a given unit of measurement (much like \hskip). The command


\vskip 40mm

places a vertical space of 40 millimeters between its preceding and succeeding paragraphs. TeX also provides vertical skipping commands in convenient units: \smallskip, \medskip, and \bigskip.

The vertical equivalent of \hfil is the \vfill command, which can distribute vertical spaces between paragraphs when extra space (nontext) is available. TeX assumes an implicit \vfill command at the end of a document.

You can also explicitly add line breaks and page breaks to your document with the \break command. If this command appears within a paragraph, TeX inserts a line break. If it appears between paragraphs, a page break is inserted. Conversely, you can specify points in your document where you want the text to be kept together and not broken across lines or pages. This is done by using the \nobreak command.

#### Page Layout

A page is composed of a header, footer, and body. The header and footer contain information such as chapter title, section heading, and page number. The body is where the main information in your document appears. By changing how this information is ordered in your TeX document, you are actually designing the look of the finished product.

The \headline and \footline commands both take arguments that specify their content. The format of these commands is as follows:


\headline={parameters}

The parameters could be a list of things such as a page number command and an \hfil command:



\footline={\hfil}

This pair of commands creates a right-justified page number and a blank footer on each page.

You can change the size of the text box that TeX uses for paragraphs by using the \hsize command. For instance, the text


\hsize=2in

This text is 2 inches wide but we could choose to make it wider or thinner.

produces the following:


This text is 2 inches wide but we could choose to make it wider or thinner.

Margins can be adjusted inward or outward using the \leftskip and \rightskip commands, respectively. By providing positive values to these commands, they move the margin inward, depending on which side you specify (left or right). As you may expect, negative values have the opposite effect: They move the margins outward. Indentation is controlled similarly using the \parindent command.

The \baselineskip and \parskip commands control the regular vertical spacing between lines and paragraphs, as in the following:


\baselineskip=0.15in

\parskip=0.3in

Baseline refers to the distance between the bottoms of characters (such as an i) on consecutive lines.

#### Using Groups

Normally, TeX continues using such things as fonts and text styles until you explicitly change the format. The grouping features of TeX enable you to define changes that are local to particular sections of text. The formatting originally specified is then restored after the group has been processed.

There are two ways to specify how text is grouped. One is to use the \begingroup and \endgroup command pair. The other is to use the braces { and }. Although both of these perform grouping roles, braces are also used to specify parameters to commands and, as such, must be used with care.

As an illustration of the use of groups in TeX, the text


Let's see \begingroup \it how {\bf this grouping stuff} really

works \endgroup, shall we?

produces the following:


Let's see how this grouping stuff really

works, shall we?

You may have noted from the example that, in fact, groups can contain other groups.

#### Mathematical Symbols

One of the most powerful features of TeX is its capability to generate correct mathematical notation for formulas with convenient commands. This is one of the key reasons behind TeX's popularity among engineers and scientists.

TeX distinguishes between formulas that must appear within regular text (inline formulas) and those that must appear on their own line (displayed formulas). You must use the $symbol to denote inline formulas, as in  The equation$2+3=x$must evaluate to$x=5$. which is generated as the following:  The equation 2+3=x must evaluate to x=5. However, displayed formulas are denoted using two consecutive$ symbols, as in


The equation $$2+3=x$$ must evaluate to $$x=5$$.

which produces the following:


The equation

2+3=x

must evaluate to

x=5.

Table 19.1 shows some of the math symbols that TeX can generate, their associated commands, and their meaning.

Table 19.1. Some of the math symbols that TeX can generate.

 Symbol TeX Command Meaning P \pi Pi å \sum Sum { \{ Open bracket } \} Close bracket ¦ \int Integral £ \leq Less than or equal to ³ \geq Greater than or equal to ¹ \neq Not equal to · \bullet Bullet _ \ldots Horizontal ellipsis D \diamond Diamond D \Delta Delta

TeX uses particular fonts for the formulas it produces. These can be overridden in the usual fashion, but the changes are applicable only to letters and digits.

#### Using Figures in Your Document

Figures that are drawn outside of TeX can be inserted into their own space. This space "floats." In other words, TeX knows that it must keep track of the figure space as the text around it is added or deleted. This flexibility means that you, the writer, need not worry about exactly where in the document your figures will appear.

To insert a figure that must appear at the top of a page, use the following command:


\topinsert figure \endinsert

Here, figure can be an external reference or an internal definition. TeX tries to place the figure at the top of the next page with sufficient space.

You can also tell TeX that you want a figure to appear on its own page by using this command:


\pageinsert figure \endinsert

#### Macros

Macros have made TeX a highly extendible system. They essentially enable you to create new commands by associating existing commands and text sequences to a macro name. After they are defined, these macros can be used in other parts of your document to replace repetitive pieces of text, or to encapsulate abstract operations.

A macro is defined once, using the following format:


\def macroname {new text}

In this case, macroname is a name or TeX command preceded by a backslash character. Any reference to this macro name is replaced by the new text throughout the document. For example, the macro definition


\def\brg{burger}

Ham\brg, cheese\brg, lim\brg.

is output as follows:


Hamburger, cheeseburger, limburger.

Macros can refer to other macros, as in


\def\tig{a tigger }

\def\wond{a wonderful thing }

\def\pooh{\wond is \tig cause \tig is \wond}

\pooh\par

which produces the following:


a wonderful thing is a tigger cause a tigger is a wonderful thing

You must be careful of recursive macro definitions—macros that refer to their own names within their definition. Such macro definitions cause TeX to continuously (and vainly) evaluate the macro, leading to an infinite loop. The following is an example of this:
\def\itself{\itself}
\itself

TeX macros have the added feature of being able to accept parameters when expanded, if a list of formal parameters has been specified in the macro definition. To create a macro using parameters, you would use this format:


\def macroname (list of formal parameters) {new text}

Here, the list of parameters is specified as #1, #1#2, #1#2#3, and so on. This is a powerful aspect of macros because it can change the output of an expanded macro based on the parameter in use. For example, the code


\def\parm#1{This is the #1 time I'll say this.}

\parm{first}

\parm{second}

\parm{last}

produces the following:


This is the first time I'll say this.

This is the second time I'll say this.

This is the last time I'll say this.

Each parameter that is used must be passed separately by enclosing it in braces, as in


\def\family#1#2{My #1 is #2.}

\family{wife}{Cindy}

\family{sister}{Sheila}

\family{father}{Myles}

which makes the following output:


My wife is Cindy.

My sister is Sheila.

My father is Myles.

You must specify an appropriate match of parameters in your macro definition. The macro definition


\def\mistake#1{This is wrong because of #2.}

is incorrect because it refers to a second parameter that is not specified in the formal parameter list.

Macros can be redefined in your document, but you should be aware that only the most recent definition will be applied. Also, macros defined within groups are only valid within the scope of the group.

Macro definitions can be nested within each other, as in the following:


\def\hey{Hey\def\hey{hey}}

\hey, \hey, \hey.

This has the following output:


Hey, hey, hey.

As with many topics within this guide, we have examined only some of the highlights of TeX. There is much more to learn but, having covered the basics regarding macros, you can now look at the most popular extension of TeX, which uses macros to enhance the creation of documents. This extension is LaTeX.

### LaTeX An Enhancement of TeX

LaTeX is a collection of macros that build on the capabilities of TeX and provide a higher level of abstraction for the creation of documents. It is essentially a style library that encourages uniform formatting and typesetting across documents. LaTeX macros shift the emphasis away from the details of things such as "set text to 8-point slanted" to concepts that writers identify more readily with, such as the emphasis of a word or phrase. Thus, LaTeX macros have names that are more representative of the way writers think when they are writing.

Because LaTeX is an extension of TeX, you'll find it easy to become quickly productive in LaTeX, assuming that you have some experience in TeX. White space and spacing between paragraphs are handled in the same manner as in TeX. The special characters in TeX are the same in LaTeX, and comments are denoted using the % character.

The key differences between TeX and LaTeX become apparent as you learn more about the macros that define the layout of your document in a convenient fashion.

#### Defining a LaTeX Document

Every LaTeX document begins with the \documentclass command. The parameter passed to this command specifies what kind of document you want to write. The basic document classes are described in Table 19.2.

Table 19.2. Document classes.

 Document Class Description article Used for short reports, reference cards, presentations, scientific journals, and so on. guide Used for complete guides. report Used for reports having several chapters, theses, and so on.

To create a very basic LaTeX document, simply place some words between the two commands \begin{document} and \end{document}. The text that precedes the \begin{document} command is called the preamble, and the text that comes after is known as the body. So, you can create a very simple document such as the following:


\documentclass{article}

\begin{document}

What a small document this is.

\end{document}

To process this document (which you will edit in a file called gloves.tex), use the following command:


% latex gloves

This produces a DVI file and a log file in the same manner used by TeX. The DVI file can either be converted to PostScript, or viewed directly using xdvi.

You can specify options with the type of document in the \documentclass command using the following format:


\documentclass[option]{document class}

These options relate to the physical structure of the document. Some of the more common ones are listed in Table 19.3.

Table 19.3. \documentclass options.

 Option Description 10pt, 11pt, 12pt The default font for the document, which is 10pt if not otherwise stated. fleqn Displays formulas as left-justified instead of centered. leqno Numbers formulas on the left side. letterpaper, a4 paper The paper size, which is letterpaper by default. openright, openany Starts the first page of a chapter on the right side, or on the next available page. titlepage, notitlepage Does or does not start a new page after the title. twocolumn Splits each page into two columns (useful for newsletters). twoside, oneside Generates double- or single-sided output.

Some of the differences between document classes are encapsulated by the defaults that they use for the options mentioned. For instance, articles and reports are single-sided by default, whereas guides are not. Articles do not use the options for title pages and starting right-sided chapters because they do not understand what a chapter is. Thus, the document classes in LaTeX are smart enough to do the kind of layout that you expect for the type of document you need.

#### Packages

LaTeX also has the \usepackage command, which enables you to extend the capabilities of LaTeX even further by using an external package of features. The format is as follows:


\usepackage{package name}

package name can be any of several available packages. For instance, the doc package is used for the documentation of LaTeX programs, and the makeidx package provides support for the production of indexes.

You can also control what page styles LaTeX applies to your document by using the \pagestyle command. Table 19.4 describes the basic page styles available.

Table 19.4. Page styles.

 Style Description empty Sets the header and footers to be empty. headings Prints the current chapter heading and page number on each page with an empty footer. plain Prints the page number centered in the footer (the default page style).

You can also vary page styles in your document using the \thispagestyle command. This applies the supplied page style to the current page only.

#### Using Special Characters

LaTeX supports the use of international characters, such as umlauts (_) and circumflexes (^). These characters are generated using a command variant on the letter itself. For example, the text


What a na\"\i ve ma^itre d' you are!

produces the following:

What a naïve ma^itre d' you are!

International spacing can also be applied using the \frenchspacing command. This command tells LaTeX not to insert the usual extra space after a period.

#### Putting Structure into a LaTeX Document

LaTeX has commands that make it easy to enhance your document structurally, thus making it easier for the reader to digest. For the article document class, the commands are as follows: \section, \subsection, \subsubsection, \paragraph, \subparagraph, and \appendix. These commands, with the exception of \appendix, accept titles as arguments, and are declared before the body of text that they represent. LaTeX takes care of the rest; it sets the appropriate spacing between sections, section numbering, and title font. The \appendix command uses alphabetic increments in order to number succeeding appendix sections.

For the report and guide classes, there are two additional commands: \part and \chapter. The \part command enables you to insert a section without affecting the numbering sequence of the chapters. You can suppress the appearance of a section in the table of contents by inserting a * character in the section command, as in the following:


\section*{I don't want to know about it}

You probably want to add a title to your document. This is done by specifying the arguments to the title commands and then calling the \maketitle command:


...

\title{Confessions of a LaTeX Enthusiast}

\author{Me}

\date

\begin{document}

\maketitle

...

To insert a table of contents in your document, issue the \tableofcontents command (big surprise) at the point where you want the table to appear. When you process your document with LaTeX, it needs two passes: one to make note of all the section numbers, and the other to build the table of contents from the information it collected in the first pass.

You can add cross-references to your document, which tie associated elements such as text, figures, and tables to text in other parts of your document. Use the \label command to set a point that you want to refer to, and give it an argument that is any name you choose. This name can then be referred to by the \ref and \pageref commands to generate a cross-reference containing the section number and page number that the section title appears on.

You can easily add footnotes using the \footnote command, which accepts the text of the footnote as an argument.

Structure is also enhanced by controlling the presentation of the text that appears between section titles. This can be easily managed by using LaTeX environments. Environments are specified by bounding a portion of text with \begin and \end commands, and passing an environment name to each command, as in the following:


\begin{hostileenvironment}

Looks like we're surrounded, said Custer.

\end{hostileenvironment}

LaTeX has many predefined environments for practical applications, as described in Table 19.5.

Table 19.5. Predefined environments.

 Environment Description center Centers text. description Used to present descriptive paragraphs. enumerate Used for numbered or bulleted lists. flushleft Paragraphs are left-aligned. flushright Paragraphs are right-aligned. itemize Used for simple lists. quote Used to quote single paragraphs. quotation Used for longer quotes that span several paragraphs. tabular Typesets tables with optional row and column separators. verbatim Produces typed text. Useful for representing programming code, for example. verse Used to control the linebreaks in poems.

#### Working with Figures and Tables

LaTeX also supports the variable placement (or "floating") of figures and tables in a document using the table and figure environments. A figure could be specified as follows:


\begin{figure}[!hbp]

\makebox[\textwidth]{\framebox[2in]{\rule{Opt}{2in}}}

\end{figure}

The options passed to the \begin{figure} command are placement specifiers that indicate your preferences for the location of the figure. LaTeX has to juggle the placement of floating figures and tables in a document by using these preferences, as well as internal guidelines such as the maximum number of floats allowed per page. In this example, you told LaTeX to keep the figure with its adjacent text (h), at the bottom of the next applicable page , or, failing that, on a special page with other floating figures (p). The ! character overrides LaTeX's best intentions for placing the figure, which may not necessarily jibe with what you are saying with the other placement specifiers.

Tables and figures can be labeled using the \caption command, which must be issued within the table or figure environment.

These are just some of the basics for using LaTeX, but hopefully they are sufficient to give you a place to start on the road to making your documents more visually appealing. You have probably noticed that LaTeX is somewhat easier to work with than TeX itself, because it hides much detail from you as an author.

### VirTeX and IniTeX

Two other TeX-related programs work together but perform slightly different roles. The IniTeX program is used to create a TeX format (.fmt) file containing font definitions and macros. The VirTeX program can then quickly load this precompiled format file, much more quickly than TeX can. The command to use a format file is as follows:


\$ virtex \&myformat sometexfile

The & character is necessary for VirTeX to recognize that it is loading a format file first; the & must be escaped using the \ character so as not to confuse the shell. The difference between VirTeX and IniTeX is that VirTeX can't be used to create TeX format files, but it executes much faster.

### Summary

TeX is a document-formatting system for Linux that enables authors to produce their own high-quality publications. It produces documents that are portable among output devices such as printers or displays. TeX supports many typographical features and is particularly well-suited to the formatting of correct mathematical notation. It has macros that can be used to enhance the power of its basic command set. LaTeX, one of the most popular extensions to TeX, uses sophisticated macros to help you organize and typeset your documents based on its contents.

|  About us | Categories | New Releases | Most Popular | Web Tutorial | Free Download | Drivers |

2013 Soft Lookup Corp. Privacy Statement