我正在用这个代码为所有浮动元素添加背景颜色:
\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
但在双列文档中,只有占据两列的表格部分会改变颜色。我想知道如何根据表格/图片的实际宽度添加背景颜色?
\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}
答案1
当用星号浮点环境调用时,在重新定义中用\columnwidth
替换两次即可。顺便说一句,你的桌子太宽了。\textwidth
\@xfloat
showframe
加载包只是为了显示页框。
\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}