多行单元格,每行的文本垂直对齐

多行单元格,每行的文本垂直对齐

是否有可能实现像下图这样的表格,而不需要像我在代码中所做的那样手动划分行标题?

期望结果

明确地说,我追求的是以下内容。

  1. 表格宽度设置为\linewidth。(我曾经tabularx这样做过。)
  2. 表格单元格之间的垂直空间增加了(我已经使用了\renewcommand{\arraystretch}{2}。)
  3. 行标题(第 1 列)太长时会自动换行。(我手动伪造了这一点。)
  4. 行标题的线条应与表格主体中的数字对齐。(我通过手动划分行标题并将各部分输入到不同的行来伪造这一点。)
  5. 换行的行标题应该有一个悬挂缩进。(我通过添加 手动伪造了这一点\hspace{1em}。)
\documentclass{article}
\usepackage{showframe}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
  \begin{table}[ht]
    \renewcommand{\arraystretch}{2}  % Exaggregated for the sake of clarity
    \begin{tabularx}{\linewidth}{@{} X *{4}{c} @{}}
      \toprule
                                                              &   A   &   B   &   C   &   D   \\
      \midrule
      The first row heading runs over one line                &  123  &  123  &  123  &  123  \\
                                                              & (456) & (456) & (456) & (456) \\
      [1em]
      The third row heading is slightly longer and            &  123  &  123  &  123  &  123  \\
      \hspace{1em} runs over two lines                        & (456) & (456) & (456) & (456) \\
      [1em]
      The third row heading is actually quite                 &  123  &  123  &  123  &  123  \\
      \hspace{1em} outrageously long and runs over a total of & (456) & (456) & (456) & (456) \\
      \hspace{1em} three lines \\
      \bottomrule
    \end{tabularx}
  \end{table}
\end{document}

答案1

  • 抱歉,我想要的表格设计对我来说不是很清楚..
  • 以下是我的三个猜测……
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
  \begin{table}[ht]
    \begin{tblr}{colspec={@{}  X[l, cmd=\hangindent 1em\hangafter 1] 
                          *{4}{c} @{}},
                 cell{odd}{1} = {r=2}{},
                 rowsep=1ex,
                 vspan = even,
                }
The first row heading runs over one lin                                                       
    &  123  &  123  &  123  &  123  \\ 
    & (456) & (456) & (456) & (456) \\ 
The second row heading is slightly longer and runs over two lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\ 
The third row heading is actually quite outrageously long and runs over a total of three lines 
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\ 
    \end{tblr}
  \end{table}
  
\noindent
or

  \begin{table}[ht]
    \begin{tblr}{colspec={@{}  X[l, cmd=\hangindent 1em\hangafter 1]
                          *{4}{c} @{}},
                 cell{odd}{1} = {r=2}{},
                 row{odd[3]} = {abovesep=2ex},
                 vspan = even,
                }
The first row heading runs over one lin
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
    \end{tblr}
  \end{table}

\noindent
or

  \begin{table}[ht]
    \begin{tblr}{colspec={@{}  X[l, cmd=\hangindent 1em\hangafter 1]
                          *{4}{c} @{}},
                 cell{odd}{1} = {r=2}{},
                 rowsep = 1pt,
                 row{odd[3]} = {abovesep=2ex},
                }
The first row heading runs over one lin
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
    \end{tblr}
  \end{table}
\end{document}

在此处输入图片描述

(红线表示文本块边框)

附录:
现在我猜你会想这样的事情:

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
  \begin{table}[ht]
    \begin{tblr}{colspec={@{}  X[l, h, cmd=\hangindent 1em\hangafter 1]
                          *{4}{Q[c, h]} @{}},
                 cell{odd}{1} = {r=2}{},
                    rowsep    = 1pt,
                 row{odd[3]}  = {abovesep=2ex},
%                        vspan = even,
                }
The first row heading runs over one lin
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
    \end{tblr}
  \end{table}
\end{document}       

在此处输入图片描述

相关内容