Tables With Latex
Tables With Latex
Tables With Latex
1
Article revision 2007/02/20
Introduction
Tables are one of the most used objects in scientific publications. Besides the
regular LATEX commands, a large number of packages can be used to customize
the tables and overcome the limitations that the regular commands have. The
documentation on tables is very fragmented because every package modifies just
an aspect of the table typeset; hence, authors have to look for information on
several guides.
This article explains how to use LATEX2ε and its packages to create tables. Each
topic is not studied in depth (the reader can refer to the package manual for
this) but the author tries to cover as many aspects as possible highlighting the
solutions that he likes most. The choice of the contents comes firstly from the
author’s experience and then from the many discussions on tables on the forum
of guIt (Gruppo Utilizzatori Italiani di TEX).1
The text presumes that the reader is familiar with the basics of LATEX2ε , which
means that he has read at least one of the many free guides available on the net
[8, 21] or a book [1, 5, 10–12, 14–18]. Whenever a package is cited, the article
1. The forum of guIt is at http://www.guit.sssup.it/forum/.
D Pu uu β Gf
5 in 269.8 lbs 0.000674 in 1.79 0.04089 psi · in
10 in 421.0 lbs 0.001035 in 3.59 "
20 in 640.2 lbs 0.001565 in 7.18 "
does not give a complete description of how it works, which can be found in the
package manual,2 but analyzes the main options and suggests their use.
This text is focused on the preparation of tables and does not address prob-
lems that are connected to it as for example how to position floating objects or
how to modify the caption [3]. For these topics, that are in common with all the
other floating objects (e.g. figures), the reader can refer to [20].
1 Basics
1.1 General rules
The typeset of tables should be based on the following rules [7]:
3. place the units in the heading of the table (instead of the body);
To better understand the importance of these simple rules, the reader can
compare tab. 1 and 2.
2. The majority of LATEX packages has a manual which describes how to use it and usually gives
examples. The location of the manual can vary between different distributions but, usually, is
under the subfolder /texmf/doc.
2
Table 2: Table in agreement of the general typeset rules.
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
or this code
\ begin { table }[ tp ] %
\ caption { Maximum load and nominal tension .}
\ label { aggiungi }\ centering %
\ begin { tabular }{ clccc }
\ toprule %
$ D $&&$ P _ u $&$\ sigma _ N $\\
( in )&&( lbs )&( psi )\\\ otoprule %
5& test 1&285&38.00\\
& test 2&287&38.27\\
& test 3&230&30.67\\\ midrule
10& test 1&430&28.67\\
& test 2&433&28.87\\
& test 3&431&28.73\\\ bottomrule
\ end { tabular }
\ end { table }
3
Table 3: Maximum load and nominal tension.
D Pu σN
(in) (lbs) (psi)
5 test 1 285 38.00
test 2 287 38.27
test 3 230 30.67
10 test 1 430 28.67
test 2 433 28.87
test 3 431 28.73
4
cols Defines the alignment and the borders of each column. It can have the
following values:
l the column is aligned to the left;
r the column is aligned to the right;
c the column is centered;
p{wth} the column is justified and its width is wth (the text is inserted into
a parbox of width wth);
*{num}{form} the format form is repeated num times; for example *{3}{|l}|
is equal to |l|l|l|.
Besides the format of the columns, we can also specify what can be inserted
between two columns with the following symbols:
| draws a vertical line (not recommended);
|| draws a double vertical line (not recommended);
@{text} insert the text text in every line of the table between the columns
where it appears. This command eliminates the space that is automati-
cally inserted between the columns. If some horizontal space is needed
between text and the columns, it can be inserted with the command
\hspace{}. The command \extraspace\fill in a tabular* environ-
ment extends the space between the columns where it appears in order
to let the table have the width defined by the user (see for example
table 12 which is obtained with the code at page 14). In order to elimi-
nate the space that is automatically inserted between two columns it is
possible to use the empty command @{}.
rows represents the content of the cells of the table for each row that is ended by
the command \\. In each row the content of each column is separated by
the symbol &; each row has the same number of cells (i.e. same number of
&)3 which must be equal to that declared in the definition cols.
\hline can be placed in the first row or at the end of a row \\ and it draws
an horizontal line as wide as the entire table.
\cline{n-m} draws an horizontal line from the left of column n up to the
right of the column m.
3. The cells might be empty.
5
Table 4: Example of the standard LATEX multicolumn and cline
commands.
Sample Roughness R a
(nm)
A ring 385
plate 397
B ring 376
plate 390
Since the command @{...} inserts its argument without any space between
the columns, it can be used without any argument if we want to eliminate that
space. For example in some cases it is useful to eliminate the space on the left of
the first column and on the right of the last column; table 5 is obtained with the
following code:
\ begin { tabular }{ @ {} >{\ bfseries } lp {6 cm } @ {}}
...
\ end { tabular }
6
Table 5: Table without horizontal space on the left of the first col-
umn and on the right of the last one.
\tabcolsep is half of the width of the space between the columns of the tabular
and tabular* environments.
\arraycolsep is half of the width of the space between the columns of the array
environments.
\doublerulesep is the space between double lines (\hline\hline).
The command \setlength can be used to modify these parameters [1, 5, 8, 10, 11,
14, 15, 17, 18, 21].
– allows to generate the list of tables with the command \listoftables (sim-
ilar to \listoffigures for the figures).
– allows to create a caption taking care of:
· assigning the right name to the environment; this depends on the lan-
guage selected with babel: in Italian it is “tabella” while in English is
“table”;
7
· assigning a number to the table.
– allows to assign a \label to the table which can be used for references in
the text.
\multirow{row}*{text} creates a cell that contains the text text and extends on
row rows and has an undefined width;
\multirow{row}{larg}*{testo} creates a cell that contains the text text and
extends on row rows and has an width equal to larg;
8
Table 6: Example of the standard LATEX multicolumn and multirow
commands.
D Pu σN
(in) (lbs) (psi)
test 1 285 38.00
5 test 2 287 38.27
test 3 230 30.67
test 1 430 28.67
10 test 2 433 28.87
test 3 431 28.73
Compare it with table 3 that does not use the \multirow command.
9
Table 7: Table obtained with the standard LATEX lines (hline).
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
the command
\ midrule [0.08 em ]
In this article the line below the heading of the tables has always thickness equal
to \toprule and \bottomrule but it is vertically centered with respect to the row
above and below (which is typical of the lines \midrule). To obtain this result, a
new line type has been defined with the following command
\ newcommand {\ otoprule }{\ midrule [\ he av yr ule wi dt h ]}
10
b{wth} defines a justified column with width wth and cells that are aligned to
the bottom (as a \parbox[b]{wth});
>{ins} can be placed before a command l, r, c, p, m or b and inserts ins before
the content of the cell;
<{ins} can be placed after a command l, r, c, p, m or b and inserts ins after the
content of the cell.
to have column respectively centered, aligned to the left or to the right. If the
cell have to contain mathematics in the displaystyle format we can define the
columns as
\ newcolumntype { A }{ >{$\ displaystyle }c <{$}}
\ newcolumntype { Q }{ >{$\ displaystyle }l <{$}}
\ newcolumntype { V }{ >{$\ displaystyle }r <{$}}
gives table 8.
Further details on the array package can be found on its manual and [13].
11
Table 8: Table with mathematics obtained with the array package.
Z
cos x dx sin x + c
Z
e x dx ex + c
Z
sec2 x dx tan x + c
12
Table 10: Use of the array package to format the text of a row.
and then + must be added on the left of the first column and ^ on the right
of every other column in the column definition of tabular. This defines a new
command
\ rowstyle {...}
which can be inserted at the beginning of a row to format its cells. For example
the code
\ begin { tabular }{+ l ^ c ^ c ^ c }
\ toprule \ rowstyle {\ bfseries } %
Quantity & Symbol & Unit & Value \\\ otoprule %
...
\ end { tabular }
13
Table 11: Table obtained with the tabularx package.
produce table 11; notice that in the similar table 9 the length of the second column
has been defined by the user, while in table 11 the user defines the total width of
the table (in this case \textwidth) and then the program computes automatically
the width of the second column. Compare the result with table 12 obtained
with the tabular* environment: in this case the space between the columns is
stretched instead of the width of the column. To obtain the table, the following
code was used
\ begin { tabular *}{\ textwidth }[ tb ] %
{ >{\ bfseries } l@ {\ extracolsep {\ fill }} p {6 cm }}
...
\ end { tabular *}
The X column are by default of type p{wth} (i.e. justified) and the width
wth is determined automatically by the program. It is anyway possible to de-
fine new column types with a different horizontal alignment with the command
\newcolumntype. For example
\ newcolumntype { Y }{ >{\ raggedright \ ar ra yb ack sl as h } X }
14
Table 12: Table obtained with the tabular* environment.
15
Table 13: Table obtained with the tabularx package and redefining
new types of horizontal alignment for the columns.
text & 9\\\ midrule 10 & 11 & 12 & Cell with text aligned to the
right \\\ bottomrule
\ end { tabularx }
produces table 14(a), while the same code with the command
\ renewcommand {\ tabularxcolumn }[1]{ >{\ a rra yb ac ksl as h } b {#1}}
16
Table 14: Tables obtained with the tabularx package and defining
the vertical alignment of the cells in order to have them centered
(a) and aligned to the bottom (b).
Table 15: Table obtained with the tabularx package to have columns
with the same width.
−2π 0 1 0 ∞ 1 ∞
√ √ √ √
−7π/4 2/2 2/2 1 2 2 1
−3π/2 1 0 ∞ 1 ∞ 0
√ √ √ √
−5π/4 2/2 − 2/2 −1 2 − 2 −1
−π 0 −1 0 ∞ −1 ∞
√ √ √ √
−3π/4 − 2/2 − 2/2 1 − 2 − 2 1
−π/2 −1 0 ∞ −1 ∞ 0
√ √ √ √
−π/4 − 2/2 2/2 −1 − 2 2 −1
0 0 1 0 ∞ 1 ∞
17
Table 16: Table obtained with the tabularx package imposing the
proportion on the width of columns: the first column is half as
wide as the second one.
18
Table 17: Table with a column of defined width and alignment to
the right.
the lines only in certain cells of a column, it is possible to insert into these cells a
parbox or a minipage.
The use of these commands is also convenient when we want to assign the
width of a non justified column.5 If for example we want a column 5 cm wide
and with text aligned to the right, it is possible to insert all the cells of that column
into minipage environments with the command
{ >{\ bfseries }l >{\ begin { minipage }[ t ]{5 cm } %
\ raggedleft \ arraybackslash } %
l <{\ end { minipage }\ arraybackslash }}
With this command it is possible to obtain table 17 with the code used for table
11.
19
Table 18: Table with cell with different alignment in the same col-
umn.
The first argument (sep-in) is the symbol used in the .tex document to separate
the integral and the fractional part (usually the decimal point . or the decimal
comma ,), the second argument (sep-out) is the symbol that we want in the
output, the third is the number of digits on the left (before) and on the right
(after) of this symbol. The numbers are aligned to the decimal point and, in case
that the third argument is negative, the decimal point is aligned to the center of
the column. If the columns have a heading, it must be inserted into the command
20
Table 19: Table with alignment to the decimal point.
Expression Value
π 3.1416
ππ 36.46
π
ππ 80662.7
In order to avoid inserting all the three parameters for every column, it is possible
to define a new type of column, for example with the code
\ newcolumntype { d }[1]{ D { ,}{.}{#1}}
where d has only one argument which defines the number of decimal digits.
Gregorio [13] shows an interesting use of \newcolumntype to let the decimal
separator be dependent on the language selected with babel: comma (,) for the
italian option and point (.) for the english option.
where the arguments have the same meaning of those for dcolumn. The argument
after in this case defines the number of digits to keep during the approximation;
for example if this parameter is equal to 3, 3.1416 becomes 3.142 and 80662.7
21
Table 20: Table with alignment to the decimal point and automatic
approximation.
Expression Value
π 3.142
ππ 36.460
π
ππ 80662.700
3 Large tables
Sometimes, especially in the appendix, the size of a table is bigger than a page in
width, height, or both. In case the table is too long, it is possible to
22
Table 21: Table with font size smaller (footnotesize) than the rest
of the text (normalsize).
23
3.2 Rotating tables
The methods to rotate tables are also valid for every other floating object; [23] is
an excellent reference on this topic.
that allows to rotate every object (or box) and can also be used for tables; options
is an optional argument to modify the origin of the rotation (see [23]), angle is
the angle of the rotation (for tables is typically 90◦ ) and object is the object to be
rotated (in this case tabular).
The rotating package provides the rotate, turn, sideways, and sidewaystable
environments.6
The rotate environment. This environment does not leave vertical space for the
rotated object thus it cannot be used for tables because they would overwrite the
preceding text.
\ begin { rotate }{ angle }
...
\ end { rotate }
6. The package also provides sidewaysfigure which is the analogous of sidewaystable for
figures.
24
The turn environment. This environment, unlike rotate, adds a vertical space for
the rotated object and thus behaves like \rotatebox (see par. 3.2.1).
\ begin { turn }{ < degree >}
...
\ end { turn }
The sidewaystable environment. This environment does the same rotation sideways
does but it must be used with floating tables for which it substitutes the table
environment. The sintax is
\ begin { sidewaystable }
\ caption {...}
\ begin { tabular }
...
\ end { tabular }
\ end { sidewaystable }
produces the table at page 1; in this example, besides sidewaystable, also the
tabularx package is used to define a W type column (horizontally centered text),
25
Figure 1: Example of the sidewaystable environment.
the text is vertically centered, and the font size is reduced with the command
\small.
26
3.3 Scaling tables
The graphicx package provides two commands that can be used to scale tables:
\scalebox and \resizebox.
The command
\ scalebox {h - scale }[ v - scale ]{ argument }
scales argument (in this case a table) by h-scale horizontally and v-scale ver-
tically; for tables the same scale factor must be used horizontally and vertically
and this can be done assigning one parameter between h-scale and v-scale and
assigning ! to the other one.
The command
\ resizebox { width }{ height }{ argument }
scales argument (in this case a table) in order to have a width equal to width and a
height equal to height; in this case is also possible to use ! to maintain the same
aspect ratio of the table. Typically these commands are used to scale down large
tables in order to let them be as wide as the text; this can be done for example
with the commands
\ begin { table }[ tb ]
\ caption {...} \ label {...} \ centering
\ resizebox {\ textwidth }{!}{ %
\ begin { tabular }{...}
...
\ end { tabular }}
\ end { table }
For example tables 14(a) and 14(b) are obtained with the same code of 13 but
are scaled with \resizebox so that their width is 65% of that of the text:
\ resizebox {0.65\ textwidth }{!}{...}
27
3.4.1 The supertabular package
The supertabular package provides the homonymic supertabular environment
that behaves like the regular tabular but, when compiling, it checks the length
of the table at every row: when the length exceeds textheight the package auto-
matically inserts the optional argument tabletail and the \end{tabular} com-
mand; then it starts a new table on the next page, after the optional argument
tablehead. The tabletail argument can be used to place at the bottom of every
page in which the table is broken “continues on the next page” while tablehead
for “continues from the previous page”. The supertabular package treats the part
of the table that is in the new page independently thus the width of the column
can vary across different pages unless the user uses fixed width columns.
28
4 Tables and colors
A colored background can be very useful when trying to highlight a part of a
table. The colortbl package can be used to color the background of cells, rows
or columns of tabular environments; it can also be used to color the lines (for
example \hline) but this topic is not analyzed since scientific document should
never use colored lines. The colortbl requires the color and array packages. The
xcolor [4] package can be used instead of color but the examples reported below
use only the latter.
The first two arguments are standard commands of the color package [2], used as
in the command \color, while the last two arguments are lengths.
clrmodel declares the color model to be used; rgb, cmyk, gray, and named are
available.
color declares the color to be used; this definition depends on the color model
chosen. rgb (Red Green Blue) requires a list of three numbers between 0
and 1 and separated by commas; each of them gives the corresponding
color component (red, green, and blue). cmyk (Cyan Magenta Yellow Black)
requires a list of four numbers between 0 and 1 and separated by com-
mas each of them gives the corresponding color component (cyan, magenta,
yellow, and black). gray requires just a number between 0 and 1 of the cor-
responding gray level; in scientific tables grays are preferred to colors thus
this command is very convenient. named allows to use colors based on their
name; the name, if not already defined by default, must be defined by the
user with the command
\ definecolor { name }{ clrmodel }{ color }
where name is the color name assigned by the user, clrmodel and color have
the same meaning they have in the command \columncolor.
29
Table 22: Table with a column with colored background obtained
with the colortbl package.
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
left overhang and right overhang specifies how much the colored background
is separated from the text on the left and on the right respectively. The use
of these commands is not recommended since, if they are not equal to zero,
the colored background does not fill all the space reserved to \tabcolsep.
The \newcolumntype command (see par. 2.1) can be used to redefine new col-
umn types with the desired background color. For example the command
\ newcolumntype { K }{\ columncolor [ gray ]{0.8}\ raggedright }
Compare the result with table 7. Note that the commands provided by the ctable
package (see par. 1.4) cannot be used for the horizontal lines: these commands
leave vertical space above and below every cell and so the background color
would be interrupted; see for example table 22.
30
Table 23: Table with a column with colored background (ctable
package).
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
Sometimes the background color is not used to highlight certain rows but to
separate them (as horizontal lines do). In this case the xcolor package is very
handy because it provides commands to automatically color all the odd and even
lines of the table. The table option of the package must be loaded in the preamble
\ usepackage [ table ]{ xcolor }
and then the following command must be used right before the tabular environ-
ment
\ rowcolors { row }{ odd - row color }{ even - row color }
where
31
Table 25: Table with colored odd rows obtained with the xcolor
package.
D Pu uu β Gf
(in) (lbs) (in) (psi · in)
5 269.8 0.000674 1.79 0.04089
10 421.0 0.001035 3.59 0.04089
20 640.2 0.001565 7.18 0.04089
Table 26: Table with a colored cell obtained with the \cellcolor
command.
For example table 25 is exactly the same as table 7 but, instead of horizontal lines
(\hline), it uses gray background for odd rows. The code that is used to create
this table is
\ rowcolors {2}{ gray !35}{}
\ begin { tabular }{ ccccc }
...
\ end { tabular }
32
5 Automatic generation of tables
Often the tables that appear in LATEX documents contain data generated by other
programs, for example statistical or mathematical software. LATEX tables can be
easily and automatically generated by other programs since their syntax is very
simple. Typically the cells of a row are just separated by & and each row is ended
by \\ (see par. 1.2.1).
5.1 Stata
An interesting application of this concept has been presented for the statistical
program Stata.7 Gini [9] explains how to use Stata to automatically generate
LATEX tables with colored rows (see par. 4) and tables in which some cells are
highlighted with boldface (for example those that contains the maximum or the
minimum of a series of data).
5.2 Spreadsheets
Many freeware softwares, as Excel2LaTeX8 and Spreadsheet2LaTeX9 , that automati-
cally generates LATEX code ready to be compiled starting from a formatted10 table
in a spreadsheet. In general it is quite easy to program macros to make LATEX
code starting from a generic spreadsheet.
6 Specialities
6.1 Line styles
As said in par. 4, the colortbl package can be used to modify the color of horizontal
and vertical lines; anyway this should be avoided in scientific documents and so
the details are left to the reader.
7. Stata is a registered trade mark of StataCorp LP.
8. Excel2LaTeX is available at ftp://ftp.tex.ac.uk/tex-archive/support/excel2latex/
xl2latex.zip.
9. Spreadsheet2LaTeX is available at http://pegasus.rutgers.edu/~elflord/unix/software/
spreadsheet2latex/.
10. For example with colors, horizontal and vertical lines.
33
The arydshln package allows to use dashed lines. The commands \hdashline
and \cdashline corresponds respectively to \hline and \cline.
a) the note must follow the numeration of the other notes in the text;
b) the numeration of the notes inside tables must be different (for example with
letters).
The package footnote provides a solution for case (a). This package defines the
savenotes environment that allows to insert notes even inside a tabular environ-
ment and place them at the bottom of the page. If we want to allow the presence
of notes in all the tables of the document, we just need to add to the preamble
the command
\ makesavenoteenv { tabular }
The ctable package, on the other hand, offers a solution to case (b). The ctable
environment, provided by the homonymic package, offers the command \tmark
which places the symbol of the note (in this case the default numbering style is
with lowercase letters) and the command \tnote{...} that contains the text of
11. With respect to the same origin.
34
Table 27: Table with a note that follows the numbering of the other
notes in the text and is placed at the bottom of the page (footnote
package).
Table 28: Table with its own numbering for the notes and with
notes placed right after the table (ctable package).
the note. The notes are placed right below the table and not at the bottom of the
page. For example table 28 is obtained with the following code
\ begin { ctable } %
[ caption = ... , label = ...]{...} %
{\ tnote { With respect to the same origin .}} %
{...\\\ midrule
Moment of a force & Moment of a force with respect to
an origin is defined as the cross product of the position
vector \ tmark \ and the force .\\\ bottomrule }
\ end { ctable }
35
Table 29: Table obtained with the slashbox package.
```
``` Argument
``` 0 π/2
```
Function `
sin 0 1
cos 1 0
Acknowledgments
The author would like to thank Valeria Angeli for her assistance during the trans-
lation of this article from the italian version [19]. The author also would like to
thank editorial staff of Ars TEXnica, on which the original paper was published
in Italian, that authorized the publication of this article on The PracTEX Journal.
References
[1] C. Beccari. LATEX, Guida a un sistema di editoria elettronica. Hoepli, 1991.
36
[2] D. P. Carlisle. Packages in the ‘graphics’ bundle, 1999. ftp://tug.ctan.org/
pub/tex-archive/macros/latex/required/graphics/grfguide.pdf.
[3] L. Caucci and M. Spadaccini. Gestione di Figure e Tabelle con LATEX, 2005. http:
//www.guit.sssup.it/downloads/fig_tut.pdf.
[5] A. Diller. LATEX Line by Line: Tips and Techniques for Document Processing. John
Wiley & Sons, 1999.
[9] R. Gini. Generazione automatica di tabelle con LATEX e Stata, 2004. http://www.
guit.sssup.it/downloads/dispensa_gini.pdf.
[13] E. Gregorio. LATEX, Breve guida ai pacchetti di uso più comune, December 2005.
http://profs.sci.univr.it/~gregorio/breveguida.pdf.
[14] J. Hahn. LATEX for Everyone: A Reference Guide and Tutorial for Typesetting
Documents Using a Computer. Prentice Hall, 1993.
[15] D. J. Higham and D. F. Griffiths. Learning LATEX. Society for Industrial and
Applied Mathematics, 1997.
37
[17] H. Kopka and P. W. Daly. A Guide to LATEX – Document Preparation for Beginners
and Advanced Users. Addison-Wesley, 1995.
[18] L. Lamport. LATEX: A Document Preparation System, User’s Guide and Reference
Manual. Addison-Wesley, 1994.
[20] L. F. Mori. Scrivere la tesi di laurea con LATEX2ε . In guItmeeting 2005, Pisa,
Italy, October 2005. guIt (Italian TEX Users Group).
[21] T. Oetiker, H. Partl, I. Hyna, and E. Schlegl. Una (mica tanto) breve introduzione
a LATEX2ε , 2000. http://www.ctan.org/tex-archive/info/lshort/italian/
itlshort.pdf.
[22] M. Trettin. Elenco dei “peccati” degli utenti di LATEX2ε , 2005. http://www.tug.
org/tex-archive/info/l2tabu/italian/l2tabuit.pdf.
38