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:

fancy tables with TikZ

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:

\documentclass{beamer}
\usetheme{default}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\setbeamertemplate{background canvas}[vertical shading]%
  [top=blue!1,bottom=blue!30]
\setbeamertemplate{navigation symbols}{}
\newcommand*\up{\textcolor{green}{%
  \ensuremath{\blacktriangle}}}
\newcommand*\down{\textcolor{red}{%
  \ensuremath{\blacktriangledown}}}
\newcommand*\const{\textcolor{darkgray}%
  {\textbf{--}}}
\begin{document}
\begin{frame}[bg=lightgray]
\textbf{\Large Linux distribution ranking,
        26th August 2009}
\begin{center}
\begin{tikzpicture}
\node (tbl) {
\begin{tabularx}{.6\textwidth}{cXrcc}
\arrayrulecolor{purple}
\textbf{Rank} & \textbf{Distribution} &
  \textbf{Hits} & \\
1 & Ubuntu\rule{0pt}{2.5ex}  &  2114 & \down \\
\midrule
2 & Fedora & 1451 & \up \\
\midrule
3 & Mint & 1297 & \const \\
\midrule
4 & OpenSUSE & 1228 & \up \\
\midrule
5 & Debian & 910 & \down \\
\midrule
6 & Mandriva & 907 & \up \\
\midrule
7 & PCLinuxOS & 764 & \up \\
\midrule
8 & Puppy & 738 & \up \\
\midrule
9 & Sabayon & 671 & \up \\
\midrule
10 & Arch & 625 & \down \\[0.5ex]
\end{tabularx}};
\begin{pgfonlayer}{background}
\draw[rounded corners,top color=red,bottom color=black,
    draw=white] ($(tbl.north west)+(0.14,0)$)
    rectangle ($(tbl.north east)-(0.13,0.9)$);
\draw[rounded corners,top color=white,bottom color=black,
    middle color=red,draw=blue!20] ($(tbl.south west)
    +(0.12,0.5)$) rectangle ($(tbl.south east)-(0.12,0)$);
\draw[top color=blue!1,bottom color=blue!20,draw=white]
    ($(tbl.north east)-(0.13,0.6)$)
    rectangle ($(tbl.south west)+(0.13,0.2)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{center}
\small
Data by DistroWatch.com, spanning over the last 6 months,
hits per day.
\end{frame}
\end{document}

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
Categories: Uncategorized | 21 comments

Comments (21)

  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. Pingback: Fancy Table with TikZ « Thai LaTeX and MATLAB

  4. 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. :-D
    For my own presentations I’m usually choosing more decent colors and I’m preferring sober beamer themes.

    Best regards,

    Stefan

  5. Tikz so hard…

  6. Pingback: tableaux tikz « Olivier Lemaire’s Blog – Tribulations d’un ingénieur

  7. 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}

  8. Hi Olivier,

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

    Stefan

  9. Pingback: Pro PageranK » Archives du Blog » tableaux tikz

  10. Pingback: Schicke Tabellen mit TikZ « Uwes kleines Technikblog

  11. 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:

    table with TikZ

    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

  12. 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

  13. Hi Kevin,

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

    Stefan

  14. 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?

  15. 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

  16. 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

  17. Pingback: univision

  18. How to change font size in above table?

  19. Hello,

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

    Thanks

  20. Amazing, thank you for publishing this!

  21. Yes i am also interested in the multicolum question

Leave a Reply to chico Cancel reply

Required fields are marked *