1

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}

enter image description here

happy
  • 149
  • 7
  • Does this answer your question? https://tex.stackexchange.com/a/106906/70641 – James Jan 05 '22 at 15:48
  • After a closer look, it looks like the code in your question is from the link I provided. Maybe you should add a link to that answer in your question. – James Jan 05 '22 at 15:50
  • Thank you for your reminder, I have modified it so far – happy Jan 06 '22 at 02:58

1 Answers1

1

Replacing two occurrences of \columnwidth with \textwidth in the redefinition of \@xfloat when it is called by a starred float environment, does the work. BTW your table is too wide.

Package showframe is loaded just to show page frame(s).

\documentclass[11pt,a4paper,twocolumn,english]{article}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{showframe}

\makeatletter \newif\iffloat@twocolumn

% redefine @dblfloat \def@dblfloat{% \if@twocolumn \let\reserved@a@dbflt \float@twocolumntrue \else \let\reserved@a@float \fi \reserved@a}

% based on https://tex.stackexchange.com/a/106906 % added \iffloat@twocolumn ... \else ... \fi \def\foo#1\normalcolor\vbox\bgroup#2!!{% \def@xfloat ##1[##2]{#1% \normalcolor \hbox\bgroup{% \color{yellow}% \iffloat@twocolumn \leaders\vrule\hskip\textwidth\hskip-\textwidth \else \leaders\vrule\hskip\columnwidth\hskip-\columnwidth \fi}% \vbox \bgroup\aftergroup\egroup #2}} \expandafter\foo@xfloat{#1}[#2]!!

\makeatother \title{Two column test}

\begin{document} \maketitle \lipsum[1] \begin{table}[h] \centering content \end{table}

\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}

example - un-starred float env example - starred float env

muzimuzhi Z
  • 26,474