\chapter{Tabulars}\label{sec:tabulars}
%\addtocontents{toc}{\protect\enlargethispage*{13bp}}

\setlength\extrarowheight{0pt}

\section{Basics}

Outside math mode, the \texttt{tabular} environment provides tables,
which can be considered the text counterpart of multicolumn arrays.
As with math arrays, columns are separated with `\texttt{\&}' and
rows with `\texttt{\bsl\bsl}'.

\TS{} has a tabular wizard, but it is not much help when things get
hairy.

\nextblock
A very basic table:

\exa
\begin{verbatim}
\begin{tabular}{lcr}
small & whatever & 1 \\
big   & huh?     & 10000
\end{tabular}
\end{verbatim}
\exb
\begin{tabular}{lcr}
small & whatever & 1 \\
big   & huh?     & 10000
\end{tabular}
\exc

\nextblock
There is a preamble \texttt{\{lcr\}} which defines the
alignment of the columns: left, center and right.

\nextblock
A table with some empty cells:

\exa
\begin{verbatim}
\begin{tabular}{lcr}
small & whatever \\
big   &          & 10000
\end{tabular}
\end{verbatim}
\exb
\begin{tabular}{lcr}
small & whatever \\
big   &          & 10000
\end{tabular}
\exc

\nextblock
You do not need to insert an ampersand \texttt{\&} for empty cells
at the end.

\nextblock
You can add vertical rules in the preamble and horizontal rules with
an \cmd{hline} command:

\exa
\begin{verbatim}
\begin{tabular}[t]{|l|r|r|}
\hline
& \textit{Butter} & \textit{Cheese} \\
\hline
2000 &  9.1 & 5.7 \\
\hline
2001 & 11.7 & 6.3 \\
\hline
2002 & 12.2 & 6.5 \\
\hline
\end{tabular}
\end{verbatim}
\exb
\begin{tabular}[t]{|l|r|r|}
\hline
& \textit{Butter} & \textit{Cheese} \\
\hline
2000 &  9.1 & 5.7 \\
\hline
2001 & 11.7 & 6.3 \\
\hline
2002 & 12.2 & 6.5 \\
\hline
\end{tabular}
\exc

\nextblock
If you use horizontal rules at all, you should include the commands
\begin{verbatim}
\usepackage{array}
\setlength\extrarowheight{1pt}
\end{verbatim}
in the preamble to get a little bit of space between rules and the
cells below. You can also issue an \cmd{extrarowheight} command in
the middle of your document (from now on, \cmd{extrarowheight} is
set to 1pt). Fewer rules are usually better, see table~\ref{tab:rules}.

\setlength\extrarowheight{1pt}

\begin{table}[t]
  \caption{Fewer rules are usually better}
  \label{tab:rules}
  \centering
  \begin{tabular}[t]{lrr}
  \hline
  & \textit{Butter} & \textit{Cheese} \\
  \hline
  2000 &  9.1 & 5.7 \\
  2001 & 11.7 & 6.3 \\
  2002 & 12.2 & 6.5 \\
  \hline
  \end{tabular}
\end{table}

\newpage

\section{Partial rules}

With a \cmd{cline} command you can insert a horizontal rule that
spans a range of columns:

\exa
\begin{verbatim}
\begin{tabular}{|lrr|}
\hline
& \textit{Butter} & \textit{Cheese} \\
\cline{2-3}
2000 &  9.1 & 5.7 \\
2001 & 11.7 & 6.3 \\
2002 & 12.2 & 6.5 \\
\hline
\end{tabular}
\end{verbatim}
\exb
\begin{tabular}{|lrr|}
\hline
& \textit{Butter} & \textit{Cheese} \\
\cline{2-3}
2000 &  9.1 & 5.7 \\
2001 & 11.7 & 6.3 \\
2002 & 12.2 & 6.5 \\
\hline
\end{tabular}
\exc

\section{Multicolumn}

the \cmd{multicolumn} macro lets you join columns, or change the
alignment of a column. Its parameters are:
\begin{enumerate}
\item number of columns to merge
\item preamble
\item content
\end{enumerate}

\exa
\begin{verbatim}
\begin{tabular}{|lrr|}
\hline
& \multicolumn{2}{c|}{Products} \\
\cline{2-3}
& \multicolumn{1}{c}{\textit{B.}}
& \multicolumn{1}{c|}{\textit{C.}} \\
\cline{2-3}
...
\end{verbatim}
\exb
\begin{tabular}{|lrr|}
\hline
& \multicolumn{2}{c|}{Products} \\
\cline{2-3}
& \multicolumn{1}{c}{\textit{B.}}
& \multicolumn{1}{c|}{\textit{C.}} \\
\cline{2-3}
2000 &  910.1 & 5.7 \\
2001 & 1111.7 & 6.3 \\
2002 & 1112.2 & 66.5 \\
\hline
\end{tabular}
\exc

\section{Decimal alignment}

Often, you can simply right-align, since typically all data in a
column are specified with the same number of decimal digits. This
is the case with the Butter / Cheese examples above.

