標題背景寬度與表格寬度相符

標題背景寬度與表格寬度相符

我正在嘗試使用背景顏色為表格著色,並使用不同的背景顏色為標題著色。我必須使用該tabular環境,tabularytablarxtabutabular*對我的設定都沒有幫助。請看下面的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}

在此輸入影像描述

相關內容