# Table border error with \toprule \midrule \vspace [closed]

I have a table with mathematical formulas and I needed to use commands to increase the cell dimensions. However, this hinders the formation of an external border for the tables. How can I get flawless borders on these tables?

TABLE 1

\begin{table}[h!]
\centering
\rowcolors{2}{white}{gray!20}
\resizebox{\textwidth}{!}{
\begin{tabular}{|cccc|}
\toprule
\rowcolor{white}
\textbf{AAAA} & \makecell{ \textbf{BBB} \\ \textbf{BBBB}} & \makecell{\textbf{CCCC} \\ \textbf{CCCCCC}} & \textbf{DDDDD} \\
\midrule
AAAAAAAAA & 324 & 1400 or 2100 & AAAAAAA \\
BBBBBBB & 324 & 1400 or 2100 & BBBBBBB \\
\makecell{CCCCC \\ CCCCCC} & 380 & 1400 ou 2100 & \makecell{DDDDDDDDDD}   \\
\midrule
\end{tabular}
}
\end{table}

TABLE 2

\bgroup
\def\arraystretch{2.7}%
\begin{table}[!h]
\centering
\caption{Har}
\rowcolors{2}{white}{gray!20}
\resizebox{\textwidth}{!}{%
\begin{tabular}{|ll|}
%\toprule
\hline
\multicolumn{1}{|l}{\textbf{BBBBBBBBBB}} &
\multicolumn{1}{l|}{\textbf{AAAAAAAAAA}} \\
\toprule

AAAAAAA
& $f_1 = \sum_{i}\sum_{j} \left\{p(i,j)\right\}^2$ \\

AAAAAAAA
& \vspace{.5ex}\makecell[|l]{$f_2 = \sum\limits_{n=0}^{N_g - 1} n^2 \left\{ \sum\limits_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\}$, where
$n = |i-j|$.}\\

AAAAAAAA

&   \makecell[l]{$f_3 = \dfrac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}}$, where $\mu_{x}$, $\mu_{y}$, $\sigma_{x}$ \\ $\sigma_{y}$.}\\

AAAAAAAA
&  $f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j)$\\

AAAAAAAA
& \vspace{.5ex} $f_5 = \sum_{i}\sum_{j}\dfrac{p(i,j)}{1+(i-j)^2}$\\

AAAAAAAAA
&  $f_6 = \sum\limits_{i=2}^{2N_g}ip_{x+y}(i)$\\

AAAAAAAAA
& \vspace{.5ex} $f_7 = \sum\limits_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i)$\\

AAAAAAAAA
& \vspace{.5ex} $f_8 = -\sum\limits_{i=2}^{2N_g}p_{x+y}(i)log\left\{p_{x+y}(i)\right\}$ \\

AAAAAAAAA
&  $f_9 = -\sum_{i}\sum_{j}p(i,j)log\left\{p(i,j)\right\}$\\

AAAAAAAAA
& \vspace{.5ex} $f_{10} = - \sum\limits_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i)$ \\

AAAAAAAAA
& \vspace{.5ex} $f_{11} = -\sum\limits_{i=0}^{N_g-1}p_{x-y}(i)log\left\{p_{x-y}(i)\right\}$\\

AAAAAAAAA
&  \makecell[l]{$f_{14} = \sqrt{\text{...}}$\\
$Q(i,j)=\sum_{k}\dfrac{p(i,k)p(j,k)}{p_x(i)p_y(k)}$}\\

\midrule

\end{tabular}
}
\end{table}
\egroup

UPDATE: Using tabularx, I am having this problem. I need a more compact table, I don't want many rows in the last column. I don't care if I need to decrease the font size. I need an effect similar to when using \resizebox There is a lot of space between the columns. This space could be reduced in order not to break the text of the last column in so many lines.

\usepackage[table]{xcolor}
\usepackage{boldline, cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{table}[h!]
\centering
\rowcolors{2}{white}{gray!20}

\begin{tabularx}{\linewidth}{|*{4}{SC}|}
\hlineB{2}
\rowcolor{white}
\textbf{Método} & \makecell{ \textbf{N. de} \\ \textbf{variáveis}} & \makecell{\textbf{N. de} \\ \textbf{amostras}} & \textbf{Descrição} \\
\hlineB{2}

Valores dos pixels s/ equalização & 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Valores dos pixels c/ equalização & 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Segmentação & 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Segmentação (maior região) & 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Máscara elíptica & 126 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Histograma & 64 & 1400 ou 2100 & Quant. de pixels em cada nível de cinza\\
Haralick & 56 & 1400 ou 2100 &  Valores dos descritores de textura \\
Haralick reduzido & 14 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick duplo & 112 & 1400 ou 2100 & Valores dos descritores de textura \\
\makecell{Haralick e valores dos pixels \\com equalização} & 380 & 1400 ou 2100 & \makecell{Valores dos descritores de textura \\e nível de intensidade de cada pixel}   \\
\hlineB{2}
\end{tabularx}
\end{table}

I would like to have this aspect (in relation to breaking lines in cells). However I need it with the lines of the outer edges.

• Please, extend your code fragment to MWE! Jan 8 at 16:51
• Please, provide in one row of your new example a real content which you have in table. Also please make yourself more familiar with writing of tables. For example , read en.wikibooks.org/wiki/LaTeX/Tables. Jan 8 at 17:11
• Still is missing \documentclass ... Jan 8 at 17:37
• ... and \begin{document} as well as \end{document}. (In case you (still) don't know what an MWE is, have a look at e.g. tex.meta.stackexchange.com/q/228.) Jan 8 at 20:59
• You must be using booktabs package: "Both \toprule and \midrule add some vertical space, so \rowcolor cannot fill completely the apparent cell spaces, but you can use a \specialrule to make ad hoc rules" -- see tex.stackexchange.com/questions/494904/… Jan 9 at 4:40

Edit: you not provide MWE (Minimal Working Example), a small but complete document, with which we can test your code fragments. Alo question is not very clear, so design of table is mostly to my taste. SO, please edit your question and clarify, what all is problems at your tables.

Now I added explanation how is handled vertical space around cells contents a remove spurious \limits left from your code for the second table and add \nolimits (which you can remove, if you liked such form of \sum)

• Rules defined in the booktabs package are not intended for use in table with vertical lines. Instead those rules you should use \hline or when you like to have table rules with different thickness hlineB{<factor>} defined the boldline packages (used in the first table) or Xhline{<thicknes>} defined in the makecell package (used in the second table).
• For more vertical space around cells contents you can use the package cellspace. This space is defined by
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
• If you find that 5pt is too much, you can reduce it to desired amount (in MWE is now reduced to 2pt)
• Use of \resizebox at tables is not recommended because with using it font size used in table is lost. Better is to use table environment, thoi which you can prescribe width, for example tabular* or tabularx as used in both MWEs below.

The first table:

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage{boldline, cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\begin{table}[ht]
\centering
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|*{4}{SC}|}
\hlineB{2}
\textbf{AAAA}
&   \makecell{\textbf{BBB} \\ \textbf{BBBB}}
&   \makecell{\textbf{CCCC} \\ \textbf{CCCCCC}}
&   \textbf{DDDDD}              \\
\hlineB{1.5}
AAAAAAAAA   & 324 & 1400 or 2100 & AAAAAAA      \\
BBBBBBB     & 324 & 1400 or 2100 & BBBBBBB      \\
\makecell{CCCCC \\ CCCCCC}
& 380 & 1400 or 2100 & DDDDDDDDDD   \\
\hlineB{2}
\end{tabularx}
\end{table}
\end{document}

The second table:

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage{cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\begin{document}
\begin{table}[ht]
\centering
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|Sl >{$\displaystyle}SL<{$}|}
\Xhline{1.2pt}
\textbf{BBBBBBBB}
&   \textbf{AAAAAAAAAA}     \\
\Xhline{0.8pt}
AAAAAAA & f_1 = \sum\nolimits_{i}\sum\nolimits_{j} \{p(i,j)\}^2         \\

AAAAAA  & f_2 = \sum_{n=0}^{N_g - 1} n^2
\left\{\sum_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\},
\text{ where }
n = |i-j|.                                              \\
AAAAAA  & f_3 = \frac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}},
\text{ where }
\mu_{x}, \mu_{y}, \sigma_{x}, \sigma_{y} ?              \\
AAAAAA  & f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j)                        \\
AAAAAA  & f_5 = \sum\nolimits_{i}\sum\nolimits_{j}\frac{p(i,j)}{1+(i-j)^2}        \\
AAAAAA  & f_6 = \sum_{i=2}^{2N_g}ip_{x+y}(i)                            \\
AAAAAA  & f_7 = \sum_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i)                   \\
AAAAAA  & f_8 = -\sum_{i=2}^{2N_g}p_{x+y}(i)\log\{p_{x+y}(i)\}          \\
AAAAAA  & f_9 = -\sum\nolimits_{i}\sum\nolimits_{j}p(i,j)\log\{p(i,j)\} \\
AAAAAA  & f_{10} = - \sum_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i)          \\
AAAAAA  & f_{11} = - \sum_{i=0}^{N_g-1}p_{x-y}(i)\log\{p_{x-y}(i)\}     \\
AAAAAA  &   \begin{aligned}
f_{14} & = \sqrt{\text{...}}  \\
Q(i,j) & =\sum_{k}\frac{p(i,k)p(j,k)}{p_x(i)p_y(k)}
\end{aligned}                                               \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}

