Designing great tables using LaTeX

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Yana Gayvoronskaya, 123RF

Yana Gayvoronskaya, 123RF

Table for Two

A basic LaTeX setup can only help you design simple tables. With some additional packages, however, you can jazz things up and build some nice professional-looking tables.

LaTeX not only gives you an attractive layout but also puts data and content into clear tables. The basic LaTeX configuration brings with it the tabular environment that provides all the necessary functions for creating basic tables. Adding further packages provide more professional functionality for LaTeX tables.

I will present a few among the multitude of available packages in this article. The booktabs [1] package optimizes your tables for printing. If you want color, the colortbl [2] would be the right choice. The multirow [3] package lets you merge cells in a column. If you want the calculation functions of a spreadsheet, spreadtab [4] is what you need. The tabu [5] package provides many additional table functions.

I'll demonstrate what LaTeX has to offer in tables based on an example of a study plan and an invoice. Listing 1 shows the sample code for the study plan, and Figure 1 shows the results. The code for the invoice is in Listing 2, and the results are in Figure 2.

Listing 1

Study Plan

01 \documentclass[10pt]{scrartcl}
02
03 \usepackage[utf8]{inputenc}
04 \usepackage[T1]{fontenc}
05
06 \usepackage[
07         paperheight=210mm,
08         paperwidth=297mm,
09         left=20mm,
10         right=20mm,
11         top=20mm,
12         bottom=20mm
13 ]
14 {geometry}
15
16 \usepackage[   english]{babel}
17
18 \usepackage{
19 tabu,
20 multirow,
21 hyperref,
22 colortbl,
23 xcolor
24 }
25
26 \title{Study Plan}
27 \author{}
28 \date{WS 2014/2015}
29
30 \begin{document}
31 \maketitle
32
33 \newcommand{\minitab}[2][c]{\begin{tabular}{#1}#2\end{tabular}}
34 \begin{tabu}{|>{\columncolor[named]{lightgray}}c|X
   [l]X[r]|X[l]X[r]|X[l]X[r]|X[l]X[r]|X[l]X[r]|}
35 \hline
36
37 \rowcolor[named]{gray}
38 \textsf{\textbf{Time}} & \multicolumn{2}{c|}
   {\textsf{\textbf{Monday}}} & \multicolumn{2}{c|}
   {\textsf{\textbf{Tuesday}}} & \multicolumn{2}{c|}
   {\textsf{\textbf{Wednesday}}} & \multicolumn{2}{c|}
   {\textsf{\textbf{Thursday}}} & \multicolumn{2}{c|}
   {\textsf{\textbf{Friday}}}\\
39 \hline
40
41  & & & \cellcolor[named]{yellow} C 1 & \cellcolor[named]
   {yellow} Matthews & & & \cellcolor[named]{orange} C 2 &
   \cellcolor[named]{orange} Smith & & \\
42 \multirow{-2}*{\minitab[c]{\textsf{\textbf{8--10}}}}
   & & & \multicolumn{2}{c|}{\cellcolor[named]
   {yellow}\textbf{Metaphysics}} & & & \multicolumn
   {2}{c|}{\cellcolor[named]{orange}\textbf{Logic}} & & \\
43 \hline
44
45  & \cellcolor[named]{green} C 7 & \cellcolor[named]{green}
    Martin & \cellcolor[named]{cyan} C 4 & \cellcolor[named]
    {cyan} White & \cellcolor[named]{yellow} C 7 & \cellcolor
    [named]{yellow} Matthews & \cellcolor[named]{magenta}
    C 3 & \cellcolor[named]{magenta} Knight & \cellcolor
    [named]{orange} C 2 & \cellcolor[named]{orange} Smith \\
46 \multirow{-2}*{\minitab[c]{\textsf{\textbf{10--12}}}} &
   \multicolumn{2}{c|}{\cellcolor[named]{green}\textbf{Ethics}} &
   \multicolumn{2}{c|}{\cellcolor[named]{cyan}\textbf{Epistomoly}} &
   \multicolumn{2}{c|}{\cellcolor[named]{yellow}\textbf{Metaphysics}} &
   \multicolumn{2}{c|}{\cellcolor[named]{magenta}\textbf{Legal Philosophy}} &
   \multicolumn{2}{c|}{\cellcolor[named]{orange}\textbf{Logic}}\\
47 \hline
48
49  & & & \cellcolor[named]{green} C 7 & \cellcolor[named]
   {green} Martin & \cellcolor[named]{magenta} C 3 & \
   cellcolor[named]{magenta} Knight & & & & \\
50 \multirow{-2}*{\minitab[c]{\textsf{\textbf{14--16}}}}
   & & & \multicolumn{2}{c|}{\cellcolor[named]{green}\
   textbf{Ethics}} & \multicolumn{2}{c|}{\cellcolor[named]
   {magenta}\textbf{Legal Philosophy}} & & & & \\
51 \hline
52
53  & \cellcolor[named]{cyan} C 4 & \cellcolor[named]
   {cyan} White & & & \cellcolor[named]{blue} C 3 & \
   cellcolor[named]{blue} Schmitz & & & & \\
54 \multirow{-2}*{\minitab[c]{\textsf{\textbf{16--18}}}} & \
   multicolumn{2}{c|}{\cellcolor[named]{cyan}\textbf
   {Epistemology}} & & & \multicolumn{2}{c|}{\
   cellcolor[named]{blue}\textbf{Theories of Truth}
   \footnote{14-day course.}} & & & & \\
55 \hline
56
57  & & & \cellcolor[named]{orange} C 1 & \cellcolor[named]
   {orange} Green & & & & & & \\
58 \multirow{-2}*{\minitab[c]{\textsf{\textbf{18--20}}}} & & & \
   multicolumn{2}{c|}{\cellcolor[named]{orange}\
   textbf{Practical Logic}} & & & & & & \\
59 \hline
60
61 \end{tabu}
62 \end{document}
Figure 1: This study plan shows one of the possibilities LaTeX tables provide.

Listing 2

Invoice

01 \documentclass[10pt]{scrartcl}
02
03 \usepackage[utf8]{inputenc}
04 \usepackage[T1]{fontenc}
05
06 \usepackage[
07         paperheight=297mm,
08         paperwidth=210mm,
09         left=20mm,
10         right=20mm,
11         top=20mm,
12         bottom=20mm
13 ]
14 {geometry}
15
16 \usepackage[english]{babel}
17 \usepackage{booktabs, multirow, numprint, spreadtab, tabu, eurosym}
18
19 \title{Invoice}
20 \author{}
21 \date{}
22
23 \begin{document}
24
25 \maketitle
26 \nprounddigits{2}
27 \begin{spreadtab}{{tabu}{rXN{3}{2}N{4}{2}r}}\toprule
28 @Am. & @Item & @\multicolumn{1}{r}{Price} & @\
   multicolumn{1}{r}{Total price} & @ \\ \midrule
29 10 & @Edwin A. Abbott: Flatland & :={9.95}\ \euro & :={a2*c2}\ \euro & @[2]\\
30 5 & @Flatland-Terrarium & :={420}\ \euro & :={a3*c3}\ \euro & @[1]\\
31 3 & @Flatland-Globus & :={73}\ \euro & :={a4*c4}\ \euro & @[1]\\\midrule
32 @\multicolumn{2}{l}{Invoice amount} & @\multicolumn
   {1}{r}{} & :={sum(d2:d4)}\ \euro & @ \\
33 @\multicolumn{2}{l}{[1] 19\%\ Value Added Tax included} & @\
   multicolumn{1}{r}{} & :={(d3+d4)/119*19}\ \euro & @ \\
34 @\multicolumn{2}{l}{[2] 7\%\ Value Added Tax included} & @\
   multicolumn{1}{r}{} & :={d2/107*7}\ \euro & @ \\
35 \bottomrule
36 \end{spreadtab}
37 \end{document}
Figure 2: This invoice shows another example of what LaTeX tables have to offer. Spreadsheet calculations are no problem.

Setting Up the Structure

Instead of using the standard tabular LaTeX function for tables, use tabu (Listing 1, line 18), which provides a variety of options for creating tables. After giving the table a title in the preamble (lines 26 through 28) and the command to add the title to the document (line 31), you can start creating the table.

The table is then set up using the tabu environment (line 34); see also the "Oversized" box. The second set of curly braces introduces the 11 columns, with the vertical bar (| ) for separators. The cells in the first column have centered text. Apart from the usual cell property definitions, tabu provides the X option to distribute the columns evenly across the table width. The text justification options are in square brackets, alternating between left and right.

Oversized

Both the standard tabular and tabu environments create tables without page breaks. For tables running across multiple pages, it's best to use the longtabu environment that is part of the tabu package.

The example study plan requires two merged vertical cells for the hours factor. To do this in a tabu table, use the multirow package. Use the \minitab command (line 33), which you can then use to create a table within a table later on.

You can merge vertically separated cells using \multirow (line 50). Normally, you would define it in the top cell, with a positive integer in the curly braces indicating the number of cells to merge. In the example, however, I defined it in the bottom cell, so that the cell color doesn't cover the text, thereby giving the number of cells to merge a negative value.

In the title definition line, I merged two cells for the separate weekdays (line 38), using the \multicolumn command, with the first curly braces indicating the number of cells to merge. The text justification and line separators are in the second set of curly braces, and the content is in the third set.

As is usual with tables, tabu uses an ampersand (& ) to separate individual cells in a column. A double backslash (\\ ) ends the cell definition. The usual \hline command defines horizontal lines. The standard LaTeX tabular environment presents some problems for footnotes and their correct linking within a table. This can only be solved with some ugly hacks. However, footnotes aren't a problem in tabu (line 54), as long as you also include the hyperref [6] package (line 21).

Bright Colors

Tables don't need to consist of black lines only. If you want to add a bit of color, include the colortbl package (Listing 1, line 22). The package, in turn, includes the color package that provides basic color functionality. If you want even more advanced color features, include the xcolor [7] package. As with a spreadsheet program, LaTeX allows you to colorize multiple lines in one shot.

The top line of the study plan shows the weekdays in a dark gray. The command for that appears in the lines for the row contents (line 37). The columns for the times are in a lighter gray (line 34), in the second set of curly braces opening the tabu environment. All cells with the same study courses take the same color value (line 50, green in this case). For more advanced colorization, use the LaTeX class colortbl , for example, to colorize the table line separators.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content