If this isn't the case, you can put the following code in your preamble:
\begin{verbatim}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\end{verbatim}
This lets you use column types \verb|d{n.m}| with $n$ digits before
the decimal point and $m$ after:
\exa
\begin{verbatim}
\begin{tabular}{|l|d{4.2}|d{4.1}|}
\hline
2000 &  910.1 & 5.7 \\
2001 & 1111.77 & 6 \\
2002 & 1112.2 & 6666.5 \\
\hline
\end{tabular}
\end{verbatim}
\exb
%\newcolumntype{d}[1]{D{.}{.}{#1}}
\begin{tabular}{|l|d{4.2}|d{4.1}|}
\hline
2000 &  910.1 & 5.7 \\
2001 & 1111.77 & 6 \\
2002 & 1112.2 & 6666.5 \\
\hline
\end{tabular}
\exc

\section{Text columns}

For multiline texts, there is the \texttt{p\{\ldots\}} column
specification:
\exa
\begin{verbatim}
\begin{tabular}{|lp{1.65in}|}
  \hline
  array   & An improved implementation of \LaTeX's
            tabular and array environment\\
  dcolumn & Provides decimal and other alignment
            for tabular- and array environments\\
\hline
\end{tabular}
\end{verbatim}
\exb
\null\hspace{-1cm}\hfill
\begin{tabular}{|lp{1.65in}|}
  \hline
  array   & An improved implementation of \LaTeX's
            tabular and array environment\\
  dcolumn & Provides decimal and other alignment
            for tabular- and array environments\\
\hline
\end{tabular}
\exc

\nextblock
Usually, text cells are far too narrow for good
justification. Here, ragged right would be much better. This can be done with
the \package{array} package, which provides syntax for adding
\LaTeX{} code before (and after) each column entry:

\exa
\begin{verbatim}
\usepackage{array}
\newcolumntype{P}[1]{%
  >{\raggedright\hspace{0pt}\arraybackslash}p{#1}}
\begin{tabular}{|l|P{1.65cm}|}
\hline
What is \TeX? & \TeX{} is a programming
 language for typesetting.\\
\hline
\end{tabular}
\end{verbatim}
\exb
\newcolumntype{P}[1]{%
  >{\raggedright\hspace{0pt}\arraybackslash}p{#1}}
\begin{tabular}{|l|P{1.65cm}|}
\hline
What is \TeX? & \TeX{} is a programming
 language for typesetting.\\
\hline
\end{tabular}
\exc

\nextblock
See the documentation of the array- and dcolumn packages
\nocite{array}\nocite{dcolumn} for additional details
on typesetting tabulars.

\section{Floating tables}

In \LaTeX-speak, a table or figure `floats' when its placement on
the page does not necessarily match its placement in the
\LaTeX{} source. It may be moved to, \emph{e.g.}, the top or bottom of
a page, or get a page by itself, as in table~\ref{tab:rules}. We
shall discuss floating tables
and figures in section~\ref{sec:float} of the next chapter.

\section{Shortcuts}
\label{sec:shortcuts}

Of course, nobody wants to re-key reams of numbers. There are
several solutions:\enlargethispage*{15bp}
\begin{itemize}
\item There is an excel2latex plugin for Excel, available from
  CTAN\nocite{ctan}, that can create a \LaTeX{} source with a
  tabular environment from a spreadsheet range. It took me some
  googling to get this package properly installed in MS Office 2010.
\item Gnumeric is a spreadsheet program that can read
  OpenOffice/LibreOffice spreadsheets and export to \LaTeX{},
  although without a preamble. It was originally created for Linux,
  but Windows binaries are also available.
\item There is a \LaTeX{} package \texttt{odsfile} that can read
  OpenOffice/LibreOffice spreadsheets directly, \emph{e.g.}:
\begin{verbatim}
\usepackage{odsfile}
...
\begin{tabular}{...}
\includespread[file=filename.ods,range=a3:f8]
\end{tabular}
\end{verbatim}
  \emph{This package requires the lualatex engine}, i.e. you need to
  compile your \LaTeX{} source with lualatex instead of
  pdflatex. \package{odsfile} is part of our \TL{}
  installation. Search the documentation list (see
  \ref{sec:doclist}) for `odsfile'.
\item If your data are in a simple text format, or at least in a
  reasonably simple binary format, it may be a nice programming
  exercise to convert them into \LaTeX. Spreadsheets can export to
  \path{.csv}; which is such a format. Gnuplot is another such
  format. Search the documentation list for `csv' or `gnuplot' for
  existing solutions.
\end{itemize}

\section {Practice}\label{sec:table_practice}

Do not forget to load the \package{array}- and \package{dcolumn}
packages in the preamble\footnote{Actually, \package{dcolumn}
already loads \package{array} so there is no real need to load
\package{array} explicitly.}:
\begin{verbatim}
\usepackage{array,dcolumn}
\end{verbatim}
No doubt, you have lots of tables and spreadsheets of your own to
convert to \LaTeX.
Otherwise, you can find various table examples in Chapter 8 of \emph{Unix Text
  Processing}\/, an old Unix text which has been republished in
O'Reilly's Open Book Project:
\url{http://oreilly.com/openbook/utp/}.

\nextblock
The practice zip, see section~\ref{sec:zipfile}, contains:
\begin{itemize}
\item an example file \path{tabulars_sample.tex}
\item Various files \path{some_data...} which together illustrate
  getting spreadsheet data into \LaTeX{}.
\end{itemize}
