表格和 parbox 的间距问题

表格和 parbox 的间距问题

如何才能校正第一列与其他空格的间距?

妇女权利委员会:

\documentclass{article}

\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{graphicx}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}

\begin{document}

\begin{table}[!ht]
    \centering
    \label{my-label}
    \begin{tabular}{P{2.25cm}P{0.75cm}P{0.75cm}P{0.75cm}P{0.75cm}}
        & \rotatebox{90}{\parbox{2cm}{{\textbf{Long\\ text}}}} & \rotatebox{90}{\parbox{2cm}{{\textbf{Long\\ text}}}} & \rotatebox{90}{\parbox{2cm}{{\textbf{Long\\ text}}}} & \rotatebox{90}{\parbox{2cm}{{\textbf{Long\\ text}}}}\\
        \toprule
        \parbox{2.25cm}{\textbf{Long\\ text}} & \cellcolor[HTML]{000000} & 0 & 1 & 1 \\[2ex]
        \parbox{2.25cm}{\textbf{Long\\ text}} & \cellcolor[HTML]{C0C0C0} 2 & \cellcolor[HTML]{000000} & 1 & 2\\[2ex]
        \parbox{2.25cm}{\textbf{Long\\ text}} & \cellcolor[HTML]{C0C0C0} 2 & \cellcolor[HTML]{C0C0C0} 1& \cellcolor[HTML]{000000} & 1\\[2ex]
        \parbox{2.25cm}{\textbf{Long\\ text}} & \cellcolor[HTML]{C0C0C0} 1 & \cellcolor[HTML]{C0C0C0} 0 & \cellcolor[HTML]{C0C0C0} 1 & \cellcolor[HTML]{000000}\\[2ex]
        \midrule
        \textbf{Sum} & \textbf{5} & \textbf{1} & \textbf{3} & \textbf{4} \\
        \bottomrule
    \end{tabular}
    \caption{Caption}
\end{table}

\end{document}

在此处输入图片描述

答案1

您可以使用内部表格拆分单元格的内容:

\documentclass{article}

\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{graphicx}

\newcommand{\splitcell}[2][c]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\newcommand{\ccell}[2]{\cellcolor[HTML]{#1}{#2}}

\begin{document}

\begin{table}[!ht]
\centering
\begin{tabular}{lcccc}
& \rotatebox{90}{\bfseries\splitcell[l]{Long\\ text}}
& \rotatebox{90}{\bfseries\splitcell[l]{Long\\ text}}
& \rotatebox{90}{\bfseries\splitcell[l]{Long\\ text}}
& \rotatebox{90}{\bfseries\splitcell[l]{Long\\ text}}
\\
\midrule[\heavyrulewidth]
\bfseries\splitcell[l]{Long\\ text} &
  \ccell{000000}{} & 0 & 1 & 1 \\[2ex]
\bfseries\splitcell[l]{Long\\ text} &
  \ccell{C0C0C0}{2} & \ccell{000000}{} & 1 & 2\\[2ex]
\bfseries\splitcell[l]{Long\\ text} &
  \ccell{C0C0C0}{2} & \ccell{C0C0C0}{1}& \ccell{000000}{} & 1\\[2ex]
\bfseries\splitcell[l]{Long\\ text} &
  \ccell{C0C0C0}{1} & \ccell{C0C0C0}{0}& \ccell{C0C0C0}{1} & \ccell{000000}{} \\[2ex]
\midrule
\textbf{Sum} & \textbf{5} & \textbf{1} & \textbf{3} & \textbf{4} \\
\bottomrule
\end{tabular}

\caption{Caption}
\label{my-label}

\end{table}

\end{document}

请记住,\label表格或图形的 必须位于标题之后。

在此处输入图片描述

答案2

像这样?

在此处输入图片描述

\documentclass{article}

\usepackage{rotating}
\usepackage[table]{xcolor}
\usepackage{array, booktabs, makecell}
\renewcommand\theadfont{\normalsize\bfseries}
\usepackage{graphicx}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{table}[!ht]
\settowidth\rotheadsize{\theadfont Long}
    \centering
    \label{my-label}
    \begin{tabular}{c P{0.75cm}P{0.75cm}P{0.75cm}P{0.75cm}}
    &   \rothead{Long\\ text}   &   \rothead{Long\\ text}
        &   \rothead{Long\\ text}   &   \rothead{Long\\ text}   \\[-5pt] % <---
    \toprule
\thead{Long\\ text} &   \cellcolor{black}     & 0 & 1 & 1       \\
\thead{Long\\ text} &   \cellcolor{gray} 2 &   \cellcolor{black}     & 1 & 2       \\
\thead{Long\\ text} &   \cellcolor{gray} 2
                        &   \cellcolor{gray} 1
                            &   \cellcolor{black}     & 1       \\
\thead{Long\\ text} &   \cellcolor{gray} 1
                        &   \cellcolor{gray} 0
                            &   \cellcolor{gray} 1
                                &   \cellcolor{black}           \\
    \midrule
\textbf{Sum}        &   \textbf{5}
                        &   \textbf{1}
                            &   \textbf{3}
                                &   \textbf{4}                  \\
    \bottomrule
    \end{tabular}
    \caption{Caption}
\end{table}

\end{document}
  • 对于第一列,只需使用列类型c
  • 删除了所有\parbox-es

其他变化:

  • 对于列标题使用rothead来自makecell包,对于旋转使用rotating
  • 对于行的标题,使用thead来自“makecell
  • 对于颜色名称的使用简单grayblack

相关内容