I'm using this code to add background color to all floating elements:
\makeatletter
\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def@xfloat ##1[##2]{#1%
\normalcolor
\hbox\bgroup{\color{yellow}\leaders\vrule\hskip\columnwidth\hskip-\columnwidth}%
\vbox \bgroup\aftergroup\egroup
#2}}
\expandafter\foo@xfloat{#1}[#2]!!
\makeatother
but only part of the table that occupies two columns in a two-column document will change color. I want to know how to add a background color based on the actual width of the table/picture?
\documentclass[11pt,a4paper,twocolumn,english]{article}
\usepackage{lipsum}
\usepackage{xcolor}
\makeatletter
\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def@xfloat ##1[##2]{#1%
\normalcolor
\hbox\bgroup{\color{yellow}\leaders\vrule\hskip\columnwidth\hskip-\columnwidth}%
\vbox \bgroup\aftergroup\egroup
#2}}
\expandafter\foo@xfloat{#1}[#2]!!
\makeatother
\title{Two column test}
\begin{document}
\maketitle
\lipsum
\begin{table}[h]
\small
\centering
\caption{Simuleringsresultat av beräkning av $\pi$ genom Monte Carlo.}
\begin{tabular}{|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| }
\hline
\multicolumn{7}{|c|}{En beräkning av $\pi$ genom Monte Carlo} \
\hline
Antal \linebreak partiklar & Värde 1 & Värde 2 & Värde 3 & Värde 4 & Värde 5 & Medelvärde\
\hline
$10^0$ & 4 & 4 & 4 & 4 & 0 & 3,2 \
$10^1$ & 3,2 & 2,4 & 4,0 & 3,6 & 3,6 & 3,36 \
$10^2$ & 3,56 & 2,92 & 3,32 & 3,04 & 3,36 & 3,24 \
$10^3$ & 3,208 & 3,160 & 3,108 & 3,136 & 3,136 & 3,1496 \
$10^4$ & 3,1404 & 3,1288 & 3,1460 & 3,1572 & 3,1308 & 3,14064 \
$10^5$ & 3,13468 & 3,14724 & 3,14316 & 3,14124 & 3,14248 & 3,14176 \
$10^6$ & 3,14286 & 3,146348 & 3,142648 & 3,141184 & 3,142144 & 3,1430368 \
$10^7$ & 3,1421208 & 3,141374 & 3,1414892 & 3,1413988 & 3,1416936 & 3,14161528 \
$10^8$ & 3,14138248 & 3,14127896 & 3,14172908 & 3,14154640 & 3,14178604 & 3,141544592 \
$10^9$ & 3,1416602762 & 3,1415840040 & 3,1415684520 & 3,1415948080 & 3,1416119840 & 3.1416039048 \
\hline
\multicolumn{7}{|c|}{$\pi = 3.1415926535 $ } \
\hline
\end{tabular}
\end{table}
\end{document}


