我有一段代码,可以生成具有最大宽度的表格:
\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}
但我希望表格具有最小宽度。所以我添加了hbox
选项。但它产生了这个错误:
! Argument of \TX@get@body has an extra }.
我该如何配置这个表以使其具有最小宽度?
答案1
像这样?
\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}
我已经定义了tabular
类似于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