# How to generate beautiful tables in LaTeX

I want to create a table for a publication.

This is what I have:

\begin{table}[!h]
\small
\renewcommand{\arraystretch}{1.7}
\begin{center}
\begin{tabular}{|p{0.5cm} | p{2.5cm} | p{3.2cm} | p{3.2cm} | p{1cm} | p{1cm} |}
\hline
\textbf{nº} & \textbf{CID Ligando} & \textbf{Nombre Ligando} & \textbf{Afinidad (Kcal/mol)²} & \textbf{RMSD l.b.} & \textbf{RMSD u.b.}  \\ \hline
1 & 234523 & LoreIpsum & 234 & 0 & 0 \\
2 & 2345 & LoreIpsum & 2365 & 0 & 0 \\
3 & 3453 & LoreIpsum & 45634 & 0 & 0 \\
4 & 83452 & LoreIpsum & 2456 & 0 & 0 \\
5 & 210 & LoreIpsum & 245 & 0 & 0 \\
6 & 3417 & LoreIpsum & 45634 & 0 & 0 \\
7 & 4345 & LoreIpsum & 3456 & 0 & 0 \\
8 & 4334 & LoreIpsum & 3456 & 0 & 0 \\
\hline
\end{tabular}
\newline\newline
\caption{Valores de afinidad obtenidos para los ocho fármacos en \textit{Autodock Vina}}
\label{tab:PECs}
\end{center}
\end{table}



But I would like something like:

Is there a way of getting this? The color and the lines specially...

Btw, I use TexStudio and MikTex.

\documentclass[12pt]{article}

\usepackage[lmargin=2cm, rmargin=2cm, top=1.5cm, bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish,english,american]{babel}

• What exactly do you want to have? The spacing? The lines (see booktabs-package)? The colored background? The caption? Jul 10, 2021 at 14:44
• Remove all |. See tex.stackexchange.com/questions/112343/beautiful-table-samples Jul 10, 2021 at 14:44
• Take a look at the booktabs documentation for general tips about table design, and particularly for the horizontal rules created with \toprule, \midrule and \bottomrule. Also take a look at siunitx's S type column for numerical columns. Take a look at the array package to inject code for formatting of a column (e.g., to make the column print bold), last take a look at the collcell package to assign the effects of a macro to a complete column (e.g., for your molecular formulae). Jul 10, 2021 at 14:44
• Using the center environment inside of table results in extra vertical white space. You may want to replace the center environment with the \centering command. Jul 10, 2021 at 15:01
• please tell us which document class, which font, and which fontsize you employ. And, how wide are the margins?
– Mico
Jul 10, 2021 at 15:07

## 4 Answers

Here are some suggestions, not made in any particular order:

• Remove center environment, use a \centering instruction instead.

