[Tex/LaTex] tcolorbox table with minimum width

tcolorbox

I have this code which produces a table with maximum width:

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}

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

\newtcolorbox{mytable}[3][]
{
     tab2,
     tabularx*={\renewcommand{\arraystretch}{1.7}}{#2},
     title=Table \ref{#3},    
     %hbox,
     before={\begin{table}[htb]\refstepcounter{table}\label{#3}},
     after={\end{table}},
     #1
}

\begin{document}

\begin{mytable}{Y|Y}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00 
\end{mytable}

\end{document}

But I want the table to have the minimum width. So I added hbox option. But it produced this error:

! Argument of \TX@get@body has an extra }.

How can I configure this table to have the minimum width?

Best Answer

Like this?

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
%\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}

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

\newtcolorbox{mytable}[3][]
{
     tab2,
     tabular*={\renewcommand{\arraystretch}{1.7}}{#2},
     title=Table \ref{#3},
     %hbox,
     before={\begin{table}[htb]\refstepcounter{table}\label{#3}},
     after={\end{table}},
     #1
}

\makeatletter
\tcbset{
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\arrayrulecolor{tcbcol@frame}\def\arraystretch{1.1}#1%
      \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother
\begin{document}

\begin{mytable}{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}

\end{document}

enter image description here

I have defined a tabular similar to tabularx:

\makeatletter
\tcbset{
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\arrayrulecolor{tcbcol@frame}\def\arraystretch{1.1}#1%
      \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother