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}
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}
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).
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.
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.
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).
If you normally work with a spreadsheet, the LaTeX table setup might seem nonintuitive at first. Instead of an easy-to-read table grid, you have to enter abstract code. If you insist on using a WYSIWYG application, you can create the tables with LibreOffice Calc and convert them into LaTeX code with Calc2LaTeX-Makro [12] (Figure 3).
Highlight the relevant cells in the Calc file and select the menu item Tools | Macros | Run macro… . This step opens a dialog where you must click on Calc2LaTeX under Libraries | My macros and then on Calc2LaTeX in the unfolded menu. From the list of macros, select Main and then click on the Run button on the right. The macro's dialog presents some further settings. The LaTeX code then appears, which you can copy directly into your LaTeX document or save as a file.
An alternative is the online Table Editor [13], where you can create tables in a web browser and export them as LaTeX code in a LaTeX document (Figure 4).
LaTeX provides extensive opportunities for creating professional tables if used with the proper add-on packages. Not only is it ideal for static tables, but it can compete with spreadsheet programs. To write new values in a table, you unfortunately need to open the source file, edit it, and start a compile with LaTeX again. Only then will new values appear in the output.
Both solutions have a significant drawback, however. Calc2LaTex and the online editor both use the tabular environment without additional packages, with the resulting limitations. The cell colorization pretty much stays intact, though. l
Installing the Calc2LaTeX Macro
Download the Calc2LaTeX macro and unzip the file. Open LibreOffice and choose Tools | Macros | Organize macros | LibreOffice Basic… . A dialog appears where you click Manage… ; then click the Libraries tab and Import… . Move to the folder where you unzipped Calc2LaTeX and choose the script.xlb file, followed by OK to confirm.
Infos