Chapter 6 – Designing Tables

Here are the Code examples of of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I sometimes use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

% Example of a bad table - too many lines reduce legibility
\documentclass{article}
\pagestyle{empty}
\begin{document}
\begin{tabular}{|c|c|c|c|}
  \hline
  $\ast$ & 1 & 2 & 3 \\\hline
     1   & 1 & 2 & 3 \\\hline
     2   & 2 & 4 & 6 \\\hline
     3   & 3 & 6 & 9 \\\hline
  \end{tabular}
\end{document}

\documentclass{article}
\usepackage{booktabs}
\usepackage{bbding}
\pagestyle{empty}
\begin{document}
\begin{table}
  \centering
  \renewcommand{\arraystretch}{1.6}
  \begin{tabular}{lccccc}
    \toprule
    Class   & Part page  & Chapters   & Abstract   &
                    Front-/Backmatter & Appendix name \\
    \cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}
    \cmidrule(lr){4-4}\cmidrule(lr){5-5}\cmidrule(l){6-6}
    article &            &            & \Checkmark &  \\
    book    & \Checkmark & \Checkmark &            &
              \Checkmark & \Checkmark                 \\
    report  & \Checkmark & \Checkmark & \Checkmark &
                         & \Checkmark                 \\
    \bottomrule
  \end{tabular}
  \caption{Structuring differences between standard
           \LaTeX\ classes}
  \label{comparison}
\end{table}
\end{document}

% Adding footnotes to a table
\documentclass{article}
\usepackage{booktabs}
\usepackage{bbding}
\usepackage{threeparttable}
\begin{document}
\begin{table}
  \centering
  \renewcommand{\arraystretch}{1.6}
  \begin{threeparttable}
    \begin{tabular}{lccccc}
      \toprule
      Class   & Part page  & Chapters   & Abstract\tnote{1} &
      Front-/Backmatter\tnote{2}  & Appendix name\tnote{3} \\
      \cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(lr){3-3}
      \cmidrule(lr){4-4}\cmidrule(lr){5-5}\cmidrule(l){6-6}
      article &            &            & \Checkmark &  \\
      book    & \Checkmark & \Checkmark &            &
                \Checkmark & \Checkmark                 \\
      report  & \Checkmark & \Checkmark & \Checkmark &
                           & \Checkmark                 \\
      \bottomrule
    \end{tabular}
    \begin{tablenotes}
      \item[1] An environment: \verb|\begin{abstract}| \ldots
               \verb|\end{abstract}|
      \item[2] Commands: \verb|\frontmatter|, \verb|\mainmatter|,
        \verb|\backmatter|
      \item[3] The command \verb|\appendix| exists in article
               too, but there's no prefix ``Appendix''.
    \end{tablenotes}
  \end{threeparttable}
  \caption{Structuring differences between standard
           \LaTeX\ classes}
  \label{comparison}
\end{table}
\end{document}

% Aligning numeric data
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{chemformula}
%\sisetup{table-figures-decimal  = 5,
%         table-figures-integer  = 2,
%         table-number-alignment = right}
%\sisetup{table-format = 2.5,
%         table-number-alignment = right}
\begin{document}
  \begin{tabular}{lSS}
    \toprule
              & {atomic mass} & {total mass} \\
    \midrule
    \ch{C}    &    12.011     & 12.011       \\
    \ch{H}    &     1.00794   &  6.04764     \\
    \ch{C2H6} &               & 30.06964     \\
    \bottomrule
  \end{tabular}
\end{document}

