9

How can I create a table like this in Latex?

enter image description here

I have tried but it didn't work. I need also that it is enumerated so that it appears in the List of Tables. It doesn't matter if it is not coloured, as long as the first two rows are bold.

\begin{center}
\begin{tabular}{ |c|c|c|c|c| } 
\hline
 \multicolumn{5}{|c|}{Number of cells per type} \\
 \hline

 a& b& c& d& e\\ 
  \hline
 44 & 39 & 7 & 32 &22 \\ 

 \hline
\end{tabular}
\end{center}
  • Welcome to TeX.SE. First of all, please try to ask one question per thread. And, please show us what you have tried so far. This is not a do-it-for-me site. – Raaja Mar 5 at 21:34
  • \begin{center} \begin{tabular}{ |c|c|c|c|c| } \hline \multicolumn{5}{|c|}{Number of cells per type} \\ \hline a& b& c& d& e\\ \hline 44 & 39 & 7 & 32 &22 \\ \hline \end{tabular} \end{center} – Federica Benassi Mar 5 at 21:35
  • 1
    Please add it in your question and kindly make it compilable. – Raaja Mar 5 at 21:36
  • 4
    I have added it – Federica Benassi Mar 5 at 21:40
  • Do you need the first two columns to be narrower than the next three, as appears to be the case in the screenshot you posted? Please advise. – Mico Mar 5 at 22:25
11

I suggest you to use booktabs for professional tables.

\documentclass[11pt,openright]{book}
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\renewcommand{\arraystretch}{1.2}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{caption}

\begin{document}
\listoftables

\chapter{My chapter}
\begin{table}[htb]\centering
\caption{Your table\label{tab:yourtab}}
\begin{tabularx}{.5\linewidth}{ |C|C|C|C|C| } 
\hline
 \multicolumn{5}{|c|}{\bfseries Number of cells per type} \\
 \hline
 \bfseries a& \bfseries b& \bfseries c& \bfseries d& \bfseries e\\ 
  \hline
 44 & 39 & 7 & 32 &22 \\ 
 \hline
\end{tabularx}
\end{table}
\begin{table}[htb]\centering
\caption{My suggestion\label{tab:mytab}}
\begin{tabularx}{.5\linewidth}{*5C} 
\toprule
 \multicolumn{5}{c}{\bfseries Number of cells per type} \\
 \midrule
 \bfseries a& \bfseries b& \bfseries c& \bfseries d& \bfseries e\\ 
  \midrule
 44 & 39 & 7 & 32 &22 \\ 
 \bottomrule
\end{tabularx}
\end{table}
\end{document}

enter image description here

enter image description here

  • 1
    Thank you! That's exactly what I was thinking about – Federica Benassi Mar 5 at 22:39
  • 1
    Is there, by any chance, a way to put "Table 1.2: My suggestion" under the table and not above? – Federica Benassi Mar 5 at 22:42
  • 1
    @FedericaBenassi Yes, just put the \caption... after \end{tabularx}. – CarLaTeX Mar 5 at 22:44
8

There is also a really handy online tool called Tables Generator.

It lets you input your data (you can even upload it from file), and adapt it WYSIWYG-style. It will then generate the correct LaTeX code for you to copy/paste into you document.

Edit: As suggested by Sanctus, here is the code that Tables Generator output me (after 2 minutes of data entry).

It does give really nice results for relatively simple tables with standard settings (colors, borders, font style, etc), but if you need something fancy, it probably won't provide enough. But I always use it as a starting point, to get the general structure working. Everything else I can then adapt myself.

\documentclass{article}

\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
\usepackage{booktabs}

\begin{document}

