Fancy tables in LaTeX

Besides using booktabs, you can use either colortbl or xcolor (with the option [table] to color the tables.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
 \begin{document}

\begin{tabular}{*5l}    \toprule
\emph{name} & \emph{foo} &&&  \\\midrule
Models    & A  & B  & C  & D  \\ 
\rowcolor{blue!50} Model $X$ & X1 & X2 & X3 & X4\\ 
\rowcolor{green!50} Model $Y$ & Y1 & Y2 & Y3 & Y4\\\bottomrule
 \hline
\end{tabular}
\end{document}

enter image description here

Another method is somewhat automatic:

\rowcolors{<starting row>}{<first color>}{<second color>}

MWE:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
 \begin{document}

\rowcolors{3}{green!25}{yellow!50}
\begin{tabular}{ *5l }    \toprule
\emph{name} & \emph{foo} &&&  \\\midrule
Models    & A  & B  & C  & D  \\ 
Model $X$ & X1 & X2 & X3 & X4\\ 
Model $Y$ & Y1 & Y2 & Y3 & Y4\\\bottomrule
 \hline
\end{tabular}
\end{document}

enter image description here

Refer to the documentation of xcolor at texdoc.net for more details.


You shouldn't use vertical lines:

\documentclass{article}
\usepackage{booktabs}
 \begin{document}

\begin{tabular}{@{} *5l @{}}    \toprule
\emph{name} & \emph{foo} &&&  \\\midrule
Models    & A  & B  & C  & D  \\ 
 Model $X$ & X1 & X2 & X3 & X4\\ 
 Model $Y$ & Y1 & Y2 & Y3 & Y4\\\bottomrule
 \hline
\end{tabular}
\end{document}

enter image description here

it is a bit tricky if one has on the left and right no \tabcolsep which looks better to me, but want to color the lines:

\documentclass{article}
\usepackage{array,booktabs}
\newcolumntype{L}{@{}>{\kern\tabcolsep}l<{\kern\tabcolsep}}
\usepackage{colortbl}
\usepackage{xcolor}
 \begin{document}

\begin{tabular}{@{} l L L L @{} >{\kern\tabcolsep}l @{}}    \toprule
\emph{name} & \emph{foo} &&&  \\\midrule
Models    & A  & B  & C  & D  \\ 
\rowcolor{black!20}[0pt][0pt] Model $X$ & X1 & X2 & X3 & X4\\ 
\rowcolor{black!40}[0pt][0pt] Model $Y$ & Y1 & Y2 & Y3 & Y4\\\bottomrule
 \hline
\end{tabular}
\end{document}

enter image description here


Use booktabs package. The documentation discuss how to produce nicer tables.

  • Don't use vertical lines
  • Don't use unnecessary horizontal lines
  • Take out the extra space at right and left of table with @{}

You can read more in this guide

\documentclass{article}
\usepackage{booktabs}

\begin{document}
\begin{table}
\begin{tabular}{@{} l *4c @{}}
\toprule
 \multicolumn{1}{c}{Models}    & A  & B  & C  & D  \\ 
\midrule
 Model $X$ & X1 & X2 & X3 & X4 \\ 
 Model $Y$ & Y1 & Y2 & Y3 & Y4 \\bottomrule
 \end{tabular}
\end{table}
\end{document}

enter image description here

Tags:

Tables