Fancy tables with TikZ

Today I wanted to create a table for a presentation and I remembered a suggestion made by Kjell Magne Fauske in comp.text.tex in August 2008, to use TikZ to create tables with rounded corners. I’ve investigated this idea further to achieve a colorful table layout in a shape that’s unusual for LaTeX. Here’s the result, displaying a ranking of Linux distributions produced by DistroWatch.com:

Click on the image to see it bigger, for a complete view open the pdf presentation.

The table has been put into a TikZ node, both at the top and at the bottom of the table I’ve used rectangles with rounded corners and some shading. A third rectangle is used to overlay parts of the other two rectangles.

The complete source code:


It’s just a draft, some lengths have been adjusted to fit in order to demonstrate using TikZ with tables in a quick way.

26. August 2009 by stefan

1. Hi,

very nice for use in presentations. especially the use of gradient colours fillings make it look like it jumped right of some financial magazine

2. This is a very interesting piece of code, well at least for me…

Aside from the LaTeX code, I would have liked a little more color contrast between the background and the foreground.

Also,the table header is red-to-black gradient, but the text being black as well makes it less readable.

Regards

3. Hi Maratonda,

Like you I’m not satisfied with contrast and color, but decided to finish the post without making more adjustments because that experiment was already sufficient for me.
For my own presentations I’m usually choosing more decent colors and I’m preferring sober beamer themes.

Best regards,

Stefan

4. Tikz so hard…

5. I think that would be interesting to do a new environment out of it. What do you think ?
Would you mind if I show the following example on my blog here: http://olivierlemaire.wordpress.com/2010/03/08/tableaux-tikz/?

%% the new environment

newsavebox{dataTableContent} % Box
newenvironment{dataTable}[1] % new environment
{%
begin{lrbox}{dataTableContent}%
begin{tabular}{#1}}%
%
{%
end{tabular}
end{lrbox}
begin{tikzpicture}
node [inner xsep=0pt] (tbl){usebox{dataTableContent}};
begin{pgfonlayer}{background}
% table
draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black]
(tbl.north east) rectangle (tbl.south west);
% top line
draw[rounded corners=1pt,top color=gray!10!black,bottom color=gray!50!black,draw=black]%
($(tbl.north west)$) rectangle ($(tbl.north east)-(0,1.5baselineskip)$);
% bottom rule
draw[rounded corners=0.25pt,fill=gray,draw=black]%
(tbl.south west) rectangle ($(tbl.south east)+(0,0.05)$);
end{pgfonlayer}
end{tikzpicture}}
%
%

%% Use of the environment

begin{table}
begin{center}
begin{dataTable}%
{@{hspace{2ex}} c @{hspace{6ex}} l @{hspace{6ex}} c @{hspace{2ex}}}%
{scshapetextcolor{white}{Rank}}
{Distribution}}
{textsc{h.p.d.}}}\ midrule[0pt]
1 & Ubuntu & 2231 \ midrule
2 & Fedora & 1617 \ midrule
3 & Mint & 1438 \ midrule
4 & openSUSE & 1325 \ midrule
5 & Mandriva & 1054 \ midrule
6 & Debian & 978 \ midrule
7 & Puppy & 792 \ midrule
8 & Sabayon & 782 \ midrule
9 & PCLinuxOS & 779 \ midrule
10 & Arch & 729
end{dataTable}
end{center}
caption{Source url{http://distrowatch.com/index.php}}
end{table}

6. Hi Olivier,

that’s a good idea, thank you for this contribution!

Stefan

7. Since I’ve noticed Uwes link, here’s another approach, using the tikz matrix feature. Though it’s a quick draft, I want to separate the formatting from the content, so the table in the document looks like

\begin{matrixtable}{1.2cm}{2.4cm}{1.2cm}{0.6cm}{ \head{Rank} & \head{Distribution} & \head{Hits} & \\ 1 & Ubuntu & 2114 & \down \\ 2 & Fedora & 1451 & \up \\ ... 10 & Arch & 625 & \down \\ } \end{matrixtable}

The output is:

My definition of the table environment was

\newenvironment{matrixtable}[4]{% \begin{tikzpicture}[matrix of nodes/.style={ execute at begin cell=\node\bgroup\strut, execute at end cell=\egroup;}] \matrix (m) [matrix of nodes,top color=blue!20, bottom color=blue!80,draw=white, nodes={draw,top color=blue!10,bottom color=blue!35, draw,inner sep=2pt,minimum height=3.1ex}, column sep=1ex,row sep=0.6ex,inner sep=2ex, rounded corners,column 1/.style={minimum width=#1}, column 2/.style={minimum width=#2}, column 3/.style={minimum width=#3}, column 4/.style={minimum width=#4}]}% {;\end{tikzpicture}}

The benefit of the separate definition is that if you use several tables in a presentation and you would like to modify the appearance, you just need to change one definition to affect the whole document.
The complete example code can be seen here.

Stefan

8. Hi Stefan
The link to the example code doesn’t seem to be working – I just get the blog’s page-furniture. Is there an alternative location? Thanks.
Kevin

9. Hi Kevin,

I’ve edited the link, hope you can see it now.

Stefan

10. Hi!

I love your example, thank you very much for this. I have one question however: I would like to use other colours like LightBlue (from the xcolors package), but this doesn’t work. Why?

11. Hi

A very nice and useful table. I still have a question. Is it possible to put a two lines heading.

I’ve tried to use parbox inside the cell and that gives two lines but then text exceeds the size (height) of the heading line.

pbox{3cm}{first line \ second line}

Any idea? thanks

12. You could raise the vertical size of the rectangles, I just chose values. For example:

\textbf{Rank} & \parbox{3cm}{\bfseries first line \\ second line} & \textbf{Hits} & \\ ... \draw[rounded corners,top color=red,bottom color=black, draw=white] ($(tbl.north west)+(0.14,0)$) rectangle ($(tbl.north east)-(0.13,1.5)$); ... \draw[top color=blue!1,bottom color=blue!20,draw=white] ($(tbl.north east)-(0.13,1)$) rectangle ($(tbl.south west)+(0.13,0.2)$); ...

I raised 0.9 to 1.5 and 0.6 to 1.

Stefan

13. Pingback: univision

14. How to change font size in above table?

15. Hello,

Is it possible to support multi-columns in the matrixtable environment?

Thanks

16. Amazing, thank you for publishing this!

17. Yes i am also interested in the multicolum question