1

I want to set a lightgray color behind my table, using colorbox. Following this solution, I created the following MWE:

\begin{table}[H]
\renewcommand{\arraystretch}{1.5}
\centering
\begingroup\setlength{\fboxsep}{0pt}
\colorbox{gray!50}{%
\begin{tabular}{ | c | c | c | c | c | c | }
 \arrayrulecolor{black} %changes color of hline
 \hline
  & &  \multicolumn{2}{c|}{\textbf{test test}} & \multicolumn{2}{c|}{\textbf{test test}} \\
 \hhline{~|~|-|-|-|-|}
 \multirow{-2}{*}{\textbf{\shortstack{teeest\\ teeeeeeest}}} & \multirow{-2}{*}{{\shortstack{\textbf{test}\\ \textbf{teeeest}\\(sec)}}} & (mW) & (hrs/days) & (mW) & (hrs/days)  \\[0.6pt]
 \hline
 \textbf{test 1} &  8 & 16 & 64 & 2 & 2\\
 \textbf{test 2} & 12 & 24 & 96 & 2 & 2\\
 \textbf{test 3} & 14.4 & 28.8 & 115.2 & 2 & 2\\
 \textbf{test 4} & 20 & 40 & 160 & 2 & 2 \\
 \textbf{test 5} & 20 & 40 & 160 & 2 & 2\\
 \hline
\end{tabular}
}\endgroup
\caption{test teest}
\end{table}

The output is this:

enter image description here

As you can see, the background is not exactly behind the tablular. In fact it seems that the tabular is also not centered after I add the background,ut the colorbox seems centered. Why is that?

2
  • 2
    Your example can't be tested, but you have a space after \end{tabular}. Apr 26, 2021 at 16:23
  • Well, that was unfortunate. apologies and thaks for the help
    – NickG
    Apr 26, 2021 at 16:44

2 Answers 2

1

Add a % after \end{tabular}. I removed the begingroup \endgroup which is unnecessary inside an environment such as table, and simplified a bit the table preamble.

\documentclass{article}
\usepackage[svgnames, table]{xcolor}
\usepackage{float, hhline, multirow}

\begin{document}

\begin{table}[H]
\renewcommand{\arraystretch}{1.5}
\centering
\setlength{\fboxsep}{0pt}
\colorbox{gray!50}{%
\begin{tabular}{|*{6}{c |}}
 \arrayrulecolor{black} %changes color of hline
 \hline
  & & \multicolumn{2}{c|}{\textbf{test test}} & \multicolumn{2}{c|}{\textbf{test test}} \\
 \hhline{~|~|-|-|-|-|}
 \multirow{-2}{*}{\textbf{\shortstack{teeest\\ teeeeeeest}}} & \multirow{-2}{*}{{\shortstack{\textbf{test}\\ \textbf{teeeest}\\(sec)}}} & (mW) & (hrs/days) & (mW) & (hrs/days) \\[0.6pt]
 \hline
 \textbf{test 1} & 8 & 16 & 64 & 2 & 2\\
 \textbf{test 2} & 12 & 24 & 96 & 2 & 2\\
 \textbf{test 3} & 14.4 & 28.8 & 115.2 & 2 & 2\\
 \textbf{test 4} & 20 & 40 & 160 & 2 & 2 \\
 \textbf{test 5} & 20 & 40 & 160 & 2 & 2\\
 \hline
\end{tabular}%
}

\end{table}

\end{document} 

enter image description here

0

Here is a way to do that table with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{float}
\usepackage{nicematrix}

\begin{document}

\begin{table}[H]
\renewcommand{\arraystretch}{1.5}
\centering

\begin{NiceTabular}{>{\bfseries}cccccc}[hvlines]
\CodeBefore
  \arraycolor{gray!50}
\Body
 \Block{2-1}{teeest\\ teeeeeeest} & 
 \Block{2-1}<\bfseries>{test\\ teeeest\\(sec)} & 
 \Block{1-2}{\bfseries test test} & &
 \Block{1-2}{\bfseries test test} \\
  &  & (mW) & (hrs/days) & (mW) & (hrs/days) \\[0.6pt]
 test 1 & 8 & 16 & 64 & 2 & 2\\
 test 2 & 12 & 24 & 96 & 2 & 2\\
 test 3 & 14.4 & 28.8 & 115.2 & 2 & 2\\
 test 4 & 20 & 40 & 160 & 2 & 2 \\
 test 5 & 20 & 40 & 160 & 2 & 2\\
\end{NiceTabular}%

\end{table}

\end{document} 

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

Output of the above code

You must log in to answer this question.

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