# How do I generate a caption instead of title?

In the following example, I want to generate Table 3.1: AAA as the caption instead of the plain title AAA. To clarify further, I do not want teh caption to be place above or below the table, but the title should be substituted by the caption text. Any idea on how to achieve that?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fancy table
% http://tex.stackexchange.com/questions/112343/beautiful-table-samples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}
\usepackage{multirow}

\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\newcolumntype{Z}{>{\centering\arraybackslash}X}

\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\normalsize\sffamily,
colback=yellow!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}

\begin{document}

\begin{tcolorbox}[tab2,tabularx={Z || Z | Z | Z},title=AAA,width=0.7\textwidth]
BBB & BBB & BBB & BBB \\ \hline\hline
\multirow{3}{*}{XXX} & AAA & AAA & AAA \\ \cline{2-4}
&  & AAA & AAA \\ \cline{2-4}
&  & AAA & AAA
\end{tcolorbox}

\end{document}


Is this what you want?

\documentclass[12pt]{article}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array, caption, chngcntr, etoolbox}
\AtBeginEnvironment{tcolorbox}{
\captionsetup{font={sf, bf}}}

\counterwithin{table}{section}
\tcbuselibrary{skins}
\usepackage{multirow}

\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\newcolumntype{Z}{>{\centering\arraybackslash}X}%
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\normalsize\sffamily,
colback=yellow!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}

\begin{document}

\setcounter{section}{3}

\begin{tcolorbox}[tab2, tabularx={Z || Z | Z | Z}, before title =\vspace*{-2pt}, after title=\vspace*{-13pt}, title=\captionof{table}{AAA},width=0.7\textwidth]
BBB & BBB & BBB & BBB \\ \hline\hline
\multirow{3}{*}{XXX} & AAA & AAA & AAA \\ \cline{2-4}
& & AAA & AAA \\ \cline{2-4}
& & AAA & AAA
\end{tcolorbox}

\end{document}


• Awesome! Exactly what I wanted. – Holmes.Sherlock Dec 1 '16 at 4:57
• I can see the thickness of the title bar is more when caption is added. Is it possible to reduce it back to the original thickness? – Holmes.Sherlock Dec 1 '16 at 15:15
• @Holmes.Sherlock: Yes. I removed the skip=0pt from caption, and used the after title and before title keys instead. Please see my updated answer. However, it shouldn't be so tight in my opinion. – Bernard Dec 1 '16 at 18:03