Designing great tables using LaTeX

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Print Ready

The booktabs package optimizes tables for printing and can be used to add tables to books, scientific papers, articles or, as in the example, an invoice. After downloading the package in the preamble (Listing 2, line 16), the table layout adjusts automatically. The package increases the text-to-cell-edge margins to make it look more attractive. The layout deliberately avoids vertical lines and coloration for easier readability.

The top and bottom table lines are thicker, as defined by the \toprule and \bottomrule commands (lines 28 and 14). For the thinner internal lines use the \midrule command (line 36), which replaces the \hline command. The layout omits lines between individual values and uses them under column headings only.

Well Calculated

LaTeX not only draws tables, it can perform calculations with help from the spreadtab package. The spreadsheet option allows you to specify the LaTeX table environment (Listing 2, line 27), in this case tabu instead of tabular . The options define five columns. Apart from the usual parameters and the X option described earlier for tabu , the example uses the N option from the numprint [9] package that aligns the numbers on the decimal separator.

The first set of curly braces after the N gives the number of digits before the decimal sign, and the second set gives the number of digits after it. The sign used (dot or comma) depends on the language setting on the first line of the code. In the LaTeX code, however, you must use the dot as decimal separator (line 30) and can't separate the thousand position with a space, as would appear in some languages.

In this case, I ask LaTeX to present the invoice amounts with two decimal positions, rounded, as defined by the nprounddigits command (line 25). The eurosym [11] package provides the euro symbol, if needed (line 30). LaTeX generally expects a value for each cell in spreadtab . If text is included and the cell is empty, you need to prefix it with the @ sign (line 30). If a column aligning at the decimal point happens to be text, you can put it in a \multicolumn command, even if the column has only one cell (line 29).

You address each cell's calculation as you would a spreadsheet. The first cell at the top left has an address of a1 , the cell to the right of it b1 , the cell under it b2 , and so forth. If a cell has text as well as an amount, differentiate the amount with :={<amount>} (line 30).

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content