% Please add the following required packages to your document preamble:
% \usepackage[table,xcdraw]{xcolor}
% If you use beamer only pass "xcolor=table" option, i.e. \documentclass[xcolor=table]{beamer}
\begin{table}[]
    \begin{tabular}{|c|c|c|c|c|}
        \hline
        \rowcolor[HTML]{DAE8FC} 
        \multicolumn{5}{|c|}{\cellcolor[HTML]{DAE8FC}\textbf{\tiny{Number of cells per type}}} \\ \hline
        \rowcolor[HTML]{DAE8FC} 
        \textbf{a}     & \textbf{b}     & \textbf{c}    & \textbf{d}    & \textbf{e}    \\ \hline
        44             & 39             & 7             & 32            & 22            \\ \hline
    \end{tabular}
\end{table}

\end{document}

Output Table

  • 1
    Simply the best answer here. Instead of fish, gives the rod! – Sanctus Mar 6 at 21:04
  • 1
    @Sanctus - Have you actually looked at the quality of the LaTeX code produced by the tablegenerators.com site? – Mico Mar 6 at 21:51
  • @Mico, Im no Latex guru, but looked legit to me. Actually, just yesterday I copy-pasted whole table from there as it is. I found only stupid style formatting, probably related to the border configuration i set, cause it generates to much buzz. Otherwise it was ok. – Sanctus Mar 7 at 12:32
  • May be @sara can also post resulting code for topic starter, just to compare to others. – Sanctus Mar 7 at 12:33
  • 1
    Good point Sanctus, I've edited my answer! @Mico, Tablesgenerator is obviously not the answer to every problem you can possibly have with tables in Latex, but it's definitely a good starting point. And with OPs relatively simple table, it does provide a great tool. – sara Mar 7 at 14:11
7

Don't mix-and-mismatch visual cues and metaphores: if you employ colored rows, don't employ horizontal rules. And, for sure, you can omit all vertical rules -- they're not needed.

enter image description here

\documentclass{article}
\usepackage[table,svgnames]{xcolor}
\usepackage{array} % for '\extrarowheight' macro
\begin{document}

\begin{center}
\sffamily
\setlength\extrarowheight{2pt} % optional
\setlength\tabcolsep{9pt} % default: 6pt
\begin{tabular}{ ccccc } 
 %\hline
 \rowcolor{LightBlue} \multicolumn{5}{c}{Number of cells per type} \\
 %\hline
 \rowcolor{LightBlue} a & b & c & d & e\\ 
 %\hline
 44 & 39 & 7 & 32 & 22 \\ 
 \hline
\end{tabular}
\end{center}

\end{document}
  • 1
    Nice remark and a good suggestion (+1). – Raaja Mar 5 at 22:33
5

A mix-and-match with colors and using something as a starting point

\documentclass[10pt,a4paper]{article}
\usepackage[table,xcdraw]{xcolor}
\begin{document}
    \listoftables\newpage
    \begin{table}[]
        \begin{tabular}{!{\color{cyan}\vrule}l!{\color{cyan}\vrule}l!{\color{cyan}\vrule}l!{\color{cyan}\vrule}l!{\color{cyan}\vrule}l!{\color{cyan}\vrule}}
            \arrayrulecolor{cyan}\hline
            \rowcolor[HTML]{38FFF8} 
            \multicolumn{5}{!{\color{cyan}\vrule}l!{\color{cyan}\vrule}}{\cellcolor[HTML]{38FFF8}Number of cells per type} \\ \arrayrulecolor{cyan}\hline
            \rowcolor[HTML]{38FFF8} 
            a            & b            & c            & d           & e           \\ \arrayrulecolor{cyan}\hline
            1            & 2            & 3            & 4           & 5           \\ \hline
        \end{tabular}
    \caption{a}
    \end{table}
\end{document}

to get:

enter image description here

enter image description here

PS: Feel free to play with colors (I right now, do not have the necessary things to find out the colors) and I was lazy enough not to input your numbers (sorry ;-))

  • 2
    Needless to load colortbl: the table option of xcolor does it for you. – Bernard Mar 5 at 21:53
  • @Bernard Thanks for the remark, updated my answer. – Raaja Mar 5 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.