Fancy tables in LaTeX

I am terrible at LaTeX tables. I managed to produce a table, but it looks so basic. How do I make it fancier?

Here is the table's code:

\begin{table}
\begin{tabular}{|l|l|l|l|l|}
\hline
Models    & A  & B  & C  & D  \\ \hline
Model $X$ & X1 & X2 & X3 & X4 \\ \hline
Model $Y$ & Y1 & Y2 & Y3 & Y4 \\
\hline
\end{tabular}
\end{table}

-
Wellcome to TeX SX! Please provide a Minimal Working Example (MWE). It is easier for people to run your document and help you. – TeXtnik Jan 16 '13 at 8:51
I never try to make anything look fancy. I hope to always listen carefully to the information that I want to get across, and let it tell me how it needs to be presented. The only thing I don't like about your table are the vertical bars and horizontal bars, which don't really help. Use spacing instead of bars perhaps, like  \[3pt  instead of  \\  on the first line, and get rid of  \hline  and  | . – Benjamin McKay Jan 22 '13 at 10:21

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}


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}


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

-
Thank you @Harish. It works in a separate document, but does not work in my own document even though I added the packages. It says "! LaTeX Error: Option clash for package xcolor". Any idea why? – Günal Jan 16 '13 at 9:41
@Günal To prevent that add \PassOptionsToPackage{table}{xcolor} before \documentclass{article} and remove \usepackage[table]{xcolor}. This happens because some package you are using (may be tikz?) also loads xcolor but with different or no options. refer to this answer: tex.stackexchange.com/a/52978/11232 for some more details. – Harish Kumar Jan 16 '13 at 9:45
Perfectly works. Cheers – Günal Jan 16 '13 at 12:34

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}


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}


-
Thank you @Herbert. It looks brilliant. Can you please also tell me how to paint the rows? I want the colors of the rows Model X and Model Y to be different. – Günal Jan 16 '13 at 8:54
see my edited answer – Herbert Jan 16 '13 at 9:55

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}


-
my table ends up before my text with your solution – David 天宇 Wong Mar 10 '15 at 2:10
@David天宇Wong: Can you elaborate more the comment? This answer has nothing to do with the position of the table in the text. – TeXtnik Mar 10 '15 at 8:05
I removed \begin{table} and no problems – David 天宇 Wong Mar 11 '15 at 12:45
@David天宇Wong, table makes the tabular to float, so LaTeX place it where it thinks is better to pagniate the text. – TeXtnik Mar 12 '15 at 8:00

+1 for the other answers- you can get some fancy input by using the pgfplotstable package

You can colour the rows if you like- see the documentation for more details.

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}

% global settings
\pgfplotstableset{
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
col sep = &,
row sep=\\,
string type,
}

\begin{document}

\begin{table}
\pgfplotstabletypeset{
Models    & A  & B  & C  & D  \\
Model $X$ & X1 & X2 & X3 & X4 \\
Model $Y$ & Y1 & Y2 & Y3 & Y4 \\
Model $Z$ & Z1 & Z2 & Z3 & Z4 \\
}
\end{table}

\end{document}

-