2

how is it possible to correct the spacing of column 1 and the other spaces?

The MWE:

\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}

enter image description here

2 Answers 2

2

You can split the contents of a cell with an inner tabular:

\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}

Remember that the \label for a table or figure must go after the caption.

enter image description here

1
  • Almost perfect - is it also possible to have the same space height between Topline and Midline? Oct 18, 2017 at 14:57
1

like this?

enter image description here

\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}
  • for the first column just use column type c
  • removed all \parbox-es

other changes:

  • for columns' headers is used rothead from makecell package, for rotating is used rotating package
  • for rows' headers is used thead from `makecell
  • for color names is used simple gray and black
2
  • Almost perfect - is it also possible to have the same space height between Topline and Midline? Oct 18, 2017 at 14:56
  • 1
    see edited answer
    – Zarko
    Oct 18, 2017 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .