Learn how to create and customize tables in Beamer. This includes content alignment, spacing, highlight cells with colors, resize tables and much more!
Contents :
1. Insert a table in Beamer
The tabular environment takes a mandatory argument that specifies the alignment for text in the different columns:
l
for left-aligned text,c
for centered text,r
for right-aligned text.
There should be the same number of alignment specifiers as columns.
Knowing this and using the \hline
command to produce horizontal lines, we can already build a frame with a simple table; the following example shows how to do so:
% Beamer table \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \begin{document} \begin{frame}{My first table} \begin{tabular}{|c||l||r|} \hline centered & left-aligned & right-aligned \\ \hline A & C & E\\ \hline B & D & F\\ \hline \end{tabular} \end{frame} \end{document}
Compiling this code yields:
Let’s now explore some more advanced functionalities of this tabular environment.
2. Horizontal lines and multicolumns in Beamer tables
The command \multicolumn{n}{pos}{item}
can be used to create items that expand for multiple columns where:
n
stands for the number of columns to be spanned,pos
specifies the horizontal positioning of the item (using the same specifiers as
in the mandatory argument of the environmentl
,c
, andr
)item
is the content itself to be printed.
Here we put into practice the previous commands:
% Draw Horizontal lines in tables \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \begin{document} \begin{frame}{A somewhat more complex table} \begin{tabular}{|c||ccc|} \hline \hline Small col & \multicolumn{3}{|c|}{Big col} \\ \hline Grouped items & \multicolumn{3}{|c|}{Item 1} \\ \cline{2-4} & \multicolumn{3}{|c|}{Item 2} \\ \hline Usual row & Spam & Bacon & Eggs \\ \hline\hline \end{tabular} \end{frame} \end{document}
When the environment argument has | characters, it’s not obvious which of them get replaced by a \multicolumn
’s positioning argument. In this case, the rule that LaTeX follows is: the part of the environment argument corresponding to any column other than the first, begins with an l
, r
or c
character.
This means that the argument of the previous example |c||ccc|
is split into |c||
, c
, c
, and c|
. For this reason, the |c|
specifier inside the \multicolumn environment can be changed for c|
and the result will be the same.
3. Table environment
The tabular environment produces a box, which is the fundamental object that the underlying system TeX uses to build the document. This means that a table produced by this environment is no different that a letter typed, which makes possible to print tables in a middle of a paragraph, sentence or word. But that would look a bit strange.
Usually the tabular environment is inserted inside the table environment, which makes it a floating object. This means that you can add positioning specifiers to the floating object, a caption and a label to reference it.
This is how our previous table would look in a more realistic use of the environment:
% Draw Horizontal lines in tables \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \begin{document} \begin{frame}{A realistic table} You can see how the table I have made looks in Table \ref{tab:table} \begin{table} \caption{A realistic table I have built} \label{tab:table} \begin{tabular}{|c||ccc|} \hline \hline Small col & \multicolumn{3}{|c|}{Big col} \\ \hline Grouped items & \multicolumn{3}{|c|}{Item 1} \\ \cline{2-4} & \multicolumn{3}{|c|}{Item 2} \\ \hline Usual row & Spam & Bacon & Eggs \\ \hline\hline \end{tabular} \end{table} \end{frame} \end{document}
4. Set the table size
We have already seen how to create a table in beamer using the tabular environment. When doing so, it was mentioned that technically, for TeX, the output of the tabular environment is the same as a letter; for the system, it is just a box.
And as all boxes in TeX, it can be resized using the command \resizebox
. Here is an example of this command in action to fit inside a single frame a very big table:
% Large table resized in Beamer table \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \begin{document} \begin{frame}{A very large table (resized)} \resizebox{10cm}{!} { \begin{tabular}{ccccccccccccc} \hline This & is & a & very & large & table & that & exceeds & the & size & of & the & frame\\ \hline a & b & c & d & e & f & g & h & i & j & k & l & m\\ \hline \end{tabular} } \end{frame} \end{document}
which yields the following result:
Here is the non resized version:
Observe that the command takes three mandatory arguments:
- the horizontal size of the new box,
- the vertical size of the new box
- the box to be resized.
When one of the two size arguments contains the symbol ! (as in the example above) the value of this argument is selected by LaTeX so that the aspect ratio of the box is preserved. It is highly recommended to use it unless you know what you are doing; in other case you may get some ugly results.
5. Change font size for tables
When resizing the box generated by a tabular environment using \resizebox
, the font size is automatically scaled to fit the size needed. However, you may want to set a fixed font size for the table, to make it larger or smaller, using one of the predefined LaTeX sizes: \tiny
, \scriptsize
, \footnotesize
, \small
, \normalsize
, \large
, \Large
, \LARGE
, \huge
and \Huge
.
To do so, you only have to put the tabular environment inside a block containing the font size declaration that you want. For example, here I fit the previous table inside the frame using this new approach:
% Large table resized in Beamer table \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \begin{document} \begin{frame}{A very large table (tiny font)} {\tiny \begin{tabular}{ccccccccccccc} \hline This & is & a & very & large & table & that & exceeds & the & size & of & the & frame\\ \hline a & b & c & d & e & f & g & h & i & j & k & l & m\\ \hline \end{tabular} } \end{frame} \end{document}
and the result in this case is shown below:
6. Spacing in tables
a. Vertical spacing
b. Horizontal spacing
If we redefine this command and dimension in the document preamble, they will be changed throughout all the document. However, if we do it inside an environment or a {}
group, they will only affect the scope of the environment or group. This is useful when we want to use the default settings for our document, but we wish to fine tune a given table that looks odd.
c. Illustrative example
In the following example, we use the previous commands to increase the padding in our table:
% Spacing in Beamer tables \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} % Change horizontal spacing \setlength{\tabcolsep}{20pt} % Change vertical spacing \renewcommand{\arraystretch}{1.5} \begin{document} \begin{frame}{A really padded table} \centering \begin{tabular}{lccc} Name & Exam 1 & Exam 2 & Global \\ \hline\hline Alice & 8.0 & 9.0 & 8.5 \\ Bob & 9.0 & 9.8 & 9.4 \\ \end{tabular} \end{frame} \end{document}
7. How to use colors to highlight tables in Beamer
A lot of color functionality is already integrated inside beamer, without the necessity of explicitly loading external packages such as xcolor. The reason for this is rather obvious: colors play a very important role in any beamer presentation; a black and white presentation is not exactly what we are looking for when using beamer.
First, let’s see an example of this package in action, and then I will explain in more detail the main commands it provides :
% Colored columns and rows in Beamer table \documentclass{beamer} % Theme choice \usetheme{CambridgeUS} \usepackage{colortbl} % define new colors \definecolor{LightRed}{RGB}{252,160,140} \definecolor{LightBlue}{RGB}{140,186,252} % use the colortbl package to define a new column \newcolumntype{a}{>{\columncolor{LightRed}}c} \begin{document} \begin{frame}{Colored tables} \centering \begin{tabular}{lrara} \hline \rowcolor{LightBlue} The variables & \multicolumn{4}{c}{The data}\\ \hline var 1 & 1 & 2 & 3 & 4 \\ \hline var 2 & 1 & 2 & 3 & 4 \\ \hline var 3 & 1 & 2 & 3 & 4 \\ \hline \end{tabular} \end{frame} \end{document}
The result of this code can be seen below:
Let’s jump into the details of the code:
- First of all, we defined some colors as it is usually done with the xcolor package; nevertheless, we didn’t load it, since beamer does it for us.
- In this case, we defined the colors using a RGB tuple, but there are other ways available (HTML, greyscale, etc).
- Then we defined a new kind of column, that is, a new kind of column specifier for the tabular environment. This definition, in spite of its complex notation, is very simple: we just specify the color of the column inside
\columncolor
and then we select its alignment with the commonl
,r
andc
specifiers. Then this specifier is used for the columns that we want to color. - Coloring rows is even simpler: we just have to use the command
\rowcolor
inside the row we want to color. As you can see, the color of the row has preference over the column color.
8. Tables inside blocks
We can insert without any trouble a tabular environment inside a block. In the following example we use the Warsaw theme to have styled blocks and we print a table inside an alertblock
:
% Beamer table inside a block \documentclass{beamer} % Theme choice \usetheme{Warsaw} \begin{document} \begin{frame}{Tables in Beamer Blocks} \begin{alertblock}{Table inside an alert block} A very important table. \centering \begin{tabular}{|c|c|} \hline \multicolumn{2}{|c|}{Meals} \\ \hline Breakfast & Spam \\ \cline{2-2} & Eggs \\ \hline \end{tabular} \end{alertblock} \end{frame} \end{document}
which produces the following image:
Summary
In this lesson, we learned how to create tables in beamer using the tabular environment. We highlighted different alignment and spacing options and how one can highlight cells with different colors.
Next Lesson: 15 Figures in Beamer – A detailed tutorial