• Drop \newline\newline before \caption. (Aside: The presence of \newline\newline is causing your tabular environment not to be centered horizontally even though it's embedded in a center environment.)

• Remove all vertical lines and suppress whitespace padding at edges of table.

• Load the booktabs package and replace \hline with \toprule, \midrule, and \bottomrule as needed, add a bit of vertical whitespace halfway through the table via \addlinespace.

Aside: The user guide of the booktabs package is chock-full of good advice on table design. It' well worth a read -- and re-read!

• Remove the \renewcommand{\arraystretch}{1.7} and \small directives.

• Load the siunitx package and use the S column type for columns 2 and 4.

• No vulgar-looking bold-facing of header cells, please.

• The appears to be no need for automatic line-breaking in cells.

• Employ the threeparttable environment to assure that the caption won't be wider than the tabular material. (Aside: the threeparttable environment can do additional things beyond assuring that the caption's width doesn't exceed width of table.)

• Impose more structure/hierarchy on the header cells of the final two columns. ("l.b." and "u.b." stands for "lower bound" and "upper bound", right?)

The framelines in the following screenshot indicate the edges of the text block. They get inserted because I set the showframe option when loading the geometry package.

\documentclass[12pt]{article}
\usepackage[hmargin=2cm, top=1.5cm, bottom=2cm, showframe]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[british,UKenglish,USenglish,english,american]{babel}
% why not '\usepackage[spanish]{babel}'?

% New:
\usepackage{booktabs,siunitx,array,threeparttable}
\sisetup{group-minimum-digits=4}

\begin{document}

\noindent
\textcolor{red}{\Large\textbf{before}}
\begin{table}[!h]
\small
\renewcommand{\arraystretch}{1.7}
\begin{center}
\begin{tabular}{|p{0.5cm} | p{2.5cm} | p{3.2cm} | p{3.2cm} | p{1cm} | p{1cm} |}
\hline
\textbf{nº} & \textbf{CID Ligando} & \textbf{Nombre Ligando} & \textbf{Afinidad (Kcal/mol)²} & \textbf{RMSD l.b.} & \textbf{RMSD u.b.}  \\ \hline
1 & 234523 & LoreIpsum & 234 & 0 & 0 \\
2 & 2345 & LoreIpsum & 2365 & 0 & 0 \\
3 & 3453 & LoreIpsum & 45634 & 0 & 0 \\
4 & 83452 & LoreIpsum & 2456 & 0 & 0 \\
5 & 210 & LoreIpsum & 245 & 0 & 0 \\
6 & 3417 & LoreIpsum & 45634 & 0 & 0 \\
7 & 4345 & LoreIpsum & 3456 & 0 & 0 \\
8 & 4334 & LoreIpsum & 3456 & 0 & 0 \\
\hline
\end{tabular}
\newline\newline
\caption{Valores de afinidad obtenidos para los ocho fármacos en \textit{Autodock Vina}}
\label{tab:version1}
\end{center}
\end{table}

%% after
\noindent
\textcolor{red}{\Large\textbf{after}}
\begin{table}[!h]
\centering
\begin{threeparttable}
\begin{tabular}{@{} l S[table-format=6.0] l S[table-format=5.0] cc @{}}
\toprule
{nº} & {CID Ligando} & {Nombre Ligando} & {Afinidad} & \multicolumn{2}{c@{}}{RMSD} \\
\cmidrule(l){5-6}
& & & {(Kcal/mol)\textsuperscript{2}} & {l.b.} & {u.b.}\\
\midrule
1 & 234523 & LoremIpsum & 234   & 0 & 0 \\
2 & 2345   & LoremIpsum & 2365  & 0 & 0 \\
3 & 3453   & LoremIpsum & 45634 & 0 & 0 \\
4 & 83452  & LoremIpsum & 2456  & 0 & 0 \\
\addlinespace
5 & 210    & LoremIpsum & 245   & 0 & 0 \\
6 & 3417   & LoremIpsum & 45634 & 0 & 0 \\
7 & 4345   & LoremIpsum & 3456  & 0 & 0 \\
8 & 4334   & LoremIpsum & 3456  & 0 & 0 \\
\bottomrule
\end{tabular}
\caption{Valores de afinidad obtenidos para los ocho fármacos en \textit{Autodock Vina}}
\label{tab:version2}
\end{threeparttable}
\end{table}

\end{document}

• What is wrong with bold-facing of header cells? Most tables in (published) books and on Wikipedia are like that. Jul 11, 2021 at 12:00
• @PeterMortensen - The main issue is that if a table is designed sufficiently well, bold-facing the header cells is absolutely unnecessary in order to get the information across. (Aside: Sadly, many tables in published books do not qualify as being "designed sufficiently well".) As with many other aspects of good typography, a well-designed table has balance, letting readers notice and absorb all of the table's information, not just the header cells. By bold-facing the header cells, one is likely doing oneself a disservice, as readers tend to focus too much on the bold stuff.
– Mico
Jul 11, 2021 at 12:14
• @PeterMortensen - For more on the subject of well-designed tables, do please study the user guide of the booktabs package. You'll likely notice that the author does not use bold-facing in a single (well-designed) table. Nuff said.
– Mico
Jul 11, 2021 at 12:18

\documentclass[12pt]{article}
\usepackage[lmargin=2cm, rmargin=2cm, top=1.5cm, bottom=2cm]{geometry}

\usepackage{caption}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{siunitx}
\usepackage{tcolorbox}

\begin{document}
\begin{table}
\begin{tcolorbox}[arc=0pt,boxrule=0pt]
\sisetup{group-minimum-digits = 4}
\centering
\caption{Valores de afinidad obtenidos para los ocho fármacos en \textit{Autodock Vina}}
\label{tab:PECs}
\begin{tabular}{lllS[table-format=5]ll}
\toprule
\thead{nº} & \thead{CID Ligando} & \thead{Nombre Ligando} & {\thead{Afinidad\\ \unit{(Kcal\per\mole)\squared}}} & \thead{RMSD l.b.} & \thead{RMSD u.b.}  \\
\midrule
1 & 234523 & LoreIpsum & 234   & 0 & 0 \\
2 & 2345   & LoreIpsum & 2365  & 0 & 0 \\
3 & 3453   & LoreIpsum & 45634 & 0 & 0 \\
4 & 83452  & LoreIpsum & 2456  & 0 & 0 \\
5 & 210    & LoreIpsum & 245   & 0 & 0 \\
6 & 3417   & LoreIpsum & 45634 & 0 & 0 \\
7 & 4345   & LoreIpsum & 3456  & 0 & 0 \\
8 & 4334   & LoreIpsum & 3456  & 0 & 0 \\
\bottomrule
\end{tabular}
\end{tcolorbox}
\end{table}
\end{document}


1. Use the booktabs package, you will love the rules!
2. Employ adjustbox to manage column width
3. Trust existing tools and do not add new lines yourself
4. Trust automatic column width for column width
5. Do not change font size unless absolutely necessary

here is the code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{adjustbox}
\begin{document}
\begin{table}[!h]
\begin{adjustbox}{center,max width=\linewidth}
\begin{tabular}{rrlrrr}
\toprule
\bf nº & \bf \stackbox[r]{CID\\ Ligando} & \bf \stackbox[l]{Nombre\\ Ligando }& \bf \stackbox[r]{Afinidad \\(Kcal/mol)²}
& \bf \stackbox[l]{RMSD \\ l.b.} & \bf \stackbox[r]{RMSD \\u.b.} \\
\midrule
1      & 234523          & LoreIpsum          & 234                      & 0             & 0             \\
2      & 2345            & LoreIpsum          & 2365                     & 0             & 0             \\
3      & 3453            & LoreIpsum          & 45634                    & 0             & 0             \\
4      & 83452           & LoreIpsum          & 2456                     & 0             & 0             \\
5      & 210             & LoreIpsum          & 245                      & 0             & 0             \\
6      & 3417            & LoreIpsum          & 45634                    & 0             & 0             \\
7      & 4345            & LoreIpsum          & 3456                     & 0             & 0             \\
8      & 4334            & LoreIpsum          & 3456                     & 0             & 0             \\
\bottomrule
\end{tabular}
\end{adjustbox}
\caption{Valores de afinidad obtenidos para los ocho fármacos en \textit{Autodock Vina}}
\label{tab:PECs}
\end{table}
\end{document}

• Don't use adjustbox. It change font size too, so the consistency of them in table and in text is lost. Jul 10, 2021 at 17:32
• You may also want to replace \bf with \bfseries or use \textbf. See also: “Correct” way to bold/italicize text? Jul 11, 2021 at 12:58
• I prefer the archaic \bf for headers. It is the shortest way to create bold face headers, and length matters, if you wish to set your table in columns also in the input. Jul 11, 2021 at 14:12
• Using \bf doesn't work if you use a document class that disables it (and its "friends" \rm, \it, etc, not to mention similar commands in math mode). There are plenty of websites already that teach people how to write obsolete LaTeX that barely works. I don't think the world needs another one. Jul 12, 2021 at 0:08
• You are absolutely right. Perhaps a more robust and a bit shorter way, is \let\B=\bfseries and then use \B in header cells. Jul 12, 2021 at 4:38

Your statement

"I want to create a table for a publication."


does not specify if said publication is / is not for a publication in a scientific journal. But if this is the case, it usually is the more efficient approach to use their templates for submitting the content in question (example American Chemical Society, Royal Society of Chemistry, Wiley, Elsevier, Springer, IUCr, MDPI).* A search e.g., on CTAN equally yields examples, and on occasion you find templates for books (example). You may use these templates for the preparation of the supplementary information of your publication, too.

In occurrence of e.g., overly large tables, or equations better displayed across two columns instead of one, their staff members (or, contractors like River Valley or le-tex) already have experience how to rekey / (re)format the content to blend-in into the recognizable layout of the publishers, or may contact you if deemed necessary.

*) Because the table shown by you suggests a publication in chemistry.