Introduction to LaTeX: 3. Miscellany


While generating tables in Plain TeX (and AmSTeX) is difficult and can be a very frustrating experience, this is not the case with LaTeX. LaTeX has simple, intuitive, and user-friendly macros for tables, and typesetting good looking tables in LaTeX is a breeze, and can be fun.

The idea is to think of the table as a matrix, consisting of rows and columns. The data is entered one row at a time, with double backslashes (\\) separating the rows, and ampersands (&) separating the entries within each row. A surrounding pair \begin{tabular}{...} ... \end{tabular} then does all the work. Here the argument of the first brace {...} consists of a "template" that specifies the positioning of the table entries: l, r, or c, for left, right, or center. Here is a very simple example:

\begin{tabular}{l r r r c} 

John&19& 28&33&C \\
Jane&49& 35&60&B  \\
Jim&76& 38&59&A 


Note that TeX automatically chooses the width of the columns and the heights of the rows so that all table entries fit, with some room to spare.

Now let's spruce up this table, by surrounding it with a frame, and separating columns and rows by "rules" (vertical and horizontal lines). This is easy to do: To get horizontal lines add "\hline" at each linebreak and at the beginning and end of the table. To get vertical lines (and also specify the positioning of the data entries - left, center, or right), add vertical bar characters ("|") to the template, e.g.: { | l | r | r | r | r } If you use a double bar symbol "||" instead of a single bar, or use a double "\hline", the separating lines get "doubled". This provides an easy way to separate the headers from the table contents.

Here is the above table with these enhancements.

\begin{tabular}{| l || r | r | r | c |}  




John&19& 28&33&C \\  
Jane&49& 35&60&B  \\
Jim&76& 38&59&A  \\



Exercise 3.1: Build a table like the one above (or paste the above code into a LaTeX article template), compile the TeX file, and look at the typeset table.
There are two additional enhancements you might want to make with the above table. The first is to center it, by surrounding the entire construct with a \begin{center} ... \end{center} pair.

The second enhancement is to surround everything by a \begin{table} ... \end{table} pair. This makes the table a "floating" table, meaning that TeX chooses an appropriate placement, so as to avoid bad page breaks. For example, if at the current location the table would run over the bottom of the page, TeX will fill the page with text, and place the table at the top of the following page. The "table" environment also enables one to add a "caption". The result is something like the following:

\begin{tabular}{ .....}


\caption{Math 361 Grades} 

Exercise 3.2: Enhance your table in this manner, by centering it and adding a caption. Note that LaTeX automatically gives the table a label of the form "Table 1: ....". If you had a second table, it would be called "Table 2: ...". Try to typeset several (possibly identical) tables, with a few paragraphs of text in between, and see how TeX chooses the placement of the tables. (Note: The positioning doesn't always come out where you'd expect or want it. See the Gratzer book for ways to get around such problems.)

Mathematical material in tables: If the table entries involve mathematical notation, they should be placed inside single dollar signs: For example, x2 has be entered as $x^2$.


LaTeX has two basic list environments: The "itemize" environment which produces a "bullet list", and the "enumerate" environment which generates a numbered list. These enviroments are similar to the <ul> and <ol> environments in html: The list items are introduced with the command "\item", which is analogous to the html code <li> . Here is an example of the first type of list:


\textbf{Some special characters in TeX:}


\item Accents  

\item Braces 

\item Dollar signs 


Replacing "itemize" by "enumerate" will create a numbered list (without any further changes - the numbers are generated automatically!).

Lists can be nested. In numbered lists (i.e., those generated by "enumerate"), the numbering style changes from one level to the next. The items at the top level are numbered 1., 2., etc., those at the next level are numbered alphabetically by (a), (b), etc. Note that the numbers are generated automatically - you don't have to enter them!

Exercise 3.3: Typeset the following set of exercises using correct TeX constructs:

1. Differentiate the following functions:
(a) f(x) = x2
(b) f(x) = log x
2. Integrate the following functions:
(a) f(x) = square root of x
(b) f(x) = sin x cos x

Fonts and spacing commands

The most important rule about font changing and spacing commands is to avoid using them! You should normally leave font and spacing matters up to TeX. If you don't like the way a document looks, try another documentclass (e.g., article instead of amsart), add an option to the documentclass, or add a global instruction to the preamble of the document, before \begin{document}. Options are specified in square brackets before the name of the class (which is enclosed in braces). Here are few such changes you might want to make: Explicit font and spacing instructions: As mentioned, it is generally a bad idea to "hand-code" font and spacing instructions. (In fact, I have a program that counts such hand-coded items in a TeX file and assigns a "badness" score depending on how many there are, with zero being a perfect score.) That said, there are situations where manually coded items are appropriate, so here is a quick rundown on the most important of these:

Error messages

As you begin doing more complex things, it is more likely that you will encounter error messages while compiling the file. Often, these error messages are cryptic, and it may be hard to figure out what is wrong. Here are some typical situations: The best strategy in most cases is to note the line number displayed in the error message, exit the compile phase by typing "x", and look at the document at or near this line number, in the hopes of spotting the error. If you don't succeed, ask someone. Some errors can be extremely frustrating and hard to locate.

Odds and ends

Here is a partial list of topics that we haven't covered here. Learn these as needed, using Gratzer's book as a reference.

How not to typeset LaTeX

Here are some examples of awfully bad TeX typing; they are more common than you think. All cases involve extensive use of hand-coded formatting. The TeX files compile okay, but the output doesn't look good, and fixing up these papers (so as to meet publisher's specifications) is an arduous and time-consuming task.
Exercise 3.5: Take a look at one of the above source files, and try to spots errors and bad habits. What would you do to fix up the file?


Online resources


One could certainly learn TeX/LaTeX entirely from online materials, but anyone serious about learning TeX should buy a book. It is a worthwhile investment that will likely be useful for a long time. Unlike books about commercial software which become obsolete as soon as a new version comes out (who would want to buy a book on Windows 95 today?), TeX is a stable system, and books on TeX have a long half-life. There are dozens of books on TeX, but there is only one you really need: Gratzer's "Math into LaTeX". Most of the books on the market are either inappropriate for beginners, or do not adequately cover mathematical typesetting. For most people, the Gratzer book is all they need. For those past the beginner level who really want to delve deeper into LaTeX, here are two additional recommendations, both at the intermediate-advanced level:

How to get the TeX software/program

If you do all your TeX work on the Math Department's Unix system (which is what I do - using ssh if I am at my home computer - and what I would strongly recommend), you don't have to worry about this. The TeX system we have is reasonably current, actively maintained, and probably more comprehensive than any system you might install on your own. If you use the departmental TeX installation, you can get help in case of problems through the help and texhelp email addresses, but if you work on your home computer, you are on your own. Another advantage to doing all your work on your Unix account is that your files will all be in one place, and you don't have to transfer files back and forth from your home machine to your Unix account.

If you really still want to have a local copy of the TeX software, here are the recommended options, depending on the OS you are using.

Back to Table of Contents

Last modified: Fri 24 Aug 2007 12:33:42 PM CDT