如何制作具有多行旋转单元格的表格?

如何制作具有多行旋转单元格的表格?

我正在尝试制作一个表格,其中的多行单元格可以自动居中、旋转。类似于图片中的情况,但单元格会自动适应其内容:

在此处输入图片描述

我尝试申请解决方案由@AboAmmar 和@Seong 给出,但它们不能确保在指定的行数内垂直居中。这个答案不清楚,因为包\multirow下的命令makecell接受行数的非整数值(?)。这里给出了一些代码,但问题是这会占用固定值(在\raisebox参数中),该值仅适用于一个特定的单元格内容。最相似的问题没有答案。

实际上我需要一些应该命名为- 作为包和函数的\multirowrothead组合的函数。我的代码:\multirowthead\rotheadmakecell

\documentclass[table]{standalone}
\usepackage{siunitx}
\usepackage{makecell}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\renewcommand{\theadfont}{\normalsize\bfseries}
\renewcommand{\arraystretch}{1.3}

\begin{document}
    \begin{tabular}{
        |l|
        S[table-format=2.1]|
        S[table-format=1.2]|
        S[table-format=2.1]|
        S[table-format=1.2]|
        S[table-format=2.1]|
        S[table-format=1.2]|
    }
        \hline
        % https://tex.stackexchange.com/questions/246889/how-to-color-column-heads-determined-width-makecell
        \rowcolor{gray}
        \multicolumn{3}{|c|}{\colorbox{gray}{\thead{First title in heading}}} &
        \multicolumn{2}{c|}{\colorbox{gray}{\thead{Second title\\in heading}}} & &\\
        \hhline{>{\arrayrulecolor{black}}|-----|>{\arrayrulecolor{gray}}-%
            >{\arrayrulecolor{black}}|>{\arrayrulecolor{gray}}->{\arrayrulecolor{black}}|%
        }
        \rowcolor{gray}
        & & & & \colorbox{gray}{\thead{data\\description}} &
        {\multirowthead{-2}{Data\\description}} &\\
        \hhline{%
            >{\arrayrulecolor{black}}|>{\arrayrulecolor{gray}}-%
            >{\arrayrulecolor{black}}|>{\arrayrulecolor{gray}}-%
            >{\arrayrulecolor{black}}|>{\arrayrulecolor{gray}}-%
            >{\arrayrulecolor{black}}|>{\arrayrulecolor{gray}}-%
            >{\arrayrulecolor{black}}|-|-|%
            >{\arrayrulecolor{gray}}->{\arrayrulecolor{black}}|%
        }
        \rowcolor{gray}
        \colorbox{gray}{\multirowthead{-2}{side-heading\\description}} &
        \colorbox{gray}{\multirowthead{-2}{data\\description}} &
        \colorbox{gray}{\multirowthead{-2}{data\\description}} &
        \colorbox{gray}{\multirowthead{-2}{data\\description}} &
        \colorbox{gray}{\thead{A}} & \colorbox{gray}{\thead{B}} &
        \colorbox{gray}{\multirowthead{-3}{Data description}}\\
        \hline
        Description 1 & 63.5 & 4.87 & 63.5 & 4.87 & 63.5 & 4.87\\
        \hline
        Description 2 & 88.4 & 5.13 & 88.4 & 5.13 & 88.4 & 5.13\\
        \hline
    \end{tabular}
\end{document}

答案1

您不能让b图片上的绿色数量变得更小,因为第 2 行和第 3 行的(总)高度受到第 5 列单元格内容的限制。

以下是您可以使用 做的{NiceTabular}事情nicematrix

\documentclass{article}
\usepackage{siunitx}
\renewcommand{\arraystretch}{1.3}

\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}[ hvlines , code-before = \rowcolor{gray!30}{1-3} ]   
    {
        c
        S[table-format=2.1]
        S[table-format=1.2]
        S[table-format=2.1]
        S[table-format=1.2]
        S[table-format=2.1]
        S[table-format=1.2]
    }%
{\Block{1-3}{First title in heading}} & & & 
  \Block{1-2}{Second title\\in heading} & &
{\Block{2-1}<\rotate>{Data\\description}} &  
{\Block{3-1}<\rotate>{Data description}} \\
  {\Block{2-1}<\rotate>{side-heading\\description}}
& {\Block{2-1}<\rotate>{data\\description}}
& {\Block{2-1}<\rotate>{data\\description}}
& {\Block{2-1}<\rotate>{data\\description}}
& \Block{}<\rotate>{data\\description\hspace*{0.5em}} \\
& & & & {A} & {B} \\
Description 1 & 63.5 & 4.87 & 63.5 & 4.87 & 63.5 & 4.87\\
Description 2 & 88.4 & 5.13 & 88.4 & 5.13 & 88.4 & 5.13\\
\end{NiceTabular}
\end{document} 
  • 在 中{NiceTabular},您可以使用内置命令 垂直和水平合并单元格\Block

  • 使用键hvlines,您可以绘制块中除外的所有规则(这是这里所期望的)。

  • 内置命令\rotate用于旋转单元格和块的内容。

  • 您可以使用 键中的命令为单元格、行和列着色code-before(但是,也可以使用类似于 的语法colortbl)。通常,在所有缩放级别下,所有 PDF 查看器中的输出效果都很好。

  • 然而,您需要多次编译。

上述代码的输出

相关内容