• Let me first noted, that it is very difficult to help you since you not help us to help you. You still provide only code fragment regardless that I ask you many times extend this fragments to complete small document. We still don't know anything about your document layout and used packages.
• Please make yourself more familiar with LaTeX, speciča with writing of tables. There exist may introductory text about LaTeX (for example Dickimaw books or No so short introduction to LaTeX) and about writing tables with LaTeX (wiki/tables).
• Reading package documentation also is of big help.

• Now is apparently, that you not need that all columns have equal width, so you not need to have all of X type.
• Columns X automatic break text in cells into more lines, so there is no need to use \makecell inside with manually breaking text. Even more, their use make your troubles because your manually breaking pf text is not adequate -- consequently text spill out of table.
• Numbers in the second column are (to my taste) nicer if are right aligned and centered in column. This can be dona by use of the S column type defined in the siunitx package.
• Using siunitx package require to redefine columns specifier defined in cellspace package from default S oto some other. In MWE below i select O for this.

Suggested MWE for this new table is:

\documentclass[a4paper]{article}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage[column=O]{cellspace}
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\usepackage{makecell, tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hsize=#1\hsize}X}
\usepackage{siunitx}

\begin{document}
\begin{table}[ht]
\centering
\renewcommand\tabularxcolumn[1]{m{#1}}
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|O{L{0.8}}
S[table-format=3.0]
c
O{L{1.2}}|}
\Xhline{1.2pt}
\rowcolor{white}
\Xhline{0.8pt}
Valores dos pixels s/ equalização
& 324   & 1400 ou 2100  & Nível de intensidade de cada pixel    \\
Valores dos pixels c/ equalização
& 324   & 1400 ou 2100  & Nível de intensidade de cada pixel    \\
Segmentação
& 324   & 1400 ou 2100  & Estado do pixel (0 ou 1)              \\
Segmentação (maior região)
& 324   & 1400 ou 2100  & Estado do pixel (0 ou 1)              \\
Máscara elíptica
& 126   & 1400 ou 2100  & Nível de intensidade de cada pixel    \\
Histograma
& 64    & 1400 ou 2100  & Quant. de pixels em cada nível de cinza   \\
Haralick
& 56    & 1400 ou 2100  &  Valores dos descritores de textura       \\
Haralick reduzido
& 14    & 1400 ou 2100  & Valores dos descritores de textura        \\
Haralick duplo
& 112   & 1400 ou 2100  & Valores dos descritores de textura    \\
Haralick e valores dos pixels com equalização
& 380   & 1400 ou 2100  & Valores dos descritores de textura e nível de intensidade de cada pixel   \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}

• Thank you very much for your answer, it made some points more evident to me. I applied the code you indicated, but the table is extremely large when compared to what I was doing. I would like it to fit the text size, as when using the resizebox. The previous table occupied less than 1/3 of the page, the new table occupied almost the entire page (the last column is a text description and it is being divided into several lines). How can I fix this? Thanks Jan 8 at 7:44
• @AntonioMendes, sorry, but your claims are not fair. I can't test your solution because you provide only code fragments so I (we) haven't any information about your document. Suggested table has the same width as yours (with resize box), of course, tables now are wee bit taller due to added vertical space (as you require). You can reduce this space, for example from 5pt to 2pt. Also about last column in table: it is not clear what is your problem, It is not mentioned in your question, provided code for last column is broken (uncomplete) . Please make your question more clear. Jan 8 at 8:01
• I'll try to make it a little clearer: using this code, the distance between the columns is large and the text contained in the cells is broken into several lines, leaving the table even higher. Using the resizebox he didn't add line breaks (I added the breaks manually using \\ inside the makecell). I removed the \ addparagraphcolumntypes {C} to stop the wrapping, but in this way the cell text exceeds the limits of the table. Jan 8 at 14:23
• @AntonioMendes, than don't use my answer. I just try to help you in frame of your provided information. And what is wrong with last row in the second table? If you have new problem, pleas ask new question. Jan 8 at 16:10
• I edited the question and added an image of what is going on. Your answer is great, I'm just trying to learn. Jan 8 at 16:33

Here is what you can do with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{nicematrix}

\begin{document}

\begin{table}[h!]
\centering
\begin{NiceTabular}{|cccc|}[code-before=\rowcolors{2}{}{gray!20}]
\toprule
\textbf{AAAA} & \Block{}{ \textbf{BBB} \\ \textbf{BBBB}} & \Block{}{\textbf{CCCC} \\ \textbf{CCCCCC}} & \textbf{DDDDD} \\
\midrule
AAAAAAAAA & 324 & 1400 or 2100 & AAAAAAA \\
BBBBBBB & 324 & 1400 or 2100 & BBBBBBB \\
\Block{}{CCCCC \\ CCCCCC} & 380 & 1400 ou 2100 & DDDDDDDDDD   \\
\midrule
\end{NiceTabular}
\end{table}

\begin{table}[!ht]
\centering
\caption{Har}
\begin{NiceTabular}{|ll|}[code-before = \rowcolors{2}{}{gray!20},cell-space-top-limit=1ex,cell-space-bottom-limit=1ex]
%\toprule
\hline
\Block[l]{}{\textbf{BBBBBBBBBB}} &
\Block[l]{}{\textbf{AAAAAAAAAA}} \\
\toprule
AAAAAAA
& $f_1 = \sum_{i}\sum_{j} \left\{p(i,j)\right\}^2$ \\
AAAAAAAA
&\Block[l]{}{$f_2 = \sum\limits_{n=0}^{N_g - 1} n^2 \left\{ \sum\limits_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\}$, where
$n = |i-j|$.}\\
AAAAAAAA
&   \Block[l]{}{$f_3 = \dfrac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}}$, \\
where $\mu_{x}$, $\mu_{y}$, $\sigma_{x}$ and $\sigma_{y}$ are...}\\
AAAAAAAA
&  $f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j)$\\
AAAAAAAA
& $f_5 = \sum_{i}\sum_{j}\dfrac{p(i,j)}{1+(i-j)^2}$\\
AAAAAAAAA
&  $f_6 = \sum\limits_{i=2}^{2N_g}ip_{x+y}(i)$\\
AAAAAAAAA
& $f_7 = \sum\limits_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i)$\\
AAAAAAAAA
& $f_8 = -\sum\limits_{i=2}^{2N_g}p_{x+y}(i)\log\left\{p_{x+y}(i)\right\}$ \\
AAAAAAAAA
&  $f_9 = -\sum_{i}\sum_{j}p(i,j)\log\left\{p(i,j)\right\}$\\
AAAAAAAAA
& $f_{10} = - \sum\limits_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i)$ \\
AAAAAAAAA
& $f_{11} = -\sum\limits_{i=0}^{N_g-1}p_{x-y}(i)\log\left\{p_{x-y}(i)\right\}$\\
AAAAAAAAA
&  \Block[l]{}{$f_{14} = \sqrt{\text{...}}$\\
$Q(i,j)=\sum_{k}\dfrac{p(i,k)p(j,k)}{p_x(i)p_y(k)}$}\\
\midrule
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

• My actual table has slightly longer sentences in one of the cells. This code causes the table to exceed the page width. I tried to use the resizebox as I used it initially, but this causes the table to be created with defects. Is there a way to adjust the size of the table with the width of the text in this case? (no problem for me if it decreases the font size) Jan 8 at 14:44
• @Antonio Mendes: I think you should use \small before your table and, then, use more instructions \\ in the \Blocks of the second column to break the lines which are too long. Jan 8 at 17:08