35

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}
2
  • 2
    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, 2013 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 ` | `. Jan 22, 2013 at 10:21

5 Answers 5

27

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.

3
  • 1
    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, 2013 at 9:41
  • 2
    @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.
    – user11232
    Jan 16, 2013 at 9:45
  • Perfectly works. Cheers
    – Günal
    Jan 16, 2013 at 12:34
20

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

2
  • 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, 2013 at 8:54
  • see my edited answer
    – user2478
    Jan 16, 2013 at 9:55
16

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

4
  • my table ends up before my text with your solution Mar 10, 2015 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, 2015 at 8:05
  • I removed \begin{table} and no problems Mar 11, 2015 at 12:45
  • 1
    @David天宇Wong, table makes the tabular to float, so LaTeX place it where it thinks is better to pagniate the text.
    – TeXtnik
    Mar 12, 2015 at 8:00
9

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

screenshot

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

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

% global settings
\pgfplotstableset{
    every head row/.style={
    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}
3

Just for fun, a proposal using the excellent tabularray package.

\documentclass{article}
\usepackage{xcolor}
\pagestyle{empty}
\usepackage{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}
  \caption{Example of table with \texttt{tabularray}.}
  \label{tab:example}
  \centering
  \begin{tblr}{
      colspec={lllll},
      row{1}={font=\bfseries},
      column{1}={font=\itshape},
      row{even}={bg=gray!10},
    }
              & A  & B  & C  & D  \\
    \toprule
    Model $X$ & X1 & X2 & X3 & X4 \\
    Model $Y$ & Y1 & Y2 & Y3 & Y4 \\
    Model $Z$ & Z1 & Z2 & Z3 & Z4 \\
    \bottomrule
  \end{tblr}
\end{table}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .