3

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?

1 Answer 1

5

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
7
  • My local latex compiler produces this error with your code: ! Package xcolor Error: Undefined color tcbcol@frame'.. However online sharelatex has no problem with it. Oct 23, 2015 at 12:42
  • @user62181: Can you update your tex distro and try again?
    – user11232
    Oct 23, 2015 at 12:51
  • I have the newest version in Ubuntu repository, It is not easy to install a newer version. Oct 23, 2015 at 12:56
  • Thanks. I replaced tcbcol@frame with black and it was solved. Oct 23, 2015 at 13:07
  • But I want the table be placed automatically in the center of the page. Oct 23, 2015 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .