# Tools for LaTeX tables

February 28, 2013
By

This is a guest post by Gregori Kanatzidis from SpanDeX.

Tables are a tricky business in LaTeX. Tables typically have their own formatting, and worse, are usually created in other applications. The commands and packages provided with LaTeX go some ways to making tables easier to use, but the clunky nature of the syntax make tables one of the worst parts of formatting a document. In this post, I’ll go over some of the drawbacks of LaTeX tables, and some tools that exist to make working with tables in LaTeX a bit easier.

### Text wrapping in tables

LaTeX does not automatically wrap text in tables. It will simply extend the width of the cell indefinitely, even if the text runs off the page.

Depending on your table environment, there are a few ways of dealing with this.

#### tabular and tabular*:

tabular’s environment options allow you to specify column widths in addition to the simple l, r, c alignments. p{x}, m{x}, and b{x} will all set the width of the column to ‘x’. p will align the top of the text to the cell, m will align the middle, and b will align the bottom. m{x} and b{x} both require the array package.

#### tabularx/tabu:

In tabularx and tabu, all you need to do is set the column specifier of the column containing the text to X. This will automatically stretch and wrap the text in the cell. There’s also an extra required environment option which sets the total width of the table, so don’t forget to include that.

#### tabulary:

tabulary is similar to tabularx, except that it adjusts the widths of the columns so that each row has equal height. Its column specifiers are L, C, R, and J for left, center, right, and left-right justification, respectively. They will all automatically wrap text.

### Tables that span multiple pages

#### longtable:

longtable works like tabular does, except that it will break tables across pages. It’s pretty simple to use; all you do is define what the overall header and footer are, and what the continuation header and footer are.

#### supertabular:

supertabular does what longtable does, except that it also supports flexible column widths like tabularx does. Note that its syntax is different from that of tabularx.

### Importing tables

Another complication is that tables included in LaTeX documents are typically created somewhere else, like MS_Excel. Excel2LaTeX is a plugin that will export a table to LaTeX from within Excel while retaining formatting and calculations. In Excel 2011, I had to manually add it as an add-in by going to “Tools -> Add-ins…”. From there all I had to do was select the cells that constituted the table and go to “Format -> Convert table to LaTeX”. It generates the LaTeX code while retaining bolds and italics. Then you can either save the table to a .tex file or copy it to your clipboard. Unfortunately the plugin isn’t perfect; for example it does not retain color formatting for either cell contents or the table in general (i.e. if you alternate row colors).

Another option is to simply write a script yourself. In principle, a basic table is easy to parse from either a csv file or even just the clipboard. If you copy a table from excel, the clipboard will contain a string where rows are separated by newlines (\n) and columns are separated by tabs (\t). You will have to reinsert formatting, but this way you needn’t install a plugin. If you don’t want to write your own script you could also use third-party spreadsheet software like Gnumeric which can open excel files and save directly to LaTeX.

### High quality design

The last thing I’m going to talk about is display formatting. The default packages in LaTeX force you to use \hline for rules and produce strange spacing for text and rows. You can change spacing parameters and modify rule sizes to fix it, but what’s the point of using LaTeX if you have to deal with formatting? So, booktabs was created to provide sensible formatting by default. It adds custom rules (\toprule, \midrule, \bottomrule) along with a number of other options, and it works with longtables and colortbl.

See an example (default table AND booktabs).

Hopefully this has shed some light on making it simpler to work with tables. I should point out that I mentioned another package, tabu, early in the post. This is a table package that was released in 2010, meant to provide a more workable and modern interface to tables in LaTeX. It combines the features of the most popular packages (tabularx, longtable, array), and allows for more complicated formatting, such as gradient color schemes for rows. tabu is fairly comprehensive so I believe it deserves its own post, hence not discussing it in detail here.

Do you have your own tips for dealing with tables? I’d love to hear them. Let me know in the comments.

Please comment on the article here: Hyndsight

Tags: , , ,

 Tweet

Email: