tcolorbox 表中的列宽根据需要

tcolorbox 表中的列宽根据需要

我正在尝试用一些数据制作美观的表格。除了列宽固定外,其他都很好。我尝试了很多解决方案,但都不起作用。如何改变列宽,以便可以完美地将小而大的语句居中。

What I would like is 'Number of Questions' to take 2 lines and 'Statement A' and 
'Statement B' to appear in a single line without getting out of the columns.

提前感谢您的时间和帮助。

在此处输入图片描述

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


\usepackage{array,multirow,tabularx}



\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}}
\begin{document}
\begin{table}[htb]
\refstepcounter{table}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.5}}{Y|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y},title={Table \thetable. This is a table},boxrule=0.8pt]
& \multicolumn{5}{|c|}{Year 1}  & \multicolumn{5}{|c|}{Year 2}\\ \hline
& \multicolumn{2}{|c|}{Part-B}& \multicolumn{2}{|c|}{Part-C} &  & \multicolumn{2}{|c|}{Part-B}& \multicolumn{2}{|c|}{Part-C} & \\ \hline
 & Number of Questions     & Marks     & Number of Questions    & Marks     & Total    & Number of Questions     & Marks     & Number of Questions    & Marks     & Total\\\hline
statement A   & 10 & 20 & 30  & 4000  & 100& sum & sum    & 60.75  & sum & 121.5\\\hline
RA & 20 & 30 & 40  & 50  & 140 & sum & 60.75    & sum  & 60.75 & 71.5\\\hline
LA  & 30 & 40 & 50  & 60  & 180 & sum & 60.75    & sum  & 60.75 & 60.75\\\hline
statement B   & 60 & 90 & 120 & 150 & 420& 60.75 & 60.75    & sum  & 60.75 & sum\\\hline
\end{tcolorbox}
\end{table}

答案1

对于geometry较小的边距,稍微小一点\tabcolsep,并借助缩写,您可以获得以下内容:

在此处输入图片描述

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

\usepackage{geometry}

\usepackage{array,multirow,tabularx}



\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}}
\begin{document}
\begin{table}[htb]
\setlength{\tabcolsep}{5pt}
\refstepcounter{table}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.5}}{c|Y|Y|Y|Y|Y|Y|Y|Y|Y|Y},title={Table \thetable. This is a table},boxrule=0.8pt]
& \multicolumn{5}{c|}{Year 1}  & \multicolumn{5}{c|}{Year 2}\\ \hline
& \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} &  & \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} & \\ \hline
 & NQ     & Marks     & NQ    & Marks     & Total    & NQ & Marks     & NQ    & Marks     & Total\\\hline
statement A   & 10 & 20 & 30  & 4000  & 100& sum & sum    & 60.75  & sum & 121.5\\\hline
RA & 20 & 30 & 40  & 50  & 140 & sum & 60.75    & sum  & 60.75 & 71.5\\\hline
LA  & 30 & 40 & 50  & 60  & 180 & sum & 60.75    & sum  & 60.75 & 60.75\\\hline
statement B   & 60 & 90 & 120 & 150 & 420& 60.75 & 60.75    & sum  & 60.75 & sum\\\hline
\multicolumn{11}{l}{NQ = Number of Questions}\\
\end{tcolorbox}
\end{table}

\end{document}

与上面的想法类似,但没有geometry包:

在此处输入图片描述

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

\usepackage{array,multirow,tabularx}



\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}}
\begin{document}
\begin{table}[htb]
\setlength{\tabcolsep}{3pt}
\refstepcounter{table}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.5}}{|Y|c|c|c|c|c|c|c|c|c|c|},title={Table \thetable. This is a table},boxrule=0.8pt]
& \multicolumn{5}{c|}{Year 1}  & \multicolumn{5}{c|}{Year 2}\\ \hline
& \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} &  & \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} & \\ \hline
 & NQ     & Marks     & NQ    & Marks     & Total    & NQ & Marks     & NQ    & Marks     & Total\\\hline
statement A   & 10 & 20 & 30  & 4000  & 100& sum & sum    & 60.75  & sum & 121.5\\\hline
RA & 20 & 30 & 40  & 50  & 140 & sum & 60.75    & sum  & 60.75 & 71.5\\\hline
LA  & 30 & 40 & 50  & 60  & 180 & sum & 60.75    & sum  & 60.75 & 60.75\\\hline
statement B   & 60 & 90 & 120 & 150 & 420& 60.75 & 60.75    & sum  & 60.75 & sum\\\hline
\multicolumn{11}{l}{NQ = Number of Questions}\\
\end{tcolorbox}
\end{table}

\end{document}

使用geometry,字体较小,且不带缩写:

在此处输入图片描述

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

\usepackage{geometry}
\usepackage{makecell}
\usepackage{array,multirow,tabularx}



\newcolumntype{Y}{>{\centering\arraybackslash}X}
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\small\sffamily,
colback=white!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}
\begin{document}
\begin{table}[htb]
\setlength{\tabcolsep}{3pt}
\refstepcounter{table}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.5}}{c|Y|c|Y|c|c|Y|c|Y|c|c},title={Table \thetable. This is a table},boxrule=0.8pt]
& \multicolumn{5}{c|}{Year 1}  & \multicolumn{5}{c|}{Year 2}\\ \hline
& \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} &  & \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} & \\ \hline
 & Number of \newline Qeustions     & Marks     & Number of \newline Qeustions   & Marks     & Total    & Number of \newline Qeustions & Marks     & Number of \newline Qeustions    & Marks     & Total\\
\hline
statement A   & 10 & 20 & 30  & 4000  & 100& sum & sum    & 60.75  & sum & 121.5\\\hline
RA & 20 & 30 & 40  & 50  & 140 & sum & 60.75    & sum  & 60.75 & 71.5\\\hline
LA  & 30 & 40 & 50  & 60  & 180 & sum & 60.75    & sum  & 60.75 & 60.75\\\hline
statement B   & 60 & 90 & 120 & 150 & 420& 60.75 & 60.75    & sum  & 60.75 & sum\\\hline
\end{tcolorbox}
\end{table}

\end{document}

没有geometry,但字体更小:

在此处输入图片描述

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

%\usepackage{geometry}
\usepackage{makecell}
\usepackage{array,multirow,tabularx}



\newcolumntype{Y}{>{\centering\arraybackslash}X}
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\scriptsize\sffamily,
colback=white!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}
\begin{document}
\begin{table}[htb]
\setlength{\tabcolsep}{2pt}
\refstepcounter{table}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.5}}{c|Y|c|Y|c|c|Y|c|Y|c|c},title={Table \thetable. This is a table},boxrule=0.8pt]
& \multicolumn{5}{c|}{Year 1}  & \multicolumn{5}{c|}{Year 2}\\ \hline
& \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} &  & \multicolumn{2}{c|}{Part-B}& \multicolumn{2}{c|}{Part-C} & \\ \hline
 & Number of \newline Qeustions     & Marks     & Number of \newline Qeustions   & Marks     & Total    & Number of \newline Qeustions & Marks     & Number of \newline Qeustions    & Marks     & Total\\
\hline
statement A   & 10 & 20 & 30  & 4000  & 100& sum & sum    & 60.75  & sum & 121.5\\\hline
RA & 20 & 30 & 40  & 50  & 140 & sum & 60.75    & sum  & 60.75 & 71.5\\\hline
LA  & 30 & 40 & 50  & 60  & 180 & sum & 60.75    & sum  & 60.75 & 60.75\\\hline
statement B   & 60 & 90 & 120 & 150 & 420& 60.75 & 60.75    & sum  & 60.75 & sum\\\hline
\end{tcolorbox}
\end{table}

\end{document}

答案2

这只是一个想法:如果你旋转表格,它很容易适合一行文本。

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


\usepackage{array,multirow,tabularx}

\newtcolorbox[blend into=tables]{mytable}[2][]{%
    enhanced,
    fonttitle=\bfseries,
    fontupper=\sffamily,
    colback=white!10!white,
    colframe=red!50!black,
    colbacktitle=Salmon!40!white,
    coltitle=black,
    center title,
    boxrule=.8pt,
    float=htb,
    title={#2},
    #1
}

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

\begin{document}
\begin{mytable}[tabularx*={\renewcommand{\arraystretch}{1.5}}{ccccccc}]{This is a table}
& & & Statement A & RA & LA & Statement B \\
\multirow{5}{*}{\rotatebox{90}{Year 1}}  & 
\multirow{2}{*}{\rotatebox{90}{Part B}}  &
Number of questions   & 10 & 20 & 30 & 60 \\
&& Marks & 20 & 30 & 40 & 90 \\
 & \multirow{2}{*}{\rotatebox{90}{Part C}}  &
Number of questions   & 30 & 40 & 50 & 120 \\
&& Marks & 4000 & 50 & 60 & 150 \\
&& Total & 100 & 140 & 180 & 420 \\
%
\hline
\multirow{5}{*}{\rotatebox{90}{Year 2}}  & 
\multirow{2}{*}{\rotatebox{90}{Part B}}  &
Number of questions   & 10 & 20 & 30 & 60 \\
&& Marks & 20 & 30 & 40 & 90 \\
 & \multirow{2}{*}{\rotatebox{90}{Part C}}  &
Number of questions   & 30 & 40 & 50 & 120 \\
&& Marks & 4000 & 50 & 60 & 150 \\
&& Total & 100 & 140 & 180 & 420 \\

\end{mytable}
\end{document}

在此处输入图片描述

相关内容