使标题背景宽度与表格宽度匹配

使标题背景宽度与表格宽度匹配

我正在尝试用一种背景颜色给表格上色,用另一种背景颜色给标题上色。我必须使用环境tabulartabularytablarx、对我的设置tabutabular*没有帮助。请查看以下 MWE:

\documentclass{article}
\usepackage[usenames,svgnames,dvipsnames]{xcolor}
\usepackage{booktabs}
\usepackage{array}
\usepackage[font={bf}]{caption}
\captionsetup[table]{box=colorbox,boxcolor=orange!20}
\begin{document}

%===========================================
\section{ Gender Distribution}
%===========================================
\begin{center}
\fcolorbox{yellow!20}{yellow!20}{%
\parbox{\textwidth}{%
\centering
\setlength{\abovecaptionskip}{10pt}
\setlength{\belowcaptionskip}{-3pt}
\captionof{table}{Duration of Use and Gender Distribution}


\begin{tabular}{>{\hspace{1pc}}c>{\hspace{1pc}}c>{\hspace{1pc}}c>   {\hspace{1pc}}c<{\hspace{1pc}}}
\toprule
 & Female & Male & All \\ 
\midrule
1 year- 2 years & 2 (50) & 2 (13.3) & 4 \\ 
2 years - 3 years & 0 (0) & 4 (26.7) & 4 \\ 
3 years + & 1 (25) & 3 (20) & 4 \\ 
6 months- 1 year & 1 (25) & 3 (20) & 4 \\ 
Less than 6 months & 0 (0) & 3 (20) & 3 \\ 
\midrule
Total & 4 & 15 & 19 \\ 
\bottomrule
\footnotesize{Source:Airtel Survey}\end{tabular}
}

}
\end{center}


\end{document}

上述代码生成下表:

在此处输入图片描述

我想扩展标题背景,以便箭头所指的间距可以被标题背景颜色覆盖。再次提醒,请注意,我必须使用环境tabular,并且表格宽度应覆盖整个文本宽度。如果能对当前设置进行最小更改,我将不胜感激。

答案1

您已标记tcolorbox但未使用!这是使用您自己的代码的版本。我已注释掉代码中所做的更改。

\documentclass{article}
\usepackage[usenames,svgnames,dvipsnames]{xcolor}
\usepackage{booktabs}
\usepackage{array}
\usepackage[font={bf}]{caption}
\captionsetup[table]{box=colorbox,boxcolor=orange!20}
\begin{document}

%===========================================
\section{ Gender Distribution}
%===========================================
\begin{center}
\fboxsep0pt %% <<<--- add this.
\fcolorbox{yellow!20}{yellow!20}{%
\parbox{\textwidth}{%
\fboxsep5pt% %%<<--- add this here for making caption's height large
\centering
%\setlength{\abovecaptionskip}{10pt}
%\setlength{\belowcaptionskip}{-3pt}   %% <<<--- this messes up
\captionof{table}{Duration of Use and Gender Distribution}
\begin{tabular}{>{\hspace{1pc}}c>{\hspace{1pc}}c>{\hspace{1pc}}c>   {\hspace{1pc}}c<{\hspace{1pc}}}
\toprule
 & Female & Male & All \\
\midrule
1 year- 2 years & 2 (50) & 2 (13.3) & 4 \\
2 years - 3 years & 0 (0) & 4 (26.7) & 4 \\
3 years + & 1 (25) & 3 (20) & 4 \\
6 months- 1 year & 1 (25) & 3 (20) & 4 \\
Less than 6 months & 0 (0) & 3 (20) & 3 \\
\midrule
Total & 4 & 15 & 19 \\
\bottomrule
\footnotesize{Source:Airtel Survey}\end{tabular}
}%   %<<<<--- % needed here
%   <<<--- don't leave blank line here for better
}
\end{center}


\end{document}

在此处输入图片描述

相关内容