我正在使用提供给漂亮的表格样本问题:
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\tcbset{tab1/.style={fonttitle=\bfseries\large,fontupper=\normalsize\sffamily,
colback=yellow!10!white,colframe=red!75!black,colbacktitle=Salmon!40!white,
coltitle=black,center title,freelance,frame code={
\foreach \n in {north east,north west,south east,south west}
{\path [fill=red!75!black] (interior.\n) circle (3mm); };},}}
\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={X||Y|Y|Y|Y||Y}]
Group & One & Two & Three & Four & Sum \\\hline\hline
Red & 1000.00 & 2000.00 & 3000.00 & 4000.00 & 10000.00 \\\hline
Green & 2000.00 & 3000.00 & 4000.00 & 5000.00 & 14000.00 \\\hline
Blue & 3000.00 & 4000.00 & 5000.00 & 6000.00 & 18000.00 \\\hline\hline
Sum & 6000.00 & 9000.00 & 12000.00 & 15000.00 & 42000.00
\end{tcolorbox}
\end{document}
我需要在每个单元格中写入不同的简单单词,但无论单词的长度是多少,列的宽度总是相等。我想知道如何调整代码,使每列的宽度与其中一个单元格中包含的最长单词一样长。
答案1
您可以只使用X
标题第一列中的一列,以便占据表格所需的任何宽度,而对于其他列使用l
(或r
或c
或其他),以便它们适合内容的自然宽度。
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}
\tcbset{tab1/.style={fonttitle=\bfseries\large,fontupper=\normalsize\sffamily,
colback=yellow!10!white,colframe=red!75!black,colbacktitle=Salmon!40!white,
coltitle=black,center title,freelance,frame code={
\foreach \n in {north east,north west,south east,south west}
{\path [fill=red!75!black] (interior.\n) circle (3mm); };},}}
\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={X||r|r|r|r||r}]
Group & One & Two & Three & Four & Sum \\\hline\hline
Red & 1000.00 & 2000.00 & 3000.00 & 4000.00 & 10000.00 \\\hline
Green & 2000.00 & 3000.00 & 4000.00 & even wider cell & 14000.00 \\\hline
Blue & 3000.00 & wider-words & 5000.00 & 6000.00 & 18000.00 \\\hline\hline
Sum & 6000.00 & 9000.00 & 12000.00 & 15000.00 & 42000.00
\end{tcolorbox}
\end{document}