表格(和 tabularx)中列之间的空格

表格(和 tabularx)中列之间的空格

我有同样的问题tabularx 中的空格在 \extracolsep 之后添加,但该解决方案对我来说不起作用。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}

\begin{document}
\noindent
\hspace*{\fill}
\begin{table*}[ht!]
\begin{tabular*}{\textwidth}{l @{\extracolsep{\fill}} llllll}
\toprule \rowcolor{light-gray} 
Test  & Test & \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\ \midrule
1 & X   & X     & X & 0  & X         & X           \\ \bottomrule
\end{tabular*}
\end{table*}
\hspace*{\fill}

\end{document}

因为标题有点太长所以我分成了两行。输出如下:这里

请问有人能帮我填补空白吗?

PS: 将 tabular* 替换为表格型产生相同的输出。

答案1

tabularx修改列标题定义的包可以帮助:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{booktabs}

\colorlet{light-gray}{gray!35}

\begin{document}

\begin{table}
        \begin{tabularx}{\textwidth}{l*6X}
        \toprule
        \rowcolor{light-gray}
        Test  & Test & \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\
        \midrule
        1 & X   & X     & X & 0  & X         & X           \\
        \bottomrule
    \end{tabularx}
\end{table}

% Long column headers should not have the 'X' column type
\begin{table}
        \begin{tabularx}{\textwidth}{XXlXXXX}
        \toprule
        \rowcolor{light-gray}
        Test  & Test & \begin{tabular}[c]{@{}l@{}}A long title \\ Another long title \end{tabular}  & \begin{tabular}[c]{@{}l@{}}Test \\ side\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular} & \begin{tabular}[c]{@{}l@{}}Test \\ Test2\end{tabular}&  \begin{tabular}[c]{@{}l@{}}Test \\ Test2 \end{tabular} \\
        \midrule
        1 & X   & X     & X & 0  & X         & X           \\
        \bottomrule
    \end{tabularx}
\end{table}

\end{document}

在此处输入图片描述 在此处输入图片描述

答案2

不要混合(错误)搭配视觉隐喻:如果你想使用颜色(或灰度)来强调标题行,请不要使用\toprule\midrule 根本。相反,如果您更喜欢使用\toprule\midrule,则不要为标题行添加颜色。回想一下(成功的)设计师和艺术家久经考验的口头禅:“少即是多。”

在此处输入图片描述

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs,tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand{\mycell}[1]{%
    \begin{tabular}{@{}c@{}} \strut #1\strut \end{tabular}}

\begin{document}
\setlength\extrarowheight{2pt}
\noindent       
    \begin{tabularx}{\textwidth}{*{7}{C}}
    \rowcolor{lightgray}
    Test  & Test & \mycell{Test \\ Test2}   
                 & \mycell{Test \\ side} 
                 & \mycell{Test \\ Test2} 
                 & \mycell{Test \\ Test2}
                 & \mycell{Test \\ Test2} \\
    1 & X & X & X & 0 & X & X \\ 
    \bottomrule
    \end{tabularx}
\end{document}

相关内容