TeXblog

 Typography with TeX and LaTeX

Fancy tables with TikZ

August 26th, 2009 by Stefan Kottwitz

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.

This entry was posted on Mittwoch, August 26th, 2009 at 21:08 and is filed under pgf/TikZ, Figures and Tables, Graphics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

20 responses about “Fancy tables with TikZ”

  1. Martin said:

    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. Maratonda said:

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

    […] blog TeXblog: Fancy tables with TikZSome TeX Developments: TeX Live on WindowsSome TeX Developments: LaTeX and Dalton TransactionsSome […]

  4. Stefan Kottwitz said:

    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. Barbero said:

    Tikz so hard…

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

    […] un commentaire » Fancy Table Version *.pdf ici. L’idée de ce tableau vient d’ici. J’ai simplement fait une fonction qui permet de l’utiliser comme un tableau Latex […]

  7. Olivier said:

    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.5\baselineskip)$);
    % 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}}}%
    {\scshape\textcolor{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. Stefan Kottwitz said:

    Hi Olivier,

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

    Stefan

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

    […] Idée originale… […]

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

    […] Hier ein schönes Beispiel, wie man sehr bunte Tabellen mit LaTeX und TikZ erstellen kann: http://texblog.net/latex-archive/graphics/tikz-table/ […]

  11. Stefan Kottwitz said:

    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. Kevin Donnelly said:

    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. Stefan Kottwitz said:

    Hi Kevin,

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

    Stefan

  14. Flux said:

    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. pacomet said:

    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. Stefan Kottwitz said:

    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. univision said:

    univision…

    […]Fancy tables with TikZ - TeXblog - Typography with TeX and LaTeX[…]…

  18. Manish said:

    How to change font size in above table?

  19. Saki said:

    Hello,

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

    Thanks

  20. chico said:

    Yes i am also interested in the multicolum question

Leave a Reply