Crash Course in LaTeX

Basics: Tables & Arrays

The Easy Way

As you'll see in the samples below, the LaTeX code to generate tables can seem a little cryptic at first. The good news is that you can use other software to write this code for you.

If you use MS Excel, there is a small free plugin called excel2latex that will let you create your tables in Excel and export them to LateX. To install excel2latex, just download it, unzip, and open the .xls file. To use it, create the table you want in Excel, select the cells you want for the table, and click the Export LaTeX Export LaTeX button on the toolbar. Then you can copy the contents of the file to your source file. Of course, there are some LaTeX table features things that excel2latex does not support, so you may want to edit the code generated. It will help to understand how the "table" commands work.

Customizing Your Tables

Three commands do about the same thing and use the same syntax:
\begin{tabular}[pos]{cols}           rows     \end{tabular}
\begin{tabular*}{width}[pos]{cols}   rows     \end{tabular}
\begin{array}[pos]{cols}             rows     \end{tabular}

The tabular command creates a table whose width is set automatically based on the content. The tabular* command gives you the option of setting the overall width of the table. The array command is used in math mode. Other than that, the syntax and use is the same.

[pos] is an optional command. If you set [t], then the top line of the table is aligned with the baseline of the text preceding the table. If you set [b] or do not use this command, the bottom of the table is aligned witht he baseline of the text preceding the table.

{cols} sets the number of columns and the default alignment in each column. You can also add vertical lines between columns (borders) that run the entire height of the table.

Column formatting symbols Border formatting symbols
l column contents are left-justified | Draws a vertical line between the columns
r column contents are right-justified || Draws two vertical lines between the columns
c column contents are centered @{text} Inserts text between each column
p{wth} text of column is set in lines of width wth

Examples:

\begin{tabular}{|c|l|r|} Starts a table with three columns. Each column is seperated by vertical lines. The first column is centered, the second is left-justified, and the third is right-justified.
\begin{tabular*}{4in}{|cccl|} Starts a 4 inch wide table with four columns. There are vertical lines on the left and right sides of the table, but not between columns.
\begin{tabular}[t]{cp{2in}c} Starts a table whose top line is aligned with the baseline of the preceding text. There are three columns. The second one is limitied to 2 inches in width, so if the content is wider, it will wrap to new lines within the cell of the table.

Rows end in \\. Columns are seperated by &. You can add horizontal lines with \hline.

Example:

\begin{tabular}{|c|l|}
  \hline
  Cell 1 & Cell 2 \\
  \hilne
  Bottom row is longer & but the table adjusts to fit! \\
  \hline
\end{tabular}

Gives this:

Cell 1 Cell 2
Bottom row is longer but the table adjusts to fit!

Centering a table: You can center a table by putting \begin{center} and \end{center} around it.

Partial horizontal lines: The command \cline{n-m} draws a horizontal line from the left side of column n to the right side of column m.

Spanning columns: The command \multicolumn{num}{col}{text} puts text is a cell that spans num columns using alignment and border formatting col.

Example:

\begin{center}
\begin{tabular}{cp{2.5in}rr}
  Qty & Description & Cost each & Sub Total \\
  \hline
  10 & Course text & \$12.95 & \$129.50 \\
  1 & Custom-built software, including full documentation & \$4,500 & 4,500.00 \\
  2 & Servers (to run the software) & \$1,495.00 & 2,990.00 \\
  \hline
  \multicolumn{3}{r}{Total (not including tax):} & \$7,619.50 \\
  \cline{4-4}
\end{tabular}

Gives us:

Note:

More features: The tabular/tabular*/array environments have more features, but not enough for everyone. Other packages are available that extend these commands with new features. Look for packages like array, dcolumn, tabularx, delarray, and longtable.