
Estoy intentando colorear una tabla con un color de fondo y el título con un color de fondo diferente. Debo usar el tabular
entorno, ninguno de los tabulary
, tablarx
, tabu
, tabular*
será útil para mi configuración. Por favor, mire el siguiente 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}
El código anterior produce la siguiente tabla:
Me gustaría ampliar el fondo del título para que el espacio indicado por la flecha pueda cubrirse con el color de fondo del título. Nuevamente, tenga en cuenta que tengo que usar el tabular
entorno y que el ancho de la tabla debe cubrir todo el ancho del texto. Agradecería mucho cualquier ayuda con cambios mínimos en la configuración actual.
Respuesta1
¡Lo has etiquetado tcolorbox
pero no lo estás usando!. Aquí hay una versión que usa su propio código. He comentado los cambios realizados en el código.
\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}