キャプションの背景の幅をテーブルの幅に合わせる

キャプションの背景の幅をテーブルの幅に合わせる

テーブルに背景色を付け、キャプションに別の背景色を付けようとしています。 環境を使用する必要がありますが、 、のtabularいずれも私の設定には役立ちません。次の MWE を参照してください。tabularytablarxtabutabular*

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

ここに画像の説明を入力してください

関連情報