% Coloring a table
\documentclass{article}
\usepackage[table]{xcolor}
\rowcolors{2}{gray!15}{white}
\newcommand{\head}[1]{%
  \textcolor{white}{\textbf{#1}}}
\renewcommand{\arraystretch}{1.5}
\pagestyle{empty}
\begin{document}
\begin{table}[ht]
  \centering
  \sffamily
  \begin{tabular}{rlr}
    \rowcolor{black!75}
     & \head{Distribution} & \head{Hits} \\
    1 & Mint       & 2364 \\
    2 & Ubuntu     & 1838 \\
    3 & Debian     & 1582 \\
    4 & openSUSE   & 1334 \\
    5 & Fedora     & 1262 \\
    6 & Mageia     & 1219 \\
    7 & CentOS     & 1171 \\
    8 & Arch       & 1040 \\
    9 & elementary &  899 \\
   10 & Zorin      &  851 \\
 \end{tabular}
\end{table}
\end{document}

% Merging cells
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{metalogo}
\renewcommand{\arraystretch}{1.6}
\begin{document}
\begin{tabular}{@{}p{1.5cm}p{1.6cm}>{\raggedleft}p{1cm}
                >{\raggedright}p{1.6cm}r@{}}
  Compiler & \multicolumn{2}{c}{Input}
           & \multicolumn{2}{c}{Output} \\
   \cmidrule(r){1-1}\cmidrule(lr){2-3}\cmidrule(l){4-5}
           & Encoding & Images  & Fonts & Format \\
   \cmidrule(lr){2-2}\cmidrule(lr){3-3}
   \cmidrule(lr){4-4}\cmidrule(l){5-5}
   \LaTeX    & utf8, ascii, applemac, latin1, \ldots
             & EPS         & Type 1, Type 3 & DVI \\
   pdf\LaTeX & utf8, ascii, applemac, latin1, \ldots
             & PDF PNG JPG & Type 1, Type 3 & PDF \\
  \XeLaTeX, \LuaLaTeX & utf8
             & PDF PNG JPG & Type 1, Type 3,
             OpenType, Graphite, TrueType & PDF \\
\end{tabular}
\end{document}

% Merging cells, merging rows
\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{tabular}{cc}
  Bundle & Main classes \\
  \cmidrule(lr){1-1}\cmidrule(lr){2-2}
  \addlinespace
  \multirow{4}{*}{\LaTeX\ base} & article  \\
       & book     \\
       & report   \\
       & letter   \\
  \addlinespace
  \multirow{4}{*}{KOMA-Script}  & scrartcl \\
       & scrbook  \\
       & scrreprt \\
       & scrlttr2 \\
\end{tabular}
\end{document}

% Splitting a cell diagonally
\documentclass{article}
\usepackage{slashbox}
\begin{document}
\renewcommand{\arraystretch}{1.8}
\begin{tabular}{|l|c|c|c|c|c|}
  \hline
  \slashbox{Time}{Weekday} & Monday    & Tuesday
                           & Wednesday & Thursday & Friday \\
  \hline
  8--10  & & & & & \\
  10--12 & & & & & \\
  12--14 & & & & & \\
  14--16 & & & & & \\
  \hline
\end{tabular}
\end{document}

% Adding shape, shading, and transparency
\documentclass[table]{beamer}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{background canvas}[vertical shading]%
  [top=blue!1,bottom=blue!40]
\usepackage{booktabs}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\rowcolors{2}{gray!25}{white}
\newcommand{\up}{\textcolor{green}{$\blacktriangle$}}
\newcommand{\down}{\textcolor{red}{$\blacktriangledown$}}
\newcommand{\same}{\textcolor{darkgray}{\textbf{--}}}
\newcommand{\heading}[1]{{\Large\bfseries #1\par\medskip}}
\arrayrulecolor{blue}
\begin{document}
\begin{frame}
  \centering
  \heading{Linux distribution ranking, December 14, 2014}
  \begin{tikzpicture}
    \node (table) {
      \begin{tabular}{rlrcc}
        & \textbf{Distribution} & \textbf{Hits} & \\
        \addlinespace[2pt]
        1 &  Mint &  2364 & \down \\
        \midrule
        2 & Ubuntu  & 1838 & \up \\
        \midrule
        3 & Debian & 1582 & \same \\
        \midrule
        4 & openSUSE & 1334 & \up \\
        \midrule
        5 &  Fedora& 1262 & \up \\
        \midrule
        6 & Mageia & 1219 & \down \\
        \midrule
        7 & CentOS & 1171 & \same \\
        \midrule
        8 & Arch & 1040 & \same \\
        \midrule
        9 & elementary & 899 & \same \\
        \midrule
        10 & Zorin & 851 & \down \\[0.5ex]
      \end{tabular}};
    \begin{pgfonlayer}{background}
      \draw[rounded corners, top color=blue!20,
        bottom color=blue!80!black, middle color=blue!80,
        opacity=0.5, draw=white] ($(table.north west)+(0.14,0)$)
        rectangle ($(table.north east)-(0.13,0.9)$);
      \draw[top color=blue!1,bottom color=blue!30,draw=white]
        ($(table.north east)-(0.13,0.6)$)
        rectangle ($(table.south west)+(0.13,0.2)$);
    \end{pgfonlayer}
  \end{tikzpicture}

  \small
  Data by DistroWatch.com, spanning over the last 6 months,
  hits per day.
\end{frame}
\end{document}

% Adding shape, shading, and transparency
% With automatic row counting
\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{background canvas}[vertical shading]%
  [top=blue!1,bottom=blue!40]
\usepackage{booktabs}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\newcommand{\up}{\textcolor{green}{$\blacktriangle$}}
\newcommand{\down}{\textcolor{red}{$\blacktriangledown$}}
\newcommand{\same}{\textcolor{darkgray}{\textbf{--}}}
\newcommand{\header}[1]{{\Large\bfseries #1\par\medskip}}
\usepackage{array}
\newcounter{rank}
\setcounter{rank}{0}
\newcommand{\steprank}{\stepcounter{rank}\makebox[2em]{\therank}\hspace{\tabcolsep}}
\begin{document}
\begin{frame}
  \centering
  \header{Linux distribution ranking, December 14, 2014}
  \begin{tikzpicture}
    \node (tbl) {
      \begin{tabular}{@{\steprank}lrcc}
        \multicolumn{1}{l}{\hspace{2em}\textbf{Distribution}} & \textbf{Hits} & \\
        \addlinespace[2pt]
        Mint       & 2364 & \down \\
        \midrule
        Ubuntu     & 1838 & \up \\
        \midrule
        Debian     & 1582 & \same \\
        \midrule
        openSUSE   & 1334 & \up \\
        \midrule
        Fedora     & 1262 & \up \\
        \midrule
        Mageia     & 1219 & \down \\
        \midrule
        CentOS     & 1171 & \same \\
        \midrule
        Arch       & 1040 & \same \\
        \midrule
        elementary &  899 & \same \\
        \midrule
        Zorin      &  851 & \down \\[0.5ex]
      \end{tabular}};
    \begin{pgfonlayer}{background}
      \draw[rounded corners, top color=blue!20,
        bottom color=blue!80!black, middle color=blue!80,
        opacity=0.5, draw=white] ($(tbl.north west)+(0.14,0)$)
        rectangle ($(tbl.north east)-(0.13,0.9)$);
      \draw[top color=blue!1,bottom color=blue!30,draw=white]
        ($(tbl.north east)-(0.13,0.6)$)
        rectangle ($(tbl.south west)+(0.13,0.2)$);
    \end{pgfonlayer}
  \end{tikzpicture}

  \small
  Data by DistroWatch.com, spanning over the last 6 months,
  hits per day.
\end{frame}
\end{document}

% Adding shape, shading, and transparency
% Styling with TikZ matrix
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\setbeamertemplate{background canvas}[vertical shading]%
  [top=blue!1,bottom=blue!40]
\setbeamertemplate{navigation symbols}{}
\newcommand{\up}{\textcolor{green!75!black}{$\blacktriangle$}}
\newcommand{\down}{\textcolor{red}{$\blacktriangledown$}}
\newcommand{\same}{\textcolor{darkgray}{\textbf{--}}}
\newcounter{rank}
\setcounter{rank}{0}
\newcommand{\steprank}{\stepcounter{rank}\therank}
\newenvironment{matrixtable}[4]{%
  \begin{tikzpicture}
  \matrix (m) [matrix of nodes, nodes in empty cells,
    nodes={draw, top color=blue!10, bottom color=blue!35,
      inner sep=2pt, minimum height=3.5ex, anchor=center},
    top color=blue!20, bottom color=blue!80, draw=white,
    column sep=1ex, row sep=0.6ex, inner sep=2ex,
    rounded corners,
    row 1/.style    = {font=\bfseries},
    column 1/.style = {minimum width=#1, font=\steprank},
    column 2/.style = {minimum width=#2},
    column 3/.style = {minimum width=#3},
    column 4/.style = {minimum width=#4}]}%
  {;\end{tikzpicture}}
\begin{document}
\begin{frame}[fragile]
  \begin{center}
    \begin{matrixtable}{1.2cm}{2.4cm}{1.2cm}{0.6cm}{
      Rank & Distribution & Hits & ?      \\
           & Mint         & 2364 & \down  \\
           & Ubuntu       & 1838 & \up    \\
           & Debian       & 1582 & \same  \\
           & openSUSE     & 1334 & \up    \\
           & Fedora       & 1262 & \up    \\
           & Mageia       & 1219 & \down  \\
           & CentOS       & 1171 & \same  \\
           & Arch         & 1040 & \same  \\
           & elementary   &  899 & \same  \\
           & Zorin        &  851 & \down  \\}
    \end{matrixtable}
  \end{center}
\end{frame}
\end{document}

% Reading in data from files 
\documentclass{article}
\usepackage{booktabs}
\usepackage{datatool}
\DTLloaddb{Linux}{linux.csv}
\DTLsort[Distribution]{Hits=descending}{Linux}
\pagestyle{empty}
\begin{document}
\begin{tabular}{rlr}
  & Distribution & Hits \\
  \cmidrule(lr){2-2}\cmidrule(lr){3-3}
  \DTLforeach{Linux}{%
  \distribution=Distribution,\hits=Hits}{%
    \theDTLrowi & \distribution & \hits \\}
\end{tabular}
\end{document}

Go to next chapter.